Ejemplo n.º 1
0
 private void SetTestResult(TestInfo info)
 {
     if (InvokeRequired)
     {
         Invoke(new MethodInvoker(() => SetTestResult(info)));
     }
     else
     {
         var rows = SelectByIp(info.IP);
         if (rows.Length > 0)
         {
             rows[0][1] = info.PortMsg + (info.PortOk ? (info.PortTime / (info.PassCount == 0 ? 1 : info.PassCount)).ToString("D4") : "");
             rows[0][2] = info.HttpMsg + " " + info.CName;
             rows[0][3] = info.PassCount.ToString();
         }
     }
 }
Ejemplo n.º 2
0
 private TestInfo TestBandwidth(TestInfo info)
 {
     const int m = 2;
     using (var socket = GetSocket(info, m))
     {
         try
         {
             if (socket.BeginConnect(info.Target, null, null).AsyncWaitHandle.WaitOne(Config.ConnTimeout * m) &&
                 socket.Connected)
             {
                 using (var nets = new NetworkStream(socket))
                 {
                     using (var ssls = new SslStream(nets, false, (a, b, c, d) => true))
                     {
                         ssls.AuthenticateAsClient(string.Empty);
                         if (ssls.IsAuthenticated)
                         {
                             var data = Encoding.UTF8.GetBytes("GET /p/gogo-tester/source/browse/1m.wiki?repo=wiki HTTP/1.1\r\nHost: code.google.com\r\nConnection: close\r\n\r\n");
                             var time = Watch.ElapsedMilliseconds;
                             ssls.Write(data, 0, data.Length);
                             ssls.Flush();
                             using (var sr = new StreamReader(ssls))
                             {
                                 sr.BaseStream.ReadTimeout = 5000;
                                 try
                                 {
                                     var buf = sr.ReadToEnd();
                                     info.Bandwidth =
                                         (buf.Length / (Watch.ElapsedMilliseconds - time)).ToString("D4") + " KB/s";
                                 }
                                 catch (Exception)
                                 {
                                     info.Bandwidth = "TimeOut!";
                                 }
                             }
                         }
                         else
                         {
                             info.Bandwidth = "SslInvalid";
                         }
                     }
                 }
             }
             else
             {
                 info.Bandwidth = "Timeout";
             }
         }
         catch (Exception ex)
         {
             info.Bandwidth = ex.Message;
         }
     }
     return info;
 }
Ejemplo n.º 3
0
 private void SetBandResult(TestInfo info)
 {
     if (InvokeRequired)
     {
         Invoke(new MethodInvoker(() => SetBandResult(info)));
     }
     else
     {
         var rows = SelectByIp(info.IP);
         if (rows.Length > 0)
             rows[0][4] = info.Bandwidth;
     }
 }
Ejemplo n.º 4
0
        private Socket GetSocket(TestInfo info, int m = 1)
        {
            var socket = new Socket(info.IP.GetAddressFamily(), SocketType.Stream, ProtocolType.Tcp)
            {
                SendTimeout = Config.ConnTimeout * m,
                ReceiveTimeout = Config.ConnTimeout * m
            };
            socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true);

            return socket;
        }
Ejemplo n.º 5
0
 private TestInfo TestProcess(TestInfo info)
 {
     using (var socket = GetSocket(info))
     {
         var loops = 0;
         do
         {
             if (TestPortViaSocket(socket, info) && TestHttpViaSocket(socket, info))
             {
                 info.PassCount++;
                 loops = 0;
             }
             else
             {
                 if (info.PassCount < 6 || ++loops > 2)
                     break;
                 Thread.Sleep(1000);
             }
             if (socket.Connected)
                 socket.Disconnect(true);
         } while (info.PassCount < Config.PassCount);
     }
     return info;
 }
Ejemplo n.º 6
0
        private bool TestPortViaSocket(Socket socket, TestInfo info)
        {
            try
            {
                var time = Watch.ElapsedMilliseconds;
                if (socket.BeginConnect(info.Target, null, null).AsyncWaitHandle.WaitOne(Config.ConnTimeout)
                    && socket.Connected)
                {

                    info.PortTime += Watch.ElapsedMilliseconds - time;
                    info.PortOk = true;
                    info.PortMsg = "_OK ";
                }
                else
                {
                    info.PortOk = false;
                    info.PortMsg = "Timeout";
                    info.HttpMsg = "NN PortInvalid";
                }
            }
            catch (Exception ex)
            {
                info.PortOk = false;
                info.PortMsg = ex.Message;
                info.HttpMsg = "NN PortInvalid";
            }

            return info.PortOk;
        }
Ejemplo n.º 7
0
        private bool TestHttpViaSocket(Socket socket, TestInfo info)
        {
            try
            {
                using (var nets = new NetworkStream(socket))
                {
                    using (var ssls = new SslStream(nets, false, (sender, cert, chain, sslpe) =>
                    {
                        var str = cert.Subject;
                        var len = str.IndexOf(",", 3) - 3;
                        info.CName = str.Substring(3, len > 0 ? len : str.Length - 3);
                        return true;
                    }))
                    {
                        ssls.AuthenticateAsClient(string.Empty);
                        if (ssls.IsAuthenticated)
                        {
                            var data = Encoding.UTF8.GetBytes(string.Format("HEAD /search?q=g HTTP/1.1\r\nHost: www.google.com.hk\r\n\r\nGET /{0} HTTP/1.1\r\nHost: azzvxgoagent{1}.appspot.com\r\nConnection: close\r\n\r\n", Application.ProductVersion, Rand.Next(7)));

                            ssls.Write(data);
                            ssls.Flush();

                            using (var sr = new StreamReader(ssls))
                            {
                                var text = sr.ReadToEnd();

                                if (text.Length == 0)
                                {
                                    info.HttpOk = false;
                                    info.HttpMsg = "NN BadResponse";
                                }
                                else
                                {
                                    info.HttpMsg = "NN";

                                    var ms = RxResult.Matches(text);
                                    for (var i = 0; i < ms.Count; i++)
                                    {
                                        if (ms[i].Groups[2].Value == "200" && ++i < ms.Count)
                                            switch (ms[i].Groups[3].Value)
                                            {
                                                case "gws\r":
                                                    info.HttpOk = true;
                                                    info.HttpMsg = "G" + info.HttpMsg[1];
                                                    break;
                                                case "Google Frontend\r":
                                                    info.HttpOk = true;
                                                    info.HttpMsg = info.HttpMsg[0] + "A";
                                                    break;
                                                default:
                                                    i--;
                                                    break;
                                            }
                                    }
                                }
                            }
                        }
                        else
                        {
                            info.HttpOk = false;
                            info.HttpMsg = "NN SslInvalid";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                info.HttpOk = false;
                info.HttpMsg = "NN " + ex.Message;
            }

            return info.HttpOk;
        }