Hopefully log proxied client ip address

This commit is contained in:
Tony Klink 2024-01-15 13:26:03 -06:00
parent ec0b068388
commit 17932c8d7f
Signed by: klink
GPG key ID: 85175567C4D19231
5 changed files with 34 additions and 10 deletions

View file

@ -10,7 +10,7 @@ argon2 = "0.5.2"
async-trait = "0.1.73" async-trait = "0.1.73"
chrono = "0.4.31" chrono = "0.4.31"
tokio = { version = "1", features = ["full"] } tokio = { version = "1", features = ["full"] }
warp = { varsion = "0.3.3", features = ["tls"] } warp = { version = "0.3.3", features = ["tls"] }
validator = { version = "0.16", features = ["derive"] } validator = { version = "0.16", features = ["derive"] }
tokio-stream = "0.1.14" tokio-stream = "0.1.14"
futures-util = "0.3.28" futures-util = "0.3.28"

View file

@ -98,6 +98,7 @@ in {
locations."/" = { locations."/" = {
proxyPass = "http://${cfg.localAddress}:8080"; proxyPass = "http://${cfg.localAddress}:8080";
proxyWebsockets = true; # needed if you need to use WebSocket proxyWebsockets = true; # needed if you need to use WebSocket
recommendedProxySettings = true;
}; };
}; };
}; };

View file

@ -6,7 +6,10 @@ use warp::{Rejection, Reply};
pub async fn ws_handler( pub async fn ws_handler(
ws: warp::ws::Ws, ws: warp::ws::Ws,
context: Context, context: Context,
client_addr: Option<SocketAddr>, client_ip: Option<SocketAddr>,
real_client_ip: Option<SocketAddr>,
) -> Result<impl Reply, Rejection> { ) -> Result<impl Reply, Rejection> {
Ok(ws.on_upgrade(move |socket| ws::client_connection(socket, context, client_addr))) Ok(ws.on_upgrade(move |socket| {
ws::client_connection(socket, context, client_ip, real_client_ip)
}))
} }

View file

@ -10,20 +10,32 @@ pub fn routes(context: Context) -> impl Filter<Extract = impl Reply, Error = Rej
} }
fn index(context: Context) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone { fn index(context: Context) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
let client_addr = warp::addr::remote(); let client_ip = warp::addr::remote();
let real_client_ip = warp::header::optional::<std::net::SocketAddr>("X-Real-IP")
.or(warp::header::optional::<std::net::SocketAddr>(
"X-Forwarded-For",
))
.unify()
.map(|ip: Option<std::net::SocketAddr>| {
// Get the IP from either header,
// and unify into the inner type.
ip
});
warp::path::end() warp::path::end()
.and(warp::ws()) .and(warp::ws())
.and(with_context(context)) .and(with_context(context))
.and(client_addr) .and(client_ip)
.and(real_client_ip)
.and_then(handler::ws_handler) .and_then(handler::ws_handler)
} }
fn static_files() -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone { fn static_files() -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
let mut foo = std::env::current_exe().unwrap(); let mut exe_dir = std::env::current_exe().unwrap();
foo.pop(); exe_dir.pop();
let mut www = foo.clone(); let mut www = exe_dir.clone();
www.pop(); www.pop();
www.push(std::path::Path::new("www/static")); www.push(std::path::Path::new("www/static"));

View file

@ -13,14 +13,22 @@ use warp::ws::{Message, WebSocket};
use futures_util::SinkExt; use futures_util::SinkExt;
pub async fn client_connection(ws: WebSocket, context: Context, client_addr: Option<SocketAddr>) { pub async fn client_connection(
ws: WebSocket,
context: Context,
client_ip: Option<SocketAddr>,
real_client_ip: Option<SocketAddr>,
) {
let (mut ws_sender, mut ws_receiver) = ws.split(); let (mut ws_sender, mut ws_receiver) = ws.split();
let (client_sender, client_receiver) = mpsc::unbounded_channel(); let (client_sender, client_receiver) = mpsc::unbounded_channel();
let mut client_receiver = UnboundedReceiverStream::new(client_receiver); let mut client_receiver = UnboundedReceiverStream::new(client_receiver);
// Create and Add to the Context new Client and set its sender // Create and Add to the Context new Client and set its sender
let ip = client_addr.unwrap().ip().to_string(); let ip = real_client_ip
.unwrap_or(client_ip.unwrap())
.ip()
.to_string();
let mut client = Client::new(ip); let mut client = Client::new(ip);
client.client_connection = Some(client_sender); client.client_connection = Some(client_sender);