From 8c9953a5d4f8801018538f74de3e5d736de7dff8 Mon Sep 17 00:00:00 2001 From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com> Date: Wed, 29 Jul 2020 14:37:29 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=82=E9=85=8D=E5=99=A8=E6=A3=80=E6=9F=A5?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Netch/Controllers/Mode/TUNTAPController.cs | 34 +++++++++++++++++----- Netch/Utils/TUNTAP.cs | 30 ++++++++++++------- 2 files changed, 45 insertions(+), 19 deletions(-) diff --git a/Netch/Controllers/Mode/TUNTAPController.cs b/Netch/Controllers/Mode/TUNTAPController.cs index ba9ccd0c..bd966693 100644 --- a/Netch/Controllers/Mode/TUNTAPController.cs +++ b/Netch/Controllers/Mode/TUNTAPController.cs @@ -281,11 +281,20 @@ namespace Netch.Controllers if (Win32Native.GetBestRoute(BitConverter.ToUInt32(IPAddress.Parse("114.114.114.114").GetAddressBytes(), 0), 0, out var pRoute) == 0) { Global.Adapter.Index = pRoute.dwForwardIfIndex; - adapter = NetworkInterface.GetAllNetworkInterfaces().First(_ => _.GetIPProperties().GetIPv4Properties().Index == Global.Adapter.Index); + Logging.Info($"出口 网关 地址:{Global.Adapter.Gateway}"); + try + { + adapter = NetworkInterface.GetAllNetworkInterfaces().First(_ => _.GetIPProperties().GetIPv4Properties().Index == Global.Adapter.Index); + } + catch (InvalidOperationException) + { + Logging.Error("找不到出口适配器"); + 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}"); } else @@ -298,7 +307,7 @@ namespace Netch.Controllers Global.TUNTAP.ComponentID = TUNTAP.GetComponentID(); if (string.IsNullOrEmpty(Global.TUNTAP.ComponentID)) { - Logging.Error("未找到可用 TAP 适配器"); + Logging.Error("找不到 TAP 适配器"); if (MessageBoxX.Show(i18N.Translate("TUN/TAP driver is not detected. Is it installed now?"), confirm: true) == DialogResult.OK) { Configuration.addtap(); @@ -308,15 +317,24 @@ namespace Netch.Controllers } else { + Logging.Info("拒绝安装 TAP 适配器 "); return false; } } - 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}"); - return true; + try + { + 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}"); + return true; + } + catch (InvalidOperationException) + { + Logging.Error("没有找到 TAP 适配器"); + return false; + } } diff --git a/Netch/Utils/TUNTAP.cs b/Netch/Utils/TUNTAP.cs index d2f5cb73..ca795950 100644 --- a/Netch/Utils/TUNTAP.cs +++ b/Netch/Utils/TUNTAP.cs @@ -1,4 +1,5 @@ -using Microsoft.Win32; +using System; +using Microsoft.Win32; namespace Netch.Utils { @@ -15,21 +16,28 @@ namespace Netch.Utils /// 适配器 ID public static string GetComponentID() { - var adaptersRegistry = Registry.LocalMachine.OpenSubKey(ADAPTER_KEY); - - foreach (var adapterRegistryName in adaptersRegistry.GetSubKeyNames()) + try { - if (adapterRegistryName != "Configuration" && adapterRegistryName != "Properties") - { - var adapterRegistry = adaptersRegistry.OpenSubKey(adapterRegistryName); + var adaptersRegistry = Registry.LocalMachine.OpenSubKey(ADAPTER_KEY); - var adapterComponentId = adapterRegistry.GetValue("ComponentId", "").ToString(); - if (adapterComponentId == TUNTAP_COMPONENT_ID_0901 || adapterComponentId == TUNTAP_COMPONENT_ID_0801) + foreach (var adapterRegistryName in adaptersRegistry.GetSubKeyNames()) + { + if (adapterRegistryName != "Configuration" && adapterRegistryName != "Properties") { - return adapterRegistry.GetValue("NetCfgInstanceId", "").ToString(); + var adapterRegistry = adaptersRegistry.OpenSubKey(adapterRegistryName); + + var adapterComponentId = adapterRegistry.GetValue("ComponentId", "").ToString(); + if (adapterComponentId == TUNTAP_COMPONENT_ID_0901 || adapterComponentId == TUNTAP_COMPONENT_ID_0801) + { + return adapterRegistry.GetValue("NetCfgInstanceId", "").ToString(); + } } } } + catch (Exception e) + { + Logging.Warning(e.ToString()); + } return ""; } @@ -55,4 +63,4 @@ namespace Netch.Utils return false; } } -} +} \ No newline at end of file