From 7fccdba5cf8f0b2dc8f9572918b10a7b37e10f20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com> Date: Fri, 18 Jun 2021 17:10:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8HttpClientFactory?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../GithubMetaProvider.cs | 6 +-- .../IPAddressComProvider.cs | 6 +-- FastGithub.Scanner/FastGithub.Scanner.csproj | 3 +- FastGithub.Scanner/HttpClientFactory.cs | 41 ------------------- .../ScanMiddlewares/HttpsScanMiddleware.cs | 15 ++++--- .../ScannerServiceCollectionExtensions.cs | 22 +++++++++- 6 files changed, 36 insertions(+), 57 deletions(-) delete mode 100644 FastGithub.Scanner/HttpClientFactory.cs diff --git a/FastGithub.Scanner/DomainAddressProviders/GithubMetaProvider.cs b/FastGithub.Scanner/DomainAddressProviders/GithubMetaProvider.cs index d7605bc..8ad9bd1 100644 --- a/FastGithub.Scanner/DomainAddressProviders/GithubMetaProvider.cs +++ b/FastGithub.Scanner/DomainAddressProviders/GithubMetaProvider.cs @@ -19,7 +19,7 @@ namespace FastGithub.Scanner.DomainAddressProviders sealed class GithubMetaProvider : IDomainAddressProvider { private readonly IOptionsMonitor options; - private readonly HttpClientFactory httpClientFactory; + private readonly IHttpClientFactory httpClientFactory; private readonly ILogger logger; private const string META_URI = "https://api.github.com/meta"; @@ -35,7 +35,7 @@ namespace FastGithub.Scanner.DomainAddressProviders /// public GithubMetaProvider( IOptionsMonitor options, - HttpClientFactory httpClientFactory, + IHttpClientFactory httpClientFactory, ILogger logger) { this.options = options; @@ -57,7 +57,7 @@ namespace FastGithub.Scanner.DomainAddressProviders try { - using var httpClient = this.httpClientFactory.Create(); + var httpClient = this.httpClientFactory.CreateClient(nameof(FastGithub)); var meta = await this.GetMetaAsync(httpClient, setting.MetaUri); if (meta != null) { diff --git a/FastGithub.Scanner/DomainAddressProviders/IPAddressComProvider.cs b/FastGithub.Scanner/DomainAddressProviders/IPAddressComProvider.cs index 0d530b8..3e4ae26 100644 --- a/FastGithub.Scanner/DomainAddressProviders/IPAddressComProvider.cs +++ b/FastGithub.Scanner/DomainAddressProviders/IPAddressComProvider.cs @@ -18,7 +18,7 @@ namespace FastGithub.Scanner.DomainAddressProviders sealed class IPAddressComProvider : IDomainAddressProvider { private readonly IOptionsMonitor options; - private readonly HttpClientFactory httpClientFactory; + private readonly IHttpClientFactory httpClientFactory; private readonly ILogger logger; private readonly Uri lookupUri = new("https://www.ipaddress.com/ip-lookup"); @@ -34,7 +34,7 @@ namespace FastGithub.Scanner.DomainAddressProviders /// public IPAddressComProvider( IOptionsMonitor options, - HttpClientFactory httpClientFactory, + IHttpClientFactory httpClientFactory, ILogger logger) { this.options = options; @@ -54,7 +54,7 @@ namespace FastGithub.Scanner.DomainAddressProviders return Enumerable.Empty(); } - using var httpClient = this.httpClientFactory.Create(); + var httpClient = this.httpClientFactory.CreateClient(nameof(FastGithub)); var result = new HashSet(); foreach (var domain in setting.Domains) { diff --git a/FastGithub.Scanner/FastGithub.Scanner.csproj b/FastGithub.Scanner/FastGithub.Scanner.csproj index 48f7e49..f0f0cd2 100644 --- a/FastGithub.Scanner/FastGithub.Scanner.csproj +++ b/FastGithub.Scanner/FastGithub.Scanner.csproj @@ -8,7 +8,8 @@ - + + diff --git a/FastGithub.Scanner/HttpClientFactory.cs b/FastGithub.Scanner/HttpClientFactory.cs deleted file mode 100644 index de7db0e..0000000 --- a/FastGithub.Scanner/HttpClientFactory.cs +++ /dev/null @@ -1,41 +0,0 @@ -using Microsoft.Extensions.DependencyInjection; -using System.Net.Http; -using System.Net.Http.Headers; -using System.Reflection; - -namespace FastGithub.Scanner -{ - /// - /// HttpClient工厂 - /// - [Service(ServiceLifetime.Singleton)] - sealed class HttpClientFactory - { - /// - /// 程序集版本信息 - /// - private static readonly AssemblyName assemblyName = typeof(HttpClientFactory).Assembly.GetName(); - - /// - /// 请求头的默认UserAgent - /// - private readonly static ProductInfoHeaderValue defaultUserAgent = new(assemblyName.Name ?? "FastGithub", assemblyName.Version?.ToString()); - - /// - /// 创建httpClient - /// - /// - public HttpClient Create(bool allowAutoRedirect = true) - { - var httpClient = new HttpClient(new HttpClientHandler - { - Proxy = null, - UseProxy = false, - AllowAutoRedirect = allowAutoRedirect - }); - httpClient.DefaultRequestHeaders.Accept.TryParseAdd("*/*"); - httpClient.DefaultRequestHeaders.UserAgent.Add(defaultUserAgent); - return httpClient; - } - } -} diff --git a/FastGithub.Scanner/ScanMiddlewares/HttpsScanMiddleware.cs b/FastGithub.Scanner/ScanMiddlewares/HttpsScanMiddleware.cs index 8d55356..b5358a1 100644 --- a/FastGithub.Scanner/ScanMiddlewares/HttpsScanMiddleware.cs +++ b/FastGithub.Scanner/ScanMiddlewares/HttpsScanMiddleware.cs @@ -18,7 +18,7 @@ namespace FastGithub.Scanner.ScanMiddlewares sealed class HttpsScanMiddleware : IMiddleware { private readonly IOptionsMonitor options; - private readonly HttpClientFactory httpClientFactory; + private readonly IHttpClientFactory httpClientFactory; private readonly ILogger logger; /// @@ -28,7 +28,7 @@ namespace FastGithub.Scanner.ScanMiddlewares /// public HttpsScanMiddleware( IOptionsMonitor options, - HttpClientFactory httpClientFactory, + IHttpClientFactory httpClientFactory, ILogger logger) { this.options = options; @@ -53,15 +53,14 @@ namespace FastGithub.Scanner.ScanMiddlewares Method = HttpMethod.Head, RequestUri = new Uri($"https://{context.Address}"), }; - request.Headers.Host = context.Domain; - request.Headers.ConnectionClose = true; + request.Headers.Host = context.Domain; var timeout = this.options.CurrentValue.Scan.HttpsScanTimeout; using var cancellationTokenSource = new CancellationTokenSource(timeout); - using var httpClient = this.httpClientFactory.Create(allowAutoRedirect: false); + var httpClient = this.httpClientFactory.CreateClient(nameof(FastGithub)); using var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationTokenSource.Token); - this.VerifyHttpsResponse(context.Domain, response); + VerifyHttpsResponse(context.Domain, response); context.Available = true; await next(); @@ -84,7 +83,7 @@ namespace FastGithub.Scanner.ScanMiddlewares /// /// /// - private void VerifyHttpsResponse(string domain, HttpResponseMessage response) + private static void VerifyHttpsResponse(string domain, HttpResponseMessage response) { response.EnsureSuccessStatusCode(); @@ -103,7 +102,7 @@ namespace FastGithub.Scanner.ScanMiddlewares } } - private string GetInnerMessage(Exception ex) + private static string GetInnerMessage(Exception ex) { while (ex.InnerException != null) { diff --git a/FastGithub.Scanner/ScannerServiceCollectionExtensions.cs b/FastGithub.Scanner/ScannerServiceCollectionExtensions.cs index 4c174e0..73a3c6d 100644 --- a/FastGithub.Scanner/ScannerServiceCollectionExtensions.cs +++ b/FastGithub.Scanner/ScannerServiceCollectionExtensions.cs @@ -1,6 +1,9 @@ using FastGithub.Scanner; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using System; +using System.Net.Http; +using System.Net.Http.Headers; namespace FastGithub { @@ -18,7 +21,24 @@ namespace FastGithub public static IServiceCollection AddGithubScanner(this IServiceCollection services, IConfiguration configuration) { var assembly = typeof(ScannerServiceCollectionExtensions).Assembly; - return services + var defaultUserAgent = new ProductInfoHeaderValue(assembly.GetName().Name ?? nameof(FastGithub), assembly.GetName().Version?.ToString()); + + services + .AddHttpClient(nameof(FastGithub)) + .SetHandlerLifetime(TimeSpan.FromMinutes(10d)) + .ConfigureHttpClient(httpClient => + { + httpClient.DefaultRequestHeaders.Accept.TryParseAdd("*/*"); + httpClient.DefaultRequestHeaders.UserAgent.Add(defaultUserAgent); + }) + .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler + { + Proxy = null, + UseProxy = false, + AllowAutoRedirect = false + }); + + return services .AddServiceAndOptions(assembly, configuration) .AddHostedService() .AddHostedService()