initial try

This commit is contained in:
Robin Appelman 2020-12-03 16:09:57 +01:00
commit 2c87a837b0
4 changed files with 1286 additions and 5 deletions

1254
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -3,7 +3,8 @@ name = "warp-real-ip"
version = "0.1.0" version = "0.1.0"
authors = ["Robin Appelman <robin@icewind.nl>"] authors = ["Robin Appelman <robin@icewind.nl>"]
edition = "2018" edition = "2018"
keywords = ["warp"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html categories = ["web-programming::http-server"]
[dependencies] [dependencies]
warp = { version = "0.2" }

29
src/lib.rs Normal file
View 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
}
})
},
)
}

View file

@ -1,3 +0,0 @@
fn main() {
println!("Hello, world!");
}