httproxide 0.2.0

Rusted HTTP router reverse-proxy
Documentation
use std::net::{IpAddr, Ipv4Addr, SocketAddr};

use http::Request;

// use of unstable library feature 'ip'
// https://github.com/rust-lang/rust/pull/79342
fn to_canonical(addr: IpAddr) -> IpAddr {
    if let IpAddr::V6(v6addr) = addr {
        if let [0, 0, 0, 0, 0, 0xffff, ab, cd] = v6addr.segments() {
            let [a, b] = ab.to_be_bytes();
            let [c, d] = cd.to_be_bytes();
            IpAddr::V4(Ipv4Addr::new(a, b, c, d))
        } else {
            addr
        }
    } else {
        addr
    }
}

pub trait GetClientIp {
    fn client_ip(&self) -> IpAddr;
}

impl<B> GetClientIp for Request<B> {
    fn client_ip(&self) -> IpAddr {
        let ip = self.extensions().get::<SocketAddr>().unwrap().ip();
        to_canonical(ip)
    }
}