qwj / python-proxy

HTTP/HTTP2/HTTP3/Socks4/Socks5/Shadowsocks/ShadowsocksR/SSH/Redirect/Pf TCP/UDP asynchronous tunnel proxy implemented in Python 3 asyncio.
MIT License
1.99k stars 333 forks source link

relay requested port to destination for SimpleProxy #180

Open rartino opened 6 months ago

rartino commented 6 months ago

I have a need to redirect all requests on any port of a host to a different host, and I don't think this is covered by the current URI syntax. I'm sure it can be discussed what the optimal implementation for this is, but the PR seems the easiest way to get this feature into the current implementation and URI syntax.

Say that I run a web UI program on host.example.com and which listens to localhost. I now want to access it in a web browser on my own machine via an ssh tunnel. If the program uses port 8088, I can do, e.g., this:

pproxy -r ssh://host.example.com/?host.example.com#login::key__httponly://localhost:8088/"

However, lets say the program on host2 uses unpredictable port numbers (perhaps it dynamically opens them). I just want to map host.example.com:<port> -> ssh to host.example.com -> localhost:<port> for any <port>. If I omit the port number in the destination URI above, pproxy uses the default value of 8088.

This PR allows using the request port as the destination port for anything covered by SimpleProxy by specifying the destination port as 0 (traditionally used to mean "any port" when opening ports, so it seems somewhat appropriate).

After merging, this PR enables the following syntax for the above example:

pproxy -r ssh://host.example.com/?host.example.com#login::key__httponly://localhost:0/"