From f941df0af2674f4c9be72487a4651fd4f99e198a Mon Sep 17 00:00:00 2001 From: Amazing_DM Date: Wed, 8 Apr 2020 10:07:25 +0800 Subject: [PATCH] =?UTF-8?q?:art:=E4=BC=98=E5=8C=96=E9=A9=B1=E5=8A=A8?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=20:art:Redirector=E6=AD=A3=E5=B8=B8=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=E5=A4=B1=E8=B4=A5=E5=90=8E=E5=B0=86=E5=B0=9D=E8=AF=95?= =?UTF-8?q?=E5=81=9C=E6=AD=A2=E9=A9=B1=E5=8A=A8=E6=9C=8D=E5=8A=A1=E5=86=8D?= =?UTF-8?q?=E9=87=8D=E6=96=B0=E5=90=AF=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Netch/Controllers/MainController.cs | 12 +++++-- Netch/Controllers/NFController.cs | 51 ++++++++++++++++++++++++----- Netch/Controllers/UpdateChecker.cs | 1 - Netch/Resources/zh-CN | 1 + 4 files changed, 53 insertions(+), 12 deletions(-) diff --git a/Netch/Controllers/MainController.cs b/Netch/Controllers/MainController.cs index e1b32c77..42617054 100644 --- a/Netch/Controllers/MainController.cs +++ b/Netch/Controllers/MainController.cs @@ -1,4 +1,5 @@ -using System; +using Netch.Forms; +using System; using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; @@ -123,7 +124,14 @@ namespace Netch.Controllers pNTTController = new NTTController(); } // 进程代理模式,启动 NF 控制器 - result = pNFController.Start(server, mode); + result = pNFController.Start(server, mode, false); + if (!result) + { + MainForm.Instance.StatusText($"{Utils.i18N.Translate("Status")}{Utils.i18N.Translate(": ")}{Utils.i18N.Translate("ReStarting Redirector")}"); + Utils.Logging.Info("正常启动失败后尝试停止驱动服务再重新启动"); + //正常启动失败后尝试停止驱动服务再重新启动 + result = pNFController.Start(server, mode, true); + } if (result) Task.Run(() => { diff --git a/Netch/Controllers/NFController.cs b/Netch/Controllers/NFController.cs index 6e2c8298..d0805a7a 100644 --- a/Netch/Controllers/NFController.cs +++ b/Netch/Controllers/NFController.cs @@ -40,18 +40,43 @@ namespace Netch.Controllers /// /// 服务器 /// 模式 + /// 先停止驱动服务再重新启动 /// 是否成功 - public bool Start(Models.Server server, Models.Mode mode) + public bool Start(Models.Server server, Models.Mode mode, bool StopServiceAndRestart) { - MainForm.Instance.StatusText($"{Utils.i18N.Translate("Status")}{Utils.i18N.Translate(": ")}{Utils.i18N.Translate("Starting Redirector")}"); + if (!StopServiceAndRestart) + MainForm.Instance.StatusText($"{Utils.i18N.Translate("Status")}{Utils.i18N.Translate(": ")}{Utils.i18N.Translate("Starting Redirector")}"); // 检查驱动是否存在 if (File.Exists(driverPath)) { - //检查驱动版本号 - FileVersionInfo fileVerInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(driverPath); - if (new Version(fileVerInfo.FileVersion) < new Version(UpdateChecker.NFDriverVersion)) + // 生成系统版本 + var version = $"{Environment.OSVersion.Version.Major.ToString()}.{Environment.OSVersion.Version.Minor.ToString()}"; + var driverName = ""; + + switch (version) { + case "10.0": + driverName = "Win-10.sys"; + break; + case "6.2": + driverName = "Win-8.sys"; + break; + case "6.0": + driverName = "Win-7.sys"; + break; + default: + Utils.Logging.Info($"不支持的系统版本:{version}"); + return false; + } + + //检查驱动版本号 + FileVersionInfo SystemfileVerInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(driverPath); + FileVersionInfo BinFileVerInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(string.Format("bin\\{0}", driverName)); + + if (!SystemfileVerInfo.FileVersion.Equals(BinFileVerInfo.FileVersion)) + { + Utils.Logging.Info("开始更新驱动"); //需要更新驱动 try { @@ -67,6 +92,8 @@ namespace Netch.Controllers File.Delete(driverPath); if (!InstallDriver()) return false; + + Utils.Logging.Info($"驱动更新完毕,当前驱动版本:{BinFileVerInfo.FileVersion}"); } catch (Exception) { @@ -86,9 +113,9 @@ namespace Netch.Controllers { //启动驱动服务 var service = new ServiceController("netfilter2"); - if (service.Status == ServiceControllerStatus.Running) + if (service.Status == ServiceControllerStatus.Running && StopServiceAndRestart) { - //防止其他程序占用 重置NF百万ID限制 待定 + //防止其他程序占用 重置NF百万ID限制 service.Stop(); service.WaitForStatus(ServiceControllerStatus.Stopped); MainForm.Instance.StatusText($"{Utils.i18N.Translate("Status")}{Utils.i18N.Translate(": ")}{Utils.i18N.Translate("Starting netfilter2 Service")}"); @@ -220,14 +247,19 @@ namespace Netch.Controllers Instance.BeginOutputReadLine(); Instance.BeginErrorReadLine(); - for (var i = 0; i < 1000; i++) + for (var i = 0; i < 10; i++) { - Thread.Sleep(10); + Thread.Sleep(1000); if (State == Models.State.Started) { + Utils.Logging.Info($"成功启动Redirector耗时:{i + 1}秒"); return true; } + else + { + Utils.Logging.Info($"Redirector启动中,已耗时:{i + 1}秒"); + } } Utils.Logging.Info("NF 进程启动超时"); @@ -256,6 +288,7 @@ namespace Netch.Controllers public bool InstallDriver() { + Utils.Logging.Info("安装驱动中"); // 生成系统版本 var version = $"{Environment.OSVersion.Version.Major.ToString()}.{Environment.OSVersion.Version.Minor.ToString()}"; diff --git a/Netch/Controllers/UpdateChecker.cs b/Netch/Controllers/UpdateChecker.cs index d40c9b4d..3096a0ec 100644 --- a/Netch/Controllers/UpdateChecker.cs +++ b/Netch/Controllers/UpdateChecker.cs @@ -27,7 +27,6 @@ namespace Netch.Controllers public const string Name = @"Netch"; public const string Copyright = @"Copyright © 2019 - 2020"; public const string Version = @"1.4.1"; - public const string NFDriverVersion = @"1.5.9.0"; public async void Check(bool notifyNoFound, bool isPreRelease) { diff --git a/Netch/Resources/zh-CN b/Netch/Resources/zh-CN index f19d7bf2..9d094e0f 100644 --- a/Netch/Resources/zh-CN +++ b/Netch/Resources/zh-CN @@ -23,6 +23,7 @@ "Starting NatTester": "正在启动Nat测试", "Starting LocalDns service": "正在启动本地DNS服务", "Starting Redirector": "正在启动Redirector", + "ReStarting Redirector": "正常启动失败,正在尝试重新启动Redirector", "Starting netfilter2 Service": "正在启动netfilter2服务", "Starting dns2tcp Service": "正在启动dns2tcp服务", "SetupBypass": "设置绕行规则",