回退dns使用tcp查询

This commit is contained in:
陈国伟 2021-09-16 10:11:25 +08:00
parent 052eedaf10
commit 8adbacc16e
2 changed files with 68 additions and 40 deletions

View File

@ -86,7 +86,7 @@ disabled_server_names = []
## (dnscrypt-proxy will always encrypt everything even using UDP), and can ## (dnscrypt-proxy will always encrypt everything even using UDP), and can
## only increase latency. ## only increase latency.
force_tcp = false force_tcp = true
## SOCKS proxy ## SOCKS proxy
@ -223,7 +223,7 @@ cert_refresh_delay = 240
## ##
## If more than one resolver is specified, they will be tried in sequence. ## If more than one resolver is specified, they will be tried in sequence.
fallback_resolvers = [] fallback_resolvers = ['9.9.9.9:53', '8.8.8.8:53']
## Always use the fallback resolver before the system DNS settings. ## Always use the fallback resolver before the system DNS settings.

View File

@ -1,5 +1,6 @@
using DNS.Client; using DNS.Client.RequestResolver;
using DNS.Protocol; using DNS.Protocol;
using DNS.Protocol.ResourceRecords;
using FastGithub.Configuration; using FastGithub.Configuration;
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@ -23,30 +24,29 @@ namespace FastGithub.DomainResolve
private readonly IMemoryCache domainResolveCache = new MemoryCache(Options.Create(new MemoryCacheOptions())); private readonly IMemoryCache domainResolveCache = new MemoryCache(Options.Create(new MemoryCacheOptions()));
private readonly IMemoryCache disableIPAddressCache = new MemoryCache(Options.Create(new MemoryCacheOptions())); private readonly IMemoryCache disableIPAddressCache = new MemoryCache(Options.Create(new MemoryCacheOptions()));
private readonly FastGithubConfig fastGithubConfig;
private readonly DnscryptProxy dnscryptProxy; private readonly DnscryptProxy dnscryptProxy;
private readonly ILogger<DomainResolver> logger; private readonly ILogger<DomainResolver> logger;
private readonly TimeSpan lookupTimeout = TimeSpan.FromSeconds(5d);
private readonly TimeSpan connectTimeout = TimeSpan.FromSeconds(5d); private readonly TimeSpan connectTimeout = TimeSpan.FromSeconds(5d);
private readonly TimeSpan disableIPExpiration = TimeSpan.FromMinutes(2d); private readonly TimeSpan disableIPExpiration = TimeSpan.FromMinutes(2d);
private readonly TimeSpan dnscryptExpiration = TimeSpan.FromMinutes(10d); private readonly TimeSpan dnscryptExpiration = TimeSpan.FromMinutes(10d);
private readonly TimeSpan systemExpiration = 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="fastGithubConfig"></param>
/// <param name="dnscryptProxy"></param> /// <param name="dnscryptProxy"></param>
/// <param name="logger"></param> /// <param name="logger"></param>
public DomainResolver( public DomainResolver(
FastGithubConfig fastGithubConfig,
DnscryptProxy dnscryptProxy, DnscryptProxy dnscryptProxy,
ILogger<DomainResolver> logger) ILogger<DomainResolver> logger)
{ {
this.fastGithubConfig = fastGithubConfig;
this.dnscryptProxy = dnscryptProxy; this.dnscryptProxy = dnscryptProxy;
this.logger = logger; this.logger = logger;
} }
@ -114,10 +114,10 @@ namespace FastGithub.DomainResolve
address = await this.LookupByDnscryptAsync(domain, cancellationToken); address = await this.LookupByDnscryptAsync(domain, cancellationToken);
} }
if (address == null && OperatingSystem.IsWindows() == false) if (address == null)
{ {
expiration = this.systemExpiration; expiration = this.fallbackExpiration;
address = await this.LookupByDnsSystemAsync(domain, cancellationToken); address = await this.LookupByFallbackAsync(domain, cancellationToken);
} }
if (address == null) if (address == null)
@ -150,57 +150,58 @@ namespace FastGithub.DomainResolve
return null; return null;
} }
try var resolver = new RequestResolver(dns, forceTcp: false);
{ return await this.LookupAsync(resolver, domain, cancellationToken);
var dnsClient = new DnsClient(dns);
var addresses = await dnsClient.Lookup(domain.Host, RecordType.A, cancellationToken);
var address = await this.FindFastValueAsync(addresses, domain.Port, cancellationToken);
if (address == null)
{
this.logger.LogWarning($"dns({dns})解析不到{domain.Host}可用的ip解析");
}
else
{
this.logger.LogInformation($"dns({dns}): {domain.Host}->{address}");
}
return address;
}
catch (Exception ex)
{
cancellationToken.ThrowIfCancellationRequested();
this.logger.LogWarning($"dns({dns})无法解析{domain.Host}{ex.Message}");
return null;
}
} }
/// <summary> /// <summary>
/// 系统DNS查找ip /// 回退查找ip
/// </summary> /// </summary>
/// <param name="domain"></param> /// <param name="domain"></param>
/// <param name="cancellationToken"></param> /// <param name="cancellationToken"></param>
/// <exception cref="OperationCanceledException"></exception> /// <exception cref="OperationCanceledException"></exception>
/// <returns></returns> /// <returns></returns>
private async Task<IPAddress?> LookupByDnsSystemAsync(DnsEndPoint domain, CancellationToken cancellationToken) private async Task<IPAddress?> LookupByFallbackAsync(DnsEndPoint domain, CancellationToken cancellationToken)
{
var resolver = new RequestResolver(this.fallbackDns, forceTcp: true);
return await this.LookupAsync(resolver, domain, cancellationToken);
}
/// <summary>
/// 查找ip
/// </summary>
/// <param name="resolver"></param>
/// <param name="domain"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
private async Task<IPAddress?> LookupAsync(IRequestResolver resolver, DnsEndPoint domain, CancellationToken cancellationToken)
{ {
try try
{ {
var allAddresses = await Dns.GetHostAddressesAsync(domain.Host); var request = new Request();
var addresses = allAddresses.Where(item => item.AddressFamily == AddressFamily.InterNetwork); request.Questions.Add(new Question(new Domain(domain.Host), RecordType.A));
using var timeoutTokenSource = new CancellationTokenSource(this.lookupTimeout);
using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutTokenSource.Token);
var response = await resolver.Resolve(request, linkedTokenSource.Token);
var addresses = response.AnswerRecords.OfType<IPAddressResourceRecord>().Select(item => item.IPAddress).ToArray();
var address = await this.FindFastValueAsync(addresses, domain.Port, cancellationToken); var address = await this.FindFastValueAsync(addresses, domain.Port, cancellationToken);
if (address == null) if (address == null)
{ {
this.logger.LogWarning($"dns(系统)解析不到{domain.Host}可用的ip解析"); this.logger.LogWarning($"dns({resolver})解析不到{domain.Host}可用的ip解析");
} }
else else
{ {
this.logger.LogInformation($"dns(系统): {domain.Host}->{address}"); this.logger.LogInformation($"dns({resolver}): {domain.Host}->{address}");
} }
return address; return address;
} }
catch (Exception ex) catch (Exception ex)
{ {
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
this.logger.LogWarning($"dns(系统)无法解析{domain.Host}{ex.Message}"); this.logger.LogWarning($"dns({resolver})无法解析{domain.Host}{ex.Message}");
return default; return default;
} }
} }
@ -268,5 +269,32 @@ namespace FastGithub.DomainResolve
return default; return default;
} }
} }
/// <summary>
/// 请求解析器
/// </summary>
private class RequestResolver : IRequestResolver
{
private readonly IPEndPoint dns;
private readonly IRequestResolver resolver;
public RequestResolver(IPEndPoint dns, bool forceTcp)
{
this.dns = dns;
this.resolver = forceTcp
? new TcpRequestResolver(dns)
: new UdpRequestResolver(dns, new TcpRequestResolver(dns));
}
public Task<IResponse> Resolve(IRequest request, CancellationToken cancellationToken = default)
{
return this.resolver.Resolve(request, cancellationToken);
}
public override string ToString()
{
return this.dns.ToString();
}
}
} }
} }