gpt4 book ai didi

Node.js - 通过 HTTPS 发出请求时仅与代理服务器通信

转载 作者:太空宇宙 更新时间:2023-11-04 00:17:49 25 4
gpt4 key购买 nike

我正在使用代理服务器发送 https 请求。我想仅与代理服务器来回传递某些信息(不传递到目标服务器)。在执行 HTTP 请求时,可以使用请求 header 轻松完成此操作,但对于 HTTPS 请求, header 已加密,因此不能选择使用请求 header 。我的代理服务器在使用 HTTPS 时提供以下选项:

The only point at which unencrypted data is sent to the proxy server is with the initial CONNECT method. This is where you must insert the custom headers. Similarly, the proxy server cannot inject an extra header into the final response. Instead, the response header is injected immediately after the Connection response, which looks like this:

HTTP/1.1 200 Connection established
X-ProxyServer-IP: 123.456.789.000
final response headers & body

所以我需要使用 CONNECT 方法传递 header ,并读取 CONNECT 响应。看来 https-proxy-agent 模块将允许指定专门与 CONNECT 请求一起发送的 header 。但是,如何在完成预期请求(例如 POST)的同时读取 CONNECT 响应?

我无法弄清楚如何使用 request 模块或 https 模块拦截 CONNECT 响应。

最佳答案

我已经测试了https

const https = require('https');
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
var server = https.createServer(function(req,res){res.end('test');}).listen(3000);

如果您console.log(server._events)

{ connection: [ [Function], [Function] ],
secureConnection: [ [Function: connectionListener], [Function] ],
request: [Function],
tlsClientError: [ [Function: addListener], [Function] ] }

添加以下内容后:

server.on('connection',function(req,socket,head){console.log('CONNECT')})
server.on('secureConnection',function(req,socket,head){console.log('CONNECT (secure)')})
server.on('tlsClientError',function(req,socket,head){console.log('TLS:ERROR')})

我测试了:curl -X CONNECT localhost:3000

在此快速测试中,客户端输出:curl: (52) 来自服务器的空回复)

...但是@服务器:

CONNECT
TLS:ERROR

正如您所看到的,它拦截了 CONNECT (即使您由于测试环境而出现 tls 错误。),

因此,如果您使用(工作)设置执行此操作,您将能够拦截它,而不会出现 TLS:ERROR

<小时/>

对于任何想要使用 http 执行此操作的人来说,事件是不同的:server.on('connect', ...

关于Node.js - 通过 HTTPS 发出请求时仅与代理服务器通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46020937/

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