diff options
author | Jonathan Davies <jpds@protonmail.com> | 2023-02-04 09:49:56 +0000 |
---|---|---|
committer | Jonathan Davies <jpds@protonmail.com> | 2023-02-04 15:19:21 +0000 |
commit | 94d70bec6962046377519521bd53dfcac37e3c3b (patch) | |
tree | 320686c441253ffd43bc632455465355732e97c9 | |
parent | fba8224cf00f7c542150e48a88c22025ff03b948 (diff) | |
download | garage-94d70bec6962046377519521bd53dfcac37e3c3b.tar.gz garage-94d70bec6962046377519521bd53dfcac37e3c3b.zip |
generic_server.rs: Added support for logging X-Forwarded-For header.
Fixes: #460
-rw-r--r-- | src/api/generic_server.rs | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/src/api/generic_server.rs b/src/api/generic_server.rs index 62fe4e5a..aa90868a 100644 --- a/src/api/generic_server.rs +++ b/src/api/generic_server.rs @@ -125,7 +125,26 @@ impl<A: ApiHandler> ApiServer<A> { addr: SocketAddr, ) -> Result<Response<Body>, GarageError> { let uri = req.uri().clone(); - info!("{} {} {}", addr, req.method(), uri); + + let has_forwarded_for_header = req.headers().contains_key("x-forwarded-for"); + if has_forwarded_for_header { + let forwarded_for_ip_addr = &req + .headers() + .get("x-forwarded-for") + .expect("Could not parse X-Forwarded-For header") + .to_str() + .unwrap_or_default(); + + info!( + "{} (via {}) {} {}", + forwarded_for_ip_addr, + addr, + req.method(), + uri + ); + } else { + info!("{} {} {}", addr, req.method(), uri); + } debug!("{:?}", req); let tracer = opentelemetry::global::tracer("garage"); |