Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Im developing an Http proxy server for a UWP application(C#) but face issues with requests sent and received by a creating a socket.I first create a socket stream object and then listen for Http requests sent by send-async to socket stream listener.However i face Http exceptions and sockets closing before requests are sent completely, the cause of this i suspect that either data is not sent to socket stream listener properly/not received by socket stream listener properly.Where am i going wrong here?

What I have tried:

//Called for each request received on StreamSocketListner , a new task is created to make connections to remote Proxy and retirve data from backend.
           public async Task listenerTask(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
           {
               try
               {
                   Debug.WriteLine("In listenerTask************ ");

                   string snirequest = null;
                   string hostName = null;
                   string reqUri = null;
                   string uriReqStr = null;
                   String orgRequest = null;

                   using (IInputStream input = args.Socket.InputStream)
                   {
                       //Reading data arrived at socket
                       orgRequest = await GetSocketDataString(input);
                   }


                   reqUri = getOriginalRequestUri(orgRequest);
                   Debug.WriteLine("*************Org uri req", reqUri);
                   string totalHostName = null;

                   if (reqUri != null)
                   {

                       if (reqUri.StartsWith("https"))
                       {
                           Debug.WriteLine("Req is https");
                           if (orgRequest.Contains("redirectrequest") == false)
                               totalHostName = reqUri.Substring(8, reqUri.Length - 8).TrimEnd('/');
                           else
                               totalHostName = reqUri.Substring(8, reqUri.Length - 8);

                           int uriIndx = totalHostName.IndexOf('/');
                           if (uriIndx > 0)
                           {
                               uriReqStr = totalHostName.Substring(uriIndx);
                               hostName = totalHostName.Substring(0, totalHostName.Length - uriReqStr.Length + 1).TrimEnd('/'); ;

                           }
                           else
                           {
                               uriReqStr = "/";
                               hostName = totalHostName.Substring(0, totalHostName.Length - uriReqStr.Length + 1);

                           }

                           Debug.WriteLine(hostName);
                           Debug.WriteLine(uriReqStr);

                           snirequest = "CONNECT " + hostName + ":443 HTTP/1.1\r\nHost: " + hostName + ":443\r\nProxy-Connection: Keep-Alive\r\nConnection: Keep-Alive\r\n\r\n";


                       }
                       else
                       {
                           if (orgRequest.Contains("redirectrequest") == false)
                               totalHostName = reqUri.Substring(7, reqUri.Length - 7).TrimEnd('/');
                           else
                               totalHostName = reqUri.Substring(7, reqUri.Length - 7);

                           int uriIndx = totalHostName.IndexOf('/');
                           if (uriIndx > 0)
                           {
                               uriReqStr = totalHostName.Substring(uriIndx);
                               hostName = totalHostName.Substring(0, totalHostName.Length - uriReqStr.Length + 1).TrimEnd('/'); ;

                           }
                           else
                           {
                               uriReqStr = "/";
                               hostName = totalHostName.Substring(0, totalHostName.Length - uriReqStr.Length + 1);

                           }

                           Debug.WriteLine(hostName);
                           Debug.WriteLine(uriReqStr);


                           snirequest = getSniRequest(reqUri, orgRequest, hostName, uriReqStr);

                       }

                       //Debug.WriteLine(snirequest);

                       Byte[] bytesSent = System.Text.Encoding.ASCII.GetBytes(snirequest);


                       // Create a socket connection with the specified server and port.
                       Windows.Networking.Sockets.StreamSocket socket = new Windows.Networking.Sockets.StreamSocket();
                       socket.Control.KeepAlive = true;
                       Windows.Networking.HostName serverHost = new Windows.Networking.HostName(PROXY_IP);

                       string serverPort = PROXY_PORT.ToString();

                       socket = await ConnectToProxy(snirequest, hostName);
                       System.IO.Stream streamOut = socket.OutputStream.AsStreamForWrite();
                       StreamWriter writer = new StreamWriter(streamOut);
                       HostName destHost = new HostName(hostName);

                       IInputStream inputStream = socket.InputStream;

                       //Debug.WriteLine(hostName);
                       //Debug.WriteLine(uriReqStr);
                       //Debug.WriteLine(reqUri);
                       //Debug.WriteLine(orgRequest.ToString());

                       String testreq = getSniRequest(reqUri, orgRequest.ToString(), hostName, uriReqStr);


                       if (reqUri.StartsWith("https"))
                       {
                           snirequest = testreq;

                           await upgradeDatamiSocket(socket, hostName, snirequest, uriReqStr);

                       }

                       StreamSocket writeSocket = args.Socket;
                       await ReadSocketData(inputStream, writeSocket);
                       socket.Dispose();  // Dispose the socket after reading data from remote proxy

                       Debug.WriteLine("Socket disposed");

                       //await System.Threading.Tasks.Task.Delay(TimeSpan.FromSeconds(5));

                   }
               }

               catch (Exception ex)
               {
                   Debug.WriteLine("Listener task Exception" + ex.Message);

               }


           }
Posted
Updated 15-Apr-17 21:55pm
Comments
RickZeeland 15-Apr-17 16:05pm    
It takes some study, but Wireshark can monitor almost all kinds of network traffic.
Chrisstone07 16-Apr-17 2:05am    
Hello Rick, how may I achieve this exactly?Could you give me any contact detail so that I can communicate more frequently
RickZeeland 16-Apr-17 3:50am    
I am not a frequent user of WireShark, and as the user interface has changed significantly I can´t tell exactly how, but in the past I defined a filter for the ip address e.g. `ip eq 127.0.0.1` then started capture.
When you see a line in the log that is interesting, you can right-click and select `Follow stream`.
This tutorial shows what I mean: https://www.howtogeek.com/104278/how-to-use-wireshark-to-capture-filter-and-inspect-packets/
Chrisstone07 16-Apr-17 4:42am    
Thank you Rick, I will use wireshark and let you know if this issue is solved.However, I also face another issue I.e the maximum number of concurrent http requests is fixed to 13 within a UWP application.Is there any way I could increase these number of requests upto a value say 100? Also could you let me know why this happens on a networking application?

1 solution

I am not a frequent user of WireShark, and as the user interface has changed significantly I can't tell exactly how, but in the past I defined a filter for the ip address e.g. `ip.addr eq 127.0.0.1` and then started capture.
When you see a line in the log that is interesting, you can right-click and select `Follow stream`.

This tutorial shows what I mean: How to Use Wireshark to Capture, Filter and Inspect Packets[^]
The filter options are explained here: DisplayFilters - The Wireshark Wiki[^]
Here is an another interesting tutorial about "Sniffing tools": Ethical Hacking Sniffing Tools[^]
 
Share this answer
 
v3

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900