Search code examples
httpsnode.jsproxy-server

How do I forward HTTPS traffic in nodejs?


I'm using nodejs to create a simple proxy server that logs http and https requests, however, I can't seem to get https requests to be logged (http requests are fine)

So how do I create a proxy server in nodejs that can set up secure connections?

My code so far looks like

var http = require('http'),
    httpProxy = require('http-proxy'),
    util = require('util'),
    url = require('url');

httpProxy.createServer(function (request, response, proxy) {
   util.log(request.connection.remoteAddress + ": " + 
        request.method + " " + 
        request.url);

   request.addListener('data', function(chunk) {
      util.log("request: " + chunk);
   });
   uri = url.parse(request.url);
   port = 80;
   if (uri.protocol == 'https:') { // NEVER HAPPENS :(
      port = 443;
   }
   util.log("port:" + port + " host:" + uri.host);
   proxy.proxyRequest(port, uri.hostname);

}).listen(8080);

It's currently using https://github.com/nodejitsu/node-http-proxy proxy library, but if the library doesn't support https (doesn't look like it does atm, since no where in the source code does it set any credentials....), I'm happy to rewrite the proxy server without it.


Solution

  • Looks like I won't be able to do this for a little while, but there's documentation on the new tls module that'll finally make https connections work properly. https://github.com/ry/node/blob/5a87bd168d8fbeca7d48b9ddaa3b4e8a9336719c/doc/api/tls.markdown