Skip to content

Commit 021d243

Browse files
committed
Add upload logs
Signed-off-by: Denys Fedoryshchenko <denys.f@collabora.com>
1 parent 4605e4e commit 021d243

1 file changed

Lines changed: 61 additions & 3 deletions

File tree

src/main.rs

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ mod storjwt;
1919

2020
use axum::{
2121
body::Body,
22-
extract::{ConnectInfo, DefaultBodyLimit, Multipart, Path, State},
22+
extract::{ConnectInfo, DefaultBodyLimit, Multipart, OriginalUri, Path, State},
2323
http::{header, Method, StatusCode},
2424
response::IntoResponse,
2525
routing::{get, post},
@@ -155,6 +155,40 @@ fn get_driver_type() -> String {
155155
.to_string()
156156
}
157157

158+
fn client_ip_from_headers(headers: &HeaderMap, fallback: SocketAddr) -> String {
159+
if let Some(forwarded_for) = headers
160+
.get("X-Forwarded-For")
161+
.and_then(|value| value.to_str().ok())
162+
{
163+
if let Some(first_ip) = forwarded_for
164+
.split(',')
165+
.map(|part| part.trim())
166+
.find(|part| !part.is_empty())
167+
{
168+
return first_ip.to_string();
169+
}
170+
}
171+
172+
if let Some(forwarded) = headers
173+
.get("Forwarded")
174+
.and_then(|value| value.to_str().ok())
175+
{
176+
for entry in forwarded.split(',') {
177+
for directive in entry.split(';') {
178+
let directive = directive.trim();
179+
if let Some(value) = directive.strip_prefix("for=") {
180+
let cleaned = value.trim_matches('"');
181+
if !cleaned.is_empty() {
182+
return cleaned.to_string();
183+
}
184+
}
185+
}
186+
}
187+
}
188+
189+
fallback.ip().to_string()
190+
}
191+
158192
/// Initial variables configuration and checks
159193
async fn initial_setup() -> Option<RustlsConfig> {
160194
let cache_dir = "cache";
@@ -412,6 +446,8 @@ fn verify_upload_permissions(owner: &str, path: &str) -> Result<(), String> {
412446
If the token is correct, it will write the content of the file to the server
413447
*/
414448
async fn ax_post_file(
449+
ConnectInfo(remote_addr): ConnectInfo<SocketAddr>,
450+
OriginalUri(original_uri): OriginalUri,
415451
headers: HeaderMap,
416452
State(state): State<AppState>,
417453
mut multipart: Multipart,
@@ -512,14 +548,36 @@ async fn ax_post_file(
512548
};
513549

514550
// TBD
515-
let message = write_file_driver(full_path, file0, content_type.to_string(), Some(owner));
551+
let upload_size = file0.len();
552+
let message = write_file_driver(
553+
full_path.clone(),
554+
file0,
555+
content_type.to_string(),
556+
Some(owner.clone()),
557+
);
516558
if !message.is_empty() {
517559
return (StatusCode::CONFLICT, Vec::new());
518560
}
561+
let status = StatusCode::OK;
562+
let client_ip = client_ip_from_headers(&headers, remote_addr);
563+
let timestamp = std::time::SystemTime::now();
564+
let human_time = chrono::DateTime::<chrono::Utc>::from(timestamp);
565+
let request_target = original_uri.to_string();
566+
println!(
567+
"{} {} {} {} {} {} {} {}",
568+
client_ip,
569+
status.as_u16(),
570+
upload_size,
571+
human_time,
572+
Method::POST,
573+
request_target,
574+
full_path,
575+
owner
576+
);
519577
// write metadata file into cache directory
520578
//let metadata_filename = format!("{}/{}.metadata", path, file0_filename);
521579
//write_cache_metadata(metadata_filename, file0.len());
522-
(StatusCode::OK, Vec::new())
580+
(status, Vec::new())
523581
}
524582

525583
fn filename_from_fullpath(filepath: &str) -> String {

0 commit comments

Comments
 (0)