From 03254aa0d9d3ae0ca0547959518f7e95d5cef9ab Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com>
Date: Thu, 30 Sep 2021 09:23:57 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0dns=E9=A2=84=E5=8A=A0?=
=?UTF-8?q?=E8=BD=BD=E5=8A=9F=E8=83=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
FastGithub.DomainResolve/DnsClient.cs | 9 +++++++++
FastGithub.DomainResolve/DomainResolver.cs | 9 +++++++++
FastGithub.DomainResolve/IDomainResolver.cs | 6 ++++++
FastGithub.Http/HttpClientHandler.cs | 2 +-
FastGithub.PacketIntercept/Dns/DnsInterceptor.cs | 11 ++++++++++-
.../FastGithub.PacketIntercept.csproj | 1 +
6 files changed, 36 insertions(+), 2 deletions(-)
diff --git a/FastGithub.DomainResolve/DnsClient.cs b/FastGithub.DomainResolve/DnsClient.cs
index 330d34f..2d0807d 100644
--- a/FastGithub.DomainResolve/DnsClient.cs
+++ b/FastGithub.DomainResolve/DnsClient.cs
@@ -54,6 +54,15 @@ namespace FastGithub.DomainResolve
this.logger = logger;
}
+ ///
+ /// 预加载
+ ///
+ /// 域名
+ public void Prefetch(string domain)
+ {
+ this.domainIPAddressCollection.TryAdd(domain, new IPAddressCollection());
+ }
+
///
/// 解析域名
///
diff --git a/FastGithub.DomainResolve/DomainResolver.cs b/FastGithub.DomainResolve/DomainResolver.cs
index 67e24c5..e65fb14 100644
--- a/FastGithub.DomainResolve/DomainResolver.cs
+++ b/FastGithub.DomainResolve/DomainResolver.cs
@@ -22,6 +22,15 @@ namespace FastGithub.DomainResolve
this.dnsClient = dnsClient;
}
+ ///
+ /// 载加载
+ ///
+ /// 域名
+ public void Prefetch(string domain)
+ {
+ this.dnsClient.Prefetch(domain);
+ }
+
///
/// 解析ip
///
diff --git a/FastGithub.DomainResolve/IDomainResolver.cs b/FastGithub.DomainResolve/IDomainResolver.cs
index e651d0f..4a1f4a7 100644
--- a/FastGithub.DomainResolve/IDomainResolver.cs
+++ b/FastGithub.DomainResolve/IDomainResolver.cs
@@ -10,6 +10,12 @@ namespace FastGithub.DomainResolve
///
public interface IDomainResolver
{
+ ///
+ /// 载加载
+ ///
+ /// 域名
+ void Prefetch(string domain);
+
///
/// 解析ip
///
diff --git a/FastGithub.Http/HttpClientHandler.cs b/FastGithub.Http/HttpClientHandler.cs
index 2b23dab..2ae92a8 100644
--- a/FastGithub.Http/HttpClientHandler.cs
+++ b/FastGithub.Http/HttpClientHandler.cs
@@ -120,7 +120,7 @@ namespace FastGithub.Http
}
}
- throw new AggregateException("没有可连接成功的IP", innerExceptions);
+ throw new AggregateException("找不到任何可成功连接的IP", innerExceptions);
}
///
diff --git a/FastGithub.PacketIntercept/Dns/DnsInterceptor.cs b/FastGithub.PacketIntercept/Dns/DnsInterceptor.cs
index 6a8a9ca..da281aa 100644
--- a/FastGithub.PacketIntercept/Dns/DnsInterceptor.cs
+++ b/FastGithub.PacketIntercept/Dns/DnsInterceptor.cs
@@ -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 logger;
+
private readonly TimeSpan ttl = TimeSpan.FromMinutes(10d);
///
@@ -37,16 +41,18 @@ namespace FastGithub.PacketIntercept.Dns
/// dns拦截器
///
///
+ ///
///
///
public DnsInterceptor(
FastGithubConfig fastGithubConfig,
+ IDomainResolver domainResolver,
ILogger logger,
IOptionsMonitor 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);
diff --git a/FastGithub.PacketIntercept/FastGithub.PacketIntercept.csproj b/FastGithub.PacketIntercept/FastGithub.PacketIntercept.csproj
index 2c8b98c..d3fca22 100644
--- a/FastGithub.PacketIntercept/FastGithub.PacketIntercept.csproj
+++ b/FastGithub.PacketIntercept/FastGithub.PacketIntercept.csproj
@@ -14,5 +14,6 @@
+