From a319833bd523bc100bb1a2bd2afa03a103453036 Mon Sep 17 00:00:00 2001 From: Amazing_DM Date: Sat, 18 Jul 2020 14:48:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E8=BF=9B=E7=A8=8B=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=E6=97=B6=E7=BD=91=E5=8D=A1DNS=E5=B0=86=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E6=9B=BF=E6=8D=A2=E4=B8=BA1.1.1.1=E5=92=8C8.8.8.8?= =?UTF-8?q?=EF=BC=8C=E5=85=B3=E9=97=AD=E5=90=8E=E8=87=AA=E5=8A=A8=E6=81=A2?= =?UTF-8?q?=E5=A4=8D=20=E6=9B=B4=E6=96=B0=E9=A9=B1=E5=8A=A8=E6=96=87?= =?UTF-8?q?=E4=BB=B61.5.9.3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Netch/Controllers/Mode/NFController.cs | 13 ++++++- Netch/Netch.csproj | 1 + Netch/Utils/DNS.cs | 51 +++++++++++++++++++++++++- binaries | 2 +- 4 files changed, 64 insertions(+), 3 deletions(-) diff --git a/Netch/Controllers/Mode/NFController.cs b/Netch/Controllers/Mode/NFController.cs index 5fc0ddbc..d5a1aa6f 100644 --- a/Netch/Controllers/Mode/NFController.cs +++ b/Netch/Controllers/Mode/NFController.cs @@ -15,6 +15,7 @@ namespace Netch.Controllers private static readonly string BinDriver = string.Empty; private static readonly string SystemDriver = $"{Environment.SystemDirectory}\\drivers\\netfilter2.sys"; + private static string[] _sysDns = { }; static NFController() { @@ -102,7 +103,15 @@ namespace Netch.Controllers { Thread.Sleep(250); - if (State == State.Started) return true; + if (State == State.Started) + { + + //备份并替换系统DNS + _sysDns = DNS.getSystemDns(); + string[] dns = { "1.1.1.1", "8.8.8.8" }; + DNS.SetDNS(dns); + + return true; } } Logging.Error(Name + "启动超时"); @@ -257,6 +266,8 @@ namespace Netch.Controllers public override void Stop() { StopInstance(); + //恢复系统DNS + DNS.SetDNS(_sysDns); } /// diff --git a/Netch/Netch.csproj b/Netch/Netch.csproj index d8de9087..23ffabe8 100644 --- a/Netch/Netch.csproj +++ b/Netch/Netch.csproj @@ -70,6 +70,7 @@ + diff --git a/Netch/Utils/DNS.cs b/Netch/Utils/DNS.cs index b06b4211..22e5a152 100644 --- a/Netch/Utils/DNS.cs +++ b/Netch/Utils/DNS.cs @@ -1,6 +1,9 @@ using System; using System.Collections; +using System.Linq; +using System.Management; using System.Net; +using System.Net.NetworkInformation; namespace Netch.Utils { @@ -45,5 +48,51 @@ namespace Netch.Utils return null; } } + /// + /// 设置DNS + /// + /// + public static void SetDNS(string[] dns) + { + ManagementClass wmi = new ManagementClass("Win32_NetworkAdapterConfiguration"); + ManagementObjectCollection moc = wmi.GetInstances(); + ManagementBaseObject inPar = null; + ManagementBaseObject outPar = null; + foreach (ManagementObject mo in moc) + { + //如果没有启用IP设置的网络设备则跳过,如果是虚拟机网卡也跳过 + if (!(bool)mo["IPEnabled"] || + mo["Description"].ToString().Contains("Virtual") || + mo["Description"].ToString().Contains("VMware") || + mo["Description"].ToString().Contains("TAP")) + continue; + + //设置DNS地址 + if (dns != null) + { + inPar = mo.GetMethodParameters("SetDNSServerSearchOrder"); + inPar["DNSServerSearchOrder"] = dns; + outPar = mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null); + } + } + } + /// + /// 从网卡获取ip设置信息 + /// + public static string[] getSystemDns() + { + var systemDns = new[] { "223.5.5.5", "1.1.1.1" }; + foreach (var network in NetworkInterface.GetAllNetworkInterfaces()) + if (!network.Description.Contains("Virtual") && + !network.Description.Contains("VMware") && + !network.Description.Contains("TAP") && + network.OperationalStatus == OperationalStatus.Up && + network.GetIPProperties()?.GatewayAddresses.Count != 0) + { + systemDns = network.GetIPProperties().DnsAddresses.Select(dns => dns.ToString()).ToArray(); + } + + return systemDns; + } } -} +} \ No newline at end of file diff --git a/binaries b/binaries index 841b75af..2adb17a3 160000 --- a/binaries +++ b/binaries @@ -1 +1 @@ -Subproject commit 841b75af2f5fab0eaabd81c82fc2b322b1854828 +Subproject commit 2adb17a398e498d8c780a16d2619f6a388127969