Browse Source

修改下载工具类配置

yuzhengyang 8 years ago
parent
commit
c203b557da

+ 2 - 2
Fork.Net/Oreo.CleverDog/Commons/P.cs

@@ -12,11 +12,11 @@ namespace Oreo.CleverDog.Commons
         {
             InitLog();
         }
-        
+
         static void InitLog()
         {
             R.Log = new Log();
-            R.Log.IsWriteFile = true;
+            R.Log.SetWriteFile(true, "Oreo.CleverDog.Log");
             R.Log.LogLevel = LogLevel.All;
         }
     }

+ 1 - 0
Fork.Net/Oreo.CleverDog/Commons/R.cs

@@ -20,6 +20,7 @@ namespace Oreo.CleverDog.Commons
         public static class Paths
         {
             public static string App = AppDomain.CurrentDomain.BaseDirectory;
+            public static string Frisbee = App + "Frisbee\\";
             //public static string Root = App + "\\" + AppName;
             //public static string Data = Root + "\\Data";//应用根目录
         }

+ 43 - 23
Fork.Net/Oreo.CleverDog/Commons/Settings.cs

@@ -1,8 +1,11 @@
-using System;
+using Oreo.CleverDog.Models;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
+using Y.Utils.DataUtils.JsonUtils;
 using Y.Utils.IOUtils.TxtUtils;
+using Y.Utils.NetUtils.HttpUtils;
 
 namespace Oreo.CleverDog.Commons
 {
@@ -11,32 +14,49 @@ namespace Oreo.CleverDog.Commons
     /// </summary>
     public static class Settings
     {
+        static string ServerUrl = "http://10.49.129.127:8001/noah/webservice/getDownloadSoftwareInfo";
         public static void Init()
         {
-            Settings.Frisbee.Read();
+            ReadFromWeb();
+            //ReadFromDemo();
         }
-        public static class Frisbee
+        public static Frisbee[] Frisbee { get; set; }
+        public static void ReadFromDemo()
         {
-            // 用来判定程序是否存在
-            public static string[] ExistFile { get; set; }
-            public static string[] ExistProcess { get; set; }
-            public static string[] ExistSetup { get; set; }
-
-            // 下载程序并执行
-            public static string Url { get; set; }
-            public static string UrlFileName { get; set; }
-            public static string[] Run { get; set; }
-            public static string SuccGetUrl { get; set; }
-            public static void Read()
-            {
-                ExistFile = IniTool.GetStringValue(R.Files.Frisbee, "Frisbee", "ExistFile", "").Split(',');
-                ExistProcess = IniTool.GetStringValue(R.Files.Frisbee, "Frisbee", "ExistProcess", "").Split(',');
-                ExistSetup = IniTool.GetStringValue(R.Files.Frisbee, "Frisbee", "ExistSetup", "").Split(',');
-                Url = IniTool.GetStringValue(R.Files.Frisbee, "Frisbee", "Url", "");
-                UrlFileName = IniTool.GetStringValue(R.Files.Frisbee, "Frisbee", "UrlFileName", "");
-                Run = IniTool.GetStringValue(R.Files.Frisbee, "Frisbee", "Run", "").Split(',');
-                SuccGetUrl = IniTool.GetStringValue(R.Files.Frisbee, "Frisbee", "SuccGetUrl", "");
-            }
+            //string fb = JsonTool.ToStr(new Frisbee[]{
+            //        new Frisbee() {
+            //            Any3264 ="32",
+            //            Term = DateTime.Parse("2017-7-1"),
+            //            ExistControl=new string[] {"趋势科技防毒墙网络版客户端"},
+            //            ExistFile=new string[] {},
+            //            ExistProcess =new string[] { "PccNTMon"},
+            //            Url = "http://10.49.129.127/file/soft/trend32.exe",
+            //            FileName="trend32.exe",
+            //            SuccUrl="",
+            //            AutoRun=true,
+            //            KillProcess=new string[] { },
+            //            RunProcess=new string[] { }
+            //        },
+            //        new Frisbee() {
+            //            Any3264 ="64",
+            //            Term = DateTime.Parse("2017-7-1"),
+            //            ExistControl=new string[] {"趋势科技防毒墙网络版客户端"},
+            //            ExistFile=new string[] {},
+            //            ExistProcess =new string[] { "PccNTMon"},
+            //            Url = "http://10.49.129.127/file/soft/trend64.exe",
+            //            FileName="trend64.exe",
+            //            SuccUrl="",
+            //            AutoRun=true,
+            //            KillProcess=new string[] { },
+            //            RunProcess=new string[] { }
+            //        },
+            //    });
+            //TxtTool.Create(@"D:\Temp\Frisbee Data.txt", fb);
+            Frisbee = JsonTool.ToObjFromFile<Frisbee[]>(@"D:\Temp\Frisbee Data.txt");
+        }
+        public static void ReadFromWeb()
+        {
+            Frisbee = HttpTool.Get<Frisbee[]>(ServerUrl);
         }
     }
 }

+ 86 - 18
Fork.Net/Oreo.CleverDog/Helpers/FrisbeeHelper.cs

@@ -1,4 +1,5 @@
 using Oreo.CleverDog.Commons;
+using Oreo.CleverDog.Models;
 using System;
 using System.Collections.Generic;
 using System.IO;
@@ -15,32 +16,99 @@ namespace Oreo.CleverDog.Helpers
 {
     public class FrisbeeHelper
     {
-        public static bool IsSoftExist()
+        public static void Fire()
         {
-            if (SoftwareTool.ExistFile(Settings.Frisbee.ExistFile) ||
-                SoftwareTool.ExistProcess(Settings.Frisbee.ExistProcess) ||
-                SoftwareTool.ExistControl(Settings.Frisbee.ExistSetup))
+            // 加载配置信息
+            if (ListTool.HasElements(Settings.Frisbee))
             {
-                return true;
+                R.Log.i("任务配置加载成功 共" + Settings.Frisbee.Count() + " 条");
+                // 循环所有任务
+                foreach (var f in Settings.Frisbee)
+                {
+                    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("发送运行信息至服务器");
+                        }
+                        else
+                        {
+                            R.Log.e("文件下载失败 任务被迫中止");
+                        }
+                    }
+                    else
+                    {
+                        R.Log.e(f.FileName + " 任务不适应此计算机");
+                    }
+                }
+            }
+            else
+            {
+                R.Log.e("配置加载失败 任务失败");
             }
-            return false;
         }
-        public static bool DownFileAndRun()
+        public static bool CanFire(Frisbee f)
         {
-            string downfile = R.Paths.App + Settings.Frisbee.UrlFileName;
-            if (HttpTool.Download(Settings.Frisbee.Url, downfile))
+            // 验证有效时间
+            if (DateTime.Now > f.Term)
+                return false;
+
+            // 验证系统
+            if (f.Any3264 == "32" && Environment.Is64BitOperatingSystem)
+                return false;
+            if (f.Any3264 == "64" && !Environment.Is64BitOperatingSystem)
+                return false;
+
+            // 验证安装
+            if (SoftwareTool.ExistFile(f.ExistFile) ||
+                SoftwareTool.ExistProcess(f.ExistProcess) ||
+                SoftwareTool.ExistControl(f.ExistControl))
             {
-                if (File.Exists(downfile))
-                    ProcessTool.StartProcess(downfile);
-                return true;
+                return false;
+            }
+            return true;
+        }
+        public static bool DownFileAndRun(Frisbee f)
+        {
+            if (!string.IsNullOrWhiteSpace(f.Url) && !string.IsNullOrWhiteSpace(f.FileName))
+            {
+                string downfile = R.Paths.Frisbee + f.FileName;
+                if (HttpTool.Download(f.Url, downfile))
+                {
+                    if (f.AutoRun && File.Exists(downfile))
+                        ProcessTool.StartProcess(downfile);
+                    return true;
+                }
             }
             return false;
         }
-        public static void RunOtherApp()
+        public static void KillProcess(Frisbee f)
+        {
+            if (!ListTool.IsNullOrEmpty(f.KillProcess))
+            {
+                foreach (var r in f.KillProcess)
+                {
+                    if (!string.IsNullOrWhiteSpace(r))
+                    {
+                        ProcessTool.KillProcess(r);
+                    }
+                }
+            }
+        }
+        public static void RunProcess(Frisbee f)
         {
-            if (!ListTool.IsNullOrEmpty(Settings.Frisbee.Run))
+            if (!ListTool.IsNullOrEmpty(f.RunProcess))
             {
-                foreach (var r in Settings.Frisbee.Run)
+                foreach (var r in f.RunProcess)
                 {
                     if (!string.IsNullOrWhiteSpace(r))
                     {
@@ -49,10 +117,10 @@ namespace Oreo.CleverDog.Helpers
                 }
             }
         }
-        public static void SuccGetUrl()
+        public static void SuccUrl(Frisbee f)
         {
-            HttpTool.Get(Settings.Frisbee.SuccGetUrl);
+            if (string.IsNullOrWhiteSpace(f.SuccUrl))
+                HttpTool.Get(f.SuccUrl);
         }
-
     }
 }

+ 33 - 0
Fork.Net/Oreo.CleverDog/Models/Frisbee.cs

@@ -0,0 +1,33 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Oreo.CleverDog.Models
+{
+    public class Frisbee
+    {
+        #region 判断执行的依据
+        public string[] ExistFile { get; set; }
+        public string[] ExistProcess { get; set; }
+        public string[] ExistControl { get; set; }
+        public DateTime Term { get; set; }
+        public string Any3264 { get; set; }
+        #endregion
+
+        #region 下载并执行
+        public string Url { get; set; }
+        public string FileName { get; set; }
+        public string SuccUrl { get; set; }
+        public bool AutoRun { get; set; }
+        #endregion
+
+        #region 任务前关闭程序
+        public string[] KillProcess { get; set; }
+        #endregion
+
+        #region 任务后运行程序
+        public string[] RunProcess { get; set; }
+        #endregion
+    }
+}

+ 1 - 1
Fork.Net/Oreo.CleverDog/Oreo.CleverDog.csproj

@@ -55,6 +55,7 @@
     <Compile Include="Commons\R.cs" />
     <Compile Include="Commons\Settings.cs" />
     <Compile Include="Helpers\FrisbeeHelper.cs" />
+    <Compile Include="Models\Frisbee.cs" />
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Views\MainForm.cs">
@@ -91,7 +92,6 @@
     <None Include="App.config" />
   </ItemGroup>
   <ItemGroup>
-    <Folder Include="Models\" />
     <Folder Include="Services\" />
   </ItemGroup>
   <ItemGroup>

+ 11 - 8
Fork.Net/Oreo.CleverDog/Program.cs

@@ -1,5 +1,6 @@
 using Oreo.CleverDog.Commons;
 using Oreo.CleverDog.Helpers;
+using Oreo.CleverDog.Models;
 using Oreo.CleverDog.Views;
 using System;
 using System.Collections.Generic;
@@ -9,6 +10,10 @@ using System.Threading.Tasks;
 using System.Windows.Forms;
 using Y.Utils.AppUtils;
 using Y.Utils.DataUtils.EncryptUtils;
+using Y.Utils.DataUtils.JsonUtils;
+using Y.Utils.IOUtils.TxtUtils;
+using Y.Utils.NetUtils.HttpUtils;
+using Y.Utils.WindowsUtils.ProcessUtils;
 
 namespace Oreo.CleverDog
 {
@@ -22,14 +27,12 @@ namespace Oreo.CleverDog
         {
             if (AppUnique.IsUnique("Oreo.NetMonitor"))
             {
-                if (File.Exists(R.Files.Frisbee))
-                {
-                    Settings.Init();
-                    P.Init();
-                    Application.EnableVisualStyles();
-                    Application.SetCompatibleTextRenderingDefault(false);
-                    Application.Run(new MainForm());
-                }
+                Settings.Init();
+                P.Init();
+                Application.EnableVisualStyles();
+                Application.SetCompatibleTextRenderingDefault(false);
+                //Application.Run(new MainForm());
+                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("1.0.0.0")]
+[assembly: AssemblyFileVersion("2.0.0.0")]

+ 1 - 21
Fork.Net/Oreo.CleverDog/Views/MainForm.cs

@@ -22,26 +22,6 @@ namespace Oreo.CleverDog.Views
         }
 
         private void MainForm_Load(object sender, EventArgs e)
-        {
-            Task.Factory.StartNew(() =>
-            {
-                // 第一步 判断本机是否存在目标程序
-                if (!FrisbeeHelper.IsSoftExist())
-                {
-                    // 第二步 下载程序
-                    if (FrisbeeHelper.DownFileAndRun())
-                    {
-                        // 第三部 运行程序
-                        FrisbeeHelper.RunOtherApp();
-                        FrisbeeHelper.SuccGetUrl();
-                    }
-                }
-
-                Invoke(new Action(() =>
-                {
-                    Close();
-                }));
-            });
-        }
+        { }
     }
 }

+ 1 - 1
Fork.Net/Oreo.NetMonitor/Commons/P.cs

@@ -17,7 +17,7 @@ namespace Oreo.NetMonitor.Commons
         static void InitLog()
         {
             R.Log = new Log();
-            R.Log.IsWriteFile = true;
+            R.Log.SetWriteFile(true, "Oreo.NetMonitor.Log");
             R.Log.LogLevel = LogLevel.All;
             Log.AllocConsole();
         }

+ 1 - 0
Fork.Net/Oreo.NetMonitor/Oreo.NetMonitor.csproj

@@ -61,6 +61,7 @@
     <Compile Include="Models\ProcessPort.cs" />
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="Services\NetflowService.cs" />
     <Compile Include="Services\NetWorkService.cs" />
     <Compile Include="Views\FloatForm.cs">
       <SubType>Form</SubType>

+ 12 - 0
Fork.Net/Oreo.NetMonitor/Services/NetflowService.cs

@@ -0,0 +1,12 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Oreo.NetMonitor.Services
+{
+    public class NetflowService
+    {
+
+    }
+}

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

@@ -1,6 +1,6 @@
 using System;
 using System.Windows.Forms;
-using Y.Utils.WindowsUtils.InfoUtils;
+using Y.Utils.WindowsUtils.ProcessUtils;
 
 namespace Oreo.NetMonitor.Views
 {

+ 16 - 3
Fork.Net/Y.Utils/IOUtils/LogUtils/Log.cs

@@ -35,9 +35,22 @@ namespace Y.Utils.IOUtils.LogUtils
 
         private object LogFileLock = new object();//写日志文件锁
 
-        public bool IsWriteFile = true;//是否写日志文件
+        private bool IsWriteFile = false;//是否写日志文件
+        public string LogPath = "Log";
         public LogLevel LogLevel = LogLevel.All;//日志输出等级
 
+        public bool SetWriteFile(bool isWrite, string logPath)
+        {
+            if (isWrite && !string.IsNullOrWhiteSpace(logPath))
+            {
+                LogPath = logPath.Trim();
+                IsWriteFile = true;
+                return true;
+            }
+            IsWriteFile = false;
+            return false;
+        }
+
         #region Console 开启/关闭 API
         [DllImport("kernel32.dll")]
         public static extern Boolean AllocConsole();
@@ -50,7 +63,7 @@ namespace Y.Utils.IOUtils.LogUtils
         /// </summary>
         /// <param name="type">输出类型</param>
         /// <returns></returns>
-        private static ConsoleColor GetColor(LogType type)
+        private ConsoleColor GetColor(LogType type)
         {
             switch (type)
             {
@@ -83,7 +96,7 @@ namespace Y.Utils.IOUtils.LogUtils
                 lock (LogFileLock)
                 {
                     //设置日志目录
-                    string logPath = AppDomain.CurrentDomain.BaseDirectory + "Log";
+                    string logPath = AppDomain.CurrentDomain.BaseDirectory + LogPath;
                     string file = string.Format(@"{0}\{1}.txt", logPath, DateTime.Now.ToString("yyyy-MM-dd"));
                     //创建日志目录
                     DirTool.Create(logPath);

+ 14 - 0
Fork.Net/Y.Utils/NetUtils/NetDiagnoseUtils/NetflowTool.cs

@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Linq;
+using System.Text;
+
+namespace Y.Utils.NetUtils.NetDiagnoseUtils
+{
+    public class NetflowTool
+    {
+        public static PerformanceCounterCategory performanceCounterCategory = new PerformanceCounterCategory("Network Interface");
+        public static string[] instances = performanceCounterCategory.GetInstanceNames();
+    }
+}

+ 1 - 0
Fork.Net/Y.Utils/Y.Utils.csproj

@@ -56,6 +56,7 @@
     <Compile Include="AppUtils\AppUnique.cs" />
     <Compile Include="DataUtils\UnitConvertUtils\ByteConvertUtils.cs" />
     <Compile Include="IOUtils\ImageUtils\IconTool.cs" />
+    <Compile Include="NetUtils\NetDiagnoseUtils\NetflowTool.cs" />
     <Compile Include="ReflectionUtils\AttributeUtils\AttributeTool.cs" />
     <Compile Include="ReflectionUtils\AttributeUtils\CustomAttributeHelper.cs" />
     <Compile Include="ReflectionUtils\AttributeUtils\ControlAttribute.cs" />