fix: prioritize client IP headers

This commit is contained in:
itsscb 2024-08-25 20:39:52 +02:00
parent 3d1dda6503
commit 23eb629fe8

View File

@ -36,20 +36,20 @@ async fn word() -> String {
async fn log_ip(req: Request<Body>, next: Next) -> Response {
let ip = req
.extensions()
.get::<SocketAddr>()
.map(|addr| addr.ip().to_string())
.or_else(|| {
req.headers()
.get("x-forwarded-for")
.and_then(|hv| hv.to_str().ok().map(String::from))
})
.headers()
.get("x-forwarded-for")
.and_then(|hv| hv.to_str().ok())
.or_else(|| {
req.headers()
.get("x-real-ip")
.and_then(|hv| hv.to_str().ok().map(String::from))
.and_then(|hv| hv.to_str().ok())
})
.unwrap_or_else(|| "Unknown".to_string());
.or_else(|| {
req.headers()
.get("cf-connecting-ip")
.and_then(|hv| hv.to_str().ok())
})
.unwrap_or("Unknown");
info!(PATH = req.uri().path().to_string(), IP = ip);