恢复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 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
/// </summary>
public class FastGithubConfig
{
private readonly ILogger<FastGithubConfig> logger;
private SortedDictionary<DomainPattern, DomainConfig> domainConfigs;
private ConcurrentDictionary<string, DomainConfig?> domainConfigCache;
/// <summary>
/// 回退的dns
/// </summary>
public IPEndPoint[] FallbackDns { get; set; }
/// <summary>
/// FastGithub配置
/// </summary>
/// <param name="options"></param>
/// <param name="logger"></param>
public FastGithubConfig(
IOptionsMonitor<FastGithubOptions> options,
ILogger<FastGithubConfig> logger)
public FastGithubConfig(IOptionsMonitor<FastGithubOptions> options)
{
this.logger = logger;
var opt = options.CurrentValue;
this.FallbackDns = ConvertToIPEndPoints(opt.FallbackDns).ToArray();
this.domainConfigs = ConvertDomainConfigs(opt.DomainConfigs);
this.domainConfigCache = new ConcurrentDictionary<string, DomainConfig?>();
@ -41,14 +42,24 @@ namespace FastGithub.Configuration
/// <param name="options"></param>
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);
this.domainConfigCache = new ConcurrentDictionary<string, DomainConfig?>();
}
catch (Exception ex)
{
this.logger.LogError(ex.Message);
if (IPEndPoint.TryParse(item, out var endPoint))
{
yield return endPoint;
}
}
}

View File

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

View File

@ -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<DomainResolver> 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<DnsEndPoint, SemaphoreSlim> semaphoreSlims = new();
/// <summary>
/// 域名解析器
/// </summary>
/// </summary>
/// <param name="dnscryptProxy"></param>
/// <param name="fastGithubConfig"></param>
/// <param name="logger"></param>
public DomainResolver(
DnscryptProxy dnscryptProxy,
FastGithubConfig fastGithubConfig,
ILogger<DomainResolver> 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);
}
/// <summary>
@ -159,8 +159,16 @@ namespace FastGithub.DomainResolve
/// <returns></returns>
private async Task<IPAddress?> 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;
}
/// <summary>

View File

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