使用HttpClientFactory
This commit is contained in:
parent
f787707494
commit
7fccdba5cf
@ -19,7 +19,7 @@ namespace FastGithub.Scanner.DomainAddressProviders
|
||||
sealed class GithubMetaProvider : IDomainAddressProvider
|
||||
{
|
||||
private readonly IOptionsMonitor<GithubOptions> options;
|
||||
private readonly HttpClientFactory httpClientFactory;
|
||||
private readonly IHttpClientFactory httpClientFactory;
|
||||
private readonly ILogger<GithubMetaProvider> logger;
|
||||
private const string META_URI = "https://api.github.com/meta";
|
||||
|
||||
@ -35,7 +35,7 @@ namespace FastGithub.Scanner.DomainAddressProviders
|
||||
/// <param name="logger"></param>
|
||||
public GithubMetaProvider(
|
||||
IOptionsMonitor<GithubOptions> options,
|
||||
HttpClientFactory httpClientFactory,
|
||||
IHttpClientFactory httpClientFactory,
|
||||
ILogger<GithubMetaProvider> 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)
|
||||
{
|
||||
|
||||
@ -18,7 +18,7 @@ namespace FastGithub.Scanner.DomainAddressProviders
|
||||
sealed class IPAddressComProvider : IDomainAddressProvider
|
||||
{
|
||||
private readonly IOptionsMonitor<GithubOptions> options;
|
||||
private readonly HttpClientFactory httpClientFactory;
|
||||
private readonly IHttpClientFactory httpClientFactory;
|
||||
private readonly ILogger<IPAddressComProvider> logger;
|
||||
private readonly Uri lookupUri = new("https://www.ipaddress.com/ip-lookup");
|
||||
|
||||
@ -34,7 +34,7 @@ namespace FastGithub.Scanner.DomainAddressProviders
|
||||
/// <param name="logger"></param>
|
||||
public IPAddressComProvider(
|
||||
IOptionsMonitor<GithubOptions> options,
|
||||
HttpClientFactory httpClientFactory,
|
||||
IHttpClientFactory httpClientFactory,
|
||||
ILogger<IPAddressComProvider> logger)
|
||||
{
|
||||
this.options = options;
|
||||
@ -54,7 +54,7 @@ namespace FastGithub.Scanner.DomainAddressProviders
|
||||
return Enumerable.Empty<DomainAddress>();
|
||||
}
|
||||
|
||||
using var httpClient = this.httpClientFactory.Create();
|
||||
var httpClient = this.httpClientFactory.CreateClient(nameof(FastGithub));
|
||||
var result = new HashSet<DomainAddress>();
|
||||
foreach (var domain in setting.Domains)
|
||||
{
|
||||
|
||||
@ -9,6 +9,7 @@
|
||||
<PackageReference Include="DNS" Version="6.1.0" />
|
||||
<PackageReference Include="IPNetwork2" Version="2.5.320" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Http" Version="5.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -18,7 +18,7 @@ namespace FastGithub.Scanner.ScanMiddlewares
|
||||
sealed class HttpsScanMiddleware : IMiddleware<GithubContext>
|
||||
{
|
||||
private readonly IOptionsMonitor<GithubOptions> options;
|
||||
private readonly HttpClientFactory httpClientFactory;
|
||||
private readonly IHttpClientFactory httpClientFactory;
|
||||
private readonly ILogger<HttpsScanMiddleware> logger;
|
||||
|
||||
/// <summary>
|
||||
@ -28,7 +28,7 @@ namespace FastGithub.Scanner.ScanMiddlewares
|
||||
/// <param name="logger"></param>
|
||||
public HttpsScanMiddleware(
|
||||
IOptionsMonitor<GithubOptions> options,
|
||||
HttpClientFactory httpClientFactory,
|
||||
IHttpClientFactory httpClientFactory,
|
||||
ILogger<HttpsScanMiddleware> logger)
|
||||
{
|
||||
this.options = options;
|
||||
@ -54,14 +54,13 @@ namespace FastGithub.Scanner.ScanMiddlewares
|
||||
RequestUri = new Uri($"https://{context.Address}"),
|
||||
};
|
||||
request.Headers.Host = context.Domain;
|
||||
request.Headers.ConnectionClose = true;
|
||||
|
||||
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
|
||||
/// <param name="response"></param>
|
||||
/// <exception cref="HttpRequestException"></exception>
|
||||
/// <exception cref="ValidationException"></exception>
|
||||
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)
|
||||
{
|
||||
|
||||
@ -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,6 +21,23 @@ namespace FastGithub
|
||||
public static IServiceCollection AddGithubScanner(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
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
|
||||
.AddServiceAndOptions(assembly, configuration)
|
||||
.AddHostedService<GithubFullScanHostedService>()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user