From 2bdec42b0a631f2a57566cfbd498e63bdae60e07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com> Date: Mon, 21 Jun 2021 08:50:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BA=94=E7=94=A8=E5=85=B3=E9=97=AD=E6=97=B6?= =?UTF-8?q?=E6=81=A2=E5=A4=8D=E5=8E=9Fdns=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.Dns/DnsHostedService.cs | 25 +++++++++++++++++-------- FastGithub.Dns/NameServiceUtil.cs | 23 ++++++++++++----------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/FastGithub.Dns/DnsHostedService.cs b/FastGithub.Dns/DnsHostedService.cs index 3475544..844e4a7 100644 --- a/FastGithub.Dns/DnsHostedService.cs +++ b/FastGithub.Dns/DnsHostedService.cs @@ -17,6 +17,7 @@ namespace FastGithub.Dns private readonly DnsServer dnsServer; private readonly IOptions options; private readonly ILogger logger; + private IPAddress[]? dnsAddresses; /// /// dns后台服务 @@ -43,7 +44,7 @@ namespace FastGithub.Dns { this.dnsServer.Listen(); this.logger.LogInformation("dns服务启用成功"); - this.SetNameServers(IPAddress.Loopback, this.options.Value.UpStream); + this.dnsAddresses = this.SetNameServers(IPAddress.Loopback, this.options.Value.UpStream); return Task.CompletedTask; } @@ -57,30 +58,38 @@ namespace FastGithub.Dns { this.dnsServer.Dispose(); this.logger.LogInformation("dns服务已终止"); - this.SetNameServers(); + + if (this.dnsAddresses != null) + { + this.SetNameServers(this.dnsAddresses); + } return Task.CompletedTask; } /// - /// 设备dns + /// 设置dns /// /// - private void SetNameServers(params IPAddress[] nameServers) + /// + private IPAddress[]? SetNameServers(params IPAddress[] nameServers) { - var action = nameServers.Length == 0 ? "清除" : "设置"; if (this.options.Value.SetToLocalMachine && OperatingSystem.IsWindows()) { try { - NameServiceUtil.SetNameServers(nameServers); - this.logger.LogInformation($"{action}本机dns成功"); + var results = NameServiceUtil.SetNameServers(nameServers); + this.logger.LogInformation($"设置本机dns成功"); + return results; } + catch (Exception ex) { - this.logger.LogWarning($"{action}本机dns失败:{ex.Message}"); + this.logger.LogWarning($"设置本机dns失败:{ex.Message}"); } } + + return default; } } } diff --git a/FastGithub.Dns/NameServiceUtil.cs b/FastGithub.Dns/NameServiceUtil.cs index d05966e..80a524e 100644 --- a/FastGithub.Dns/NameServiceUtil.cs +++ b/FastGithub.Dns/NameServiceUtil.cs @@ -32,12 +32,9 @@ namespace FastGithub.Dns var dwBestIfIndex = 0u; var dwDestAddr = BitConverter.ToUInt32(remoteAddress.GetAddressBytes()); var errorCode = GetBestInterface(dwDestAddr, ref dwBestIfIndex); - if (errorCode != 0) - { - throw new NetworkInformationException(errorCode); - } - - return NetworkInterface + return errorCode != 0 + ? throw new NetworkInformationException(errorCode) + : NetworkInterface .GetAllNetworkInterfaces() .Where(item => item.GetIPProperties().GetIPv4Properties().Index == dwBestIfIndex) .FirstOrDefault(); @@ -49,19 +46,23 @@ namespace FastGithub.Dns /// /// /// - public static void SetNameServers(params IPAddress[] nameServers) + /// 未设置之前的记录 + public static IPAddress[] SetNameServers(params IPAddress[] nameServers) { - var networkIF = GetBestNetworkInterface(www_baidu_com); - if (networkIF == null) + var networkInterface = GetBestNetworkInterface(www_baidu_com); + if (networkInterface == null) { throw new NotSupportedException("找不到网络适配器用来设置dns"); } + var dnsAddresses = networkInterface.GetIPProperties().DnsAddresses.ToArray(); - Netsh($@"interface ipv4 delete dns ""{networkIF.Name}"" all"); + Netsh($@"interface ipv4 delete dns ""{networkInterface.Name}"" all"); foreach (var address in nameServers) { - Netsh($@"interface ipv4 add dns ""{networkIF.Name}"" {address} validate=no"); + Netsh($@"interface ipv4 add dns ""{networkInterface.Name}"" {address} validate=no"); } + + return dnsAddresses; } ///