Get the "real ip" of an incoming request
  • Rust 95.5%
  • Nix 4.5%
Find a file
2025-10-10 23:26:35 +02:00
.forgejo/workflows workflow updates 2025-05-23 22:05:38 +02:00
src fix potentially confusing lifetime 2025-10-10 23:26:35 +02:00
.envrc init 2024-11-27 20:15:01 +01:00
.gitignore init 2024-11-27 20:15:01 +01:00
Cargo.lock replace itertools with just either 2025-05-23 22:13:50 +02:00
Cargo.toml replace itertools with just either 2025-05-23 22:13:50 +02:00
flake.lock flake update 2025-10-10 23:25:09 +02:00
flake.nix flake update 2025-10-10 23:25:09 +02:00
README.md update repo url 2025-05-23 22:05:31 +02:00

real-ip

Get the "real-ip" of an incoming request using the "forwarded", "x-forwarded-for" or "x-real-ip" headers set by reverse proxies.

See the crate documentation for more details and examples.

Example

use http::Request;
use std::net::IpAddr;
use real_ip::{real_ip, IpNet};

// in a real program this info would of course come from the http server
let incoming_ip = IpAddr::from([10, 0, 0, 1]);
let request = Request::builder().header("x-forwarded-for", "192.0.2.1").body(()).unwrap();

// the reverse-proxies in our network that we trust
let trusted_proxies = [
    IpAddr::from([10, 0, 0, 1]).into(),
];
let client_ip = real_ip(request.headers(), incoming_ip, &trusted_proxies);
assert_eq!(Some(IpAddr::from([192, 0, 2, 1])), client_ip);