Пример #1
0
        /// <summary>
        /// 开启网络流量监控
        /// </summary>
        public void Start()
        {
            #region 启动系统性能计数器统计
            try
            {
                _NetFlow.Start();
                _NetFlow.DataMonitorEvent += DataMonitorEvent;
                _IsNetFlowRun              = true;
            }
            catch { }
            #endregion

            #region 启动Socket包统计
            if (PermissionTool.IsAdmin())
            {
                List <IPAddress> hosts = NetCardInfoTool.GetIPv4Address();
                AllIPv4Address = NetCardInfoTool.GetAllIPv4Address();
                foreach (var host in hosts)
                {
                    try
                    {
                        NetPacketTool p = new NetPacketTool(host);
                        p.NewPacket += new NewPacketEventHandler(NewPacketEvent);
                        p.Start();
                        NetPacketList.Add(p);
                    }
                    catch { }
                }
                if (ListTool.HasElements(NetPacketList))
                {
                    _IsNetPacketRun = true;
                }
            }
            #endregion
        }
Пример #2
0
        /// <summary>
        /// 联网断网重启计划(应对断网或重连后网卡抓包报错造成的不准确)
        /// </summary>
        private void CheckRestart()
        {
            bool rest = false;

            string[] ints = NetCardInfoTool.GetInstanceNames();
            if (ListTool.IsNullOrEmpty(_NetFlow.Instances) && ListTool.HasElements(ints))
            {
                rest = true;
            }
            if (ListTool.HasElements(_NetFlow.Instances) && ListTool.HasElements(ints) &&
                string.Join("-", _NetFlow.Instances) != string.Join("-", ints))
            {
                rest = true;
            }

            if (rest)
            {
                //重启 系统性能计数器
                _NetFlow.Restart();
                //重启 抓包监听
                List <IPAddress> hosts = NetCardInfoTool.GetIPv4Address();
                AllIPv4Address = NetCardInfoTool.GetAllIPv4Address();
                foreach (var host in hosts)
                {
                    try
                    {
                        if (!NetPacketList.Any(x => x.IP.ToString() == host.ToString()))
                        {
                            NetPacketTool p = new NetPacketTool(host);
                            p.NewPacket += new NewPacketEventHandler(NewPacketEvent);
                            p.Start();
                            NetPacketList.Add(p);
                        }
                    }
                    catch { }
                }
            }
        }
Пример #3
0
        /// <summary>
        /// 整理数据包到所属的进程
        /// </summary>
        /// <param name="tool"></param>
        /// <param name="packet"></param>
        private void NewPacketEvent(NetPacketTool tool, Packet packet)
        {
            bool isGather = false;

            #region 整理TCP包
            if (packet.Protocol == Protocol.Tcp && ListTool.HasElements(TcpConnection) && ListTool.HasElements(NowProcess))
            {
                lock (TcpConnection)
                {
                    // tcp 下载
                    if (TcpConnection.Any(x => x.RemoteIP.ToString() == packet.DestinationAddress.ToString() && x.RemotePort == packet.DestinationPort))
                    {
                        var tcpDownload = TcpConnection.FirstOrDefault(x => x.RemoteIP.ToString() == packet.DestinationAddress.ToString() && x.RemotePort == packet.DestinationPort);
                        var process     = NowProcess.FirstOrDefault(x => x.Id == tcpDownload.ProcessId);
                        if (process != null)
                        {
                            var info = _NetProcessInfoList.FirstOrDefault(x => x.ProcessName == process.ProcessName);
                            if (info != null)
                            {
                                isGather               = true;
                                info.DownloadBag      += packet.TotalLength;
                                info.DownloadBagCount += packet.TotalLength;
                            }
                        }
                    }
                    // tcp 上传
                    if (TcpConnection.Any(x => x.LocalIP.ToString() == packet.SourceAddress.ToString() && x.LocalPort == packet.SourcePort))
                    {
                        var tcUpload = TcpConnection.FirstOrDefault(x => x.LocalIP.ToString() == packet.SourceAddress.ToString() && x.LocalPort == packet.SourcePort);
                        var process  = NowProcess.FirstOrDefault(x => x.Id == tcUpload.ProcessId);
                        if (process != null)
                        {
                            var info = _NetProcessInfoList.FirstOrDefault(x => x.ProcessName == process.ProcessName);
                            if (info != null)
                            {
                                isGather             = true;
                                info.UploadBag      += packet.TotalLength;
                                info.UploadBagCount += packet.TotalLength;
                            }
                        }
                    }
                }
            }
            #endregion
            #region 整理UDP包
            if (packet.Protocol == Protocol.Udp && ListTool.HasElements(UdpConnection) && ListTool.HasElements(NowProcess))
            {
                lock (UdpConnection)
                {
                    // tcp 下载
                    if (UdpConnection.Any(x => x.LocalPort == packet.DestinationPort) && AllIPv4Address.Contains(packet.DestinationAddress.ToString()))
                    {
                        var udpDownload = UdpConnection.FirstOrDefault(x => AllIPv4Address.Contains(x.LocalIP.ToString()) && x.LocalPort == packet.DestinationPort);
                        var process     = NowProcess.FirstOrDefault(x => x.Id == udpDownload.ProcessId);
                        if (process != null)
                        {
                            var info = _NetProcessInfoList.FirstOrDefault(x => x.ProcessName == process.ProcessName);
                            if (info != null)
                            {
                                isGather               = true;
                                info.DownloadBag      += packet.TotalLength;
                                info.DownloadBagCount += packet.TotalLength;
                                if (info.ProcessName == "Idle")
                                {
                                }
                            }
                        }
                    }
                    // udp 上传
                    if (UdpConnection.Any(x => x.LocalPort == packet.SourcePort) && AllIPv4Address.Contains(packet.SourceAddress.ToString()))
                    {
                        var udpIp    = AllIPv4Address.FirstOrDefault(x => x == packet.SourceAddress.ToString());
                        var ucUpload = UdpConnection.FirstOrDefault(x => AllIPv4Address.Contains(x.LocalIP.ToString()) && x.LocalPort == packet.SourcePort);
                        var process  = NowProcess.FirstOrDefault(x => x.Id == ucUpload.ProcessId);
                        if (process != null)
                        {
                            var info = _NetProcessInfoList.FirstOrDefault(x => x.ProcessName == process.ProcessName);
                            if (info != null)
                            {
                                isGather             = true;
                                info.UploadBag      += packet.TotalLength;
                                info.UploadBagCount += packet.TotalLength;
                                if (info.ProcessName == "Idle")
                                {
                                }
                            }
                        }
                    }
                }
            }
            #endregion
            if (!isGather)
            {
                LostPacketCount++;
            }
        }