Use correct IP in logs
This commit is contained in:
parent
1075ab0ed1
commit
2405c9cf31
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "chela"
|
name = "chela"
|
||||||
version = "0.3.0"
|
version = "0.3.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
37
src/get.rs
37
src/get.rs
@ -43,7 +43,10 @@ pub async fn id(
|
|||||||
Path(id): Path<String>,
|
Path(id): Path<String>,
|
||||||
) -> impl IntoResponse {
|
) -> impl IntoResponse {
|
||||||
let mut show_request = false;
|
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;
|
let mut use_id = id;
|
||||||
if use_id.ends_with('+') {
|
if use_id.ends_with('+') {
|
||||||
show_request = true;
|
show_request = true;
|
||||||
@ -93,20 +96,7 @@ pub async fn id(
|
|||||||
|
|
||||||
async fn save_analytics(headers: HeaderMap, item: UrlRow, addr: SocketAddr, state: ServerState) {
|
async fn save_analytics(headers: HeaderMap, item: UrlRow, addr: SocketAddr, state: ServerState) {
|
||||||
let id = item.id;
|
let id = item.id;
|
||||||
let ip: Option<String> = if state.behind_proxy {
|
let ip = get_ip(headers.clone(), addr, state.clone()).await;
|
||||||
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 referer = match headers.get("referer") {
|
let referer = match headers.get("referer") {
|
||||||
Some(it) => {
|
Some(it) => {
|
||||||
if let Ok(i) = it.to_str() {
|
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> {
|
pub async fn create_id(Extension(state): Extension<ServerState>) -> Html<String> {
|
||||||
Html(format!(
|
Html(format!(
|
||||||
r#"
|
r#"
|
||||||
|
Loading…
Reference in New Issue
Block a user