恢复FallbackDns配置
This commit is contained in:
parent
da7c4ee564
commit
7424605aac
@ -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?>();
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
/// <summary>
|
||||
/// 转换为IPEndPoint
|
||||
/// </summary>
|
||||
/// <param name="ipEndPoints"></param>
|
||||
/// <returns></returns>
|
||||
private static IEnumerable<IPEndPoint> ConvertToIPEndPoints(IEnumerable<string> ipEndPoints)
|
||||
{
|
||||
this.logger.LogError(ex.Message);
|
||||
foreach (var item in ipEndPoints)
|
||||
{
|
||||
if (IPEndPoint.TryParse(item, out var endPoint))
|
||||
{
|
||||
yield return endPoint;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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>
|
||||
|
||||
@ -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>
|
||||
/// <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>
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user