fix: add souce header to log
This commit is contained in:
parent
23eb629fe8
commit
9d7a931045
20
src/main.rs
20
src/main.rs
@ -8,7 +8,6 @@ use axum::{
|
|||||||
};
|
};
|
||||||
use http::Method;
|
use http::Method;
|
||||||
use rand::seq::SliceRandom;
|
use rand::seq::SliceRandom;
|
||||||
use std::net::SocketAddr;
|
|
||||||
use tower_http::cors::{Any, CorsLayer};
|
use tower_http::cors::{Any, CorsLayer};
|
||||||
use tower_http::services::ServeDir;
|
use tower_http::services::ServeDir;
|
||||||
use tracing::info;
|
use tracing::info;
|
||||||
@ -35,23 +34,26 @@ async fn word() -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn log_ip(req: Request<Body>, next: Next) -> Response {
|
async fn log_ip(req: Request<Body>, next: Next) -> Response {
|
||||||
|
let mut head = "x-forwarded-for";
|
||||||
let ip = req
|
let ip = req
|
||||||
.headers()
|
.headers()
|
||||||
.get("x-forwarded-for")
|
.get(head)
|
||||||
.and_then(|hv| hv.to_str().ok())
|
.and_then(|hv| hv.to_str().ok())
|
||||||
.or_else(|| {
|
.or_else(|| {
|
||||||
req.headers()
|
head = "x-real-ip";
|
||||||
.get("x-real-ip")
|
req.headers().get(head).and_then(|hv| hv.to_str().ok())
|
||||||
.and_then(|hv| hv.to_str().ok())
|
|
||||||
})
|
})
|
||||||
.or_else(|| {
|
.or_else(|| {
|
||||||
req.headers()
|
head = "cf-connection-ip";
|
||||||
.get("cf-connecting-ip")
|
req.headers().get(head).and_then(|hv| hv.to_str().ok())
|
||||||
.and_then(|hv| hv.to_str().ok())
|
|
||||||
})
|
})
|
||||||
.unwrap_or("Unknown");
|
.unwrap_or("Unknown");
|
||||||
|
|
||||||
info!(PATH = req.uri().path().to_string(), IP = ip);
|
if format!("{ip}") == "Unknown" {
|
||||||
|
head = "none"
|
||||||
|
}
|
||||||
|
|
||||||
|
info!(PATH = req.uri().path().to_string(), IP = ip, HEADER = head);
|
||||||
|
|
||||||
next.run(req).await
|
next.run(req).await
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user