Hopefully log proxied client ip address
This commit is contained in:
		
							parent
							
								
									ec0b068388
								
							
						
					
					
						commit
						17932c8d7f
					
				
					 5 changed files with 34 additions and 10 deletions
				
			
		|  | @ -10,7 +10,7 @@ argon2 = "0.5.2" | |||
| async-trait = "0.1.73" | ||||
| chrono = "0.4.31"        | ||||
| 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"] } | ||||
| tokio-stream = "0.1.14" | ||||
| futures-util = "0.3.28" | ||||
|  |  | |||
|  | @ -98,6 +98,7 @@ in { | |||
|         locations."/" = { | ||||
|           proxyPass = "http://${cfg.localAddress}:8080"; | ||||
|           proxyWebsockets = true; # needed if you need to use WebSocket | ||||
|           recommendedProxySettings = true; | ||||
|         }; | ||||
|       }; | ||||
|     }; | ||||
|  |  | |||
|  | @ -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) | ||||
|     })) | ||||
| } | ||||
|  |  | |||
|  | @ -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")); | ||||
| 
 | ||||
|  |  | |||
|  | @ -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); | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue