Fastforward is library for writing reverse proxies in rust.
To implement a proxy that just receives traffic on listen_addr
and forwards the it to proxy_addr
use the simple_proxy
function.
```
extern crate fastforward;
extern crate http;
use std::net::SocketAddr; use proxy::simple_proxy;
fn main() { let listenaddr: SocketAddr = "127.0.0.1:8080".parse().unwrap(); let proxyaddr: SocketAddr = "127.0.0.1:4000".parse().unwrap();
println!("running on port :8080");
simple_proxy(listen_addr, proxy_addr);
} ```
To implement arbitrary logic for your proxy write a request director function. The request, after be passed into the director, will be proxied to the address specified by the "Host" header on the request. ``` extern crate fastforward; extern crate http;
use http::{ header::HeaderValue, Response }; use std::io; use std::net::SocketAddr; use fastforward::generic_proxy;
// The director function mutates the incoming request before proxying it.
// In this example, the request URI is changed to the proxy URI.
// This examle mimics the functionality of the simple_proxy
function.
fn reqtransform(req: &mut http::Request
let reqheaders = req.headersmut(); reqheaders.remove(http::header::HOST); reqheaders.insert(http::header::HOST, proxy_addr);
None // ignore the return type for this example }
fn resptransform(resp: &mut http::Response
let resp_headers = resp.headers_mut();
resp_headers.insert("X-HEADER-VAL", x_header_val);
}
fn main() { let listenaddr: SocketAddr = "127.0.0.1:8080".parse().unwrap(); genericproxy(listenaddr, reqtransform, resp_transform); } ```