From 2324634a860daad432a3cfd70ad4b6d26e9721aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=81=E4=B9=9D?= <366193849@qq.com> Date: Fri, 27 Aug 2021 21:35:00 +0800 Subject: [PATCH] =?UTF-8?q?=E7=B2=BE=E7=A1=AE=E5=88=A4=E6=96=AD=E4=B8=BBDN?= =?UTF-8?q?S?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.Dns/DnsOverUdpHostedService.cs | 4 +-- FastGithub.Dns/SystemDnsUtil.cs | 40 +++++++++++++---------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/FastGithub.Dns/DnsOverUdpHostedService.cs b/FastGithub.Dns/DnsOverUdpHostedService.cs index 2edc2b2..e2747ee 100644 --- a/FastGithub.Dns/DnsOverUdpHostedService.cs +++ b/FastGithub.Dns/DnsOverUdpHostedService.cs @@ -57,7 +57,7 @@ namespace FastGithub.Dns { try { - SystemDnsUtil.SetPrimitiveDns(IPAddress.Loopback); + SystemDnsUtil.SetAsPrimitiveDns(); SystemDnsUtil.FlushResolverCache(); } catch (Exception ex) @@ -100,7 +100,7 @@ namespace FastGithub.Dns try { - SystemDnsUtil.RemovePrimitiveDns(IPAddress.Loopback); + SystemDnsUtil.RemoveFromPrimitiveDns(); } catch (Exception ex) { diff --git a/FastGithub.Dns/SystemDnsUtil.cs b/FastGithub.Dns/SystemDnsUtil.cs index 4fba8e2..9fec28b 100644 --- a/FastGithub.Dns/SystemDnsUtil.cs +++ b/FastGithub.Dns/SystemDnsUtil.cs @@ -15,11 +15,6 @@ namespace FastGithub.Dns /// static class SystemDnsUtil { - /// - /// www.baidu.com的ip - /// - private static readonly IPAddress www_baidu_com = IPAddress.Parse("183.232.231.172"); - /// /// 刷新DNS缓存 /// @@ -39,21 +34,22 @@ namespace FastGithub.Dns } /// - /// 设置主dns - /// - /// + /// 设置为主dns + /// /// - public static void SetPrimitiveDns(IPAddress primitive) + public static void SetAsPrimitiveDns() { var @interface = GetOutboundNetworkInterface(); if (@interface == null) { - throw new FastGithubException($"找不到匹配的网络适配器来设置主DNS值:{primitive}"); + throw new FastGithubException($"找不到匹配的网络适配器来设置主DNS"); } var dnsAddresses = @interface.GetIPProperties().DnsAddresses; - if (primitive.Equals(dnsAddresses.FirstOrDefault()) == false) + var firstRecord = dnsAddresses.FirstOrDefault(); + if (firstRecord == null || LocalMachine.ContainsIPAddress(firstRecord) == false) { + var primitive = IPAddress.Loopback; var nameServers = dnsAddresses.Prepend(primitive); if (OperatingSystem.IsWindows()) { @@ -71,26 +67,34 @@ namespace FastGithub.Dns } /// - /// 移除主dns - /// - /// + /// 从主dns移除 + /// /// - public static void RemovePrimitiveDns(IPAddress primitive) + public static void RemoveFromPrimitiveDns() { var @interface = GetOutboundNetworkInterface(); if (@interface == null) { - throw new FastGithubException($"找不到匹配的网络适配器来移除主DNS值:{primitive}"); + throw new FastGithubException($"找不到匹配的网络适配器来移除主DNS"); } var dnsAddresses = @interface.GetIPProperties().DnsAddresses; - if (primitive.Equals(dnsAddresses.FirstOrDefault())) + var firstRecord = dnsAddresses.FirstOrDefault(); + if (firstRecord != null && LocalMachine.ContainsIPAddress(firstRecord)) { var nameServers = dnsAddresses.Skip(1); if (OperatingSystem.IsWindows()) { SetNameServers(@interface, nameServers); } + else if (OperatingSystem.IsLinux()) + { + throw new FastGithubException($"不支持自动移除本机主DNS,请手工移除/etc/resolv.conf的第一条记录"); + } + else if (OperatingSystem.IsMacOS()) + { + throw new FastGithubException($"不支持自动移除本机主DNS,请手工移除连接网络的DNS的第一条记录"); + } } } @@ -101,7 +105,7 @@ namespace FastGithub.Dns /// private static NetworkInterface? GetOutboundNetworkInterface() { - var remoteEndPoint = new IPEndPoint(www_baidu_com, 443); + var remoteEndPoint = new IPEndPoint(IPAddress.Parse("1.1.1.1"), 53); var address = LocalMachine.GetLocalIPAddress(remoteEndPoint); if (address == null) {