Skip to content

Commit 2d96509

Browse files
committed
fix(log): fix start time being incorrect on some proxies
1 parent 47884e3 commit 2d96509

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/api/ffi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub unsafe extern "C" fn redirectionio_api_create_log_in_json(
4848
let request = &*_request;
4949
let response_headers = header_map_to_http_headers(_response_headers);
5050

51-
let log = Log::from_proxy(request, code, &response_headers, action, proxy, time, client_ip, None);
51+
let log = Log::from_proxy(request, code, &response_headers, action, proxy, time as u128, client_ip, None);
5252

5353
let log_serialized = match json_encode(&log) {
5454
Err(_) => return null(),

src/api/log.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ use std::time::{SystemTime, UNIX_EPOCH};
77
pub struct Log {
88
code: u16,
99
to: String,
10-
time: u64,
10+
time: u128,
1111
proxy: String,
1212
ips: Option<Vec<String>>,
1313
from: FromLog,
14+
duration: Option<u128>,
1415
}
1516

1617
#[derive(Serialize, Deserialize, Debug, Clone)]
@@ -46,7 +47,7 @@ impl Log {
4647
pub fn from_legacy(legacy: LegacyLog, proxy: String) -> Self {
4748
let now = match SystemTime::now().duration_since(UNIX_EPOCH) {
4849
Err(_) => 0,
49-
Ok(time) => time.as_millis() as u64,
50+
Ok(time) => time.as_millis(),
5051
};
5152

5253
Log {
@@ -65,6 +66,7 @@ impl Log {
6566
user_agent: legacy.user_agent,
6667
content_type: None,
6768
},
69+
duration: None,
6870
}
6971
}
7072

@@ -75,7 +77,7 @@ impl Log {
7577
response_headers: &[Header],
7678
action: Option<&Action>,
7779
proxy: &str,
78-
time: u64,
80+
request_start_time: u128,
7981
client_ip: &str,
8082
trusted_proxies: Option<&TrustedProxies>,
8183
) -> Log {
@@ -84,6 +86,10 @@ impl Log {
8486
let mut referer = None;
8587
let mut content_type = None;
8688
let mut ips = Vec::new();
89+
let duration = match SystemTime::now().duration_since(UNIX_EPOCH) {
90+
Err(_) => None,
91+
Ok(time) => time.as_millis().checked_sub(request_start_time),
92+
};
8793

8894
match client_ip.parse::<Addr>() {
8995
Ok(addr) => ips.push(addr.addr),
@@ -157,9 +163,10 @@ impl Log {
157163
code,
158164
from,
159165
proxy: proxy.to_string(),
160-
time,
166+
time: request_start_time,
161167
ips: Some(untrusted_ips.iter().map(|ip| ip.to_string()).collect()),
162168
to: location.unwrap_or_default(),
169+
duration,
163170
}
164171
}
165172
}

0 commit comments

Comments
 (0)