From e8fbde707baaa290b140a12653cf8a9988508b31 Mon Sep 17 00:00:00 2001 From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com> Date: Mon, 10 Aug 2020 23:34:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DTUN/TAP=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E6=97=B6=E4=BC=9A=E8=A2=AB=E6=97=A0=E5=85=B3=E9=80=82=E9=85=8D?= =?UTF-8?q?=E5=99=A8=E7=9A=84=E5=BC=82=E5=B8=B8=E6=89=80=E4=B8=AD=E6=96=AD?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E5=90=AF=E5=8A=A8=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Netch/Controllers/Interface/Instance.cs | 8 +- Netch/Controllers/Mode/TUNTAPController.cs | 95 ++++++++++++---------- 2 files changed, 54 insertions(+), 49 deletions(-) diff --git a/Netch/Controllers/Interface/Instance.cs b/Netch/Controllers/Interface/Instance.cs index 51800e23..6a226952 100644 --- a/Netch/Controllers/Interface/Instance.cs +++ b/Netch/Controllers/Interface/Instance.cs @@ -92,8 +92,7 @@ namespace Netch.Controllers RedirectStandardOutput = RedirectStd, UseShellExecute = !RedirectStd, WindowStyle = ProcessWindowStyle.Hidden - }, - EnableRaisingEvents = true + } }; } @@ -111,11 +110,12 @@ namespace Netch.Controllers { // 初始化程序 InitInstance(argument); + Instance.EnableRaisingEvents = true; if (RedirectStd) { // 清理日志 _logPath ??= Path.Combine(Global.NetchDir, $"logging\\{Name}.log"); - if (_logFileStream == null && File.Exists(_logPath)) + if (File.Exists(_logPath)) File.Delete(_logPath); _logFileStream = new FileStream(_logPath, FileMode.Create, FileAccess.Write); @@ -239,7 +239,7 @@ namespace Netch.Controllers } catch (Exception e) { - Logging.Error($"写入 {Name} 日志错误:\n" + e.Message); + Logging.Warning($"写入 {Name} 日志错误:\n" + e.Message); } } } diff --git a/Netch/Controllers/Mode/TUNTAPController.cs b/Netch/Controllers/Mode/TUNTAPController.cs index 2c6faeea..e95327b4 100644 --- a/Netch/Controllers/Mode/TUNTAPController.cs +++ b/Netch/Controllers/Mode/TUNTAPController.cs @@ -285,58 +285,63 @@ namespace Netch.Controllers } } + // 根据 IP Index 寻找 出口适配器 + var errorAdaptersId = new List(); try { - try + var adapter = NetworkInterface.GetAllNetworkInterfaces().First(_ => { - var adapter = NetworkInterface.GetAllNetworkInterfaces().First(_ => _.GetIPProperties().GetIPv4Properties().Index == Global.Adapter.Index); - Global.Adapter.Address = adapter.GetIPProperties().UnicastAddresses.First(ip => ip.Address.AddressFamily == AddressFamily.InterNetwork).Address; - Global.Adapter.Gateway = new IPAddress(pRoute.dwForwardNextHop); - Logging.Info($"出口 IPv4 地址:{Global.Adapter.Address}"); - Logging.Info($"出口 网关 地址:{Global.Adapter.Gateway}"); - Logging.Info($"出口适配器:{adapter.Name} {adapter.Id} {adapter.Description}, index: {Global.Adapter.Index}"); - // Ex NetworkInformationException: 此接口不支持 IPv4 协议。 - - // Ex NetworkInformationException: Windows 系统函数调用失败。 - // Ex System.ArgumentNullException: source 或 predicate 为 null。 - } - catch (Exception e) - { - if (e is InvalidOperationException) - Logging.Error($"找不到网络接口索引为 {Global.Adapter.Index} 的出口适配器"); - throw; - } - - try - { - var adapter = NetworkInterface.GetAllNetworkInterfaces().First(_ => _.Id == Global.TUNTAP.ComponentID); - Global.TUNTAP.Adapter = adapter; - Global.TUNTAP.Index = adapter.GetIPProperties().GetIPv4Properties().Index; - Logging.Info($"TAP 适配器:{adapter.Name} {adapter.Id} {adapter.Description}, index: {Global.TUNTAP.Index}"); - } - catch (Exception e) - { - if (e is InvalidOperationException) - Logging.Error($"找不到标识符为 {Global.TUNTAP.ComponentID} 的 TAP 适配器"); - throw; - } - - return true; + try + { + return _.GetIPProperties().GetIPv4Properties().Index == Global.Adapter.Index; + } + catch (NetworkInformationException) + { + errorAdaptersId.Add(_.Id); + return false; + } + }); + Global.Adapter.Address = adapter.GetIPProperties().UnicastAddresses.First(ip => ip.Address.AddressFamily == AddressFamily.InterNetwork).Address; + Global.Adapter.Gateway = new IPAddress(pRoute.dwForwardNextHop); + Logging.Info($"出口 IPv4 地址:{Global.Adapter.Address}"); + Logging.Info($"出口 网关 地址:{Global.Adapter.Gateway}"); + Logging.Info($"出口适配器:{adapter.Name} {adapter.Id} {adapter.Description}, index: {Global.Adapter.Index}"); } - catch (InvalidOperationException) + catch (Exception e) + { + Logging.Error($"找不到 IP Index 为 {Global.Adapter.Index} 的出口适配器: {e.Message}"); + PrintAdapters(); + return false; + } + + // 根据 ComponentID 寻找 Tap适配器 + try + { + var adapter = NetworkInterface.GetAllNetworkInterfaces().First(_ => _.Id == Global.TUNTAP.ComponentID); + Global.TUNTAP.Adapter = adapter; + Global.TUNTAP.Index = adapter.GetIPProperties().GetIPv4Properties().Index; + Logging.Info($"TAP 适配器:{adapter.Name} {adapter.Id} {adapter.Description}, index: {Global.TUNTAP.Index}"); + } + catch (Exception e) + { + var msg = e switch + { + InvalidOperationException _ => $"找不到标识符为 {Global.TUNTAP.ComponentID} 的 TAP 适配器: {e.Message}", + NetworkInformationException _ => $"获取 Tap 适配器信息错误: {e.Message}", + _ => $"Tap 适配器其他异常: {e}" + }; + Logging.Error(msg); + PrintAdapters(); + return false; + } + + return true; + + void PrintAdapters() { - // 理论上如果得到了网络接口的索引/网络适配器的标识符不会找不到网卡 - // 只是异常处理 Logging.Info("所有适配器:\n" + NetworkInterface.GetAllNetworkInterfaces().Aggregate(string.Empty, (current, adapter) - => current + $"{adapter.Name} {adapter.Id} {adapter.Description}, index: {Global.TUNTAP.Index}{Global.EOF}")); - return false; - } - catch (NetworkInformationException e) - { - if (e.ErrorCode == 10043) - MessageBoxX.Show("适配器未开启IPv4协议", LogLevel.ERROR, owner: Global.MainForm); - return false; + => current + $"{ /*如果加了 ’*‘ 代表遍历中出现异常的适配器 */(errorAdaptersId.Contains(adapter.Id) ? "*" : "")}{adapter.Name} {adapter.Id} {adapter.Description}{Global.EOF}")); } }