Skip to content

Commit 21ef7ca

Browse files
committed
Use last IP from X-Forwarded-For header
1 parent 1e86baa commit 21ef7ca

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ edition = "2021"
44
license = "MIT"
55
name = "axum-client-ip"
66
repository = "https://github.com/imbolc/axum-client-ip"
7-
version = "0.3.0"
7+
version = "0.3.1"
88

99
[dependencies]
1010
axum = "0.6"

src/lib.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ fn maybe_x_forwarded_for(headers: &HeaderMap) -> Option<IpAddr> {
8282
headers
8383
.get(X_FORWARDED_FOR)
8484
.and_then(|hv| hv.to_str().ok())
85-
.and_then(|s| s.split(',').find_map(|s| s.trim().parse::<IpAddr>().ok()))
85+
.and_then(|s| {
86+
s.split(',')
87+
.rev()
88+
.find_map(|s| s.trim().parse::<IpAddr>().ok())
89+
})
8690
}
8791

8892
/// Tries to parse the `x-real-ip` header
@@ -149,7 +153,7 @@ mod tests {
149153
.body(Body::empty())
150154
.unwrap();
151155
let res = app().oneshot(req).await.unwrap();
152-
assert_eq!(body_string(res.into_body()).await, "1.1.1.1");
156+
assert_eq!(body_string(res.into_body()).await, "2.2.2.2");
153157
}
154158

155159
#[tokio::test]

0 commit comments

Comments
 (0)