diff --git a/FastGithub.Core/DnsIPEndPoint.cs b/FastGithub.Core/DnsIPEndPoint.cs index 8c6c6df..aa70b35 100644 --- a/FastGithub.Core/DnsIPEndPoint.cs +++ b/FastGithub.Core/DnsIPEndPoint.cs @@ -1,5 +1,6 @@ using System.Diagnostics.CodeAnalysis; using System.Net; +using System.Net.NetworkInformation; namespace FastGithub { @@ -29,14 +30,43 @@ namespace FastGithub } /// - /// 验证 + /// 验证dns + /// 防止使用自己使用自己来解析域名造成死循环 /// /// public bool Validate() { - return System.Net.IPAddress.TryParse(this.IPAddress, out var address) - ? !(address.Equals(System.Net.IPAddress.Loopback) && this.Port == 53) - : false; + if (System.Net.IPAddress.TryParse(this.IPAddress, out var address) == false) + { + return false; + } + + if (this.Port == 53 && IsLocalMachineIPAddress(address)) + { + return false; + } + + return true; + } + + /// + /// 是否为本机ip + /// + /// + /// + private static bool IsLocalMachineIPAddress(IPAddress address) + { + foreach (var @interface in NetworkInterface.GetAllNetworkInterfaces()) + { + foreach (var addressInfo in @interface.GetIPProperties().UnicastAddresses) + { + if (addressInfo.Address.Equals(address)) + { + return true; + } + } + } + return false; } } } diff --git a/FastGithub.ReverseProxy/TrustedResolver.cs b/FastGithub.ReverseProxy/DomainResolver.cs similarity index 95% rename from FastGithub.ReverseProxy/TrustedResolver.cs rename to FastGithub.ReverseProxy/DomainResolver.cs index b2ebefc..04886b2 100644 --- a/FastGithub.ReverseProxy/TrustedResolver.cs +++ b/FastGithub.ReverseProxy/DomainResolver.cs @@ -12,7 +12,7 @@ namespace FastGithub.ReverseProxy /// /// 受信任的域名解析器 /// - sealed class TrustedResolver + sealed class DomainResolver { private readonly IMemoryCache memoryCache; private readonly TimeSpan cacheTimeSpan = TimeSpan.FromSeconds(10d); @@ -22,7 +22,7 @@ namespace FastGithub.ReverseProxy /// 受信任的域名解析器 /// /// - public TrustedResolver( + public DomainResolver( IMemoryCache memoryCache, IOptionsMonitor options) { @@ -66,6 +66,7 @@ namespace FastGithub.ReverseProxy throw new Exception($"解析不到{domain}的ip"); } + // 受干扰的dns,常常返回127.0.0.1来阻断请求 // 如果解析到的ip为本机ip,会产生反向代理请求死循环 if (address.Equals(IPAddress.Loopback)) { diff --git a/FastGithub.ReverseProxy/NoSniHttpClientHanlder.cs b/FastGithub.ReverseProxy/NoSniHttpClientHanlder.cs index 6d2bf0d..b6a1dab 100644 --- a/FastGithub.ReverseProxy/NoSniHttpClientHanlder.cs +++ b/FastGithub.ReverseProxy/NoSniHttpClientHanlder.cs @@ -13,7 +13,7 @@ namespace FastGithub.ReverseProxy /// class NoSniHttpClientHanlder : DelegatingHandler { - private readonly TrustedResolver trustedDomainResolver; + private readonly DomainResolver trustedDomainResolver; private readonly ILogger logger; /// @@ -21,7 +21,7 @@ namespace FastGithub.ReverseProxy /// /// public NoSniHttpClientHanlder( - TrustedResolver trustedDomainResolver, + DomainResolver trustedDomainResolver, ILogger logger) { this.trustedDomainResolver = trustedDomainResolver; diff --git a/FastGithub.ReverseProxy/ReverseProxyServiceCollectionExtensions.cs b/FastGithub.ReverseProxy/ReverseProxyServiceCollectionExtensions.cs index 0e099ed..cbd98ee 100644 --- a/FastGithub.ReverseProxy/ReverseProxyServiceCollectionExtensions.cs +++ b/FastGithub.ReverseProxy/ReverseProxyServiceCollectionExtensions.cs @@ -18,7 +18,7 @@ namespace FastGithub return services .AddMemoryCache() .AddHttpForwarder() - .AddSingleton() + .AddSingleton() .AddTransient(); } }