增加DnsClient类型
This commit is contained in:
parent
1bd5d8da95
commit
001452a3e5
66
FastGithub.DomainResolve/DnsClient.cs
Normal file
66
FastGithub.DomainResolve/DnsClient.cs
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
using DNS.Client;
|
||||||
|
using DNS.Client.RequestResolver;
|
||||||
|
using DNS.Protocol;
|
||||||
|
using DNS.Protocol.ResourceRecords;
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace FastGithub.DomainResolve
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// DNS客户端
|
||||||
|
/// </summary>
|
||||||
|
sealed class DnsClient
|
||||||
|
{
|
||||||
|
private readonly IPEndPoint dns;
|
||||||
|
private readonly IRequestResolver resolver;
|
||||||
|
private readonly TimeSpan timeout = TimeSpan.FromSeconds(5d);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// DNS客户端
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dns"></param>
|
||||||
|
/// <param name="forceTcp"></param>
|
||||||
|
public DnsClient(IPEndPoint dns, bool forceTcp)
|
||||||
|
{
|
||||||
|
this.dns = dns;
|
||||||
|
this.resolver = forceTcp
|
||||||
|
? new TcpRequestResolver(dns)
|
||||||
|
: new UdpRequestResolver(dns, new TcpRequestResolver(dns));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 解析域名
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="domain"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<IPAddress[]> LookupAsync(string domain, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
var request = new Request
|
||||||
|
{
|
||||||
|
RecursionDesired = true,
|
||||||
|
OperationCode = OperationCode.Query
|
||||||
|
};
|
||||||
|
request.Questions.Add(new Question(new Domain(domain), RecordType.A));
|
||||||
|
var clientRequest = new ClientRequest(this.resolver, request);
|
||||||
|
|
||||||
|
using var timeoutTokenSource = new CancellationTokenSource(this.timeout);
|
||||||
|
using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutTokenSource.Token);
|
||||||
|
var response = await clientRequest.Resolve(linkedTokenSource.Token);
|
||||||
|
return response.AnswerRecords.OfType<IPAddressResourceRecord>().Select(item => item.IPAddress).ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 转换为文本
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return this.dns.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,8 +1,4 @@
|
|||||||
using DNS.Client;
|
using FastGithub.Configuration;
|
||||||
using DNS.Client.RequestResolver;
|
|
||||||
using DNS.Protocol;
|
|
||||||
using DNS.Protocol.ResourceRecords;
|
|
||||||
using FastGithub.Configuration;
|
|
||||||
using Microsoft.Extensions.Caching.Memory;
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
@ -28,7 +24,6 @@ namespace FastGithub.DomainResolve
|
|||||||
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);
|
||||||
|
|
||||||
@ -151,8 +146,8 @@ namespace FastGithub.DomainResolve
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
var resolver = new RequestResolver(dns, forceTcp: false);
|
var dnsClient = new DnsClient(dns, forceTcp: false);
|
||||||
return await this.LookupAsync(resolver, domain, cancellationToken);
|
return await this.LookupAsync(dnsClient, domain, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -164,49 +159,38 @@ 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 resolver = new RequestResolver(this.fallbackDns, forceTcp: true);
|
var dnsClient = new DnsClient(this.fallbackDns, forceTcp: true);
|
||||||
return await this.LookupAsync(resolver, domain, cancellationToken);
|
return await this.LookupAsync(dnsClient, domain, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 查找ip
|
/// 查找ip
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="resolver"></param>
|
/// <param name="dnsClient"></param>
|
||||||
/// <param name="domain"></param>
|
/// <param name="domain"></param>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
private async Task<IPAddress?> LookupAsync(IRequestResolver resolver, DnsEndPoint domain, CancellationToken cancellationToken)
|
private async Task<IPAddress?> LookupAsync(DnsClient dnsClient, DnsEndPoint domain, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var request = new Request
|
var addresses = await dnsClient.LookupAsync(domain.Host, cancellationToken);
|
||||||
{
|
|
||||||
RecursionDesired = true,
|
|
||||||
OperationCode = OperationCode.Query
|
|
||||||
};
|
|
||||||
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({resolver})解析不到{domain.Host}可用的ip解析");
|
this.logger.LogWarning($"dns({dnsClient})解析不到{domain.Host}可用的ip解析");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
this.logger.LogInformation($"dns({resolver}): {domain.Host}->{address}");
|
this.logger.LogInformation($"dns({dnsClient}): {domain.Host}->{address}");
|
||||||
}
|
}
|
||||||
return address;
|
return address;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
cancellationToken.ThrowIfCancellationRequested();
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
this.logger.LogWarning($"dns({resolver})无法解析{domain.Host}:{ex.Message}");
|
this.logger.LogWarning($"dns({dnsClient})无法解析{domain.Host}:{ex.Message}");
|
||||||
return default;
|
return default;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -274,33 +258,5 @@ 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)
|
|
||||||
{
|
|
||||||
var clientRequest = new ClientRequest(this.resolver, request);
|
|
||||||
return clientRequest.Resolve(cancellationToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString()
|
|
||||||
{
|
|
||||||
return this.dns.ToString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user