Ejemplo n.º 1
0
 /// <summary>
 /// Compares the string against a given pattern.
 /// </summary>
 /// <param name="str">The string.</param>
 /// <param name="globPattern">The pattern to match, where "*" means any sequence of characters, and "?" means any single character.</param>
 /// <returns><c>true</c> if the string matches the given pattern; otherwise <c>false</c>.</returns>
 public static bool MatchesPackagePattern(this string str, string globPattern)
 {
     return(RegexEx.CreateWithTimeout(
                "^" + Regex.Escape(globPattern).Replace(@"\*", ".*") + "$",
                RegexOptions.IgnoreCase | RegexOptions.Singleline
                ).IsMatch(str));
 }
Ejemplo n.º 2
0
        private string IsChatWnd(HwndInfo hwndInfo)
        {
            string isChatWindow = "";
            string txt;

            if (WinApi.GetText(hwndInfo, out txt))
            {
                isChatWindow = RegexEx.Match(txt, QnAccountFinderFactory.Finder.ChatWindowTitlePattern);
            }
            return(isChatWindow);
        }
Ejemplo n.º 3
0
        private string GetSellerOfDesk(HwndInfo deskHwnd, out bool hasTitle)
        {
            string matchChatWindowTitle = "";
            string title;

            hasTitle = WinApi.GetText(deskHwnd, out title);
            if (hasTitle)
            {
                matchChatWindowTitle = RegexEx.Match(title, QnAccountFinderFactory.Finder.ChatWindowTitlePattern);
            }
            return(matchChatWindowTitle);
        }
Ejemplo n.º 4
0
        private long GetGoodsNumiidFromUrl(string txt)
        {
            long   rt      = 0L;
            string pattern = "(?<=id=)\\d+";

            try
            {
                string value = RegexEx.Match(txt, pattern);
                rt = Convert.ToInt64(value);
            }
            catch (Exception ex)
            {
                Log.Error(string.Format("CtlAnswer,GetGoodsNumiidFromUrl,txt={0},exp={1}", txt, ex.Message));
            }
            return(rt);
        }
Ejemplo n.º 5
0
        private string ParseContentDisposition(byte[] buffer)
        {
            // The Content-Disposition field follows immediately after the boundary. It has the format:
            //     Content-Disposition: form-data; name="asyncFileUpload"; filename="c:\users\documents\test.txt"
            //
            // We want to extract the file name out of it.
            string content = _encoding.GetString(buffer);
            var    match   = RegexEx.MatchWithTimeoutOrNull(content, @"filename\=""(.*)\""", RegexOptions.None);

            if (match != null && match.Success && match.Groups.Count > 1)
            {
                string filename = match.Groups[1].Value;
                return(filename);
            }
            return(null);
        }
Ejemplo n.º 6
0
 private string MatchWidgetWindowTitle(string title)
 {
     return(RegexEx.Match(title.Trim(), this.WidgetWindowTitlePattern).Trim());
 }
Ejemplo n.º 7
0
 private string MatchChatTitle(string title)
 {
     return(RegexEx.Match(title.Trim(), QnAccountFinderFactory.Finder.ChatWindowTitlePattern).Trim());
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Scan the client machine for services such as HTTP or samba shares
        /// </summary>
        /// <param name="n"></param>
        private void ScanClient(Node n)
        {
            //Check for HTTP
            string webTitle = string.Empty;

            try
            {
                var    wc   = new WebClient();
                string html = wc.DownloadString("http://" + n.Host);

                if (!string.IsNullOrEmpty(html))
                {
                    webTitle = RegexEx.FindMatches("<title>.*</title>", html).FirstOrDefault();
                    if (null != webTitle && !string.IsNullOrEmpty(html) && webTitle.Length > 14)
                    {
                        webTitle = webTitle.Substring(7);
                        webTitle = webTitle.Substring(0, webTitle.Length - 8);
                    }
                }

                if (string.IsNullOrEmpty(webTitle))
                {
                    webTitle = "Web";
                }
            }
            catch
            {
            }

            //Check for FTP
            string ftp = string.Empty;

            try
            {
                var client = new TcpClient();
                client.Connect(n.Host, 21);
                ftp = "FTP";
                var  sb    = new StringBuilder();
                long start = Environment.TickCount + 3000;
                var  data  = new byte[20000];
                client.ReceiveBufferSize = data.Length;

                while (start > Environment.TickCount && client.Connected)
                {
                    if (client.GetStream().DataAvailable)
                    {
                        int length = client.GetStream().Read(data, 0, data.Length);
                        sb.Append(Encoding.ASCII.GetString(data, 0, length));
                    }
                    else
                    {
                        Thread.Sleep(50);
                    }
                }
                client.Close();

                string title = sb.ToString();
                if (!string.IsNullOrEmpty(title))
                {
                    ftp = title;
                }
                data = null;
            }
            catch
            {
            }

            //Check for samba shares

            string samba = string.Empty;

            try
            {
                ShareCollection shares = ShareCollection.GetShares(n.Host);
                var             sb     = new StringBuilder();
                foreach (SambaShare share in shares)
                {
                    if (share.IsFileSystem && share.ShareType == ShareType.Disk)
                    {
                        try
                        {
                            //Make sure its readable
                            DirectoryInfo[] Flds = share.Root.GetDirectories();
                            if (sb.Length > 0)
                            {
                                sb.Append("|");
                            }
                            sb.Append(share.NetName);
                        }
                        catch
                        {
                        }
                    }
                }
                samba = sb.ToString();
            }
            catch
            {
            }

            lock (sync)
            {
                //update clients and overlords
                var r = new Node();
                r.SetData("HTTP", webTitle.Replace("\n", "").Replace("\r", ""));
                r.SetData("FTP", ftp.Replace("\n", "").Replace("\r", ""));
                r.SetData("Shares", samba.Replace("\n", "").Replace("\r", ""));
                r.ID         = n.ID;
                r.OverlordID = serverNode.ID;
                lock (sync)
                {
                    //Check the client is still connected..
                    if (connectedClientNodes.Where(nx => nx.Node.ID == r.ID).Count() > 0)
                    {
                        var verb = new UpdateVerb();
                        verb.Nodes.Add(r);
                        NetworkRequest req = verb.CreateRequest();
                        req.OverlordID = serverNode.ID;
                        SendToStandardClients(req);
                        //Dont updates about overlords to other overlords
                        if (n.NodeType != ClientType.Overlord)
                        {
                            SendToOverlordClients(req);
                        }
                    }
                }
                //Store info
                n.SetData("HTTP", webTitle.Replace("\n", "").Replace("\r", ""));
                n.SetData("FTP", ftp.Replace("\n", "").Replace("\r", ""));
                n.SetData("Shares", samba.Replace("\n", "").Replace("\r", ""));
            }
        }
        /// <summary>
        /// 根据原始的Http请求字符生成HttpRequestMessage
        /// </summary>
        /// <param name="request">用于表示拓展方法的this</param>
        /// <param name="reqRaw">原始Http请求字符</param>
        /// <returns></returns>
        // ReSharper disable once MemberCanBePrivate.Global
        public static HttpRequestMessage CreateFromRaw(this HttpRequestMessage request, string reqRaw)
        {
            // 解析reqRaw
            var splitLine = Regex.Split(reqRaw.Trim(), Environment.NewLine).Select(m => m.Trim()).ToList();

            #region 1. 解析请求行
            // 1. 解析请求行
            var requestLine = Regex.Split(splitLine.FirstOrDefault() ?? "", "\\s+");
            if (requestLine.Count() != 3 ||
                !RegexEx.IsUrl(requestLine[1]) ||
                !Regex.IsMatch(requestLine[2].ToLower(), @"http/\d+\.\d+"))
            {
                throw new ArgumentException("请求行解析出错");
            }
            var httpMethod  = requestLine[0].Trim();
            var httpUrl     = requestLine[1].Trim();
            var httpVersion = requestLine[2].Trim();                // 这里先使用默认的, 不用reqRaw中的
            request.Method     = new HttpMethod(httpMethod);
            request.RequestUri = new Uri(httpUrl);
            splitLine.Remove(splitLine.First());
            #endregion
            #region 2. 解析请求体
            // 2. 解析请求体
            var    contentFlag = splitLine.FirstOrDefault(string.IsNullOrWhiteSpace);
            var    indexFlag   = splitLine.IndexOf(contentFlag);
            string content     = "";
            if (contentFlag != null)
            {
                for (int i = indexFlag; i < splitLine.Count; i++)
                {
                    content += splitLine[i];
                }
                splitLine.RemoveRange(indexFlag, splitLine.Count - indexFlag);
            }
            request.Content = new StringContent(content);
            // 我截取的Content内容可能不跟reqRaw中的完全长度一样, 所以还是依赖框架自己计算Content-Length吧
            splitLine = splitLine.Where(m => !Regex.IsMatch(m, "Content-Length", RegexOptions.IgnoreCase)).ToList();
            #endregion
            // 3. 解析请求头
            foreach (var keyvalue in splitLine)
            {
                var keyValue = keyvalue.Split(":".ToCharArray(), 2);
                var key      = keyValue.FirstOrDefault()?.Trim();
                var value    = keyValue.LastOrDefault()?.Trim();
                if (key == null)
                {
                    throw new ArgumentException("请求头解析出错");
                }
                // 先尝试添加到HttpRequestHeaders中, 如果不行尝试添加到HttpContentHeaders中
                var removeOk = false;
                try
                {
                    request.Headers.Remove(key);
                    removeOk = true;
                }
                catch (Exception)
                {
                    // ignored
                }
                // 如果可以添加到HttpRequestHeaders中
                if (removeOk && request.Headers.TryAddWithoutValidation(key, value))
                {
                    continue;
                }
                // 尝试添加到HttpContentHeaders中
                removeOk = false;
                try
                {
                    request.Content.Headers.Remove(key);
                    removeOk = true;
                }
                catch (Exception)
                {
                    // ignored
                }
                if (!removeOk || !request.Content.Headers.TryAddWithoutValidation(key, value))
                {
                    throw new ArgumentException(key);
                }
            }
            return(request);
        }