From 938be8949fbb9735953309611c73589be04a2ccb Mon Sep 17 00:00:00 2001 From: Shav Kinderlehrer Date: Sun, 7 Apr 2024 21:06:21 -0400 Subject: [PATCH] Use proper IP address when behind proxy --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/get.rs | 16 +++++++++++++++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 15a7252..dd371e4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -194,7 +194,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "chela" -version = "1.1.0" +version = "1.2.0" dependencies = [ "axum", "color-eyre", diff --git a/Cargo.toml b/Cargo.toml index 7f8867f..9e5ef6e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "chela" -version = "1.1.0" +version = "1.2.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/get.rs b/src/get.rs index efe2711..a00ac48 100644 --- a/src/get.rs +++ b/src/get.rs @@ -41,7 +41,21 @@ pub async fn id_unix( Extension(state): Extension, Path(id): Path, ) -> impl IntoResponse { - let ip = format!("{:?}", addr.peer_addr); + let ip = if state.behind_proxy { + match headers.get("x-real-ip") { + Some(it) => { + if let Ok(i) = it.to_str() { + Some(i.to_string()) + } else { + None + } + } + None => None, + } + } else { + Some(format!("{:?}", addr.peer_addr)) + } + .unwrap_or_default(); run_id(headers, ip, state, id).await }