http-ip
Utilities to determine HTTP client's IP
Features
http- Enables filter implementation using http's header map;axum08- Enablesaxumextractor implementation for0.8.x;tonic014- Enablestonicextension implementation for0.14.x.
Example
A very simple example to extract IP from given header value using CIDR filtering to determine client's IP
This is different from a very common approach selecting leftmost IP as client's IP Instead you can search starting through the right, filtering out your cloud's CIDRs to guarantee you get client's real external IP. In complicated network environments individual clients are rarely having static IPs and most likely hidden by corporate proxy that shields company's network, which often inserts extra IP in between you and client.
This in-between IP is more often than not, what you'd want to get and use, rather than leftmost (which may be client IP, but it is not client IP from perspective of public network)
const IPS: &str = "203.0.113.195,2001:db8:85a3:8d3:1319:8a2e:370:7348,198.51.100.178";
const CIDR: Cidr = match from_text ;
//Get ips in reverse (from right) order to filter out proxy IPs manually until we reach client's IP
let ips = parse_x_forwarded_for_rev;
let client_ip = find_next_ip_after_filter.expect;
assert_eq!;