gpt4 book ai didi

proxy - http转https节点代理

转载 作者:行者123 更新时间:2023-12-02 04:56:32 27 4
gpt4 key购买 nike

我想知道设置代理的最简单方法,我可以在(即)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

我想用节点来做,但是,其他解决方案可能有效。(代理将在 localhost 中仅用于测试目的,因此安全性不是问题)

最佳答案

我需要一个 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);
});
});

关于proxy - http转https节点代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21532291/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com