我最近建立了一个网站,它使用地理 DNS 将 DNS 解析为两个不同的 IP,具体取决于您的位置。
然而,这意味着要监控网站,我需要确保该网站在两个地理位置都可用。为此,我在 .net 中编写了一个小程序,不断尝试使用 HttpWebRequest 在网站上获取一个小 html 文件,一次使用本地互联网设置,一次使用基于该区域的代理,该代理会将名称解析为第二个 IP 地址。
这在我家里的笔记本电脑上运行良好,但是在办公室,要在几乎所有计算机上连接到互联网,您将需要通过代理,这意味着我之前设置的代理不再起作用。
我需要做的是通过办公室代理发送请求,然后通过远程国家/地区的代理发送请求,最后发送到网站。
如果这还不够清楚,请告诉我!
首先,您需要确保两个代理都是 HTTPS 并且它们都支持 CONNECT 方法,即“代理链接”。普通HTTP协议的设计不提供对“代理链”的支持。
这个想法是建立 2 个 CONNECT 隧道,一个在另一个内部。
算法如下:
- 通过 TCP 连接到第一个代理
- 向第二个代理请求 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();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)