diff --git a/Directory.Build.props b/Directory.Build.props index b2b1270..1c190b7 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,6 +1,6 @@ - 1.0.0-rc2 + 1.0.0 enable github定制版的dns服务,解析github最优的ip https://github.com/xljiulang/FastGithub diff --git a/FastGithub.Dns/DnsHostedService.cs b/FastGithub.Dns/DnsHostedService.cs index bbad889..3475544 100644 --- a/FastGithub.Dns/DnsHostedService.cs +++ b/FastGithub.Dns/DnsHostedService.cs @@ -2,6 +2,8 @@ using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; +using System; +using System.Net; using System.Threading; using System.Threading.Tasks; @@ -13,6 +15,7 @@ namespace FastGithub.Dns sealed class DnsHostedService : IHostedService { private readonly DnsServer dnsServer; + private readonly IOptions options; private readonly ILogger logger; /// @@ -27,6 +30,7 @@ namespace FastGithub.Dns ILogger logger) { this.dnsServer = new DnsServer(githubRequestResolver, options.Value.UpStream); + this.options = options; this.logger = logger; } @@ -39,6 +43,8 @@ namespace FastGithub.Dns { this.dnsServer.Listen(); this.logger.LogInformation("dns服务启用成功"); + this.SetNameServers(IPAddress.Loopback, this.options.Value.UpStream); + return Task.CompletedTask; } @@ -51,7 +57,30 @@ namespace FastGithub.Dns { this.dnsServer.Dispose(); this.logger.LogInformation("dns服务已终止"); + this.SetNameServers(); + return Task.CompletedTask; } + + /// + /// 设备dns + /// + /// + private void 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成功"); + } + catch (Exception ex) + { + this.logger.LogWarning($"{action}本机dns失败:{ex.Message}"); + } + } + } } } diff --git a/FastGithub.Dns/DnsOptions.cs b/FastGithub.Dns/DnsOptions.cs index 40d561e..1b6ee60 100644 --- a/FastGithub.Dns/DnsOptions.cs +++ b/FastGithub.Dns/DnsOptions.cs @@ -18,5 +18,10 @@ namespace FastGithub.Dns /// 获取或设置github相关域名的ip存活时长 /// public TimeSpan GithubTTL { get; set; } = TimeSpan.FromMinutes(10d); + + /// + /// 是否设置本机使用此dns + /// + public bool SetToLocalMachine { get; set; } = true; } } diff --git a/FastGithub.Dns/FastGithub.Dns.csproj b/FastGithub.Dns/FastGithub.Dns.csproj index 7165c3c..b0068ab 100644 --- a/FastGithub.Dns/FastGithub.Dns.csproj +++ b/FastGithub.Dns/FastGithub.Dns.csproj @@ -4,6 +4,10 @@ net5.0 + + + + diff --git a/FastGithub.Dns/NameServiceUtil.cs b/FastGithub.Dns/NameServiceUtil.cs new file mode 100644 index 0000000..d05966e --- /dev/null +++ b/FastGithub.Dns/NameServiceUtil.cs @@ -0,0 +1,84 @@ +using System; +using System.Diagnostics; +using System.Linq; +using System.Net; +using System.Net.NetworkInformation; +using System.Runtime.InteropServices; +using System.Runtime.Versioning; + +namespace FastGithub.Dns +{ + /// + /// 域名服务工具 + /// + [SupportedOSPlatform("windows")] + static class NameServiceUtil + { + /// + /// www.baidu.com的ip + /// + private static readonly IPAddress www_baidu_com = IPAddress.Parse("183.232.231.172"); + + [DllImport("iphlpapi")] + private static extern int GetBestInterface(uint dwDestAddr, ref uint pdwBestIfIndex); + + /// + /// 通过远程地址查找匹配的网络适接口 + /// + /// + /// + private static NetworkInterface? GetBestNetworkInterface(IPAddress remoteAddress) + { + var dwBestIfIndex = 0u; + var dwDestAddr = BitConverter.ToUInt32(remoteAddress.GetAddressBytes()); + var errorCode = GetBestInterface(dwDestAddr, ref dwBestIfIndex); + if (errorCode != 0) + { + throw new NetworkInformationException(errorCode); + } + + return NetworkInterface + .GetAllNetworkInterfaces() + .Where(item => item.GetIPProperties().GetIPv4Properties().Index == dwBestIfIndex) + .FirstOrDefault(); + } + + /// + /// 设置域名服务 + /// + /// + /// + /// + public static void SetNameServers(params IPAddress[] nameServers) + { + var networkIF = GetBestNetworkInterface(www_baidu_com); + if (networkIF == null) + { + throw new NotSupportedException("找不到网络适配器用来设置dns"); + } + + Netsh($@"interface ipv4 delete dns ""{networkIF.Name}"" all"); + foreach (var address in nameServers) + { + Netsh($@"interface ipv4 add dns ""{networkIF.Name}"" {address} validate=no"); + } + } + + /// + /// 执行Netsh + /// + /// + private static void Netsh(string arguments) + { + var netsh = new ProcessStartInfo + { + FileName = "netsh.exe", + Arguments = arguments, + CreateNoWindow = true, + UseShellExecute = false, + WindowStyle = ProcessWindowStyle.Hidden + }; + Process.Start(netsh)?.WaitForExit(); + } + } +} diff --git a/FastGithub/FastGithub.csproj b/FastGithub/FastGithub.csproj index 370e3a8..781c80a 100644 --- a/FastGithub/FastGithub.csproj +++ b/FastGithub/FastGithub.csproj @@ -4,12 +4,17 @@ Exe net5.0;net6.0 MIT + app.ico true + + app.manifest + + diff --git a/FastGithub/app.ico b/FastGithub/app.ico new file mode 100644 index 0000000..8f5f6ae Binary files /dev/null and b/FastGithub/app.ico differ diff --git a/FastGithub/app.manifest b/FastGithub/app.manifest new file mode 100644 index 0000000..60a45a2 --- /dev/null +++ b/FastGithub/app.manifest @@ -0,0 +1,75 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/FastGithub/appsettings.json b/FastGithub/appsettings.json index 5d958c6..049c4c7 100644 --- a/FastGithub/appsettings.json +++ b/FastGithub/appsettings.json @@ -1,7 +1,8 @@ { "Dns": { "UpStream": "114.114.114.114", // dns - "GithubTTL": "00:10:00" // githubĴʱ + "GithubTTL": "00:10:00", // githubĴʱ + "SetToLocalMachine": true // Ƿñʹôdns(֧windows) }, "Github": { "Lookup": { // ip