pub trait RealIp {
// Required method
fn real_ip_header(&self, header: impl Into<HeaderType>) -> IpAddr;
// Provided method
fn real_ip(&self) -> IpAddr { ... }
}
Expand description
Trait that adds methods for getting the real IP of a client through a reverse proxy.
If you are using the “X-Forwarded-For” header you can use req.real_ip()
but if you are using a different header you will have to use req.real_ip_header(...)
.
Required Methods§
sourcefn real_ip_header(&self, header: impl Into<HeaderType>) -> IpAddr
fn real_ip_header(&self, header: impl Into<HeaderType>) -> IpAddr
Gets the ‘real IP’ of a client by parsing the value of header
into an IpAddr.
If the connection is not coming from localhost, the header isn’t found or the header contains an invalid IP address, the raw socket address will be returned.
Warning: Make sure your reverse proxy is overwriting the specified header on the incoming requests so clients cant spoof their original Ips.
Provided Methods§
sourcefn real_ip(&self) -> IpAddr
fn real_ip(&self) -> IpAddr
Uses RealIp::real_ip_header
with the “X-Forwarded-For” header.
Example
use afire::extension::RealIp;
server.route(Method::GET, "/", |req| {
let ip = req.real_ip();
Response::new().text(format!("Hello, {ip}"))
});