Use proper IP address when behind proxy

This commit is contained in:
Shav Kinderlehrer 2024-04-07 21:06:21 -04:00
parent 2faf289829
commit 938be8949f
3 changed files with 17 additions and 3 deletions

2
Cargo.lock generated
View File

@ -194,7 +194,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chela"
version = "1.1.0"
version = "1.2.0"
dependencies = [
"axum",
"color-eyre",

View File

@ -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

View File

@ -41,7 +41,21 @@ pub async fn id_unix(
Extension(state): Extension<ServerState>,
Path(id): Path<String>,
) -> 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
}