mirror of
https://github.com/icewind1991/warp-real-ip.git
synced 2026-06-03 10:44:07 +02:00
initial try
This commit is contained in:
parent
4c21ea0f75
commit
2c87a837b0
4 changed files with 1286 additions and 5 deletions
1254
Cargo.lock
generated
Normal file
1254
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -3,7 +3,8 @@ name = "warp-real-ip"
|
|||
version = "0.1.0"
|
||||
authors = ["Robin Appelman <robin@icewind.nl>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
keywords = ["warp"]
|
||||
categories = ["web-programming::http-server"]
|
||||
|
||||
[dependencies]
|
||||
warp = { version = "0.2" }
|
||||
29
src/lib.rs
Normal file
29
src/lib.rs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
use std::convert::Infallible;
|
||||
use std::net::{IpAddr, SocketAddr};
|
||||
use warp::filters::addr::remote;
|
||||
use warp::Filter;
|
||||
|
||||
/// Creates a `Filter` that provides the "real ip" of the connected client.
|
||||
pub fn real_ip<'a>(
|
||||
trusted_proxies: &'a [IpAddr],
|
||||
) -> impl Filter<Extract = (Option<IpAddr>,), Error = Infallible> + Clone + 'a {
|
||||
let forwarded_for = warp::header::<IpAddr>("X-FORWARDED-FOR")
|
||||
.or(warp::header("x-real-ip"))
|
||||
.unify()
|
||||
.map(Some)
|
||||
.or(warp::any().map(|| None))
|
||||
.unify();
|
||||
|
||||
remote().and(forwarded_for).map(
|
||||
move |addr: Option<SocketAddr>, forwarded_for: Option<IpAddr>| {
|
||||
addr.map(|addr| {
|
||||
let ip = addr.ip();
|
||||
if trusted_proxies.contains(&ip) {
|
||||
forwarded_for.unwrap_or(ip)
|
||||
} else {
|
||||
ip
|
||||
}
|
||||
})
|
||||
},
|
||||
)
|
||||
}
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue