增加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; this.logger = logger;
} }
/// <summary>
/// 预加载
/// </summary>
/// <param name="domain">域名</param>
public void Prefetch(string domain)
{
this.domainIPAddressCollection.TryAdd(domain, new IPAddressCollection());
}
/// <summary> /// <summary>
/// 解析域名 /// 解析域名
/// </summary> /// </summary>

View File

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

View File

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

View File

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

View File

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

View File

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