Skip to content

Support for HTTP CONNECT? #230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tonygambone opened this issue Apr 13, 2012 · 12 comments
Closed

Support for HTTP CONNECT? #230

tonygambone opened this issue Apr 13, 2012 · 12 comments

Comments

@tonygambone
Copy link

Does node-http-proxy support HTTP CONNECT requests, like:

CONNECT example.com:443 HTTP/1.1
Host: example.com:443

The proxy would then maintain the two connections, and forward the SSL traffic between the client and the target server.

If so, how do I set this up?

Thanks

@coderarity
Copy link
Contributor

I don't know too much about HTTP CONNECT requests, but it looks like you can do this. You just need to change the method to a GET request when forwarding it to the target server. Here's a (basic) example:

var httpProxy = require('http-proxy');

httpProxy.createServer(function (req, res, proxy) {
  if (req.method === "CONNECT") req.method = "GET";
  proxy.proxyRequest(req, res, {
    host: 'localhost',
    port: 443
  });
}).listen(80);

(You can add your proxy's SSL certificate to the createServer function, see the documentation.)

From what I can see, an HTTP CONNECT request can be used with any type of TCP/IP tunnel, but node-http-proxy should only be used to proxy to another HTTP(S) server.

@tonygambone
Copy link
Author

Thanks - I'll try that, it looks closer to the mark than what I was trying.

The proxy shouldn't need an SSL cert - it should establish a connection to the remote server and port, and then from that point on it's just a plain network proxy, forwarding data back and forth. Not really HTTP at all, or as you say, just a generic TCP tunnel.

It's what a browser would send if it were connecting to an SSL site through a proxy - like in a corporate environment. I'm trying to write a proxy server that can handle both - so maybe the trick is to look for that CONNECT method, and if so set up a tunnel under a different code path, and use node-http-proxy for regular HTTP.

Thanks for your help.

@coderarity
Copy link
Contributor

The proxy's SSL certificate would be for the client connecting to the proxy, not for the proxy connecting to the server. If you just wanted it to be HTTP between the client and the proxy, then you wouldn't need an SSL certificate.

I mean, if you just want to proxy TCP sockets (like the net module), that's beyond the scope of node-http-proxy at this point - it's just an HTTP proxy. But as you said, you should probably set up a tunnel using a different code path, and just use node-http-proxy for the HTTP stuff.

@tonygambone
Copy link
Author

Just wanted to update this with some working code:
https://gist.github.com/2422322

It uses node-http-proxy for regular HTTP and sets up a tunnel for CONNECT requests.

This works well, although there are some occasional ETIMEDOUT errors (which I'm not sure is the fault of the proxy, or if they would have timed out anyway).

Thanks for the help, very nice project you've made here.

@coderarity
Copy link
Contributor

cool! Interesting that it send an upgrade event for CONNECT requests, although I guess it makes sense.

@tonygambone
Copy link
Author

Yes, that was the key - I found that in the http module source code. It looks like 'connect' should work too, but it didn't for me.

@rbdixon
Copy link

rbdixon commented Dec 30, 2012

@Mogrify: This was really helpful. Thank you! I guess the connect module changed because now the 'connect' event works but 'upgrade' does not.

@regevbr
Copy link

regevbr commented Jan 17, 2018

@imhazige
Copy link

Inspired by these discussion, I have create a working project. https://github.com/imhazige/node-http-connect-proxy

@hktalent
Copy link

@coderarity @tonygambone @regevbr @imhazige
socks4 or socks5 througth http proxy use
``
CONNECT % HTTP/1.1
Host: %

@hktalent
Copy link

@coderarity @tonygambone @regevbr @imhazige

node node-http-proxy -x "http://127.0.0.1:883" -u http://ip.cn
node node-http-proxy -x "socks4://127.0.0.1:883" -u http://ip.cn

@shirshak55
Copy link

I recommend using proxy-chain for such task.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants