Hyper proxy based on outgoing connection and not inbound

I'm trying to build a proxy server, but instead of listening on a port.
I want it to connect to a remote server and proxy inbound requests from it.

I started off hyper/examples/http_proxy.rs at master · hyperium/hyper · GitHub , but it uses TcpListener::bind and I need to use TcpStream::connect.

I'm unable to find any API to use the TcpStream and process incoming requests.

What I'm trying to do is:

[Client] --> [Proxy Master] --> [Proxy] --> [Internet]

The client will send a request to the Proxy Master, which will decide where to direct it to.
The proxy master will send it to one of the proxies which in turn will send it to the target site.
And the response will bubble back.

The issue is, that the proxies, can't open a public port for incoming requests.
So they need to hole-punch or keep an open connection with the proxy master.

I have never tried this so I can only speculate. hyper::client::conn::http1::handshake() and hyper::server::conn::http1::Builder::serve_connection() both take a stream, so it shouldn't matter if the stream is created from TcpStream::connect() or TcpListener::accept().

However, one obvious problem I can think of is that since you are building proxies, you need to use HTTP CONNECT. With HTTP/1.1, you can only use one TCP stream to connect to one domain, then your LAN proxies would have to continuously make new connections to your master proxy server so that your master proxy has spare streams to use for new proxy requests.

I imagine HTTP/2 would be better suited for your scenario, but I don't remember that Hyper supports server push, and I have no idea how to use an HTTP/2 stream for HTTP CONNECT in the reverse direction.

This is basically what I had in mind.

I tried putting the incoming TcpStream inside an Arc<Mutex> but it can't be used in copy_bidirectional.

Also Wrapping tokio::net::TcpStream in Arc<Mutex<TcpStream>> @alice discourages against it.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.