11 unstable releases

0.6.1 Oct 6, 2024
0.6.0 Apr 9, 2024
0.5.1 Mar 2, 2024
0.5.0 Nov 27, 2023
0.1.0 Feb 9, 2022

#348 in Network programming

Download history 5033/week @ 2024-08-21 4036/week @ 2024-08-28 4286/week @ 2024-09-04 4550/week @ 2024-09-11 3978/week @ 2024-09-18 4861/week @ 2024-09-25 5294/week @ 2024-10-02 4782/week @ 2024-10-09 5111/week @ 2024-10-16 4386/week @ 2024-10-23 5582/week @ 2024-10-30 5534/week @ 2024-11-06 6631/week @ 2024-11-13 6195/week @ 2024-11-20 8577/week @ 2024-11-27 7345/week @ 2024-12-04

29,777 downloads per month
Used in 9 crates (8 directly)

MIT license

39KB
742 lines

axum-client-ip

License Crates.io Docs.rs

Client IP address extractors for Axum

Why different extractors?

There are two distinct use cases for client IP which should be treated differently:

  1. You can't tolerate the possibility of spoofing (you're working on rate limiting, spam protection, etc). In this case, you should use SecureClientIp or an extractor for a particular header.
  2. You can trade potential spoofing for a statistically better IP determination. E.g. you use the IP for geolocation when the correctness of the location isn't critical for your app. For something like this, you can use InsecureClientIp.

For a deep dive into the trade-off refer to this Adam Pritchard's article

SecureClientIp vs specific header extractors

Apart from SecureClientIp there are concrete CfConnectingIp, CloudFrontViewerAddress, FlyClientIp, Forwarded, RightmostForwarded, RightmostXForwardedFor, TrueClientIp, XForwardedFor and XRealIp secure extractors. You can use them directly if your code assumes a specific proxy configuration.

They work the same way - by extracting IP from the specified header you control. The only difference is in the target header specification. With SecureClientIp you can specify the header at runtime, so you can use e.g. environment variable for this setting (look at the implementation example). While with specific extractors you'd need to recompile your code if you'd like to change the target header (e.g. you're moving to another cloud provider). To mitigate this change you can create a type alias e.g. type InsecureIp = XRealIp and use it in your handlers, then the change will affect only one line.

Usage

use axum::{routing::get, Router};
use axum_client_ip::{InsecureClientIp, SecureClientIp, SecureClientIpSource};
use std::net::SocketAddr;

async fn handler(insecure_ip: InsecureClientIp, secure_ip: SecureClientIp) -> String {
    format!("{insecure_ip:?} {secure_ip:?}")
}

#[tokio::main]
async fn main() {
    async fn handler(insecure_ip: InsecureClientIp, secure_ip: SecureClientIp) -> String {
        format!("{insecure_ip:?} {secure_ip:?}")
    }

    let app = Router::new().route("/", get(handler))
        .layer(SecureClientIpSource::ConnectInfo.into_extension());

    let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
    let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
    axum::serve(
        listener,
        // Don't forget to add `ConnectInfo` if you aren't behind a proxy
        app.into_make_service_with_connect_info::<SocketAddr>(),
    )
    .await
    .unwrap()
}

A common issue with Axum extractors

The most often issue with this extractor is using it after one consuming body e.g. axum::extract::Json. To fix this rearrange extractors in your handler definition moving body consumption to the end, see details.

Contributing

  • please run .pre-commit.sh before sending a PR, it will check everything

License

This project is licensed under the MIT license.

Dependencies

~6–15MB
~191K SLoC