diff --git a/FastGithub.Configuration/FastGithubConfig.cs b/FastGithub.Configuration/FastGithubConfig.cs index a8a52f0..173fd40 100644 --- a/FastGithub.Configuration/FastGithubConfig.cs +++ b/FastGithub.Configuration/FastGithubConfig.cs @@ -1,10 +1,9 @@ -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; -using System; +using Microsoft.Extensions.Options; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; +using System.Net; namespace FastGithub.Configuration { @@ -13,22 +12,24 @@ namespace FastGithub.Configuration /// public class FastGithubConfig { - private readonly ILogger logger; private SortedDictionary domainConfigs; private ConcurrentDictionary domainConfigCache; + /// + /// 回退的dns + /// + public IPEndPoint[] FallbackDns { get; set; } + /// /// FastGithub配置 /// /// /// - public FastGithubConfig( - IOptionsMonitor options, - ILogger logger) + public FastGithubConfig(IOptionsMonitor options) { - this.logger = logger; var opt = options.CurrentValue; + this.FallbackDns = ConvertToIPEndPoints(opt.FallbackDns).ToArray(); this.domainConfigs = ConvertDomainConfigs(opt.DomainConfigs); this.domainConfigCache = new ConcurrentDictionary(); @@ -41,14 +42,24 @@ namespace FastGithub.Configuration /// private void Update(FastGithubOptions options) { - try + this.FallbackDns = ConvertToIPEndPoints(options.FallbackDns).ToArray(); + this.domainConfigs = ConvertDomainConfigs(options.DomainConfigs); + this.domainConfigCache = new ConcurrentDictionary(); + } + + /// + /// 转换为IPEndPoint + /// + /// + /// + private static IEnumerable ConvertToIPEndPoints(IEnumerable ipEndPoints) + { + foreach (var item in ipEndPoints) { - this.domainConfigs = ConvertDomainConfigs(options.DomainConfigs); - this.domainConfigCache = new ConcurrentDictionary(); - } - catch (Exception ex) - { - this.logger.LogError(ex.Message); + if (IPEndPoint.TryParse(item, out var endPoint)) + { + yield return endPoint; + } } } diff --git a/FastGithub.Configuration/FastGithubOptions.cs b/FastGithub.Configuration/FastGithubOptions.cs index 3a09396..582d4cc 100644 --- a/FastGithub.Configuration/FastGithubOptions.cs +++ b/FastGithub.Configuration/FastGithubOptions.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; namespace FastGithub.Configuration { @@ -12,6 +13,11 @@ namespace FastGithub.Configuration /// public int HttpProxyPort { get; set; } + /// + /// 回退的dns + /// + public string[] FallbackDns { get; set; } = Array.Empty(); + /// /// 代理的域名配置 /// diff --git a/FastGithub.DomainResolve/DomainResolver.cs b/FastGithub.DomainResolve/DomainResolver.cs index e34844f..3c8777d 100644 --- a/FastGithub.DomainResolve/DomainResolver.cs +++ b/FastGithub.DomainResolve/DomainResolver.cs @@ -22,6 +22,7 @@ namespace FastGithub.DomainResolve private readonly IMemoryCache disableIPAddressCache = new MemoryCache(Options.Create(new MemoryCacheOptions())); private readonly DnscryptProxy dnscryptProxy; + private readonly FastGithubConfig fastGithubConfig; private readonly ILogger logger; private readonly TimeSpan connectTimeout = TimeSpan.FromSeconds(5d); @@ -31,19 +32,22 @@ namespace FastGithub.DomainResolve private readonly TimeSpan fallbackExpiration = TimeSpan.FromMinutes(2d); private readonly TimeSpan loopbackExpiration = TimeSpan.FromSeconds(5d); - private readonly IPEndPoint fallbackDns = new(IPAddress.Parse("114.114.114.114"), 53); private readonly ConcurrentDictionary semaphoreSlims = new(); /// /// 域名解析器 - /// + /// /// + /// /// public DomainResolver( + DnscryptProxy dnscryptProxy, + FastGithubConfig fastGithubConfig, ILogger logger) { this.dnscryptProxy = dnscryptProxy; + this.fastGithubConfig = fastGithubConfig; this.logger = logger; } @@ -105,11 +109,6 @@ namespace FastGithub.DomainResolve var expiration = this.dnscryptExpiration; address = await this.LookupByDnscryptAsync(domain, cancellationToken); - if (address == null) - { - address = await this.LookupByDnscryptAsync(domain, cancellationToken); - } - if (address == null) { expiration = this.fallbackExpiration; @@ -147,7 +146,8 @@ namespace FastGithub.DomainResolve } var dnsClient = new DnsClient(dns, forceTcp: false); - return await this.LookupAsync(dnsClient, domain, cancellationToken); + var address = await this.LookupAsync(dnsClient, domain, cancellationToken); + return address ?? await this.LookupAsync(dnsClient, domain, cancellationToken); } /// @@ -159,8 +159,16 @@ namespace FastGithub.DomainResolve /// private async Task LookupByFallbackAsync(DnsEndPoint domain, CancellationToken cancellationToken) { - var dnsClient = new DnsClient(this.fallbackDns, forceTcp: true); - return await this.LookupAsync(dnsClient, domain, cancellationToken); + foreach (var dns in this.fastGithubConfig.FallbackDns) + { + var dnsClient = new DnsClient(dns, forceTcp: true); + var address = await this.LookupAsync(dnsClient, domain, cancellationToken); + if (address != null) + { + return address; + } + } + return default; } /// diff --git a/FastGithub/appsettings.json b/FastGithub/appsettings.json index 43081bf..246ae6e 100644 --- a/FastGithub/appsettings.json +++ b/FastGithub/appsettings.json @@ -1,7 +1,11 @@ { // 新增的子配置文件appsettings.*.json,重启应用程序才生效 "FastGithub": { - "HttpProxyPort": 38457, // http代理端口,非windows才使用 + "HttpProxyPort": 38457, // http代理端口,linux/osx平台使用 + "FallbackDns": [ // dnscrypt-proxy不可用时使用 + "114.114.114.114:53", + "8.8.8.8:53" + ], "DomainConfigs": { "*.fastgithub.com": { // 域名的*表示除.之外0到多个任意字符 "TlsSni": false, // 指示tls握手时是否发送SNI