Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# TCP/IP https proxy
Hi All
I have to trace the HTTP traffic .It work without Https. I try for the https, it successfully get the response via Sslstream through the certificate. When i forward ssl response for the relay but it doesn’t getting the response.
 
Please let me how to relay SSL response?
 
Please check the following code for the same.
 
Thanks
 
//This mange the http request and response.
public void ThreadHandleHTTPClient(object o)
        {
 try
            {
                Socket client = (Socket)o;
                NetworkStream ns = new NetworkStream(client);
                //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 with the right certificate.
                   SslStream sslStream = sslTunnel(ns, uri.OriginalString, "1.0");
                   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 = "GET ";
                   data += remoteUri;
                   data += " ";
                   data += "HTTP/1.0";
                   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(serverbytes, 0, rec);
                    } while (rec == buffer.Length);
 
                    if (transferred == 0)
                        break;
 
                } while (transferred > 0);
 
                client.Close();
            }
            catch (Exception ex)
            {
                
            }           
        }
 
//Start The thread
 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();         
            }
Posted 17-May-13 20:18pm
GAJERA1.8K
Comments
Mohammed Hameed at 18-May-13 2:01am
   
Very interesting question asked. But not have much time to check it out and try. +5 for it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 475
1 Maciej Los 339
2 Richard MacCutchan 225
3 BillWoodruff 219
4 Suraj Sahoo | Coding Passion 155
0 OriginalGriff 8,759
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,269
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411028.1 | Last Updated 18 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100