2013-04-24 232 views
4

我想为Node.js创建HTTPS连接的代理。我正在使用http-proxy库,效果很好。我可以得到一个HTTP代理工作完美,但是当我尝试HTTPS时,对代理的请求只是超时。这里是我的代码(稍微修改了node-http-proxyproxy-https-to-https例子的版本):Node.js HTTPS代理服务器不工作

var http = require("http"), 
    https = require("https"), 
    httpProxy = require("http-proxy"), 
    fs = require('fs'); 

var httpsConfig = { 
    key: fs.readFileSync('./jackos2500-key.pem'), 
    cert: fs.readFileSync('./jackos2500-cert.crt'), 
}; 

https.createServer(httpsConfig, function (req, res) { 
    res.writeHead(200, { 'Content-Type': 'text/plain' }); 
    res.write('hello https\n'); 
    res.end(); 
}).listen(8000); 

httpProxy.createServer(8000, 'localhost', { 
    https: httpsConfig, 
    target: { 
    https: true, 
    rejectUnauthorized: false 
    } 
}).listen(443); 

是有我丢失在这里还是有一些其他的问题了一些东西明显?

+0

一个开放的问题。这是否有助于洞察力? – Roemer 2013-05-10 19:58:28

回答

0

我遇到同样的问题。我没有看到任何记录。这里是我的,这是一个有点不同(第二个从GitHub的例子我看到的例子匹配):

var https = require('https'); 
var httpProxy = require('http-proxy/lib/node-http-proxy'); 
var helpers = require('http-proxy/test/helpers'); 
var request = require('request'); 

https.createServer(function(req, response) { 
    try { 
    console.log("forwarding https", req); 
    var proxy_request = request(req); 
    proxy_request.pipe(response); 
    } catch (err) { 
    console.log("Forwarding server caught error:\n" + err + "\n") 
    } 
}).listen(9001); 

httpProxy.createServer(function (req, res, proxy) { 
    console.log("Got request!"); 
    proxy.proxyRequest(req, res, { 
    port: 9001, 
    host: 'localhost', 
    buffer: httpProxy.buffer(req) 
    }); 
}, { https: helpers.https }).listen(8001); 

...我也曾尝试简单的一个:

httpProxy.createServer(9001, 
    'localhost', 
    { https: helpers.https }).listen(8001) 

当我将Firefox的https代理端口设置为8001,然后去任何地方,我得到“连接已重置”。

我用'http'替换'https'的做法完全一样,它都可以正常工作。

相关问题