增加dns预加载功能

This commit is contained in:
陈国伟 2021-09-30 09:23:57 +08:00
parent daef121bf2
commit 03254aa0d9
6 changed files with 36 additions and 2 deletions

View File

@ -54,6 +54,15 @@ namespace FastGithub.DomainResolve
this.logger = logger;
}
/// <summary>
/// 预加载
/// </summary>
/// <param name="domain">域名</param>
public void Prefetch(string domain)
{
this.domainIPAddressCollection.TryAdd(domain, new IPAddressCollection());
}
/// <summary>
/// 解析域名
/// </summary>

View File

@ -22,6 +22,15 @@ namespace FastGithub.DomainResolve
this.dnsClient = dnsClient;
}
/// <summary>
/// 载加载
/// </summary>
/// <param name="domain">域名</param>
public void Prefetch(string domain)
{
this.dnsClient.Prefetch(domain);
}
/// <summary>
/// 解析ip
/// </summary>

View File

@ -10,6 +10,12 @@ namespace FastGithub.DomainResolve
/// </summary>
public interface IDomainResolver
{
/// <summary>
/// 载加载
/// </summary>
/// <param name="domain">域名</param>
void Prefetch(string domain);
/// <summary>
/// 解析ip
/// </summary>

View File

@ -120,7 +120,7 @@ namespace FastGithub.Http
}
}
throw new AggregateException("没有可连接成功的IP", innerExceptions);
throw new AggregateException("找不到任何可成功连接的IP", innerExceptions);
}
/// <summary>

View File

@ -1,6 +1,7 @@
using DNS.Protocol;
using DNS.Protocol.ResourceRecords;
using FastGithub.Configuration;
using FastGithub.DomainResolve;
using FastGithub.WinDiverts;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
@ -23,8 +24,11 @@ namespace FastGithub.PacketIntercept.Dns
sealed class DnsInterceptor : IDnsInterceptor
{
private const string DNS_FILTER = "udp.DstPort == 53";
private readonly FastGithubConfig fastGithubConfig;
private readonly IDomainResolver domainResolver;
private readonly ILogger<DnsInterceptor> logger;
private readonly TimeSpan ttl = TimeSpan.FromMinutes(10d);
/// <summary>
@ -37,16 +41,18 @@ namespace FastGithub.PacketIntercept.Dns
/// dns拦截器
/// </summary>
/// <param name="fastGithubConfig"></param>
/// <param name="domainResolver"></param>
/// <param name="logger"></param>
/// <param name="options"></param>
public DnsInterceptor(
FastGithubConfig fastGithubConfig,
IDomainResolver domainResolver,
ILogger<DnsInterceptor> logger,
IOptionsMonitor<FastGithubOptions> options)
{
this.fastGithubConfig = fastGithubConfig;
this.logger = logger;
this.domainResolver = domainResolver;
options.OnChange(_ => DnsFlushResolverCache());
}
@ -129,6 +135,9 @@ namespace FastGithub.PacketIntercept.Dns
return;
}
// dns预加载
this.domainResolver.Prefetch(domain.ToString());
// dns响应数据
var response = Response.FromRequest(request);
var record = new IPAddressResourceRecord(domain, IPAddress.Loopback, this.ttl);

View File

@ -14,5 +14,6 @@
<ItemGroup>
<ProjectReference Include="..\FastGithub.Configuration\FastGithub.Configuration.csproj" />
<ProjectReference Include="..\FastGithub.DomainResolve\FastGithub.DomainResolve.csproj" />
</ItemGroup>
</Project>