proxy - http转https节点代理

标签 proxy http-proxy node-http-proxy

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

相关文章:

Apache 不代理对 TomCat 的 HTTPS 请求,只显示本地 index.html

Go RoundTrip/传输代理地址

node.js - 如何使用 http-proxy 隐藏 Node.js 服务器

node.js - HTTP 代理 (Node.js) 未执行正确的 SSL 验证

angular - 使用 NTLM 身份验证的 ng serve --proxy-config 不起作用

javascript - 如何使用代理合并数组中的相同项目?

python - Docker Apache Proxy指向运行Python脚本

php - Apple Push with proxy 和 stream_context

javascript - 如何使用http代理获取请求的正文?

python - 使用 selenium python 更改代理后页面未加载?