减少DnsClient的创建

This commit is contained in:
陈国伟 2021-07-19 15:22:30 +08:00
parent f220613178
commit 110e28bd88
2 changed files with 16 additions and 12 deletions

View File

@ -40,6 +40,11 @@ namespace FastGithub
return new IPEndPoint(address, this.Port); return new IPEndPoint(address, this.Port);
} }
public override string ToString()
{
return $"{this.IPAddress}:{this.Port}";
}
/// <summary> /// <summary>
/// 是否为本机ip /// 是否为本机ip
/// </summary> /// </summary>

View File

@ -1,6 +1,7 @@
using DNS.Client; using DNS.Client;
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System; using System;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
@ -14,8 +15,9 @@ namespace FastGithub.ReverseProxy
/// </summary> /// </summary>
sealed class DomainResolver sealed class DomainResolver
{ {
private DnsClient dnsClient;
private readonly IMemoryCache memoryCache; private readonly IMemoryCache memoryCache;
private readonly FastGithubConfig fastGithubConfig; private readonly IOptionsMonitor<FastGithubOptions> options;
private readonly ILogger<DomainResolver> logger; private readonly ILogger<DomainResolver> logger;
private readonly TimeSpan cacheTimeSpan = TimeSpan.FromSeconds(10d); private readonly TimeSpan cacheTimeSpan = TimeSpan.FromSeconds(10d);
@ -26,12 +28,15 @@ namespace FastGithub.ReverseProxy
/// <param name="fastGithubConfig"></param> /// <param name="fastGithubConfig"></param>
public DomainResolver( public DomainResolver(
IMemoryCache memoryCache, IMemoryCache memoryCache,
FastGithubConfig fastGithubConfig, IOptionsMonitor<FastGithubOptions> options,
ILogger<DomainResolver> logger) ILogger<DomainResolver> logger)
{ {
this.memoryCache = memoryCache; this.memoryCache = memoryCache;
this.fastGithubConfig = fastGithubConfig; this.options = options;
this.logger = logger; this.logger = logger;
this.dnsClient = new DnsClient(options.CurrentValue.FastDns.ToIPEndPoint());
options.OnChange(opt => this.dnsClient = new DnsClient(opt.FastDns.ToIPEndPoint()));
} }
/// <summary> /// <summary>
@ -63,32 +68,26 @@ namespace FastGithub.ReverseProxy
{ {
try try
{ {
var dns = this.fastGithubConfig.PureDns;
var dnsClient = new DnsClient(dns);
var addresses = await dnsClient.Lookup(domain, DNS.Protocol.RecordType.A, cancellationToken); var addresses = await dnsClient.Lookup(domain, DNS.Protocol.RecordType.A, cancellationToken);
var address = addresses?.FirstOrDefault(); var address = addresses?.FirstOrDefault();
if (address == null) if (address == null)
{ {
throw new FastGithubException($"dns({dns})解析不到{domain}的ip"); throw new Exception($"解析不到{domain}的ip");
} }
// 受干扰的dns常常返回127.0.0.1来阻断请求 // 受干扰的dns常常返回127.0.0.1来阻断请求
// 如果解析到的ip为本机ip会产生反向代理请求死循环 // 如果解析到的ip为本机ip会产生反向代理请求死循环
if (address.Equals(IPAddress.Loopback)) if (address.Equals(IPAddress.Loopback))
{ {
throw new FastGithubException($"dns({dns})被污染:解析{domain}为{address}"); throw new Exception($"dns被污染解析{domain}为{address}");
} }
this.logger.LogInformation($"[{domain}->{address}]"); this.logger.LogInformation($"[{domain}->{address}]");
return address; return address;
} }
catch (FastGithubException)
{
throw;
}
catch (Exception ex) catch (Exception ex)
{ {
var dns = this.fastGithubConfig.PureDns; var dns = this.options.CurrentValue.PureDns;
throw new FastGithubException($"dns({dns})服务器异常:{ex.Message}", ex); throw new FastGithubException($"dns({dns})服务器异常:{ex.Message}", ex);
} }
} }