Skip to content

Commit cf7aa5f

Browse files
authored
Merge pull request #26 from nuclearcat/get-client-ip
logging: Get client IP from headers as we operate behind proxy (k8s/docker/nginx)
2 parents d1dcdbf + 0816a0a commit cf7aa5f

3 files changed

Lines changed: 695 additions & 16 deletions

File tree

Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,11 @@ tower = "0.5.2"
3636
tower-http = { version = "0.6.2", features = ["trace", "fs", "limit"] }
3737
tracing = "0.1.41"
3838
tracing-subscriber = "0.3.19"
39+
40+
[dev-dependencies]
41+
reqwest = { version = "0.12.9", features = ["blocking", "multipart"] }
42+
hmac = "0.12.1"
43+
jwt = "0.16.0"
44+
sha2 = "0.10.8"
45+
tokio = { version = "1.42.0", features = ["rt", "rt-multi-thread", "macros", "time", "process"] }
46+
tempfile = "3.14.0"

src/main.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,10 @@ async fn main() {
399399
logging::init(get_args().verbose);
400400
tracing_subscriber::fmt::init();
401401
let tlscfg = initial_setup().await;
402-
let port = 3000;
402+
let port: u16 = std::env::var("KCI_STORAGE_PORT")
403+
.ok()
404+
.and_then(|p| p.parse().ok())
405+
.unwrap_or(3000);
403406
let state = AppState {
404407
file_locks: Arc::new(RwLock::new(HashMap::new())),
405408
};
@@ -436,7 +439,8 @@ async fn main() {
436439
} else {
437440
//let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
438441
//axum::serve(listener, app).await.unwrap();
439-
axum_server::bind("0.0.0.0:3000".parse().unwrap())
442+
let addr = SocketAddr::from(([0, 0, 0, 0], port));
443+
axum_server::bind(addr)
440444
.serve(app.into_make_service_with_connect_info::<SocketAddr>())
441445
.await
442446
.unwrap();
@@ -963,6 +967,8 @@ async fn ax_get_file(
963967
None => "",
964968
};
965969

970+
let client_ip = client_ip_from_headers(&rxheaders, remote_addr);
971+
966972
let semaphore = get_or_create_semaphore(&state.file_locks, &filepath).await;
967973
// Wait for permit with timeout
968974
let _permit =
@@ -986,8 +992,8 @@ async fn ax_get_file(
986992

987993
if !received_file.valid {
988994
println!(
989-
"{:?} 404 0 {} {} {} {}",
990-
remote_addr, human_time, method, filepath, user_agent_str
995+
"{} 404 0 {} {} {} {}",
996+
client_ip, human_time, method, filepath, user_agent_str
991997
);
992998
return (StatusCode::NOT_FOUND, format!("Not Found: {}", filepath)).into_response();
993999
}
@@ -1033,8 +1039,8 @@ async fn ax_get_file(
10331039
if let Some(etag) = upstream_headers.get(ETAG) {
10341040
if if_none_match == etag {
10351041
println!(
1036-
"{:?} 304 0 {} {} {} {}",
1037-
remote_addr, human_time, method, filepath, user_agent_str
1042+
"{} 304 0 {} {} {} {}",
1043+
client_ip, human_time, method, filepath, user_agent_str
10381044
);
10391045
return (StatusCode::NOT_MODIFIED, headers, Body::empty()).into_response();
10401046
}
@@ -1045,8 +1051,8 @@ async fn ax_get_file(
10451051
// TODO: Validate properly last_modified
10461052
if if_modified_since == last_modified {
10471053
println!(
1048-
"{:?} 304 0 {} {} {} {}",
1049-
remote_addr, human_time, method, filepath, user_agent_str
1054+
"{} 304 0 {} {} {} {}",
1055+
client_ip, human_time, method, filepath, user_agent_str
10501056
);
10511057
return (StatusCode::NOT_MODIFIED, headers, Body::empty()).into_response();
10521058
}
@@ -1059,8 +1065,8 @@ async fn ax_get_file(
10591065
headers.insert(header::CONTENT_LENGTH, val);
10601066
}
10611067
println!(
1062-
"{:?} 200 0 {} {} {} {}",
1063-
remote_addr, human_time, method, filepath, user_agent_str
1068+
"{} 200 0 {} {} {} {}",
1069+
client_ip, human_time, method, filepath, user_agent_str
10641070
);
10651071
return (headers, Body::empty()).into_response();
10661072
}
@@ -1114,14 +1120,14 @@ async fn ax_get_file(
11141120
if start != 0 {
11151121
let body_size = end - start;
11161122
println!(
1117-
"{:?} 206 {} {} {} {} {}",
1118-
remote_addr, body_size, human_time, method, filepath, user_agent_str
1123+
"{} 206 {} {} {} {} {}",
1124+
client_ip, body_size, human_time, method, filepath, user_agent_str
11191125
);
11201126
return (StatusCode::PARTIAL_CONTENT, headers, axbody).into_response();
11211127
}
11221128
println!(
1123-
"{:?} 200 {} {} {} {} {}",
1124-
remote_addr,
1129+
"{} 200 {} {} {} {} {}",
1130+
client_ip,
11251131
metadata.len(),
11261132
human_time,
11271133
method,
@@ -1133,8 +1139,8 @@ async fn ax_get_file(
11331139
Err(_) => {
11341140
eprintln!("Error opening file in ax_get_file");
11351141
println!(
1136-
"{:?} 404 0 {} {} {} {}",
1137-
remote_addr, human_time, method, filepath, user_agent_str
1142+
"{} 404 0 {} {} {} {}",
1143+
client_ip, human_time, method, filepath, user_agent_str
11381144
);
11391145
(StatusCode::NOT_FOUND, headers, Body::empty()).into_response()
11401146
}

0 commit comments

Comments
 (0)