HTTPS代理服务器在node.js

我正在开发一个node.js代理服务器应用程序,我希望它支持HTTPHTTPS(SSL)协议(作为服务器)。

我目前正在使用像这样的node-http-proxy

 const httpProxy = require('http-proxy'), http = require('http'); var server = httpProxy.createServer(9000, 'localhost', function(req, res, proxy) { console.log(req.url); proxy.proxyRequest(req, res); }); http.createServer(function(req, res) { res.end('hello!'); }).listen(9000); server.listen(8000); 

我设置我的浏览器在localhost:8000上使用HTTP代理,它工作。 我也想捕捉HTTPS请求(即设置浏览器使用localhost:8000作为HTTPS代理,并在我的应用程序中捕获请求)。 你能帮我吗我该怎么做?

PS

如果我订阅httpProxy服务器对象的upgrade事件,我可以得到请求,但我不知道如何转发请求并发送响应到客户端:

 server.on('upgrade', function(req, socket, head) { console.log(req.url); // I don't know how to forward the request and send the response to client }); 

任何帮助,将不胜感激。

解决scheme几乎不存在,而且文档在一台服务器上同时支持这两种文档。 这里的技巧是了解客户端代理configuration可以发送https请求到一个http代理服务器。 如果您指定一个HTTP代理,然后选中“对于所有协议相同”,那么对于Firefox来说就是这样。

您可以通过侦听“连接”事件来处理发送到HTTP服务器的https连接。 请注意,您将无法访问连接事件上的响应对象,只有套接字和bodyhead。 通过此套接字发送的数据将作为代理服务器保持encryption状态。

在此解决scheme中,您不必制作自己的证书,因此不会有证书冲突。 stream量被简单地代理,不被截获,并被不同的证书重写。

 // Install npm dependencies first // npm init // npm install --save url@0.10.3 // npm install --save http-proxy@1.11.1 var httpProxy = require("http-proxy"); var http = require("http"); var url = require("url"); var net = require('net'); var server = http.createServer(function (req, res) { var urlObj = url.parse(req.url); var target = urlObj.protocol + "//" + urlObj.host; console.log("Proxy HTTP request for:", target); var proxy = httpProxy.createProxyServer({}); proxy.on("error", function (err, req, res) { console.log("proxy error", err); res.end(); }); proxy.web(req, res, {target: target}); }).listen(8080); //this is the port your clients will connect to var regex_hostport = /^([^:]+)(:([0-9]+))?$/; var getHostPortFromString = function (hostString, defaultPort) { var host = hostString; var port = defaultPort; var result = regex_hostport.exec(hostString); if (result != null) { host = result[1]; if (result[2] != null) { port = result[3]; } } return ( [host, port] ); }; server.addListener('connect', function (req, socket, bodyhead) { var hostPort = getHostPortFromString(req.url, 443); var hostDomain = hostPort[0]; var port = parseInt(hostPort[1]); console.log("Proxying HTTPS request for:", hostDomain, port); var proxySocket = new net.Socket(); proxySocket.connect(port, hostDomain, function () { proxySocket.write(bodyhead); socket.write("HTTP/" + req.httpVersion + " 200 Connection established\r\n\r\n"); } ); proxySocket.on('data', function (chunk) { socket.write(chunk); }); proxySocket.on('end', function () { socket.end(); }); proxySocket.on('error', function () { socket.write("HTTP/" + req.httpVersion + " 500 Connection error\r\n\r\n"); socket.end(); }); socket.on('data', function (chunk) { proxySocket.write(chunk); }); socket.on('end', function () { proxySocket.end(); }); socket.on('error', function () { proxySocket.end(); }); }); 

我使用http-proxy模块创build了一个http / https代理: https : //gist.github.com/ncthis/6863947

代码截至目前:

 var fs = require('fs'), http = require('http'), https = require('https'), httpProxy = require('http-proxy'); var isHttps = true; // do you want a https proxy? var options = { https: { key: fs.readFileSync('key.pem'), cert: fs.readFileSync('key-cert.pem') } }; // this is the target server var proxy = new httpProxy.HttpProxy({ target: { host: '127.0.0.1', port: 8080 } }); if (isHttps) https.createServer(options.https, function(req, res) { console.log('Proxying https request at %s', new Date()); proxy.proxyRequest(req, res); }).listen(443, function(err) { if (err) console.log('Error serving https proxy request: %s', req); console.log('Created https proxy. Forwarding requests from %s to %s:%s', '443', proxy.target.host, proxy.target.port); }); else http.createServer(options.https, function(req, res) { console.log('Proxying http request at %s', new Date()); console.log(req); proxy.proxyRequest(req, res); }).listen(80, function(err) { if (err) console.log('Error serving http proxy request: %s', req); console.log('Created http proxy. Forwarding requests from %s to %s:%s', '80', proxy.target.host, proxy.target.port); }); 

node-http-proxy文档包含这个例子。 在https://github.com/nodejitsu/node-http-proxy中查找“从HTTPS代理到HTTPS”。每个浏览器的configuration过程略有不同。 有些可以select使用所有协议的代理设置; 有些需要单独configurationSSL代理。