简体   繁体   English

使用套接字c#通过代理将客户端的请求中继到服务器

[英]Relay a request from client to Server via Proxy using sockets c#

I have custom tcp server listening on port 5888(dummy port). 我在端口5888(虚拟端口)上监听了自定义的TCP服务器。 The proxy server listens for incoming connections. 代理服务器侦听传入的连接。 When the proxy receives a HTTP Request, it should relay the same request to a different server. 代理收到HTTP请求时,应将同一请求中继到另一台服务器。

Eg: 例如:

Proxy receives: http://proxyserver.com/mypage.html Proxy should Relay: http://MainServer.com/mypage.html 代理接收: http : //proxyserver.com/mypage.html代理应中继: http : //MainServer.com/mypage.html

The response from the Main Server should be sent directly to the requesting client. 来自主服务器的响应应直接发送到发出请求的客户端。

I have used sockets for accepting connections and parsing the raw HTTP Request before relaying the new HTTP request to the main server. 在将新的HTTP请求中继到主服务器之前,我已经使用套接字来接受连接并解析原始的HTTP请求。 This is becoming too complicated, since I have to send the Raw HTTP Request to the main server, get the response and send it to the client machine.The proxy is basically becoming a middle-man. 这变得太复杂了,因为我必须将原始HTTP请求发送到主服务器,获取响应并将其发送到客户端计算机。代理基本上已经成为中间人。 I want the proxy only for the one side of communication ie to change the host address to the MainServer and then the main server should take care of sending the response to the requesting client. 我希望代理仅用于通信的一侧,即将主机地址更改为MainServer,然后主服务器应负责将响应发送到发出请求的客户端。

How to achieve this ? 如何实现呢?

HTTP works over a TCP connection initiated from a client to a server. HTTP通过从客户端到服务器启动的TCP连接工作。 In presence of proxy you need at least two connections - one from client to the proxy, and one from proxy to the server. 在存在代理的情况下,您至少需要两个连接-一个从客户端到代理,一个从代理到服务器。 These two connections are sort of "independent" in a sense that both transmit standard HTTP requests and responses. 从传输标准HTTP请求和响应的意义上来说,这两个连接是“独立的”。 Proxy here acts as a client to the end server. 这里的代理充当终端服务器的客户端。

What you want involves at least three TCP connections, and requires sending HTTP response on a different connection from the one where initiating HTTP request came on. 您想要的至少涉及三个TCP连接,并且需要在与发起HTTP请求的连接不同的连接上发送HTTP响应。 That is outside of the HTTP proper - the client wouldn't know that it needs a second connection somewhere else, and how to match requests on one connection to responses on the other. 这超出了HTTP的适用范围-客户端不会知道它在其他位置需要第二个连接,以及如何将一个连接上的请求与另一个连接上的响应进行匹配。 The best you can do within HTTP is redirecting to a different server, say, with a special generated URL or something. 在HTTP中,您可以做的最好的事情就是重定向到其他服务器,例如,使用生成的特殊URL或其他内容。

Just remember that HTTP response has to come on the same TCP connection as the original request. 请记住,HTTP响应必须与原始请求位于同一TCP连接上。

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

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