Reflect the suggestion.

This commit is contained in:
Moriyoshi Koizumi 2021-07-28 09:07:57 +09:00
commit c819a9a85c
2 changed files with 9 additions and 2 deletions

View file

@ -30,6 +30,12 @@ impl IpNetworks {
} }
} }
impl From<Vec<IpAddr>> for IpNetworks {
fn from(addrs: Vec<IpAddr>) -> Self {
Self::from_ipaddr_iter(addrs.iter())
}
}
impl From<&Vec<IpAddr>> for IpNetworks { impl From<&Vec<IpAddr>> for IpNetworks {
fn from(addrs: &Vec<IpAddr>) -> Self { fn from(addrs: &Vec<IpAddr>) -> Self {
Self::from_ipaddr_iter(addrs.iter()) Self::from_ipaddr_iter(addrs.iter())
@ -65,8 +71,9 @@ impl FromIterator<IpNetwork> for IpNetworks {
/// .map(|addr: Option<IpAddr>| format!("Hello {}", addr.unwrap())); /// .map(|addr: Option<IpAddr>| format!("Hello {}", addr.unwrap()));
/// ``` /// ```
pub fn real_ip( pub fn real_ip(
trusted_proxies: IpNetworks, trusted_proxies: impl Into<IpNetworks>,
) -> impl Filter<Extract = (Option<IpAddr>,), Error = Infallible> + Clone { ) -> impl Filter<Extract = (Option<IpAddr>,), Error = Infallible> + Clone {
let trusted_proxies = trusted_proxies.into();
remote().and(get_forwarded_for()).map( remote().and(get_forwarded_for()).map(
move |addr: Option<SocketAddr>, forwarded_for: Vec<IpAddr>| { move |addr: Option<SocketAddr>, forwarded_for: Vec<IpAddr>| {
addr.map(|addr| { addr.map(|addr| {

View file

@ -4,7 +4,7 @@ use warp_real_ip::real_ip;
fn serve<'a>(trusted: Vec<IpAddr>) -> impl Filter<Extract = (String,)> + 'a { fn serve<'a>(trusted: Vec<IpAddr>) -> impl Filter<Extract = (String,)> + 'a {
warp::any() warp::any()
.and(real_ip((&trusted).into())) .and(real_ip(trusted))
.map(|addr: Option<IpAddr>| addr.unwrap().to_string()) .map(|addr: Option<IpAddr>| addr.unwrap().to_string())
} }