简体   繁体   English

关闭从代理服务器到目标服务器的连接,而不是从客户端到代理服务器的连接

[英]close connection from proxy server to target server and not from client to proxy server

Objective: Never close connection between client and SOCKS proxy + reuse it to send multiple HTTPS requests to different targets (example targets: google.com, cloudflare.com) without closing the socket during the switch to different target.目标:永远不要关闭客户端和 SOCKS 代理之间的连接 + 重用它发送多个 HTTPS 请求到不同的目标(示例目标:google.com,cloudflare.com),而无需在切换到不同目标期间关闭套接字。

Step 1: So I have client which connects to SOCKS proxy server over TCP connection.第 1 步:所以我有通过 TCP 连接连接到SOCKS代理服务器的客户端。 That is client socket(and only socket(file descriptor) used in this project).那是客户端套接字(并且只有这个项目中使用的套接字(文件描述符))。 client -> proxy

Step 2: Then after connection is established and verified.第 2 步:然后在建立连接并验证之后。 Then it does TLS connect to the target server which can be for example google.com (DNS lookup is done before this).然后它使用 TLS 连接到target服务器,例如 google.com(在此之前完成 DNS 查找)。

Now we have connection: client -> proxy -> target现在我们有了连接: client -> proxy -> target

Step 3: Then client sends HTTPS request over it and receives response successfully.第3步:然后客户端通过它发送HTTPS请求并成功收到响应。

Issue appears: After that I want to close connection explicitly between proxy and target so I can send request to another target.出现问题:之后我想明确close connection ,以便我可以将请求发送到另一个目标。 For this it is required to close TLS connection and I don't know how to do it without closing connection between client and proxy which is not acceptable.为此,需要关闭 TLS 连接,我不知道如何在不关闭客户端和代理之间的连接的情况下执行此操作,这是不可接受的。

Possible solutions?:可能的解决方案?:

1: Would sending Connection: close\n\r request to current target close connection only between proxy and target and not close the socket. 1:将向当前目标发送Connection: close\n\r请求,仅关闭代理和目标之间的连接,而不关闭套接字。

2: If I added Connection: close\n\r to headers of every request, would that close the socket and thus it's not valid solution? 2:如果我将Connection: close\n\r添加到每个请求的标头,是否会关闭套接字,因此它不是有效的解决方案?

Question: (NodeJS) I made custom https Agent which handles Agent-s method -> callback(req, opts) where opts argument is request options from what client sent to target (through proxy).问题: (NodeJS)我制作了自定义 https Agent,它处理 Agent-s 方法 -> callback(req, opts) ,其中 opts 参数是客户端发送到目标(通过代理)的请求选项。 This callback returns tls socket after it's connected, I built tls socket connection outside of the callback and passed it to agent.此回调在连接后返回 tls 套接字,我在callback外部构建了 tls 套接字连接并将其传递给代理。 Is it possible to use this to close connection between proxy and target using req.close() , would this close the socket?是否可以使用它来使用req.close()关闭代理和目标之间的连接,这会关闭套接字吗? Also what is the point of req in Agent's callback, can it be used in this case?另外代理回调中的req有什么意义,在这种情况下可以使用吗?

Any help is appreciated.任何帮助表示赞赏。

If you spin up wireshark and look at what is happening through your proxy, you should quickly see that HTTP/S requests are connection oriented, end-to-end (for HTTPS) and also time-boxed.如果启动 wireshark 并查看通过代理发生的情况,您应该很快就会看到 HTTP/S 请求是面向连接的、端到端的(对于 HTTPS)并且也是有时间限制的。 If you stop and think about it, they are necasarily so, to avoid issues such as the confused deputy problem etc.如果你停下来想一想,他们是necasarily这样的,以避免混淆代理问题等问题。

So the first bit to note is that for HTTPS, the proxy will only see the initial CONNECT request, and then from there on everything is just a TCP stream of TLS bytes.所以首先要注意的是,对于 HTTPS,代理只会看到初始的 CONNECT 请求,然后从那里开始,一切都只是一个 TCP stream 的 TLS 字节。 Which means that the proxy won't be able to see the headers (that is, unless your proxy is a MITM that intercepts the TLS handshake, and you haven't mentioned this, so I've assumed not).这意味着代理将无法看到标头(也就是说,除非您的代理是拦截 TLS 握手的 MITM,并且您没有提到这一点,所以我假设没有)。

The next bit is that the agent/browser will open connections in parallel (typically a half-dozen for a browser) and will also use pipelining and keep-alive to send multiple requests down the same connection.下一点是代理/浏览器将并行打开连接(对于浏览器通常是六个)并且还将使用流水线保持活动状态在同一连接下发送多个请求。

Then there are connection limits imposed by the browser, and servers.然后是浏览器和服务器强加的连接限制。 These typically cap the number of requests, and the duration that they are held open, before speculatively closing them.这些通常会在推测性关闭请求之前限制请求的数量以及它们保持打开状态的持续时间。 If they didn't, any reasonably busy server would quickly exhaust all their TCP sockets.如果他们不这样做,任何相当繁忙的服务器都会很快耗尽他们所有的 TCP sockets。

So all-in, what you are looking to achieve isn't going to work.所以总而言之,你想要实现的目标是行不通的。

That said, if you are looking to improve performance, the node client has a few things you can enable and tweak:也就是说,如果您希望提高性能,节点客户端有一些您可以启用和调整的功能:

  • Enable TLS session reuse , which will make connections much more efficient to establish.启用 TLS session reuse ,这将使建立连接的效率更高。

  • Enable keep-alive , which will funnel multiple requests through the same connection.启用keep-alive ,这将通过同一连接汇集多个请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM