hyper-reverse-proxy 0.2.0

A simple reverse proxy, to be used with Hyper and Tokio.

A simple reverse proxy, to be used with Hyper and Tokio.

The implementation ensures that Hop-by-hop headers are stripped correctly in both directions, and adds the client's IP address to a comma-space-separated list of forwarding addresses in the X-Forwarded-For header.

The implementation is based on Go's httputil.ReverseProxy.

extern crate futures;
extern crate hyper;
extern crate hyper_reverse_proxy;
extern crate tokio_core;

fn run() -> hyper::Result<()> {
    use futures::Stream;
    use hyper::Client;
    use hyper::server::Http;
    use hyper_reverse_proxy::ReverseProxy;
    use tokio_core::net::TcpListener;
    use tokio_core::reactor::Core;
    use std::net::{SocketAddr, Ipv4Addr};

    let mut core = Core::new()?;
    let handle = core.handle();
    let listen_addr = SocketAddr::new(Ipv4Addr::new(127, 0, 0, 1).into(), 8080);
    let listener = TcpListener::bind(&listen_addr, &handle)?;

    let http = Http::new();
    let server = listener.incoming().for_each(|(socket, addr)| {
        let service = ReverseProxy::new(Client::new(&handle), Some(addr.ip()));
        http.bind_connection(&handle, socket, addr, service);
        Ok(())
    });

    core.run(server)?;

    Ok(())
}

fn main() {
    use std::io::Write;

    if let Err(error) = run() {
        write!(&mut std::io::stderr(), "{}", error).expect("Error writing to stderr");
        std::process::exit(1);
    }
}