Skip to content

Commit

Permalink
Merge pull request #4624 from jolicode/feature/client-ip-logging
Browse files Browse the repository at this point in the history
Log client ip + config
  • Loading branch information
joelwurtz committed Jan 29, 2021
2 parents 6f53bb3 + 6bcfe65 commit 00e4cbf
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 53 deletions.
4 changes: 3 additions & 1 deletion src/api/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,19 @@ pub unsafe extern "C" fn redirectionio_api_create_log_in_json(
_action: *mut Action,
_proxy: *const c_char,
time: u64,
_client_ip: *const c_char,
) -> *const c_char {
if _request.is_null() {
return null();
}

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

let log = Log::from_proxy(request, code, &response_headers, action, proxy, time);
let log = Log::from_proxy(request, code, &response_headers, action, proxy, time, client_ip);

let log_serialized = match json_encode(&log) {
Err(_) => return null(),
Expand Down
23 changes: 22 additions & 1 deletion src/api/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct Log {
to: String,
time: u64,
proxy: String,
ips: Option<Vec<String>>,
from: FromLog,
}

Expand All @@ -28,11 +29,22 @@ struct FromLog {
}

impl Log {
pub fn from_proxy(request: &Request, code: u16, response_headers: &[Header], action: Option<&Action>, proxy: &str, time: u64) -> Log {
pub fn from_proxy(
request: &Request,
code: u16,
response_headers: &[Header],
action: Option<&Action>,
proxy: &str,
time: u64,
client_ip: &str,
) -> Log {
let mut location = None;
let mut user_agent = None;
let mut referer = None;
let mut content_type = None;
let mut ips = Vec::new();

ips.push(client_ip.to_string());

for header in &request.headers {
if header.name.to_lowercase() == "user-agent" {
Expand All @@ -42,6 +54,14 @@ impl Log {
if header.name.to_lowercase() == "referer" {
referer = Some(header.value.clone())
}

if header.name.to_lowercase() == "x-forwarded-for" {
let forwarded_ips = header.value.split(",");

for forwarded_ip in forwarded_ips {
ips.push(forwarded_ip.trim().to_string());
}
}
}

for header in response_headers {
Expand Down Expand Up @@ -76,6 +96,7 @@ impl Log {
from,
proxy: proxy.to_string(),
time,
ips: Some(ips),
to: location.unwrap_or_else(|| "".to_string()),
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/api/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub fn create_log_in_json(
action: &Action,
proxy: String,
time: u64,
client_ip: String,
) -> String {
let log = Log::from_proxy(
&request.request,
Expand All @@ -20,6 +21,7 @@ pub fn create_log_in_json(
action.action.as_ref(),
proxy.as_str(),
time,
client_ip.as_str(),
);

match json_encode(&log) {
Expand Down

0 comments on commit 00e4cbf

Please sign in to comment.