如何检查send-async是否发送http请求并由套接字流侦听器接收 [英] How do I check if http requests are being sent by send-async and received by socket stream listener

查看:102
本文介绍了如何检查send-async是否发送http请求并由套接字流侦听器接收的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为UWP应用程序(C#)开发一个Http代理服务器,但是面对创建套接字发送和接收的请求的问题。我首先创建一个套接字流对象,然后监听send-async发送给套接字的Http请求流侦听器。但是,在完全发送请求之前,我面临Http异常和套接字关闭,我怀疑这两个数据没有正确地发送到套接字流监听器/套接字流监听器没有正确接收。我在哪里出错?



我的尝试:



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);

               }


           }

推荐答案

我不是 WireShark 的常用用户,并且由于用户界面发生了重大变化,我无法确切知道如何,但过去我为ip地址定义了一个过滤器,例如` ip.addr eq 127.0.0.1 `然后开始捕获。

当你在日志中看到有趣的一行时,你就可以了 - 点击并选择关注流



本教程显示我的意思:如何使用Wireshark捕获,过滤和检查数据包 [ ^ ]

过滤器选项在此解释: DisplayFilters - Wireshark Wiki [ ^ ]

这是另一个关于嗅探工具的有趣教程:道德黑客嗅探工具 [ ^ ]
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[^]


这篇关于如何检查send-async是否发送http请求并由套接字流侦听器接收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆