From aeb905357e0d7828a8bd25ced62d1e7e8e64bcd7 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Wed, 1 Jul 2020 06:08:06 -0400 Subject: [PATCH] this should finally report the correct IP --- src/main.rs | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index 33a2a83..b731b52 100644 --- a/src/main.rs +++ b/src/main.rs @@ -131,20 +131,26 @@ impl Router { fn speak_proxy(req: Request, remote_addr: SocketAddr, router: Arc, endpoint: &'static str) -> BoxFut { let mut headers = req.headers().clone(); - for (k, v) in headers.iter() { - info!("Header: {:?} = {:?}", k, v); - } - - // This is the correct client IP, not the IP of the load balancer. - // We're adding it to the headers so that downstream can use it for rate limiting and logging. - let forwarded_ip = HeaderValue::from_str(&remote_addr.ip().to_string()) + // This is the IP of the Digital Ocean load balancer. + let gateway_ip = HeaderValue::from_str(&remote_addr.ip().to_string()) .ok() - .unwrap_or(HeaderValue::from_static("127.0.0.1")); - headers.insert(HeaderName::from_static("forwarded"), forwarded_ip.clone()); - headers.insert(HeaderName::from_static("x-forwarded-for"), forwarded_ip.clone()); + .unwrap_or(HeaderValue::from_static("")); + + // This is the IP of the upstream client browser. + let upstream_client_ip = req.headers().get(HeaderName::from_static("x-forwarded-for")) + .map(|ip| ip.to_str().unwrap_or("")) + .unwrap_or(""); + + let upstream_client_ip = HeaderValue::from_str(upstream_client_ip) + .ok() + .unwrap_or(HeaderValue::from_static("")); + + headers.insert(HeaderName::from_static("forwarded"), upstream_client_ip.clone()); + headers.insert(HeaderName::from_static("x-forwarded-for"), upstream_client_ip.clone()); // Unfortunately it looks like this middleware trounces the standard headers, so // here we put it somewhere it won't be overwritten. - headers.insert(HeaderName::from_static("x-voder-proxy-for"), forwarded_ip.clone()); + headers.insert(HeaderName::from_static("x-voder-proxy-for"), upstream_client_ip.clone()); + headers.insert(HeaderName::from_static("x-gateway-ip"), gateway_ip); Box::new(req.into_body().concat2().map(move |b| { // Builds a BoxedFut to return let request_bytes = b.as_ref();