xpepermint / rawproxy

Proxy handler for asynchronous streams.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Proxy handler for asynchronous streams.

This library allows for writing reverse proxies in rust. It's built on top of async-std and uses async-uninet which enables unified handling of asynchronous TCP & Unix streams.

Example

Program:

let address = SocketAddr::from_str("127.0.0.1:4444").await.unwrap(); // or `unix:`
let listener = Listener::bind(&address).await.unwrap();

while let Some(stream) = listener.incoming().next().await {
    let stream = stream.unwrap();

    task::spawn(async move {
        let mut router = Router::new(stream);
        let mut router = Router::new(stream);
        router.parse_request().await.unwrap();
        router.set_request_header("Host", "jsonplaceholder.typicode.com:80"); // override header
        router.relay_request().await.unwrap();
        router.parse_response().await.unwrap();
        router.write_response_header("Status", "fast"); // override header
        router.relay_response().await.unwrap();
    })
}

Execute:

$ curl -N \
  -H "Host: typicode" \
  -H "Content-Type: application/json" \
  http://localhost:4444/posts

About

Proxy handler for asynchronous streams.

License:MIT License


Languages

Language:Rust 100.0%