Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
tiberiuv committed Feb 10, 2025
1 parent 512499b commit 3d64c3e
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,7 @@ pub fn get_client_ip_x_forwarded_for(
.and_then(|mut x_forwarded_headers_ips| {
x_forwarded_headers_ips.push(remote_client_ip);
let mut rev_ips = x_forwarded_headers_ips.into_iter().rev().peekable();
while let Some(ip) = rev_ips.next() {
let trusted = trusted_proxies.iter().any(|proxy| proxy.contains(&ip));
if !trusted || rev_ips.peek().is_none() {
return Some(ip);
}
}
None
rev_ips.find(|ip| !trusted_proxies.iter().any(|proxy| proxy.contains(ip)))
})
.unwrap_or(remote_client_ip)
}
Expand Down Expand Up @@ -228,6 +222,14 @@ mod tests {
),
"127.0.0.2".parse::<IpAddr>().unwrap(),
),
(
(
vec!["192.168.0.0/30".parse().unwrap()],
Some(HeaderValue::from_str("127.0.0.3").unwrap()),
"192.168.0.3".parse().unwrap(),
),
"127.0.0.3".parse::<IpAddr>().unwrap(),
),
(
(
vec!["127.0.0.2/32".parse().unwrap()],
Expand Down

0 comments on commit 3d64c3e

Please sign in to comment.