c#HTTPS代理(仅从浏览器中继 - >代理 - >网站 - >代理 - >浏览器)

时间:2017-04-27 15:43:20

标签: c# https proxy

所以我有这个代码,我将所有连接中继到目标服务器,我在一个byte []调用结果中得到响应,但在处理SSL / HTTPS时,终端服务器总是返回一个空响应

public static byte[] ForwardRequest(byte[] requestBytes)
    {
        byte[] result = new byte[0];

        try
        {
            string requestString = Encoding.UTF8.GetString(requestBytes);
            string[] requestLines = requestString.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

            string host = "";
            int port = 80;

            if (requestString.Contains("CONNECT"))
            {
                string connectLine = requestLines[0];
                string hostWithPort = connectLine.Split(' ')[1];
                host = hostWithPort.Split(':')[0];
                port = Convert.ToInt32(hostWithPort.Split(':')[1]);
            }
            else
            {
                host = requestLines[1].Split(' ')[1];
                if (host.Contains(":"))
                {
                    string[] split = host.Split(':');
                    host = split[0];
                    port = Convert.ToInt32(split[1]);
                }
            }

            Console.WriteLine("HOST:" + host + "#PORT:" + port);

            TcpClient client = new TcpClient();
            client.Connect(host, port);

            if(client.Connected == true && requestString.Contains("CONNECT"))
            {
                Console.WriteLine("client connected to ssl");
            }

            Network.WriteBytes(client, requestBytes);
            result = Network.ReadBytes(client, 10000);


            if (requestString.Contains("CONNECT"))
            {
                string resultString = Encoding.UTF8.GetString(result);
                Console.WriteLine("############BROWSER REQUEST################" + Environment.NewLine + requestString + Environment.NewLine + "############BROWSER REQUEST################");
                Console.WriteLine("############WEBSITE RESPONSE###############" + Environment.NewLine + resultString + Environment.NewLine + "############WEBSITE RESPONSE###############");
                Console.WriteLine("responseLenght: " + result.Length);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Network.Forward() -> " + ex.Message);
        }

        return result;
    }

我认为它应该可行,因为我只是将原始数据从浏览器发送到服务器并返回。同样,所有其他代码都可以正常工作,因为使用HTTP网站可以正常工作。

这是我发送尝试与HTTPS交谈后的控制台窗口

empty response when trying https

0 个答案:

没有答案