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

@ -6,7 +6,10 @@ use warp::{Rejection, Reply};
pub async fn ws_handler(
ws: warp::ws::Ws,
context: Context,
client_addr: Option<SocketAddr>,
client_ip: Option<SocketAddr>,
real_client_ip: Option<SocketAddr>,
) -> 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 {
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()
.and(warp::ws())
.and(with_context(context))
.and(client_addr)
.and(client_ip)
.and(real_client_ip)
.and_then(handler::ws_handler)
}
fn static_files() -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
let mut foo = std::env::current_exe().unwrap();
foo.pop();
let mut exe_dir = std::env::current_exe().unwrap();
exe_dir.pop();
let mut www = foo.clone();
let mut www = exe_dir.clone();
www.pop();
www.push(std::path::Path::new("www/static"));

View file

@ -13,14 +13,22 @@ use warp::ws::{Message, WebSocket};
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 (client_sender, client_receiver) = mpsc::unbounded_channel();
let mut client_receiver = UnboundedReceiverStream::new(client_receiver);
// 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);
client.client_connection = Some(client_sender);