Skip to content

Commit 00e4cbf

Browse files
authored
Merge pull request #4624 from jolicode/feature/client-ip-logging
Log client ip + config
2 parents 6f53bb3 + 6bcfe65 commit 00e4cbf

File tree

5 files changed

+80
-53
lines changed

5 files changed

+80
-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: 22 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,22 @@ 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(
33+
request: &Request,
34+
code: u16,
35+
response_headers: &[Header],
36+
action: Option<&Action>,
37+
proxy: &str,
38+
time: u64,
39+
client_ip: &str,
40+
) -> Log {
3241
let mut location = None;
3342
let mut user_agent = None;
3443
let mut referer = None;
3544
let mut content_type = None;
45+
let mut ips = Vec::new();
46+
47+
ips.push(client_ip.to_string());
3648

3749
for header in &request.headers {
3850
if header.name.to_lowercase() == "user-agent" {
@@ -42,6 +54,14 @@ impl Log {
4254
if header.name.to_lowercase() == "referer" {
4355
referer = Some(header.value.clone())
4456
}
57+
58+
if header.name.to_lowercase() == "x-forwarded-for" {
59+
let forwarded_ips = header.value.split(",");
60+
61+
for forwarded_ip in forwarded_ips {
62+
ips.push(forwarded_ip.trim().to_string());
63+
}
64+
}
4565
}
4666

4767
for header in response_headers {
@@ -76,6 +96,7 @@ impl Log {
7696
from,
7797
proxy: proxy.to_string(),
7898
time,
99+
ips: Some(ips),
79100
to: location.unwrap_or_else(|| "".to_string()),
80101
}
81102
}

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)