我想知道设置代理的最简单方法,我可以在({)localhost:8011
中发出HTTP请求,代理在localhost:443
中发出HTTPS请求(HTTPS的答案来自服务器也应该由代理转换为HTTP)
我正在使用node.js
我试过http-proxy
这样:
var httpProxy = require('http-proxy');
var options = {
changeOrigin: true,
target: {
https: true
}
}
httpProxy.createServer(443, 'localhost', options).listen(8011);
我也试过这个:
httpProxy.createProxyServer({
target: {
host:'https://development.beigebracht.com',
rejectUnauthorized: false,
https: true,
}
}).listen(port);
但是当我试图连接时,我收到了这个错误
/Users/adrian/Development/beigebracht-v2/app/webroot/node_modules/http-proxy/lib/http-proxy/passes/web-incoming.js:103
var proxyReq = (options.target.protocol === 'https:' ? https : http).reque
^
TypeError: Cannot read property 'protocol' of undefined
我想用node做,但是,其他解决方案可能有效。 (代理将仅在测试时用于localhost,因此安全性不是问题)
答案 0 :(得分:0)
我需要一个HTTP-> HTTPS节点代理来进行单元测试。我最终做的是创建HTTP代理,然后让它监听并处理connect
事件。当服务器收到CONNECT请求时,它会建立到HTTPS目标URL的隧道,并将所有数据包从客户端套接字转发到目标套接字,反之亦然。
示例代码:
var httpProxy = require('http-proxy');
var net = require('net');
var url = require('url');
var options = {
host: 'localhost',
port: 3002
};
var proxy = httpProxy.createProxyServer();
proxy.http = http.createServer(function(req, res) {
var target = url.parse(req.url);
// The `path` attribute would cause problems later.
target.path = undefined;
proxy.web(req, res, {
target: target
});
}).listen(options.port, options.host);
// This allows the HTTP proxy server to handle CONNECT requests.
proxy.http.on('connect', function connectTunnel(req, cltSocket, head) {
// Bind local address of proxy server.
var srvSocket = new net.Socket({
handle: net._createServerHandle(options.host)
});
// Connect to an origin server.
var srvUrl = url.parse('http://' + req.url);
srvSocket.connect(srvUrl.port, srvUrl.hostname, function() {
cltSocket.write(
'HTTP/1.1 200 Connection Established\r\n' +
'Proxy-agent: Node.js-Proxy\r\n' +
'\r\n'
);
srvSocket.write(head);
srvSocket.pipe(cltSocket);
cltSocket.pipe(srvSocket);
});
});