【问题标题】:c#- HTTPS proxyc#- HTTPS 代理
【发布时间】:2016-07-11 23:42:07
【问题描述】:

我试图在 c# 中创建一个 HTTPS 代理服务器。 有人在这里发布了一个解决方案:

        string host = "encrypted.google.com";
        string proxy = "127.0.0.1";//host;
        int proxyPort = 8888;//443;

        byte[] buffer = new byte[2048];
        int bytes;

        // Connect socket
        TcpClient client = new TcpClient(proxy, proxyPort);
        NetworkStream stream = client.GetStream();

        // Establish Tcp tunnel
        byte[] 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 in SSL stream
        SslStream sslStream = new SslStream(stream);
        sslStream.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));
        sslStream.Write(request, 0, request.Length);
        sslStream.Flush();

        // Read response
        do
        {
            bytes = sslStream.Read(buffer, 0, buffer.Length);
            Console.Write(Encoding.UTF8.GetString(buffer, 0, bytes));
        } while (bytes != 0);

        client.Close();
        Console.ReadKey();

但是,这不是服务器的代码。它打印响应(作为纯文本),但客户端套接字(浏览器)没有得到响应。

感谢您的帮助, 谢谢。

【问题讨论】:

    标签: c# ssl https proxy server


    【解决方案1】:

    我不是 C# 专家,但不会将 Console.Write 替换为对原始 CONNECT 请求来自的 TCP 连接的写操作来解决此问题?

    看起来缺少一些代码,就像在这一切发生之前,您必须通过 TCP 连接来监听客户端。例如,您正在侦听 IP 地址 192.168.1.1 端口 8080。然后客户端发送类似的东西

    HTTP/1.1 www.example.com:443

    然后你去做所有这些事情,然后你想做 (192.168.1.1:8080).write() 或 C# 等价物,而不是 Console.write。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-21
      • 2012-02-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多