fix: always log ip - even if unknown
This commit is contained in:
parent
bc2b74c401
commit
3d1dda6503
26
src/main.rs
26
src/main.rs
@ -35,14 +35,24 @@ async fn word() -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn log_ip(req: Request<Body>, next: Next) -> Response {
|
async fn log_ip(req: Request<Body>, next: Next) -> Response {
|
||||||
if let Some(addr) = req.extensions().get::<SocketAddr>() {
|
let ip = req
|
||||||
info!(
|
.extensions()
|
||||||
PATH = req.uri().path().to_string(),
|
.get::<SocketAddr>()
|
||||||
IP = addr.ip().to_string()
|
.map(|addr| addr.ip().to_string())
|
||||||
);
|
.or_else(|| {
|
||||||
} else {
|
req.headers()
|
||||||
info!(PATH = req.uri().path().to_string());
|
.get("x-forwarded-for")
|
||||||
}
|
.and_then(|hv| hv.to_str().ok().map(String::from))
|
||||||
|
})
|
||||||
|
.or_else(|| {
|
||||||
|
req.headers()
|
||||||
|
.get("x-real-ip")
|
||||||
|
.and_then(|hv| hv.to_str().ok().map(String::from))
|
||||||
|
})
|
||||||
|
.unwrap_or_else(|| "Unknown".to_string());
|
||||||
|
|
||||||
|
info!(PATH = req.uri().path().to_string(), IP = ip);
|
||||||
|
|
||||||
next.run(req).await
|
next.run(req).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user