using DNS.Client;
using DNS.Client.RequestResolver;
using DNS.Protocol;
using DNS.Protocol.ResourceRecords;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
namespace FastGithub.DomainResolve
{
///
/// DNS客户端
///
sealed class DnsClient
{
private readonly ILogger logger;
private readonly int resolveTimeout = (int)TimeSpan.FromSeconds(2d).TotalMilliseconds;
private readonly IMemoryCache dnsCache = new MemoryCache(Options.Create(new MemoryCacheOptions()));
private readonly TimeSpan dnsExpiration = TimeSpan.FromMinutes(2d);
///
/// DNS客户端
///
///
public DnsClient(ILogger logger)
{
this.logger = logger;
}
///
/// 解析域名
///
///
///
///
///
public async Task LookupAsync(IPEndPoint dns, string domain, CancellationToken cancellationToken = default)
{
var key = $"{dns}:{domain}";
if (this.dnsCache.TryGetValue(key, out var value))
{
return value;
}
try
{
value = await this.LookupCoreAsync(dns, domain, cancellationToken);
this.dnsCache.Set(key, value, this.dnsExpiration);
var items = string.Join(", ", value.Select(item => item.ToString()));
this.logger.LogInformation($"{dns}:{domain}->[{items}]");
return value;
}
catch (Exception ex)
{
this.logger.LogWarning($"{dns}无法解析{domain}:{ex.Message}");
return Array.Empty();
}
}
///
/// 解析域名
///
///
///
///
///
private async Task LookupCoreAsync(IPEndPoint dns, string domain, CancellationToken cancellationToken = default)
{
if (domain == "localhost")
{
return new[] { IPAddress.Loopback };
}
var resolver = dns.Port == 53
? (IRequestResolver)new TcpRequestResolver(dns)
: new UdpRequestResolver(dns, new TcpRequestResolver(dns), this.resolveTimeout);
var request = new Request
{
RecursionDesired = true,
OperationCode = OperationCode.Query
};
request.Questions.Add(new Question(new Domain(domain), RecordType.A));
var clientRequest = new ClientRequest(resolver, request);
var response = await clientRequest.Resolve(cancellationToken);
return response.AnswerRecords.OfType().Select(item => item.IPAddress).ToArray();
}
}
}