diff --git a/contrib/chorus.nginx.com b/contrib/chorus.nginx.com index 231c29e..5c64fc8 100644 --- a/contrib/chorus.nginx.com +++ b/contrib/chorus.nginx.com @@ -23,7 +23,7 @@ server { proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "Upgrade"; proxy_set_header Host $host; - proxy_set_header X-Forwarded-For $remote_addr; + proxy_set_header X-Real-Ip $remote_addr; proxy_read_timeout 1d; proxy_send_timeout 1d; } diff --git a/src/main.rs b/src/main.rs index d1082e7..b0709a4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,7 +30,7 @@ use std::error::Error as StdError; use std::fs::OpenOptions; use std::future::Future; use std::io::Read; -use std::net::SocketAddr; +use std::net::{IpAddr, SocketAddr}; use std::pin::Pin; use std::sync::atomic::Ordering; use std::task::{Context, Poll}; @@ -224,7 +224,21 @@ impl Service> for HttpService { // This is called for each HTTP request made by the client // NOTE: it is not called for each websocket message once upgraded. fn call(&mut self, req: Request) -> Self::Future { - let peer = self.peer; + let mut peer = self.peer; + + // If chorus is behind a proxy that sets an "X-Real-Ip" header, we use + // that ip address instead (otherwise their log file will just say "127.0.0.1" + // for every peer) + if peer.ip().is_loopback() { + if let Some(rip) = req.headers().get("x-real-ip") { + if let Ok(ripstr) = rip.to_str() { + if let Ok(ipaddr) = ripstr.parse::() { + peer.set_ip(ipaddr); + } + } + } + } + Box::pin(async move { handle_http_request(peer, req).await }) } }