Exemplo n.º 1
0
        /// <summary>
        /// 判断页面注入true或false
        /// </summary>
        /// <param name="server">服务器响应对象ServerInfo</param>
        /// <param name="isUseCode">是否使用状态码判断</param>
        /// <param name="key">关键字</param>
        /// <returns></returns>
        public static Boolean isTrue(ServerInfo server, String key, Boolean reverKey, KeyType keyType)
        {
            switch (keyType)
            {
            case KeyType.Key:

                //用关键字判断
                if (server.body.Length > 0 && server.body.IndexOf(key) != -1)
                {
                    ;
                    if (reverKey)
                    {
                        return(false);
                    }
                    return(true);
                }
                else
                {
                    if (reverKey)
                    {
                        return(true);
                    }
                    return(false);
                }

            case KeyType.Reg:

                //用正则判断
                if (server.body.Length > 0 && Regex.IsMatch(server.body, key))
                {
                    ;
                    if (reverKey)
                    {
                        return(false);
                    }
                    return(true);
                }
                else
                {
                    if (reverKey)
                    {
                        return(true);
                    }
                    return(false);
                }

            case KeyType.Code:
                //用状态码判断
                if (server.code > 0 && key.Equals(server.code + ""))
                {
                    if (reverKey)
                    {
                        return(false);
                    }
                    return(true);
                }
                else
                {
                    if (reverKey)
                    {
                        return(true);
                    }
                    return(false);
                }


            case KeyType.Time:
                int time = Tools.convertToInt(key);
                if (server.runTime > time * 1000)
                {
                    if (reverKey)
                    {
                        return(false);
                    }
                    return(true);
                }
                else
                {
                    if (reverKey)
                    {
                        return(true);
                    }
                    return(false);
                }

            case KeyType.EQLen:
                //用长度判断
                if (key.Equals(server.length.ToString()))
                {
                    if (reverKey)
                    {
                        return(false);
                    }
                    return(true);
                }
                else
                {
                    if (reverKey)
                    {
                        return(true);
                    }
                    return(false);
                }

            case KeyType.MaxLen:
                //用长度判断
                if (server.length > Tools.convertToInt(key))
                {
                    if (reverKey)
                    {
                        return(false);
                    }
                    return(true);
                }
                else
                {
                    if (reverKey)
                    {
                        return(true);
                    }
                    return(false);
                }

            case KeyType.MinLen:
                //用长度判断
                if (server.length < Tools.convertToInt(key))
                {
                    if (reverKey)
                    {
                        return(false);
                    }
                    return(true);
                }
                else
                {
                    if (reverKey)
                    {
                        return(true);
                    }
                    return(false);
                }
            }
            return(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 判断页面注入true或false
        /// </summary>
        /// <param name="server">服务器响应对象ServerInfo</param>
        /// <param name="isUseCode">是否使用状态码判断</param>
        /// <param name="key">关键字</param>
        /// <returns></returns>
        public static Boolean isTrue(ServerInfo server, String key, Boolean reverKey, KeyType keyType, int trueHTTPCode)
        {
            switch (keyType)
            {
            case KeyType.Key:

                //用关键字判断
                if (server.body.Length > 0 && server.body.IndexOf(key) != -1)
                {
                    if (reverKey)
                    {
                        return(false);
                    }
                    else
                    {
                        //判断httpcode是否一致
                        if (trueHTTPCode != 0 && server.code == trueHTTPCode)
                        {
                            return(true);
                        }
                        return(false);
                    }
                }
                else
                {
                    if (reverKey)
                    {
                        //判断httpcode是否一致
                        if (trueHTTPCode != 0 && server.code == trueHTTPCode)
                        {
                            return(true);
                        }
                        return(true);
                    }
                    return(false);
                }

            case KeyType.Reg:

                //用正则判断
                if (server.body.Length > 0 && Regex.IsMatch(server.body, key))
                {
                    ;
                    if (reverKey)
                    {
                        return(false);
                    }
                    return(true);
                }
                else
                {
                    if (reverKey)
                    {
                        return(true);
                    }
                    return(false);
                }

            case KeyType.Code:
                //用状态码判断
                if (server.code > 0 && key.Equals(server.code + ""))
                {
                    if (reverKey)
                    {
                        return(false);
                    }
                    return(true);
                }
                else
                {
                    if (reverKey)
                    {
                        return(true);
                    }
                    return(false);
                }


            case KeyType.Time:
                //由于计数器有误差(可能客户端计数小于服务端,,如果页面正常响应时间非常快,可能导致返回时间可能提前,所以考虑设置一个误差值)
                int time = Tools.convertToInt(key);
                if (server.runTime > (time * 1000 - (time * 20)))
                {
                    if (reverKey)
                    {
                        return(false);
                    }
                    return(true);
                }
                else
                {
                    if (reverKey)
                    {
                        return(true);
                    }
                    return(false);
                }

            case KeyType.EQLen:
                //用长度判断
                if (key.Equals(server.length.ToString()))
                {
                    if (reverKey)
                    {
                        return(false);
                    }
                    return(true);
                }
                else
                {
                    if (reverKey)
                    {
                        return(true);
                    }
                    return(false);
                }

            case KeyType.MaxLen:
                //用长度判断
                if (server.length > Tools.convertToInt(key))
                {
                    if (reverKey)
                    {
                        return(false);
                    }
                    return(true);
                }
                else
                {
                    if (reverKey)
                    {
                        return(true);
                    }
                    return(false);
                }

            case KeyType.MinLen:
                //用长度判断
                if (server.length < Tools.convertToInt(key))
                {
                    if (reverKey)
                    {
                        return(false);
                    }
                    return(true);
                }
                else
                {
                    if (reverKey)
                    {
                        return(true);
                    }
                    return(false);
                }
            }
            return(false);
        }
Exemplo n.º 3
0
 public static void sysHTTPLog(String index, ServerInfo server)
 {
     FileTool.AppendLogToFile(httpLogPath + index + "-request.txt", server.request);
     FileTool.AppendLogToFile(httpLogPath + index + "-response.txt", server.header + "\r\n\r\n" + server.body);
 }
Exemplo n.º 4
0
        private static ServerInfo sendHTTPSRequest(int count, String host, int port, String payload, String request, int timeout, String encoding, Boolean foward_302)
        {
            String    index = Thread.CurrentThread.Name + HTTP.index;
            Stopwatch sw    = new Stopwatch();

            sw.Start();
            ServerInfo server = new ServerInfo();

            int sum = 0;

            TcpClient clientSocket = null;;

            try
            {
                if (port > 0 && port <= 65556)
                {
                    TimeOutSocket tos = new TimeOutSocket();
                    clientSocket = tos.Connect(host, port, timeout);
                    if (sw.ElapsedMilliseconds >= timeout)
                    {
                        return(server);
                    }
                    clientSocket.SendTimeout = timeout - tos.useTime;

                    SslStream ssl = null;
                    if (clientSocket.Connected)
                    {
                        ssl = new SslStream(clientSocket.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate));
                        SslProtocols protocol = SslProtocols.Ssl3 | SslProtocols.Ssl2 | SslProtocols.Tls;
                        ssl.AuthenticateAsClient(host, null, protocol, false);
                        if (ssl.IsAuthenticated)
                        {
                            checkContentLength(ref server, ref request);
                            server.request = request;
                            byte[] requestByte = Encoding.UTF8.GetBytes(request);
                            ssl.Write(requestByte);
                            ssl.Flush();
                        }
                    }
                    server.request = request;
                    byte[] responseBody = new byte[1024 * 1000];
                    int    len          = 0;
                    //获取header头
                    String tmp = "";

                    StringBuilder sb      = new StringBuilder();
                    StringBuilder bulider = new StringBuilder();
                    clientSocket.ReceiveTimeout = timeout - (int)sw.ElapsedMilliseconds;
                    do
                    {
                        byte[] responseHeader = new byte[1];
                        int    read           = ssl.ReadByte();

                        char c = (char)read;
                        sb.Append(c);
                        if (c.Equals(T))
                        {
                            tmp = String.Concat(sb[sb.Length - 4], sb[sb.Length - 3], sb[sb.Length - 2], c);
                        }
                    } while (!tmp.Equals(CTRL) && sw.ElapsedMilliseconds < timeout);

                    server.header = sb.ToString().Replace(CTRL, "");
                    String[] headers = Regex.Split(server.header, CT);
                    //处理header
                    doHeader(ref server, ref headers);
                    //自动修正编码
                    if (!String.IsNullOrEmpty(server.encoding))
                    {
                        encoding = server.encoding;
                    }
                    Encoding encod = Encoding.GetEncoding(encoding);
                    //302 301跳转
                    if ((server.code == 302 || server.code == 301) && foward_302)
                    {
                        int urlStart = server.request.IndexOf(" ");
                        int urlEnd   = server.request.IndexOf(" HTTP");
                        if (urlStart != -1 && urlEnd != -1)
                        {
                            String url = server.request.Substring(urlStart + 1, urlEnd - urlStart - 1);
                            if (!server.headers["location"].StartsWith("/") && !server.headers["location"].StartsWith("https"))
                            {
                                server.request = server.request.Replace(url, Tools.getCurrentPath(url) + server.headers["location"]);
                            }
                            else
                            {
                                server.request = server.request.Replace(url, server.headers["location"]);
                            }

                            return(sendHTTPSRequest(count, host, port, payload, server.request, timeout, encoding, false));
                        }
                    }


                    //根据请求头解析
                    if (server.headers.ContainsKey(Content_Length))
                    {
                        int length = int.Parse(server.headers[Content_Length]);
                        while (sum < length && sw.ElapsedMilliseconds < timeout)
                        {
                            len = ssl.Read(responseBody, sum, length - sum);
                            if (len > 0)
                            {
                                sum += len;
                            }
                        }
                    }
                    //解析chunked传输
                    else if (server.headers.ContainsKey(Transfer_Encoding))
                    {
                        //读取长度
                        int    chunkedSize = 0;
                        byte[] chunkedByte = new byte[1];
                        //读取总长度
                        sum = 0;
                        do
                        {
                            String ctmp = "";
                            do
                            {
                                len   = ssl.Read(chunkedByte, 0, 1);
                                ctmp += Encoding.UTF8.GetString(chunkedByte);
                            } while (ctmp.IndexOf(CT) == -1 && sw.ElapsedMilliseconds < timeout);

                            chunkedSize = Tools.convertToIntBy16(ctmp.Replace(CT, ""));

                            //chunked的结束0\r\n\r\n是结束标志,单个chunked块\r\n结束
                            if (ctmp.Equals(CT))
                            {
                                continue;
                            }
                            if (chunkedSize == 0)
                            {
                                //结束了
                                break;
                            }
                            int onechunkLen = 0;

                            while (onechunkLen < chunkedSize && sw.ElapsedMilliseconds < timeout)
                            {
                                len = ssl.Read(responseBody, sum, chunkedSize - onechunkLen);
                                if (len > 0)
                                {
                                    onechunkLen += len;
                                    sum         += len;
                                }
                            }

                            //判断
                        } while (sw.ElapsedMilliseconds < timeout);
                    }
                    //connection close方式或未知body长度
                    else
                    {
                        while (sw.ElapsedMilliseconds < timeout)
                        {
                            if (clientSocket.Client.Poll(timeout, SelectMode.SelectRead))
                            {
                                if (clientSocket.Available > 0)
                                {
                                    len = ssl.Read(responseBody, sum, (1024 * 200) - sum);
                                    if (len > 0)
                                    {
                                        sum += len;
                                    }
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                    }
                    //判断是否gzip
                    if (server.headers.ContainsKey(Content_Encoding))
                    {
                        server.body = unGzip(responseBody, sum, encod);
                    }
                    else
                    {
                        server.body = encod.GetString(responseBody, 0, sum);
                    }
                }
            }
            catch (Exception e)
            {
                Exception ee = new Exception("HTTPS发包错误!错误消息:" + e.Message + "----发包编号:" + index);
                throw ee;
            }
            finally
            {
                sw.Stop();
                server.length  = sum;
                server.runTime = (int)sw.ElapsedMilliseconds;

                if (clientSocket != null)
                {
                    clientSocket.Close();
                }
            }
            return(server);
        }