使用HttpClientFactory

This commit is contained in:
陈国伟 2021-06-18 17:10:16 +08:00
parent f787707494
commit 7fccdba5cf
6 changed files with 36 additions and 57 deletions

View File

@ -19,7 +19,7 @@ namespace FastGithub.Scanner.DomainAddressProviders
sealed class GithubMetaProvider : IDomainAddressProvider sealed class GithubMetaProvider : IDomainAddressProvider
{ {
private readonly IOptionsMonitor<GithubOptions> options; private readonly IOptionsMonitor<GithubOptions> options;
private readonly HttpClientFactory httpClientFactory; private readonly IHttpClientFactory httpClientFactory;
private readonly ILogger<GithubMetaProvider> logger; private readonly ILogger<GithubMetaProvider> logger;
private const string META_URI = "https://api.github.com/meta"; private const string META_URI = "https://api.github.com/meta";
@ -35,7 +35,7 @@ namespace FastGithub.Scanner.DomainAddressProviders
/// <param name="logger"></param> /// <param name="logger"></param>
public GithubMetaProvider( public GithubMetaProvider(
IOptionsMonitor<GithubOptions> options, IOptionsMonitor<GithubOptions> options,
HttpClientFactory httpClientFactory, IHttpClientFactory httpClientFactory,
ILogger<GithubMetaProvider> logger) ILogger<GithubMetaProvider> logger)
{ {
this.options = options; this.options = options;
@ -57,7 +57,7 @@ namespace FastGithub.Scanner.DomainAddressProviders
try try
{ {
using var httpClient = this.httpClientFactory.Create(); var httpClient = this.httpClientFactory.CreateClient(nameof(FastGithub));
var meta = await this.GetMetaAsync(httpClient, setting.MetaUri); var meta = await this.GetMetaAsync(httpClient, setting.MetaUri);
if (meta != null) if (meta != null)
{ {

View File

@ -18,7 +18,7 @@ namespace FastGithub.Scanner.DomainAddressProviders
sealed class IPAddressComProvider : IDomainAddressProvider sealed class IPAddressComProvider : IDomainAddressProvider
{ {
private readonly IOptionsMonitor<GithubOptions> options; private readonly IOptionsMonitor<GithubOptions> options;
private readonly HttpClientFactory httpClientFactory; private readonly IHttpClientFactory httpClientFactory;
private readonly ILogger<IPAddressComProvider> logger; private readonly ILogger<IPAddressComProvider> logger;
private readonly Uri lookupUri = new("https://www.ipaddress.com/ip-lookup"); private readonly Uri lookupUri = new("https://www.ipaddress.com/ip-lookup");
@ -34,7 +34,7 @@ namespace FastGithub.Scanner.DomainAddressProviders
/// <param name="logger"></param> /// <param name="logger"></param>
public IPAddressComProvider( public IPAddressComProvider(
IOptionsMonitor<GithubOptions> options, IOptionsMonitor<GithubOptions> options,
HttpClientFactory httpClientFactory, IHttpClientFactory httpClientFactory,
ILogger<IPAddressComProvider> logger) ILogger<IPAddressComProvider> logger)
{ {
this.options = options; this.options = options;
@ -54,7 +54,7 @@ namespace FastGithub.Scanner.DomainAddressProviders
return Enumerable.Empty<DomainAddress>(); return Enumerable.Empty<DomainAddress>();
} }
using var httpClient = this.httpClientFactory.Create(); var httpClient = this.httpClientFactory.CreateClient(nameof(FastGithub));
var result = new HashSet<DomainAddress>(); var result = new HashSet<DomainAddress>();
foreach (var domain in setting.Domains) foreach (var domain in setting.Domains)
{ {

View File

@ -9,6 +9,7 @@
<PackageReference Include="DNS" Version="6.1.0" /> <PackageReference Include="DNS" Version="6.1.0" />
<PackageReference Include="IPNetwork2" Version="2.5.320" /> <PackageReference Include="IPNetwork2" Version="2.5.320" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" /> <PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="5.0.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -1,41 +0,0 @@
using Microsoft.Extensions.DependencyInjection;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
namespace FastGithub.Scanner
{
/// <summary>
/// HttpClient工厂
/// </summary>
[Service(ServiceLifetime.Singleton)]
sealed class HttpClientFactory
{
/// <summary>
/// 程序集版本信息
/// </summary>
private static readonly AssemblyName assemblyName = typeof(HttpClientFactory).Assembly.GetName();
/// <summary>
/// 请求头的默认UserAgent
/// </summary>
private readonly static ProductInfoHeaderValue defaultUserAgent = new(assemblyName.Name ?? "FastGithub", assemblyName.Version?.ToString());
/// <summary>
/// 创建httpClient
/// </summary>
/// <returns></returns>
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;
}
}
}

View File

@ -18,7 +18,7 @@ namespace FastGithub.Scanner.ScanMiddlewares
sealed class HttpsScanMiddleware : IMiddleware<GithubContext> sealed class HttpsScanMiddleware : IMiddleware<GithubContext>
{ {
private readonly IOptionsMonitor<GithubOptions> options; private readonly IOptionsMonitor<GithubOptions> options;
private readonly HttpClientFactory httpClientFactory; private readonly IHttpClientFactory httpClientFactory;
private readonly ILogger<HttpsScanMiddleware> logger; private readonly ILogger<HttpsScanMiddleware> logger;
/// <summary> /// <summary>
@ -28,7 +28,7 @@ namespace FastGithub.Scanner.ScanMiddlewares
/// <param name="logger"></param> /// <param name="logger"></param>
public HttpsScanMiddleware( public HttpsScanMiddleware(
IOptionsMonitor<GithubOptions> options, IOptionsMonitor<GithubOptions> options,
HttpClientFactory httpClientFactory, IHttpClientFactory httpClientFactory,
ILogger<HttpsScanMiddleware> logger) ILogger<HttpsScanMiddleware> logger)
{ {
this.options = options; this.options = options;
@ -54,14 +54,13 @@ namespace FastGithub.Scanner.ScanMiddlewares
RequestUri = new Uri($"https://{context.Address}"), RequestUri = new Uri($"https://{context.Address}"),
}; };
request.Headers.Host = context.Domain; request.Headers.Host = context.Domain;
request.Headers.ConnectionClose = true;
var timeout = this.options.CurrentValue.Scan.HttpsScanTimeout; var timeout = this.options.CurrentValue.Scan.HttpsScanTimeout;
using var cancellationTokenSource = new CancellationTokenSource(timeout); 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); using var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationTokenSource.Token);
this.VerifyHttpsResponse(context.Domain, response); VerifyHttpsResponse(context.Domain, response);
context.Available = true; context.Available = true;
await next(); await next();
@ -84,7 +83,7 @@ namespace FastGithub.Scanner.ScanMiddlewares
/// <param name="response"></param> /// <param name="response"></param>
/// <exception cref="HttpRequestException"></exception> /// <exception cref="HttpRequestException"></exception>
/// <exception cref="ValidationException"></exception> /// <exception cref="ValidationException"></exception>
private void VerifyHttpsResponse(string domain, HttpResponseMessage response) private static void VerifyHttpsResponse(string domain, HttpResponseMessage response)
{ {
response.EnsureSuccessStatusCode(); 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) while (ex.InnerException != null)
{ {

View File

@ -1,6 +1,9 @@
using FastGithub.Scanner; using FastGithub.Scanner;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
namespace FastGithub namespace FastGithub
{ {
@ -18,6 +21,23 @@ namespace FastGithub
public static IServiceCollection AddGithubScanner(this IServiceCollection services, IConfiguration configuration) public static IServiceCollection AddGithubScanner(this IServiceCollection services, IConfiguration configuration)
{ {
var assembly = typeof(ScannerServiceCollectionExtensions).Assembly; var assembly = typeof(ScannerServiceCollectionExtensions).Assembly;
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 return services
.AddServiceAndOptions(assembly, configuration) .AddServiceAndOptions(assembly, configuration)
.AddHostedService<GithubFullScanHostedService>() .AddHostedService<GithubFullScanHostedService>()