From 9d7a931045353effa6d20d6fbc00e107eb1241f9 Mon Sep 17 00:00:00 2001 From: itsscb Date: Sun, 25 Aug 2024 20:50:39 +0200 Subject: [PATCH] fix: add souce header to log --- src/main.rs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0522792..4cc8ce8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,6 @@ use axum::{ }; use http::Method; use rand::seq::SliceRandom; -use std::net::SocketAddr; use tower_http::cors::{Any, CorsLayer}; use tower_http::services::ServeDir; use tracing::info; @@ -35,23 +34,26 @@ async fn word() -> String { } async fn log_ip(req: Request, next: Next) -> Response { + let mut head = "x-forwarded-for"; let ip = req .headers() - .get("x-forwarded-for") + .get(head) .and_then(|hv| hv.to_str().ok()) .or_else(|| { - req.headers() - .get("x-real-ip") - .and_then(|hv| hv.to_str().ok()) + head = "x-real-ip"; + req.headers().get(head).and_then(|hv| hv.to_str().ok()) }) .or_else(|| { - req.headers() - .get("cf-connecting-ip") - .and_then(|hv| hv.to_str().ok()) + head = "cf-connection-ip"; + req.headers().get(head).and_then(|hv| hv.to_str().ok()) }) .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 }