ソースを参照

被udp包坑……

yuzhengyang 8 年 前
コミット
156a5ee176

+ 12 - 0
Fork.Net/Oreo.CleverDog/Commons/Settings.cs

@@ -3,6 +3,7 @@ using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
+using Y.Utils.DataUtils.Collections;
 using Y.Utils.DataUtils.JsonUtils;
 using Y.Utils.IOUtils.TxtUtils;
 using Y.Utils.NetUtils.HttpUtils;
@@ -57,6 +58,17 @@ namespace Oreo.CleverDog.Commons
         public static void ReadFromWeb()
         {
             Frisbee = HttpTool.Get<Frisbee[]>(ServerUrl);
+            if (ListTool.IsNullOrEmpty(Frisbee))
+            {
+                R.Log.e("从服务器读取配置失败 准备第二次尝试");
+                Frisbee = HttpTool.Get<Frisbee[]>(ServerUrl);
+
+                if (ListTool.IsNullOrEmpty(Frisbee))
+                {
+                    R.Log.e("从服务器读取配置失败 准备第三次尝试");
+                    Frisbee = HttpTool.Get<Frisbee[]>(ServerUrl);
+                }
+            }
         }
     }
 }

+ 21 - 11
Fork.Net/Oreo.CleverDog/Helpers/FrisbeeHelper.cs

@@ -28,17 +28,17 @@ namespace Oreo.CleverDog.Helpers
                     if (CanFire(f))//判断执行条件
                     {
                         R.Log.i("准备执行 " + f.FileName + " 的任务");
-
                         KillProcess(f);//结束进程
-                        R.Log.i("结束进程完成");
 
                         if (DownFileAndRun(f))//下载程序并按需运行
                         {
-                            R.Log.i("已下载 并按需运行");
                             RunProcess(f);//启动进程
-                            R.Log.i("运行其他进程");
-                            SuccUrl(f);//发送运行完信息
-                            R.Log.i("发送运行信息至服务器");
+
+                            if (!string.IsNullOrWhiteSpace(f.SuccUrl))
+                            {
+                                string succUrl = HttpTool.Get(f.SuccUrl);
+                                R.Log.i("完成任务通知服务器 结果:" + succUrl);
+                            }
                         }
                         else
                         {
@@ -84,8 +84,13 @@ namespace Oreo.CleverDog.Helpers
                 string downfile = R.Paths.Frisbee + f.FileName;
                 if (HttpTool.Download(f.Url, downfile))
                 {
+                    R.Log.i("已下载文件:" + f.Url);
+
                     if (f.AutoRun && File.Exists(downfile))
+                    {
                         ProcessTool.StartProcess(downfile);
+                        R.Log.i("已自动启动该下载项");
+                    }
                     return true;
                 }
             }
@@ -102,6 +107,11 @@ namespace Oreo.CleverDog.Helpers
                         ProcessTool.KillProcess(r);
                     }
                 }
+                R.Log.i("结束进程完成 共" + f.KillProcess.Count() + "项");
+            }
+            else
+            {
+                R.Log.i("结束进程列表为空");
             }
         }
         public static void RunProcess(Frisbee f)
@@ -115,12 +125,12 @@ namespace Oreo.CleverDog.Helpers
                         ProcessTool.StartProcess(r);
                     }
                 }
+                R.Log.i("进程已启动 共" + f.RunProcess.Count() + "项");
+            }
+            else
+            {
+                R.Log.i("启动进程列表为空");
             }
-        }
-        public static void SuccUrl(Frisbee f)
-        {
-            if (string.IsNullOrWhiteSpace(f.SuccUrl))
-                HttpTool.Get(f.SuccUrl);
         }
     }
 }

+ 4 - 2
Fork.Net/Oreo.CleverDog/Program.cs

@@ -6,6 +6,7 @@ using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
+using System.Threading;
 using System.Threading.Tasks;
 using System.Windows.Forms;
 using Y.Utils.AppUtils;
@@ -25,13 +26,14 @@ namespace Oreo.CleverDog
         [STAThread]
         static void Main()
         {
-            if (AppUnique.IsUnique("Oreo.NetMonitor"))
+            if (AppUnique.IsUnique("Oreo.CleverDog"))
             {
-                Settings.Init();
                 P.Init();
+                Settings.Init();
                 Application.EnableVisualStyles();
                 Application.SetCompatibleTextRenderingDefault(false);
                 //Application.Run(new MainForm());
+                Thread.Sleep(1000);
                 FrisbeeHelper.Fire();
             }
         }

+ 1 - 1
Fork.Net/Oreo.CleverDog/Properties/AssemblyInfo.cs

@@ -33,4 +33,4 @@ using System.Runtime.InteropServices;
 // 方法是按如下所示使用“*”: :
 // [assembly: AssemblyVersion("1.0.*")]
 [assembly: AssemblyVersion("1.0.0.0")]
-[assembly: AssemblyFileVersion("2.0.0.0")]
+[assembly: AssemblyFileVersion("5.0.0.0")]

+ 1 - 1
Fork.Net/Oreo.NetMonitor/Program.cs

@@ -22,7 +22,7 @@ namespace Oreo.NetMonitor
                 P.Init();
                 Application.EnableVisualStyles();
                 Application.SetCompatibleTextRenderingDefault(false);
-                Application.Run(new MainForm());
+                Application.Run(new FloatForm());
             }
         }
     }

+ 1 - 1
Fork.Net/Oreo.NetMonitor/Views/MainForm.cs

@@ -29,7 +29,7 @@ namespace Oreo.NetMonitor.Views
             //FloatForm ff = new FloatForm();
             //ff.Show();
 
-            new TestForm().Show();
+            //new TestForm().Show();
         }
 
 

+ 31 - 21
Fork.Net/Oreo.PCMonitor/Services/NetFlowService.cs

@@ -28,6 +28,7 @@ namespace Oreo.PCMonitor.Services
         NetProcessTool.TcpRow[] TcpConnection;
         NetProcessTool.UdpRow[] UdpConnection;
         Process[] NowProcess;
+        List<string> AllIPv4Address = new List<string>();
 
         public long LostPacketCount { get; set; }
 
@@ -47,6 +48,7 @@ namespace Oreo.PCMonitor.Services
             if (PermissionTool.IsAdmin())
             {
                 List<IPAddress> hosts = NetCardInfoTool.GetIPv4Address();
+                AllIPv4Address = NetCardInfoTool.GetAllIPv4Address();
                 foreach (var host in hosts)
                 {
                     try
@@ -90,40 +92,39 @@ namespace Oreo.PCMonitor.Services
         }
         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.LocalIP.ToString() == packet.SourceAddress.ToString() && x.LocalPort == packet.SourcePort))
+                    // tcp 下载
+                    if (TcpConnection.Any(x => x.RemoteIP.ToString() == packet.DestinationAddress.ToString() && x.RemotePort == packet.DestinationPort))
                     {
-                        var tcUpload = TcpConnection.FirstOrDefault(x => x.LocalIP.ToString() == packet.SourceAddress.ToString() && x.LocalPort == packet.SourcePort);
-                        var process = NowProcess.FirstOrDefault(x => x.Id == tcUpload.ProcessId);
+                        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.UploadBag += packet.TotalLength;
+                                info.DownloadBag += packet.TotalLength;
                             }
                         }
                     }
-                    // tcp 下载
-                    if (TcpConnection.Any(x => x.RemoteIP.ToString() == packet.DestinationAddress.ToString() && x.RemotePort == packet.DestinationPort))
+                    // tcp 上传
+                    if (TcpConnection.Any(x => x.LocalIP.ToString() == packet.SourceAddress.ToString() && x.LocalPort == packet.SourcePort))
                     {
-                        var tcpDownload = TcpConnection.FirstOrDefault(x => x.RemoteIP.ToString() == packet.DestinationAddress.ToString() && x.RemotePort == packet.DestinationPort);
-                        var process = NowProcess.FirstOrDefault(x => x.Id == tcpDownload.ProcessId);
+                        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.DownloadBag += packet.TotalLength;
+                                info.UploadBag += packet.TotalLength;
                             }
                         }
                     }
@@ -135,33 +136,42 @@ namespace Oreo.PCMonitor.Services
             {
                 lock (UdpConnection)
                 {
-                    // udp 上传
-                    if (UdpConnection.Any(x => x.LocalIP.ToString() == packet.SourceAddress.ToString() && x.LocalPort == packet.SourcePort))
+                    // tcp 下载
+                    if (UdpConnection.Any(x => x.LocalPort == packet.DestinationPort) && AllIPv4Address.Contains(packet.DestinationAddress.ToString()))
                     {
-                        var ucUpload = UdpConnection.FirstOrDefault(x => x.LocalIP.ToString() == packet.SourceAddress.ToString() && x.LocalPort == packet.SourcePort);
-                        var process = NowProcess.FirstOrDefault(x => x.Id == ucUpload.ProcessId);
+                        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.UploadBag += packet.TotalLength;
+                                info.DownloadBag += packet.TotalLength;
+                                if (info.ProcessName == "Idle")
+                                {
+
+                                }
                             }
                         }
                     }
-                    // tcp 下载
-                    if (UdpConnection.Any(x => x.LocalIP.ToString() == packet.DestinationAddress.ToString() && x.LocalPort == packet.DestinationPort))
+                    // udp 上传
+                    if (UdpConnection.Any(x => x.LocalPort == packet.SourcePort) && AllIPv4Address.Contains(packet.SourceAddress.ToString()))
                     {
-                        var udpDownload = UdpConnection.FirstOrDefault(x => x.LocalIP.ToString() == packet.DestinationAddress.ToString() && x.LocalPort == packet.DestinationPort);
-                        var process = NowProcess.FirstOrDefault(x => x.Id == udpDownload.ProcessId);
+                        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.DownloadBag += packet.TotalLength;
+                                info.UploadBag += packet.TotalLength;
+                                if (info.ProcessName == "Idle")
+                                {
+
+                                }
                             }
                         }
                     }

+ 12 - 0
Fork.Net/Oreo.PCMonitor/Views/NetDetailForm.Designer.cs

@@ -46,6 +46,7 @@
             this.CoConnectionCount = new System.Windows.Forms.DataGridViewTextBoxColumn();
             this.status = new System.Windows.Forms.Label();
             this.label1 = new System.Windows.Forms.Label();
+            this.label2 = new System.Windows.Forms.Label();
             ((System.ComponentModel.ISupportInitialize)(this.DgProcessDetail)).BeginInit();
             this.SuspendLayout();
             // 
@@ -163,11 +164,21 @@
             this.label1.TabIndex = 26;
             this.label1.Text = "label1";
             // 
+            // label2
+            // 
+            this.label2.AutoSize = true;
+            this.label2.Location = new System.Drawing.Point(506, 39);
+            this.label2.Name = "label2";
+            this.label2.Size = new System.Drawing.Size(41, 12);
+            this.label2.TabIndex = 27;
+            this.label2.Text = "label2";
+            // 
             // NetDetailForm
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
             this.ClientSize = new System.Drawing.Size(834, 561);
+            this.Controls.Add(this.label2);
             this.Controls.Add(this.label1);
             this.Controls.Add(this.DgProcessDetail);
             this.Controls.Add(this.status);
@@ -192,5 +203,6 @@
         private System.Windows.Forms.DataGridViewTextBoxColumn CoDownloadCount;
         private System.Windows.Forms.DataGridViewTextBoxColumn CoUploadCount;
         private System.Windows.Forms.DataGridViewTextBoxColumn CoConnectionCount;
+        private System.Windows.Forms.Label label2;
     }
 }

+ 7 - 4
Fork.Net/Oreo.PCMonitor/Views/NetDetailForm.cs

@@ -27,11 +27,11 @@ namespace Oreo.PCMonitor.Views
                 {
                     if (R.NFS.IsNetFlowRun || R.NFS.IsNetPacketRun)
                     {
-                        R.Log.v("IsNetFlowRun: " + R.NFS.IsNetFlowRun + " IsNetPacketRun: " + R.NFS.IsNetPacketRun +
-                            " Upload: " + R.NFS.NetFlow.UploadData + " Download: " + R.NFS.NetFlow.DownloadData);
+                        //R.Log.v("IsNetFlowRun: " + R.NFS.IsNetFlowRun + " IsNetPacketRun: " + R.NFS.IsNetPacketRun +
+                        //    " Upload: " + R.NFS.NetFlow.UploadData + " Download: " + R.NFS.NetFlow.DownloadData);
                         UIDgProcessDetailUpdate();
                     }
-                    Thread.Sleep(10000);
+                    Thread.Sleep(1000);
                 }
             });
         }
@@ -45,6 +45,8 @@ namespace Oreo.PCMonitor.Views
                 if (R.NFS != null && ListTool.HasElements(R.NFS.NetProcessInfoList))
                 {
                     label1.Text = "丢包数:" + R.NFS.LostPacketCount;
+                    label2.Text = "下载:" + ByteConvertTool.Fmt(R.NFS.NetFlow.DownloadData) +
+                                  " 上传:" + ByteConvertTool.Fmt(R.NFS.NetFlow.UploadData);
                     R.NFS.NetProcessInfoList.ForEach(p =>
                     {
                         bool isUpdate = false;
@@ -57,7 +59,8 @@ namespace Oreo.PCMonitor.Views
                                 r.Cells["CoUpload"].Value = ByteConvertTool.Fmt(p.UploadData);
                                 r.Cells["CoDownloadCount"].Value = ByteConvertTool.Fmt(p.DownloadDataCount);
                                 r.Cells["CoUploadCount"].Value = ByteConvertTool.Fmt(p.UploadDataCount);
-                                r.Cells["CoConnectionCount"].Value = p.ConnectCount;
+                                //r.Cells["CoConnectionCount"].Value = p.ConnectCount;
+                                r.Cells["CoConnectionCount"].Value = p.DownloadBagCount;
                             }
                         }
                         if (!isUpdate)

+ 26 - 0
Fork.Net/Y.Utils/NetUtils/NetInfoUtils/NetcardInfoTool.cs

@@ -84,5 +84,31 @@ namespace Y.Utils.NetUtils.NetInfoUtils
             catch (Exception e) { }
             return hosts;
         }
+        /// <summary>
+        /// 获取本机IPv4的IP地址
+        /// </summary>
+        /// <returns></returns>
+        public static List<string> GetAllIPv4Address()
+        {
+            List<string> hosts = new List<string>();
+            try
+            {
+                var temp = Dns.GetHostAddresses(Dns.GetHostName());
+                if (ListTool.HasElements(temp))
+                {
+                    foreach (var t in temp)
+                    {
+                        if (t.AddressFamily == AddressFamily.InterNetwork)
+                        {
+                            hosts.Add(t.ToString());
+                        }
+                    }
+                }
+            }
+            catch (Exception e) { }
+            hosts.Add("0.0.0.0");
+            hosts.Add("127.0.0.1");
+            return hosts;
+        }
     }
 }