增加HttpClientFactory
This commit is contained in:
parent
f047aa2059
commit
b61b469168
@ -19,6 +19,7 @@ namespace FastGithub.Scanner.DomainAddressProviders
|
||||
sealed class GithubMetaProvider : IDomainAddressProvider
|
||||
{
|
||||
private readonly IOptionsMonitor<GithubOptions> options;
|
||||
private readonly HttpClientFactory httpClientFactory;
|
||||
private readonly ILogger<GithubMetaProvider> logger;
|
||||
private const string META_URI = "https://api.github.com/meta";
|
||||
|
||||
@ -29,9 +30,11 @@ namespace FastGithub.Scanner.DomainAddressProviders
|
||||
/// <param name="logger"></param>
|
||||
public GithubMetaProvider(
|
||||
IOptionsMonitor<GithubOptions> options,
|
||||
HttpClientFactory httpClientFactory,
|
||||
ILogger<GithubMetaProvider> logger)
|
||||
{
|
||||
this.options = options;
|
||||
this.httpClientFactory = httpClientFactory;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
@ -49,7 +52,7 @@ namespace FastGithub.Scanner.DomainAddressProviders
|
||||
|
||||
try
|
||||
{
|
||||
using var httpClient = new HttpClient();
|
||||
using var httpClient = this.httpClientFactory.Create();
|
||||
var meta = await this.GetMetaAsync(httpClient, setting.MetaUri);
|
||||
if (meta != null)
|
||||
{
|
||||
@ -91,6 +94,9 @@ namespace FastGithub.Scanner.DomainAddressProviders
|
||||
[JsonPropertyName("web")]
|
||||
public string[] Web { get; set; } = Array.Empty<string>();
|
||||
|
||||
[JsonPropertyName("api")]
|
||||
public string[] Api { get; set; } = Array.Empty<string>();
|
||||
|
||||
/// <summary>
|
||||
/// 转换为域名与ip关系
|
||||
/// </summary>
|
||||
@ -107,6 +113,17 @@ namespace FastGithub.Scanner.DomainAddressProviders
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var range in IPAddressRange.From(this.Api).OrderBy(item => item.Size))
|
||||
{
|
||||
if (range.AddressFamily == AddressFamily.InterNetwork)
|
||||
{
|
||||
foreach (var address in range)
|
||||
{
|
||||
yield return new DomainAddress("api.github.com", address);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -18,6 +18,7 @@ namespace FastGithub.Scanner.DomainAddressProviders
|
||||
sealed class IPAddressComProvider : IDomainAddressProvider
|
||||
{
|
||||
private readonly IOptionsMonitor<GithubOptions> options;
|
||||
private readonly HttpClientFactory httpClientFactory;
|
||||
private readonly ILogger<IPAddressComProvider> logger;
|
||||
private readonly Uri lookupUri = new("https://www.ipaddress.com/ip-lookup");
|
||||
|
||||
@ -28,9 +29,11 @@ namespace FastGithub.Scanner.DomainAddressProviders
|
||||
/// <param name="logger"></param>
|
||||
public IPAddressComProvider(
|
||||
IOptionsMonitor<GithubOptions> options,
|
||||
HttpClientFactory httpClientFactory,
|
||||
ILogger<IPAddressComProvider> logger)
|
||||
{
|
||||
this.options = options;
|
||||
this.httpClientFactory = httpClientFactory;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
@ -46,7 +49,7 @@ namespace FastGithub.Scanner.DomainAddressProviders
|
||||
return Enumerable.Empty<DomainAddress>();
|
||||
}
|
||||
|
||||
using var httpClient = new HttpClient();
|
||||
using var httpClient = this.httpClientFactory.Create();
|
||||
var result = new HashSet<DomainAddress>();
|
||||
foreach (var domain in setting.Domains)
|
||||
{
|
||||
|
||||
41
FastGithub.Scanner/HttpClientFactory.cs
Normal file
41
FastGithub.Scanner/HttpClientFactory.cs
Normal file
@ -0,0 +1,41 @@
|
||||
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,6 +18,7 @@ namespace FastGithub.Scanner.ScanMiddlewares
|
||||
sealed class HttpsScanMiddleware : IMiddleware<GithubContext>
|
||||
{
|
||||
private readonly IOptionsMonitor<GithubOptions> options;
|
||||
private readonly HttpClientFactory httpClientFactory;
|
||||
private readonly ILogger<HttpsScanMiddleware> logger;
|
||||
|
||||
/// <summary>
|
||||
@ -27,9 +28,11 @@ namespace FastGithub.Scanner.ScanMiddlewares
|
||||
/// <param name="logger"></param>
|
||||
public HttpsScanMiddleware(
|
||||
IOptionsMonitor<GithubOptions> options,
|
||||
HttpClientFactory httpClientFactory,
|
||||
ILogger<HttpsScanMiddleware> logger)
|
||||
{
|
||||
this.options = options;
|
||||
this.httpClientFactory = httpClientFactory;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
@ -52,18 +55,12 @@ namespace FastGithub.Scanner.ScanMiddlewares
|
||||
};
|
||||
request.Headers.Host = context.Domain;
|
||||
request.Headers.ConnectionClose = true;
|
||||
request.Headers.Accept.TryParseAdd("*/*");
|
||||
|
||||
using var httpClient = new HttpMessageInvoker(new SocketsHttpHandler
|
||||
{
|
||||
Proxy = null,
|
||||
UseProxy = false,
|
||||
AllowAutoRedirect = false,
|
||||
});
|
||||
|
||||
var timeout = this.options.CurrentValue.Scan.HttpsScanTimeout;
|
||||
using var cancellationTokenSource = new CancellationTokenSource(timeout);
|
||||
using var response = await httpClient.SendAsync(request, cancellationTokenSource.Token);
|
||||
using var httpClient = this.httpClientFactory.Create(allowAutoRedirect: false);
|
||||
using var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationTokenSource.Token);
|
||||
|
||||
this.VerifyHttpsResponse(context.Domain, response);
|
||||
context.Available = true;
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user