fix: prioritize client IP headers
This commit is contained in:
parent
3d1dda6503
commit
23eb629fe8
20
src/main.rs
20
src/main.rs
@ -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);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user