我最近建立了一个网站,该网站使用地理DNS将DNS解析为两个不同的IP,具体取决于您的位置。
但是,这意味着要监控网站,我需要确保该网站在两个地理位置都可用。为此,我在.net中编写了一个小程序,不断尝试和HttpWebRequest在网站上获取一个小的html文件一次使用本地互联网设置,一次使用基于该区域的代理将名称解析为第二个IP地址
这在家里的笔记本电脑上工作正常,但在办公室,几乎所有机器上都需要通过代理连接到互联网,这意味着我之前设置的代理不再有效。
我需要做的是通过办公室代理发送请求,然后通过远程国家/地区的代理发送请求,最后发送到网站。
如果这还不够明确,请告诉我!
答案 0 :(得分:4)
首先,您需要确保两个代理都是HTTPS,并且它们都支持CONNECT方法,即“代理链接”。通常的HTTP协议的设计不支持“代理链接”。 我们的想法是建立2个CONNECT隧道,一个在另一个内部。 算法如下:
发送请求。请求将通过代理#1和代理#2转到目标主机。 下面是我在盒子上测试的示例代码:
string host = "encrypted.google.com";
string proxy2 = "213.240.237.149";//host;
int proxyPort2 = 3128;//443;
string proxy = "180.183.236.63";//host;
int proxyPort = 3128;//443;
byte[] buffer = new byte[2048];
int bytes;
// Connect to the 1st proxy
TcpClient client = new TcpClient(proxy, proxyPort);
NetworkStream stream = client.GetStream();
// Establish tunnel to 2nd proxy
byte[] tunnelRequest = Encoding.UTF8.GetBytes(String.Format("CONNECT {0}:{1} HTTP/1.1\r\nHost:{0}\r\n\r\n", proxy2, proxyPort2));
stream.Write(tunnelRequest, 0, tunnelRequest.Length);
stream.Flush();
// Read response to CONNECT request
// There should be loop that reads multiple packets
bytes = stream.Read(buffer, 0, buffer.Length);
Console.Write(Encoding.UTF8.GetString(buffer, 0, bytes));
// Establish tunnel to target host
tunnelRequest = Encoding.UTF8.GetBytes(String.Format("CONNECT {0}:443 HTTP/1.1\r\nHost:{0}\r\n\r\n", host));
stream.Write(tunnelRequest, 0, tunnelRequest.Length);
stream.Flush();
// Read response to CONNECT request
// There should be loop that reads multiple packets
bytes = stream.Read(buffer, 0, buffer.Length);
Console.Write(Encoding.UTF8.GetString(buffer, 0, bytes));
// Wrap into SSL stream
SslStream sslStream2 = new SslStream(stream);
sslStream2.AuthenticateAsClient(host);
// Send request
byte[] request = Encoding.UTF8.GetBytes(String.Format("GET https://{0}/ HTTP/1.1\r\nHost: {0}\r\n\r\n", host));
sslStream2.Write(request, 0, request.Length);
sslStream2.Flush();
// Read response
do
{
bytes = sslStream2.Read(buffer, 0, buffer.Length);
Console.Write(Encoding.UTF8.GetString(buffer, 0, bytes));
} while (bytes != 0);
client.Close();
Console.ReadKey();