A simple reverse proxy for use with Hyper and Tokio
Go to file
2022-03-10 16:24:36 -03:00
src chore: upgrade hyper/tokio libraries 2022-03-10 16:24:36 -03:00
.gitignore port lib to hyper 0.13 and future 0.3 2020-05-04 00:18:27 -03:00
.travis.yml Initial commit 2017-07-15 16:28:59 +10:00
Cargo.toml updates tokio reactor version 2022-03-10 16:24:36 -03:00
LICENSE Initial commit 2017-07-15 16:28:59 +10:00
README.md chore: upgrade hyper/tokio libraries 2022-03-10 16:24:36 -03:00

hyper-reverse-proxy

Build Status Documentation Version License

A simple reverse proxy, to be used with Hyper.

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.

Example

Add these dependencies to your Cargo.toml file.

[dependencies]
hyper-reverse-proxy = "0.5"
hyper = { version = "0.14", features = ["full"] }
tokio = { version = "0.3", features = ["full"] }

The following example will set up a reverse proxy listening on 127.0.0.1:13900, and will proxy these calls:

  • "/target/first" will be proxied to http://127.0.0.1:13901

  • "/target/second" will be proxied to http://127.0.0.1:13902

  • All other URLs will be handled by debug_request function, that will display request information.

use hyper::server::conn::AddrStream;
use hyper::{Body, Request, Response, Server};
use hyper::service::{service_fn, make_service_fn};
use futures::future::{self, Future};

type BoxFut = Box<Future<Item=Response<Body>, Error=hyper::Error> + Send>;

fn debug_request(req: Request<Body>) -> BoxFut {
    let body_str = format!("{:?}", req);
    let response = Response::new(Body::from(body_str));
    Box::new(future::ok(response))
}

fn main() {

    // This is our socket address...
    let addr = ([127, 0, 0, 1], 13900).into();

    // A `Service` is needed for every connection.
    let make_svc = make_service_fn(|socket: &AddrStream| {
        let remote_addr = socket.remote_addr();
        service_fn(move |req: Request<Body>| { // returns BoxFut

            if req.uri().path().starts_with("/target/first") {

                // will forward requests to port 13901
                return hyper_reverse_proxy::call(remote_addr.ip(), "http://127.0.0.1:13901", req)

            } else if req.uri().path().starts_with("/target/second") {

                // will forward requests to port 13902
                return hyper_reverse_proxy::call(remote_addr.ip(), "http://127.0.0.1:13902", req)

            } else {
                debug_request(req)
            }
        })
    });

    let server = Server::bind(&addr)
        .serve(make_svc)
        .map_err(|e| eprintln!("server error: {}", e));

    println!("Running server on {:?}", addr);

    // Run this server for... forever!
    hyper::rt::run(server);
}