Skip to content

Commit e1d1aa8

Browse files
committed
Add client ip logging on proxy
1 parent e15b324 commit e1d1aa8

File tree

5 files changed

+72
-53
lines changed

5 files changed

+72
-53
lines changed

src/api/ffi.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,19 @@ pub unsafe extern "C" fn redirectionio_api_create_log_in_json(
3636
_action: *mut Action,
3737
_proxy: *const c_char,
3838
time: u64,
39+
_client_ip: *const c_char,
3940
) -> *const c_char {
4041
if _request.is_null() {
4142
return null();
4243
}
4344

4445
let proxy = c_char_to_str(_proxy).unwrap_or("");
46+
let client_ip = c_char_to_str(_client_ip).unwrap_or("");
4547
let action = if _action.is_null() { None } else { Some(&*_action) };
4648
let request = &*_request;
4749
let response_headers = header_map_to_http_headers(_response_headers);
4850

49-
let log = Log::from_proxy(request, code, &response_headers, action, proxy, time);
51+
let log = Log::from_proxy(request, code, &response_headers, action, proxy, time, client_ip);
5052

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

src/api/log.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub struct Log {
99
to: String,
1010
time: u64,
1111
proxy: String,
12+
ips: Option<Vec<String>>,
1213
from: FromLog,
1314
}
1415

@@ -28,11 +29,14 @@ struct FromLog {
2829
}
2930

3031
impl Log {
31-
pub fn from_proxy(request: &Request, code: u16, response_headers: &[Header], action: Option<&Action>, proxy: &str, time: u64) -> Log {
32+
pub fn from_proxy(request: &Request, code: u16, response_headers: &[Header], action: Option<&Action>, proxy: &str, time: u64, client_ip: &str) -> Log {
3233
let mut location = None;
3334
let mut user_agent = None;
3435
let mut referer = None;
3536
let mut content_type = None;
37+
let mut ips = Vec::new();
38+
39+
ips.push(client_ip.to_string());
3640

3741
for header in &request.headers {
3842
if header.name.to_lowercase() == "user-agent" {
@@ -42,6 +46,14 @@ impl Log {
4246
if header.name.to_lowercase() == "referer" {
4347
referer = Some(header.value.clone())
4448
}
49+
50+
if header.name.to_lowercase() == "x-forwarded-for" {
51+
let forwarded_ips = header.value.split(",");
52+
53+
for forwarded_ip in forwarded_ips {
54+
ips.push(forwarded_ip.trim().to_string());
55+
}
56+
}
4557
}
4658

4759
for header in response_headers {
@@ -76,6 +88,7 @@ impl Log {
7688
from,
7789
proxy: proxy.to_string(),
7890
time,
91+
ips: Some(ips),
7992
to: location.unwrap_or_else(|| "".to_string()),
8093
}
8194
}

src/api/wasm.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub fn create_log_in_json(
1212
action: &Action,
1313
proxy: String,
1414
time: u64,
15+
client_ip: String,
1516
) -> String {
1617
let log = Log::from_proxy(
1718
&request.request,
@@ -20,6 +21,7 @@ pub fn create_log_in_json(
2021
action.action.as_ref(),
2122
proxy.as_str(),
2223
time,
24+
client_ip.as_str(),
2325
);
2426

2527
match json_encode(&log) {

0 commit comments

Comments
 (0)