常量化

This commit is contained in:
老九 2021-09-26 21:03:40 +08:00
parent e58cb37b9b
commit abafeed7f5

View File

@ -19,6 +19,8 @@ namespace FastGithub.DomainResolve
/// </summary> /// </summary>
sealed class DnsClient sealed class DnsClient
{ {
private const int DNS_PORT = 53;
private const string LOCALHOST = "localhost";
private readonly ILogger<DnsClient> logger; private readonly ILogger<DnsClient> logger;
private readonly ConcurrentDictionary<string, SemaphoreSlim> semaphoreSlims = new(); private readonly ConcurrentDictionary<string, SemaphoreSlim> semaphoreSlims = new();
@ -56,13 +58,13 @@ namespace FastGithub.DomainResolve
this.dnsCache.Set(key, value, this.dnsExpiration); this.dnsCache.Set(key, value, this.dnsExpiration);
var items = string.Join(", ", value.Select(item => item.ToString())); var items = string.Join(", ", value.Select(item => item.ToString()));
this.logger.LogInformation($"{dns}{domain}->[{items}]"); this.logger.LogInformation($"dns://{dns}{domain}->[{items}]");
} }
return value; return value;
} }
catch (Exception ex) catch (Exception ex)
{ {
this.logger.LogWarning($"{dns}无法解析{domain}{ex.Message}"); this.logger.LogWarning($"dns://{dns}无法解析{domain}{ex.Message}");
return Array.Empty<IPAddress>(); return Array.Empty<IPAddress>();
} }
finally finally
@ -80,12 +82,12 @@ namespace FastGithub.DomainResolve
/// <returns></returns> /// <returns></returns>
private async Task<IPAddress[]> LookupCoreAsync(IPEndPoint dns, string domain, CancellationToken cancellationToken = default) private async Task<IPAddress[]> LookupCoreAsync(IPEndPoint dns, string domain, CancellationToken cancellationToken = default)
{ {
if (domain == "localhost") if (domain == LOCALHOST)
{ {
return new[] { IPAddress.Loopback }; return new[] { IPAddress.Loopback };
} }
var resolver = dns.Port == 53 var resolver = dns.Port == DNS_PORT
? (IRequestResolver)new TcpRequestResolver(dns) ? (IRequestResolver)new TcpRequestResolver(dns)
: new UdpRequestResolver(dns, new TcpRequestResolver(dns), this.resolveTimeout); : new UdpRequestResolver(dns, new TcpRequestResolver(dns), this.resolveTimeout);