Prechádzať zdrojové kódy

修复GithubHttpClientHanlder的bug

陈国伟 4 rokov pred
rodič
commit
eb9bdfe906

+ 3 - 6
FastGithub.Dns/GithubRequestResolver.cs

@@ -23,7 +23,6 @@ namespace FastGithub.Dns
     {
         private readonly IGithubScanResults githubScanResults;
         private readonly IOptionsMonitor<DnsOptions> options;
-        private readonly IOptionsMonitor<GithubLookupFactoryOptions> lookupOptions;
         private readonly IOptionsMonitor<GithubReverseProxyOptions> reverseProxyOptions;
         private readonly ILogger<GithubRequestResolver> logger;
 
@@ -36,13 +35,11 @@ namespace FastGithub.Dns
         public GithubRequestResolver(
             IGithubScanResults githubScanResults,
             IOptionsMonitor<DnsOptions> options,
-            IOptionsMonitor<GithubLookupFactoryOptions> lookupOptions,
             IOptionsMonitor<GithubReverseProxyOptions> reverseProxyOptions,
             ILogger<GithubRequestResolver> logger)
         {
             this.githubScanResults = githubScanResults;
             this.options = options;
-            this.lookupOptions = lookupOptions;
             this.reverseProxyOptions = reverseProxyOptions;
             this.logger = logger;
         }
@@ -64,7 +61,7 @@ namespace FastGithub.Dns
             }
 
             var domain = question.Name.ToString();
-            if (this.lookupOptions.CurrentValue.Domains.Contains(domain) == false)
+            if (this.githubScanResults.Support(domain) == false)
             {
                 return response;
             }
@@ -82,7 +79,7 @@ namespace FastGithub.Dns
             }
             else
             {
-                var address = await this.GetLocalHostAddress();
+                var address = await GetLocalHostAddress();
                 var record = new IPAddressResourceRecord(question.Name, address, TimeSpan.FromMinutes(1));
                 response.AnswerRecords.Add(record);
                 this.logger.LogInformation($"[{domain}->{address}]");
@@ -100,7 +97,7 @@ namespace FastGithub.Dns
         /// 获取本机ip
         /// </summary>
         /// <returns></returns>
-        private async Task<IPAddress> GetLocalHostAddress()
+        private static async Task<IPAddress> GetLocalHostAddress()
         {
             try
             {

+ 2 - 2
FastGithub.ReverseProxy/ReverseProxyApplicationBuilderExtensions.cs

@@ -23,13 +23,13 @@ namespace FastGithub
         {
             var httpForwarder = app.ApplicationServices.GetRequiredService<IHttpForwarder>();
             var httpClientHanlder = app.ApplicationServices.GetRequiredService<GithubHttpClientHanlder>();
-            var lookupOptions = app.ApplicationServices.GetRequiredService<IOptionsMonitor<GithubLookupFactoryOptions>>();
+            var scanResults = app.ApplicationServices.GetRequiredService<IGithubScanResults>();
             var options = app.ApplicationServices.GetRequiredService<IOptionsMonitor<GithubReverseProxyOptions>>();
 
             app.Use(next => async context =>
             {
                 var host = context.Request.Host.Host;
-                if (lookupOptions.CurrentValue.Domains.Contains(host) == false)
+                if (scanResults.Support(host) == false)
                 {
                     await context.Response.WriteAsJsonAsync(new { message = $"不支持以{host}访问" });
                 }

+ 38 - 4
FastGithub.Scanner/GithubHttpClientHanlder.cs

@@ -1,6 +1,8 @@
-using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Caching.Memory;
+using Microsoft.Extensions.DependencyInjection;
 using Microsoft.Extensions.Logging;
 using System;
+using System.Net;
 using System.Net.Http;
 using System.Net.Security;
 using System.Net.Sockets;
@@ -17,18 +19,22 @@ namespace FastGithub.Scanner
     {
         private readonly IGithubScanResults githubScanResults;
         private readonly ILogger<GithubHttpClientHanlder> logger;
+        private readonly IMemoryCache memoryCache;
 
         /// <summary>
         /// 请求github的HttpClientHandler
         /// </summary>
         /// <param name="githubScanResults"></param>
         /// <param name="logger"></param>
+        /// <param name="memoryCache"></param>
         public GithubHttpClientHanlder(
             IGithubScanResults githubScanResults,
-            ILogger<GithubHttpClientHanlder> logger)
+            ILogger<GithubHttpClientHanlder> logger,
+            IMemoryCache memoryCache)
         {
             this.githubScanResults = githubScanResults;
             this.logger = logger;
+            this.memoryCache = memoryCache;
             this.InnerHandler = CreateNoneSniHttpHandler();
         }
 
@@ -76,10 +82,9 @@ namespace FastGithub.Scanner
             var uri = request.RequestUri;
             if (uri != null && uri.HostNameType == UriHostNameType.Dns)
             {
-                var address = this.githubScanResults.FindBestAddress(uri.Host);
+                var address = this.Resolve(uri.Host);
                 if (address != null)
                 {
-                    this.logger.LogInformation($"使用{address} No SNI请求{uri.Host}");
                     var builder = new UriBuilder(uri)
                     {
                         Host = address.ToString()
@@ -90,5 +95,34 @@ namespace FastGithub.Scanner
             }
             return await base.SendAsync(request, cancellationToken);
         }
+
+
+        /// <summary>
+        /// 解析域名
+        /// </summary>
+        /// <param name="domain"></param>
+        /// <returns></returns>
+        private IPAddress? Resolve(string domain)
+        {
+            if (this.githubScanResults.Support(domain) == false)
+            {
+                return default;
+            }
+
+            var key = $"domain:{domain}";
+            var address = this.memoryCache.GetOrCreate(key, e =>
+            {
+                e.SetAbsoluteExpiration(TimeSpan.FromSeconds(1d));
+                return this.githubScanResults.FindBestAddress(domain);
+            });
+
+            if (address == null)
+            {
+                throw new HttpRequestException($"无法解析{domain}的ip");
+            }
+
+            this.logger.LogInformation($"使用{address} No SNI请求{domain}");
+            return address;
+        }
     }
 }

+ 1 - 1
FastGithub.Scanner/GithubLookupFactoryOptions.cs

@@ -6,7 +6,7 @@ namespace FastGithub.Scanner
     /// 域名
     /// </summary>
     [Options("Lookup")]
-    public class GithubLookupFactoryOptions
+    class GithubLookupFactoryOptions
     {
         /// <summary>
         /// 反查的域名

+ 8 - 20
FastGithub.Scanner/GithubScanResults.cs

@@ -1,7 +1,5 @@
-using Microsoft.Extensions.Caching.Memory;
-using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.DependencyInjection;
 using Microsoft.Extensions.Options;
-using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Net;
@@ -16,14 +14,10 @@ namespace FastGithub.Scanner
     {
         private readonly object syncRoot = new();
         private readonly List<GithubContext> contexts = new();
-        private readonly IMemoryCache memoryCache;
         private readonly IOptionsMonitor<GithubLookupFactoryOptions> options;
 
-        public GithubScanResults(
-            IMemoryCache memoryCache,
-            IOptionsMonitor<GithubLookupFactoryOptions> options)
+        public GithubScanResults(IOptionsMonitor<GithubLookupFactoryOptions> options)
         {
-            this.memoryCache = memoryCache;
             this.options = options;
         }
 
@@ -58,29 +52,23 @@ namespace FastGithub.Scanner
         }
 
         /// <summary>
-        /// 查找最优的ip
+        /// 是否支持指定域名
         /// </summary>
         /// <param name="domain"></param>
         /// <returns></returns>
-        public IPAddress? FindBestAddress(string domain)
+        public bool Support(string domain)
         {
-            var key = $"domain:{domain}";
-            return this.memoryCache.GetOrCreate(key, e =>
-            {
-                e.SetAbsoluteExpiration(TimeSpan.FromSeconds(1d));
-                return this.Resolve(domain);
-            });
+            return this.options.CurrentValue.Domains.Contains(domain);
         }
 
-
         /// <summary>
-        /// 解析域名
+        /// 查找最优的ip
         /// </summary>
         /// <param name="domain"></param>
         /// <returns></returns>
-        private IPAddress? Resolve(string domain)
+        public IPAddress? FindBestAddress(string domain)
         {
-            if (this.options.CurrentValue.Domains.Contains(domain) == false)
+            if (this.Support(domain) == false)
             {
                 return default;
             }

+ 7 - 0
FastGithub.Scanner/IGithubScanResults.cs

@@ -7,6 +7,13 @@ namespace FastGithub.Scanner
     /// </summary>
     public interface IGithubScanResults
     {
+        /// <summary>
+        /// 是否支持指定域名
+        /// </summary>
+        /// <param name="domain"></param>
+        /// <returns></returns>
+        bool Support(string domain);
+
         /// <summary>
         /// 查找最优的ip
         /// </summary>

+ 1 - 1
FastGithub.Scanner/LookupProviders/GithubMetaProvider.cs

@@ -92,7 +92,7 @@ namespace FastGithub.Scanner.LookupProviders
             catch (Exception)
             {
                 cancellationToken.ThrowIfCancellationRequested();
-                this.logger.LogWarning($"当前网络无法从{META_URI}加载github维护的ip数据,{Environment.NewLine}本轮扫描暂时使用{metaUri}的副本数据");
+                this.logger.LogWarning($"使用{metaUri}的副本数据");
                 return await httpClient.GetFromJsonAsync<Meta>(metaUri, cancellationToken);
             }
         }

+ 1 - 2
FastGithub/FastGithub.csproj

@@ -4,8 +4,7 @@
 		<OutputType>Exe</OutputType>
 		<TargetFrameworks>net5.0;net6.0</TargetFrameworks>
 		<PackageLicenseExpression>MIT</PackageLicenseExpression>
-		<ApplicationIcon>app.ico</ApplicationIcon>
-		<ServerGarbageCollection>false</ServerGarbageCollection>
+		<ApplicationIcon>app.ico</ApplicationIcon> 
 		<IsWebConfigTransformDisabled>true</IsWebConfigTransformDisabled>
 	</PropertyGroup>