Example #1
0
        private void ProcessConnect(SocketAsyncEventArgs e)
        {
            TCPScanTaskInfo ti = e.UserToken as TCPScanTaskInfo;

            ti.LastTime = DateTime.Now;
            Socket    sock = ti.Socket;
            ProbeInfo pi   = ti.CurrentProbe;

            //SocketError.AccessDenied
            switch (e.SocketError)
            {
            case SocketError.Success:
                ti.RunedRetry = 0;
                ti.CanConnect = true;
                break;

            case SocketError.ConnectionReset:           //此连接由远程对等计算机重置
            case SocketError.ConnectionRefused:         //远程主机正在主动拒绝连接
            case SocketError.AddressNotAvailable:       //选定的 IP 地址在此上下文中无效
            case SocketError.Fault:                     //基础套接字提供程序检测到无效的指针地址
            case SocketError.HostDown:                  //由于远程主机被关闭 操作失败
            case SocketError.HostNotFound:              //无法识别这种主机 该名称不是正式的主机名或别名
            case SocketError.HostUnreachable:           //没有到指定主机的网络路由
            case SocketError.NetworkDown:               //网络不可用
            case SocketError.NetworkUnreachable:        //不存在到远程主机的路由
                this.PushSAE(e);
                this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, ti.CanConnect, e.SocketError.ToString()));
                return;
            }
            if (SocketError.Success != e.SocketError || !ti.IsStarted)
            {
                this.PushSAE(e);
                if (++ti.RunedRetry > ti.Retry || ti.IsTotalTimeout)
                {
                    this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, ti.CanConnect, e.SocketError.ToString()));
                }
                else
                {
                    this.StartConnect(ti);
                }
                return;
            }
            try {
                if (!sock.ReceiveAsync(ti.RecvSAE))
                {
                    IOProcessPool.QueueWork(this.ProcessRecv, ti.RecvSAE);
                }
            } catch (Exception ex) {
                this.PushSAE(e);
                this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, ti.CanConnect, "[SOCKET-RECV]-" + ex.Message));
                return;
            }
            try {
                if (pi != null)
                {
                    if (e.Buffer.Length < pi.Data.Length)
                    {
                        e.SetBuffer(new byte[pi.Data.Length], 0, pi.Data.Length);
                    }
                    Array.Copy(pi.Data, e.Buffer, pi.Data.Length);
                    e.SetBuffer(0, pi.Data.Length);
                    if (!sock.SendAsync(e))
                    {
                        IOProcessPool.QueueWork(this.ProcessSend, e);
                    }
                }
                else
                {
                    this.PushSAE(e);
                }
            } catch {
                this.PushSAE(e);
            }
        }
Example #2
0
        public void LoadProbeConfig(string strConfigProbes)
        {
            string[]         strLines = strConfigProbes.Split('\n');
            List <ProbeInfo> lst      = new List <ProbeInfo>();
            ProbeInfo        pi       = null;

            char[] spliter = new char[] { ',', '-' };
            int    nLine   = 0;
            string strLine = string.Empty;

            try {
                foreach (var n in strLines)
                {
                    nLine++;
                    strLine = n.Trim();
                    if (strLine == string.Empty || strLine[0] == '#')
                    {
                        continue;
                    }
                    if (strLine.StartsWith("**[PROBE_S]**"))
                    {
                        pi = new ProbeInfo();
                    }
                    if (strLine.StartsWith("**[PROBE_E]**"))
                    {
                        if (pi.Ports.Count == 0)
                        {
                            pi.Ports.Add(0);
                        }
                        lst.Add(pi);
                        pi = null;
                    }
                    if (strLine.StartsWith("PROBE_DATA"))
                    {
                        var m = Regex.Match(strLine, @"\[(.*?)\]\s*\{(.*)\}");
                        pi.IsTcp = m.Groups[1].Value.Trim().ToLower() == "tcp";
                        pi.Data  = ProbeConfiger.StringToByte(m.Groups[2].Value);
                    }
                    else if (strLine.StartsWith("PROBE_PORTS"))
                    {
                        foreach (var v in Regex.Match(strLine, @"\[(.*)\]").Groups[1].Value.Trim().Trim(',').Split(','))
                        {
                            foreach (var p in v.Trim().Trim('-').Split('-'))
                            {
                                pi.Ports.Add(int.Parse(p));
                            }
                        }
                    }
                    else if (strLine.StartsWith("PROBE_INDEX"))
                    {
                        pi.Index = int.Parse(Regex.Match(strLine, @"\[(.*)\]").Groups[1].Value);
                    }
                    else if (strLine.StartsWith("PROBE_REGEX"))
                    {
                        Match mr = Regex.Match(strLine, @"\[(.*?)\]\s*\{(.*)\}");
                        ProbeInfo.RegexInfo ri = new ProbeInfo.RegexInfo();
                        if (mr.Groups[1].Value == string.Empty || mr.Groups[2].Value == string.Empty)
                        {
                            throw new Exception();
                        }
                        ri.Name    = mr.Groups[1].Value;
                        ri.RegLine = nLine;
                        ri.Regex   = new Regex(mr.Groups[2].Value);
                        pi.RegexList.Add(ri);
                    }
                }
            } catch (ArgumentException ex) {
                throw new Exception("Load probes error on Line:" + nLine, ex);
            }
            int nLen  = lst.Count;
            int nFlag = nLen;

            while (nFlag > 0)
            {
                nLen  = nFlag;
                nFlag = 0;
                for (int i = 1; i < nLen; i++)
                {
                    if (lst[i - 1].Index > lst[i].Index)
                    {
                        var temp = lst[i - 1];
                        lst[i - 1] = lst[i];
                        lst[i]     = temp;
                    }
                }
            }
            Dictionary <int, List <ProbeInfo> > dic = new Dictionary <int, List <ProbeInfo> >();

            lst.ForEach(p => {
                foreach (var x in p.Ports)
                {
                    if (dic.ContainsKey(x))
                    {
                        dic[x].Add(p);
                    }
                    else
                    {
                        List <ProbeInfo> l = new List <ProbeInfo>();
                        l.Add(p);
                        dic.Add(x, l);
                    }
                }
            });
            lock (m_obj_sync) {
                this._ProbesDictionary = dic;
                this._AllProbes        = lst;
            }
        }
Example #3
0
        public MatchResult MatchData(byte[] byBuffer, int nLen, int nPort, ProbeType type, ProbeInfo probeInfo)
        {
            StringBuilder sb_m      = new StringBuilder();
            StringBuilder sb_r      = new StringBuilder();
            string        strMatch  = string.Empty;
            string        strResult = string.Empty;

            for (int i = 0; i < nLen; i++)
            {
                sb_m.Append((char)byBuffer[i]);
                sb_r.Append(ProbeConfiger.ByteToChar(byBuffer[i]));
            }
            strMatch  = sb_m.ToString();
            strResult = sb_r.ToString();

            var probesDic = this._ProbesDictionary;

            if (probeInfo != null)
            {
                foreach (var r in probeInfo.RegexList)
                {
                    if (r.Regex.IsMatch(strMatch))
                    {
                        return(new MatchResult(r.Name, strResult, r.RegLine));
                    }
                }
            }
            HashSet <ProbeInfo> hs = new HashSet <ProbeInfo>();

            hs.Add(probeInfo);
            if (probesDic.ContainsKey(nPort))
            {
                foreach (var p in probesDic[nPort])
                {
                    if (!p.IsTcp)
                    {
                        continue;
                    }
                    if (type == ProbeType.Tcp && !p.IsTcp)
                    {
                        continue;
                    }
                    if (type == ProbeType.Udp && p.IsTcp)
                    {
                        continue;
                    }
                    if (hs.Contains(p))
                    {
                        continue;
                    }
                    foreach (var r in p.RegexList)
                    {
                        if (r.Regex.IsMatch(strMatch))
                        {
                            return(new MatchResult(r.Name, strResult, r.RegLine));
                        }
                    }
                    hs.Add(p);
                }
            }
            var allProbes = this._AllProbes;

            foreach (var p in allProbes)
            {
                if (type == ProbeType.Tcp && !p.IsTcp)
                {
                    continue;
                }
                if (type == ProbeType.Udp && p.IsTcp)
                {
                    continue;
                }
                if (hs.Contains(p))
                {
                    continue;
                }
                foreach (var r in p.RegexList)
                {
                    if (r.Regex.IsMatch(strMatch))
                    {
                        return(new MatchResult(r.Name, strResult, r.RegLine));
                    }
                }
            }
            return(new MatchResult(null, strResult, 0));
        }
        private void ProcessConnect(SocketAsyncEventArgs e)
        {
            TCPScanTaskInfo ti = e.UserToken as TCPScanTaskInfo;

            ti.LastTime = DateTime.Now;
            Socket    sock = ti.Socket;
            ProbeInfo pi   = ti.CurrentProbe;

            //SocketError.AccessDenied
            switch (e.SocketError)
            {
            case SocketError.Success: break;

            case SocketError.ConnectionReset:
            case SocketError.ConnectionRefused:
                this.PushSAE(e);
                this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, ti.CanConnect, e.SocketError.ToString()));
                return;

            default:
                if (e.SocketError != SocketError.OperationAborted)
                {
                    DateTime dd = DateTime.Now;
                    lock (m_writer) {
                        if (dd.Subtract(ti.StartTime).TotalSeconds > 70)
                        {
                            int a = 0;
                            a++;
                        }
                        m_writer.WriteLine("ERR:" + e.SocketError + " tt:" + DateTime.Now.Subtract(ti.StartTime) + " rr:" + ti.RunedRetry);
                        m_writer.Flush();
                    }
                }
                this.PushSAE(e);
                if (++ti.RunedRetry > ti.Retry)
                {
                    this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, ti.CanConnect, e.SocketError.ToString()));
                }
                else
                {
                    this.StartConnect(ti);
                }
                return;
            }
            //if (e.SocketError != SocketError.Success) {
            //    m_writer.WriteLine(e.SocketError);
            //    m_writer.Flush();
            //    this.PushSAE(e);
            //    if (++ti.RunedRetry > ti.Retry)
            //        this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, ti.CanConnect, e.SocketError.ToString()));
            //    else
            //        this.StartConnect(ti);
            //    return;
            //} else {
            //    //m_writer.WriteLine(e.SocketError);
            //    //m_writer.Flush();
            //}
            ti.RunedRetry = 0;
            ti.CanConnect = true;
            try {
                if (!sock.ReceiveAsync(ti.RecvSAE))
                {
                    IOProcessPool.QueueWork(this.ProcessRecv, ti.RecvSAE);
                }
            } catch (Exception ex) {
                this.PushSAE(e);
                lock (m_writer) {
                    m_writer.WriteLine("[SOCKET-RECV]-" + ex.Message);
                    m_writer.Flush();
                }
                this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, ti.CanConnect, "[SOCKET-RECV]-" + ex.Message));
                return;
            }
            try {
                if (pi != null)
                {
                    if (e.Buffer.Length < pi.Data.Length)
                    {
                        e.SetBuffer(new byte[pi.Data.Length], 0, pi.Data.Length);
                    }
                    Array.Copy(pi.Data, e.Buffer, pi.Data.Length);
                    e.SetBuffer(0, pi.Data.Length);
                    if (!sock.SendAsync(e))
                    {
                        IOProcessPool.QueueWork(this.ProcessSend, e);
                    }
                }
                else
                {
                    this.PushSAE(e);
                }
            } catch {
                this.PushSAE(e);
            }
            //try {
            //    if (!ti.Socket.ReceiveAsync(ti.RecvSAE)) IOProcessPool.QueueWork(this.ProcessRecv, ti.RecvSAE);
            //    if (ti.CurrentProbe != null) {
            //        if (e.Buffer.Length < ti.CurrentProbe.Data.Length)
            //            e.SetBuffer(new byte[ti.CurrentProbe.Data.Length], 0, ti.CurrentProbe.Data.Length);
            //        Array.Copy(ti.CurrentProbe.Data, e.Buffer, ti.CurrentProbe.Data.Length);
            //        e.SetBuffer(0, ti.CurrentProbe.Data.Length);
            //        if (!ti.Socket.SendAsync(e)) IOProcessPool.QueueWork(this.ProcessSend, e);
            //    } else this.PushSAE(e);
            //} catch (Exception ex) {
            //    this.PushSAE(e);
            //    this.EndTask(ti, new ScanEventArgs(ti.TaskID, ti.EndPoint, ti.CanConnect, "[SOCKET]-" + ex.Message));
            //}
        }