From eb9bdfe9069669a6c9d516f5681a5ba952e9a730 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, 15 Jul 2021 11:55:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DGithubHttpClientHanlder?= =?UTF-8?q?=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.Dns/GithubRequestResolver.cs | 9 ++-- ...everseProxyApplicationBuilderExtensions.cs | 4 +- FastGithub.Scanner/GithubHttpClientHanlder.cs | 42 +++++++++++++++++-- .../GithubLookupFactoryOptions.cs | 2 +- FastGithub.Scanner/GithubScanResults.cs | 38 ++++++----------- FastGithub.Scanner/IGithubScanResults.cs | 7 ++++ .../LookupProviders/GithubMetaProvider.cs | 2 +- FastGithub/FastGithub.csproj | 3 +- 8 files changed, 66 insertions(+), 41 deletions(-) diff --git a/FastGithub.Dns/GithubRequestResolver.cs b/FastGithub.Dns/GithubRequestResolver.cs index 84e7a8b..c871298 100644 --- a/FastGithub.Dns/GithubRequestResolver.cs +++ b/FastGithub.Dns/GithubRequestResolver.cs @@ -23,7 +23,6 @@ namespace FastGithub.Dns { private readonly IGithubScanResults githubScanResults; private readonly IOptionsMonitor options; - private readonly IOptionsMonitor lookupOptions; private readonly IOptionsMonitor reverseProxyOptions; private readonly ILogger logger; @@ -36,13 +35,11 @@ namespace FastGithub.Dns public GithubRequestResolver( IGithubScanResults githubScanResults, IOptionsMonitor options, - IOptionsMonitor lookupOptions, IOptionsMonitor reverseProxyOptions, ILogger 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 /// /// - private async Task GetLocalHostAddress() + private static async Task GetLocalHostAddress() { try { diff --git a/FastGithub.ReverseProxy/ReverseProxyApplicationBuilderExtensions.cs b/FastGithub.ReverseProxy/ReverseProxyApplicationBuilderExtensions.cs index 908b827..74150b2 100644 --- a/FastGithub.ReverseProxy/ReverseProxyApplicationBuilderExtensions.cs +++ b/FastGithub.ReverseProxy/ReverseProxyApplicationBuilderExtensions.cs @@ -23,13 +23,13 @@ namespace FastGithub { var httpForwarder = app.ApplicationServices.GetRequiredService(); var httpClientHanlder = app.ApplicationServices.GetRequiredService(); - var lookupOptions = app.ApplicationServices.GetRequiredService>(); + var scanResults = app.ApplicationServices.GetRequiredService(); var options = app.ApplicationServices.GetRequiredService>(); 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}访问" }); } diff --git a/FastGithub.Scanner/GithubHttpClientHanlder.cs b/FastGithub.Scanner/GithubHttpClientHanlder.cs index 907215a..21b36aa 100644 --- a/FastGithub.Scanner/GithubHttpClientHanlder.cs +++ b/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 logger; + private readonly IMemoryCache memoryCache; /// /// 请求github的HttpClientHandler /// /// /// + /// public GithubHttpClientHanlder( IGithubScanResults githubScanResults, - ILogger logger) + ILogger 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); } + + + /// + /// 解析域名 + /// + /// + /// + 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; + } } } diff --git a/FastGithub.Scanner/GithubLookupFactoryOptions.cs b/FastGithub.Scanner/GithubLookupFactoryOptions.cs index da8e753..0530441 100644 --- a/FastGithub.Scanner/GithubLookupFactoryOptions.cs +++ b/FastGithub.Scanner/GithubLookupFactoryOptions.cs @@ -6,7 +6,7 @@ namespace FastGithub.Scanner /// 域名 /// [Options("Lookup")] - public class GithubLookupFactoryOptions + class GithubLookupFactoryOptions { /// /// 反查的域名 diff --git a/FastGithub.Scanner/GithubScanResults.cs b/FastGithub.Scanner/GithubScanResults.cs index abb1d02..4852252 100644 --- a/FastGithub.Scanner/GithubScanResults.cs +++ b/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 contexts = new(); - private readonly IMemoryCache memoryCache; private readonly IOptionsMonitor options; - public GithubScanResults( - IMemoryCache memoryCache, - IOptionsMonitor options) + public GithubScanResults(IOptionsMonitor options) { - this.memoryCache = memoryCache; this.options = options; } @@ -57,6 +51,16 @@ namespace FastGithub.Scanner } } + /// + /// 是否支持指定域名 + /// + /// + /// + public bool Support(string domain) + { + return this.options.CurrentValue.Domains.Contains(domain); + } + /// /// 查找最优的ip /// @@ -64,23 +68,7 @@ namespace FastGithub.Scanner /// public IPAddress? FindBestAddress(string domain) { - var key = $"domain:{domain}"; - return this.memoryCache.GetOrCreate(key, e => - { - e.SetAbsoluteExpiration(TimeSpan.FromSeconds(1d)); - return this.Resolve(domain); - }); - } - - - /// - /// 解析域名 - /// - /// - /// - private IPAddress? Resolve(string domain) - { - if (this.options.CurrentValue.Domains.Contains(domain) == false) + if (this.Support(domain) == false) { return default; } diff --git a/FastGithub.Scanner/IGithubScanResults.cs b/FastGithub.Scanner/IGithubScanResults.cs index 0dc1134..2a259db 100644 --- a/FastGithub.Scanner/IGithubScanResults.cs +++ b/FastGithub.Scanner/IGithubScanResults.cs @@ -7,6 +7,13 @@ namespace FastGithub.Scanner /// public interface IGithubScanResults { + /// + /// 是否支持指定域名 + /// + /// + /// + bool Support(string domain); + /// /// 查找最优的ip /// diff --git a/FastGithub.Scanner/LookupProviders/GithubMetaProvider.cs b/FastGithub.Scanner/LookupProviders/GithubMetaProvider.cs index 5191e96..62ddba8 100644 --- a/FastGithub.Scanner/LookupProviders/GithubMetaProvider.cs +++ b/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(metaUri, cancellationToken); } } diff --git a/FastGithub/FastGithub.csproj b/FastGithub/FastGithub.csproj index 975dc1f..63dcf0e 100644 --- a/FastGithub/FastGithub.csproj +++ b/FastGithub/FastGithub.csproj @@ -4,8 +4,7 @@ Exe net5.0;net6.0 MIT - app.ico - false + app.ico true