Răsfoiți Sursa

增加dns预加载功能

陈国伟 3 ani în urmă
părinte
comite
03254aa0d9

+ 9 - 0
FastGithub.DomainResolve/DnsClient.cs

@@ -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>

+ 9 - 0
FastGithub.DomainResolve/DomainResolver.cs

@@ -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>

+ 6 - 0
FastGithub.DomainResolve/IDomainResolver.cs

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

+ 1 - 1
FastGithub.Http/HttpClientHandler.cs

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

+ 10 - 1
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<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);

+ 1 - 0
FastGithub.PacketIntercept/FastGithub.PacketIntercept.csproj

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