Use correct IP in logs

This commit is contained in:
Shav Kinderlehrer 2024-04-07 14:59:07 -04:00
parent 1075ab0ed1
commit 2405c9cf31
2 changed files with 23 additions and 16 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "chela"
version = "0.3.0"
version = "0.3.1"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -43,7 +43,10 @@ pub async fn id(
Path(id): Path<String>,
) -> impl IntoResponse {
let mut show_request = false;
log!("Request for '{}' from {}", id.clone(), addr.ip());
let ip = get_ip(headers.clone(), addr, state.clone())
.await
.unwrap_or_default();
log!("Request for '{}' from {}", id.clone(), ip);
let mut use_id = id;
if use_id.ends_with('+') {
show_request = true;
@ -93,20 +96,7 @@ pub async fn id(
async fn save_analytics(headers: HeaderMap, item: UrlRow, addr: SocketAddr, state: ServerState) {
let id = item.id;
let ip: Option<String> = 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(addr.ip().to_string())
};
let ip = get_ip(headers.clone(), addr, state.clone()).await;
let referer = match headers.get("referer") {
Some(it) => {
if let Ok(i) = it.to_str() {
@ -146,6 +136,23 @@ VALUES ($1,$2,$3,$4)
}
}
async fn get_ip(headers: HeaderMap, addr: SocketAddr, state: ServerState) -> Option<String> {
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(addr.ip().to_string())
}
}
pub async fn create_id(Extension(state): Extension<ServerState>) -> Html<String> {
Html(format!(
r#"