http-client icon indicating copy to clipboard operation
http-client copied to clipboard

Implement SOCKS4 proxy support

Open ghost opened this issue 4 years ago • 5 comments

I would find SOCKS4 proxy support useful in http-client. SSH provides an easy way to set up a SOCKS4 proxy on your local machine to a remote SSH server. HTTP requests could then be proxied through the remote SSH server.

I have been considering implementing SOCKS4 support for http-client. The Proxy type, which currently only represents HTTP proxies, would be replaced with a choice of either HTTP Proxy or SOCKS4.

Would this feature be useful and accepted into the code base? Is there already a way to do this in a custom way? I thought that withConnection might be useful for implementing the initial SOCKS4 connection setup, but it seems that connectionRead does not block.

Any suggestions would be appreciated.

ghost avatar Dec 10 '20 16:12 ghost

The connection page, which lives underneath http-client-tls, already supports SOCKS proxies. You can configure it here:

https://www.stackage.org/haddock/nightly-2020-12-10/http-client-tls-0.3.5.3/Network-HTTP-Client-TLS.html#v:mkManagerSettings

If I'm not mistaken, using that will automatically use SOCKS proxying for both TLS and non-TLS connections.

snoyberg avatar Dec 10 '20 16:12 snoyberg

Thanks for the swift reply. I'm going to give that a shot.

ghost avatar Dec 10 '20 16:12 ghost

I'm trying to debug this.

  1. I set environment variable to https_proxy='socks5://localhost:9150'
  2. A manager created with newTlsManager uses the proxy.
  3. A manager created with newTlsManagerWith tlsManagerSettings doesn't use proxy.

I suppose this is because newTlsManagerWith resets managerTlsProxyConnection after it is set to use SOCKS proxy: https://github.com/snoyberg/http-client/blob/master/http-client-tls/Network/HTTP/Client/TLS.hs#L214

cblp avatar Feb 06 '21 09:02 cblp

Also, it's very hard to debug the library, because

  1. there seem to be 2 different layers of proxy (CKProxy in http-client and SockSettings in -tls)
  2. both Manager and Request are being rewritten too many times, especially 2 times in a row in responseOpen:
(manager, req0) <- getModifiedRequestManager manager' inputReq
go manager (redirectCount req0) req0
...
go manager0 count req' =
  ...
  getModifiedRequestManager manager0 req

cblp avatar Feb 06 '21 09:02 cblp

image

It is working as well for me @snoyberg

diqye avatar Nov 12 '21 16:11 diqye