恢复FallbackDns配置

This commit is contained in:
老九 2021-09-20 21:03:52 +08:00
parent da7c4ee564
commit 7424605aac
4 changed files with 56 additions and 27 deletions

View File

@ -1,10 +1,9 @@
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Linq; using System.Linq;
using System.Net;
namespace FastGithub.Configuration namespace FastGithub.Configuration
{ {
@ -13,22 +12,24 @@ namespace FastGithub.Configuration
/// </summary> /// </summary>
public class FastGithubConfig public class FastGithubConfig
{ {
private readonly ILogger<FastGithubConfig> logger;
private SortedDictionary<DomainPattern, DomainConfig> domainConfigs; private SortedDictionary<DomainPattern, DomainConfig> domainConfigs;
private ConcurrentDictionary<string, DomainConfig?> domainConfigCache; private ConcurrentDictionary<string, DomainConfig?> domainConfigCache;
/// <summary>
/// 回退的dns
/// </summary>
public IPEndPoint[] FallbackDns { get; set; }
/// <summary> /// <summary>
/// FastGithub配置 /// FastGithub配置
/// </summary> /// </summary>
/// <param name="options"></param> /// <param name="options"></param>
/// <param name="logger"></param> /// <param name="logger"></param>
public FastGithubConfig( public FastGithubConfig(IOptionsMonitor<FastGithubOptions> options)
IOptionsMonitor<FastGithubOptions> options,
ILogger<FastGithubConfig> logger)
{ {
this.logger = logger;
var opt = options.CurrentValue; var opt = options.CurrentValue;
this.FallbackDns = ConvertToIPEndPoints(opt.FallbackDns).ToArray();
this.domainConfigs = ConvertDomainConfigs(opt.DomainConfigs); this.domainConfigs = ConvertDomainConfigs(opt.DomainConfigs);
this.domainConfigCache = new ConcurrentDictionary<string, DomainConfig?>(); this.domainConfigCache = new ConcurrentDictionary<string, DomainConfig?>();
@ -41,14 +42,24 @@ namespace FastGithub.Configuration
/// <param name="options"></param> /// <param name="options"></param>
private void Update(FastGithubOptions options) private void Update(FastGithubOptions options)
{ {
try this.FallbackDns = ConvertToIPEndPoints(options.FallbackDns).ToArray();
this.domainConfigs = ConvertDomainConfigs(options.DomainConfigs);
this.domainConfigCache = new ConcurrentDictionary<string, DomainConfig?>();
}
/// <summary>
/// 转换为IPEndPoint
/// </summary>
/// <param name="ipEndPoints"></param>
/// <returns></returns>
private static IEnumerable<IPEndPoint> ConvertToIPEndPoints(IEnumerable<string> ipEndPoints)
{
foreach (var item in ipEndPoints)
{ {
this.domainConfigs = ConvertDomainConfigs(options.DomainConfigs); if (IPEndPoint.TryParse(item, out var endPoint))
this.domainConfigCache = new ConcurrentDictionary<string, DomainConfig?>(); {
} yield return endPoint;
catch (Exception ex) }
{
this.logger.LogError(ex.Message);
} }
} }

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
namespace FastGithub.Configuration namespace FastGithub.Configuration
{ {
@ -12,6 +13,11 @@ namespace FastGithub.Configuration
/// </summary> /// </summary>
public int HttpProxyPort { get; set; } public int HttpProxyPort { get; set; }
/// <summary>
/// 回退的dns
/// </summary>
public string[] FallbackDns { get; set; } = Array.Empty<string>();
/// <summary> /// <summary>
/// 代理的域名配置 /// 代理的域名配置
/// </summary> /// </summary>

View File

@ -22,6 +22,7 @@ namespace FastGithub.DomainResolve
private readonly IMemoryCache disableIPAddressCache = new MemoryCache(Options.Create(new MemoryCacheOptions())); private readonly IMemoryCache disableIPAddressCache = new MemoryCache(Options.Create(new MemoryCacheOptions()));
private readonly DnscryptProxy dnscryptProxy; private readonly DnscryptProxy dnscryptProxy;
private readonly FastGithubConfig fastGithubConfig;
private readonly ILogger<DomainResolver> logger; private readonly ILogger<DomainResolver> logger;
private readonly TimeSpan connectTimeout = TimeSpan.FromSeconds(5d); private readonly TimeSpan connectTimeout = TimeSpan.FromSeconds(5d);
@ -31,19 +32,22 @@ namespace FastGithub.DomainResolve
private readonly TimeSpan fallbackExpiration = TimeSpan.FromMinutes(2d); private readonly TimeSpan fallbackExpiration = TimeSpan.FromMinutes(2d);
private readonly TimeSpan loopbackExpiration = TimeSpan.FromSeconds(5d); private readonly TimeSpan loopbackExpiration = TimeSpan.FromSeconds(5d);
private readonly IPEndPoint fallbackDns = new(IPAddress.Parse("114.114.114.114"), 53);
private readonly ConcurrentDictionary<DnsEndPoint, SemaphoreSlim> semaphoreSlims = new(); private readonly ConcurrentDictionary<DnsEndPoint, SemaphoreSlim> semaphoreSlims = new();
/// <summary> /// <summary>
/// 域名解析器 /// 域名解析器
/// </summary> /// </summary>
/// <param name="dnscryptProxy"></param> /// <param name="dnscryptProxy"></param>
/// <param name="fastGithubConfig"></param>
/// <param name="logger"></param> /// <param name="logger"></param>
public DomainResolver( public DomainResolver(
DnscryptProxy dnscryptProxy, DnscryptProxy dnscryptProxy,
FastGithubConfig fastGithubConfig,
ILogger<DomainResolver> logger) ILogger<DomainResolver> logger)
{ {
this.dnscryptProxy = dnscryptProxy; this.dnscryptProxy = dnscryptProxy;
this.fastGithubConfig = fastGithubConfig;
this.logger = logger; this.logger = logger;
} }
@ -105,11 +109,6 @@ namespace FastGithub.DomainResolve
var expiration = this.dnscryptExpiration; var expiration = this.dnscryptExpiration;
address = await this.LookupByDnscryptAsync(domain, cancellationToken); address = await this.LookupByDnscryptAsync(domain, cancellationToken);
if (address == null)
{
address = await this.LookupByDnscryptAsync(domain, cancellationToken);
}
if (address == null) if (address == null)
{ {
expiration = this.fallbackExpiration; expiration = this.fallbackExpiration;
@ -147,7 +146,8 @@ namespace FastGithub.DomainResolve
} }
var dnsClient = new DnsClient(dns, forceTcp: false); 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);
} }
/// <summary> /// <summary>
@ -159,8 +159,16 @@ namespace FastGithub.DomainResolve
/// <returns></returns> /// <returns></returns>
private async Task<IPAddress?> LookupByFallbackAsync(DnsEndPoint domain, CancellationToken cancellationToken) private async Task<IPAddress?> LookupByFallbackAsync(DnsEndPoint domain, CancellationToken cancellationToken)
{ {
var dnsClient = new DnsClient(this.fallbackDns, forceTcp: true); foreach (var dns in this.fastGithubConfig.FallbackDns)
return await this.LookupAsync(dnsClient, domain, cancellationToken); {
var dnsClient = new DnsClient(dns, forceTcp: true);
var address = await this.LookupAsync(dnsClient, domain, cancellationToken);
if (address != null)
{
return address;
}
}
return default;
} }
/// <summary> /// <summary>

View File

@ -1,7 +1,11 @@
{ {
// appsettings.*.json // appsettings.*.json
"FastGithub": { "FastGithub": {
"HttpProxyPort": 38457, // httpwindows使 "HttpProxyPort": 38457, // httplinux/osx使
"FallbackDns": [ // dnscrypt-proxy使
"114.114.114.114:53",
"8.8.8.8:53"
],
"DomainConfigs": { "DomainConfigs": {
"*.fastgithub.com": { // *.0 "*.fastgithub.com": { // *.0
"TlsSni": false, // tlsSNI "TlsSni": false, // tlsSNI