diff --git a/FastGithub.Configuration/DnsConfig.cs b/FastGithub.Configuration/DnsConfig.cs index e03055b..c8febce 100644 --- a/FastGithub.Configuration/DnsConfig.cs +++ b/FastGithub.Configuration/DnsConfig.cs @@ -1,7 +1,6 @@ using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Net; -using System.Net.NetworkInformation; namespace FastGithub.Configuration { @@ -33,7 +32,7 @@ namespace FastGithub.Configuration throw new FastGithubException($"无效的ip:{this.IPAddress}"); } - if (this.Port == 53 && IsLocalMachineIPAddress(address)) + if (this.Port == 53 && IsLocalIPAddress(address)) { throw new FastGithubException($"配置的dns值不能指向{nameof(FastGithub)}自身:{this.IPAddress}:{this.Port}"); } @@ -51,12 +50,18 @@ namespace FastGithub.Configuration /// /// /// - private static bool IsLocalMachineIPAddress(IPAddress address) + private static bool IsLocalIPAddress(IPAddress address) { - return IPGlobalProperties - .GetIPGlobalProperties() - .GetUnicastAddresses() - .Any(item => item.Address.Equals(address)); + if (address.Equals(System.Net.IPAddress.Loopback)) + { + return true; + } + if (address.Equals(System.Net.IPAddress.IPv6Loopback)) + { + return true; + } + var addresses = Dns.GetHostAddresses(Dns.GetHostName()); + return addresses.Contains(address); } } } diff --git a/FastGithub.Dns/HostsValidator.cs b/FastGithub.Dns/HostsValidator.cs index e3aae79..739ce46 100644 --- a/FastGithub.Dns/HostsValidator.cs +++ b/FastGithub.Dns/HostsValidator.cs @@ -1,11 +1,11 @@ using FastGithub.Configuration; using Microsoft.Extensions.Logging; using System; +using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.IO; -using System.Linq; using System.Net; -using System.Net.NetworkInformation; +using System.Net.Sockets; using System.Threading.Tasks; namespace FastGithub.Dns @@ -48,12 +48,7 @@ namespace FastGithub.Dns return; } - var localAddresses = IPGlobalProperties - .GetIPGlobalProperties() - .GetUnicastAddresses() - .Select(item => item.Address) - .ToArray(); - + var localAddresses = await GetLocalIPv4AddressesAsync(); var lines = await File.ReadAllLinesAsync(hostsPath); foreach (var line in lines) { @@ -72,6 +67,29 @@ namespace FastGithub.Dns } } + /// + /// 获取本机所有IPv4 + /// + /// + private static async Task> GetLocalIPv4AddressesAsync() + { + var hashSet = new HashSet + { + IPAddress.Loopback + }; + + var hostName = System.Net.Dns.GetHostName(); + var addresses = await System.Net.Dns.GetHostAddressesAsync(hostName); + foreach (var address in addresses) + { + if (address.AddressFamily == AddressFamily.InterNetwork) + { + hashSet.Add(address); + } + } + return hashSet; + } + /// /// hosts文件记录 diff --git a/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs b/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs index 87fbec0..09a1253 100644 --- a/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs +++ b/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs @@ -187,17 +187,14 @@ namespace FastGithub yield break; } - var globalPropreties = IPGlobalProperties.GetIPGlobalProperties(); - if (string.IsNullOrEmpty(globalPropreties.HostName) == false) - { - yield return globalPropreties.HostName; - } + var hostName = Dns.GetHostName(); + yield return hostName; - foreach (var item in globalPropreties.GetUnicastAddresses()) + foreach (var address in Dns.GetHostAddresses(hostName)) { - if (item.Address.AddressFamily == AddressFamily.InterNetwork) + if (address.AddressFamily == AddressFamily.InterNetwork) { - yield return item.Address.ToString(); + yield return address.ToString(); } } }