HttpWebRequest through two proxies

前端 未结 1 561
孤城傲影
孤城傲影 2021-01-03 06:17

I have recently set up a website which uses geographic DNS to resolve the DNS to two different IP\'s depending on your location.

However, this means to monitor the w

相关标签:
1条回答
  • 2021-01-03 06:24

    First you need to ensure that both proxies are HTTPS and they both support CONNECT method, i.e. "proxy chaining". Design of usual HTTP protocol doesn't provide support for "proxy chaining". The idea is to establish 2 CONNECT tunnels, one inside another. The algorithm is the following:

    1. Connect to the 1st proxy via TCP
    2. Request CONNECT tunnel to 2nd proxy
    3. Once tunnel is created, request tunnel to target host
    4. Send request. Request will go to target host through proxy #1 and proxy #2. Below is a sample code which I have tested on my box:

      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();
      
    0 讨论(0)
提交回复
热议问题