0

我必须跟踪 HTTP 流量。它在没有 Https 的情况下工作。我尝试使用 https,它通过证书通过 Sslstream 成功获得响应。当我转发中继的 ssl 响应但它没有得到响应时。

请让我如何中继 SSL 响应?

请检查以下代码是否相同。

谢谢

 public void ThreadHandleHTTPClient(object o)
 {
try{
                Socket client = (Socket)o;

                Stream ns = new NetworkStream(client);
                //Stream outStrem = ns;
                //RECEIVE CLIENT DATA
                byte[] buffer = new byte[2048];
                int rec = 0, sent = 0, transferred = 0, rport = 0;
                string data = "";
                do
                {
                    rec = ns.Read(buffer, 0, buffer.Length);
                    data += Encoding.ASCII.GetString(buffer, 0, rec);
                } while (rec == buffer.Length);

                //PARSE DESTINATION AND SEND REQUEST
                string line = data.Replace("\r\n", "\n").Split(new string[] { "\n" }, StringSplitOptions.None)[0];
                string e=line.Split(new string[] { " " }, StringSplitOptions.None)[1];
                Uri uri = new Uri(e);
                if (uri.Scheme == "https" || line.Contains("CONNECT"))
                {
                    rport = 443;
                    //Make tunnel for the HTTPS which is authentic host.
                   SslStream sslStream = sslTunnel(ns, uri.OriginalString, "1.0");
                   ns = sslStream;

                   string remoteUri = "https://" + uri.Scheme;
                   StreamReader clientStreamReader = new StreamReader(sslStream);


                   string httpCmd = clientStreamReader.ReadLine();
                   string[] splitBuffer = httpCmd.Split(_spaceSplit, 3);
                   remoteUri = remoteUri + splitBuffer[1];

                   myQuery = string.Empty;
                   //Read the SSL Stream save command in myQuery variable.
                   readRequestHeadersEx(clientStreamReader);
                   data = myQuery;
                   data = splitBuffer[0]+" ";
                   data += remoteUri;
                   data += " ";
                   data += splitBuffer[2];
                   data += "\r\n";
                   data += myQuery;
                   data += "\r\n";

                   line = data.Replace("\r\n", "\n").Split(new string[] { "\n" }, StringSplitOptions.None)[0];
                   uri = new Uri(line.Split(new string[] { " " }, StringSplitOptions.None)[1]);               

                }
                else
                {
                    rport = 80;                   
                }

                IPHostEntry rh = Dns.GetHostEntry(uri.Host);
                Socket remoteserver = new Socket(rh.AddressList[0].AddressFamily, SocketType.Stream, ProtocolType.IP);
                remoteserver.Connect(new IPEndPoint(rh.AddressList[0], rport));
                byte[] databytes = Encoding.ASCII.GetBytes(data);
                remoteserver.Send(databytes, databytes.Length, SocketFlags.None);

                //START RELAY
                buffer = new byte[2048];
                rec = 0;
                data = "";
                do
                {

                    transferred = 0;
                    do
                    {
                        rec = remoteserver.Receive(buffer, buffer.Length, SocketFlags.None);
                        sent = client.Send(buffer, rec, SocketFlags.None);
                        transferred += rec;
                        data += Encoding.ASCII.GetString(buffer, 0, rec);
                    } while (rec == buffer.Length);

                    if (transferred == 0)
                        break;

                } while (transferred > 0);

                client.Close();

            }
            catch (Exception ex)
            {

            }
        }

public void Start(IPAddress ip, int port)
            {     
 TcpListener listener = new TcpListener(ip, port);
                listener.Start(100);
                while (true)
                {
                    Socket client = listener.AcceptSocket();
                    Thread th = new Thread(ThreadHandleHTTPClient);
                    th.Start(client);
                }

                listener.Stop();         
            }
4

0 回答 0