增加HttpClientFactory

This commit is contained in:
陈国伟 2021-06-18 15:27:08 +08:00
parent f047aa2059
commit b61b469168
4 changed files with 69 additions and 11 deletions

View File

@ -19,6 +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 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";
@ -29,9 +30,11 @@ namespace FastGithub.Scanner.DomainAddressProviders
/// <param name="logger"></param> /// <param name="logger"></param>
public GithubMetaProvider( public GithubMetaProvider(
IOptionsMonitor<GithubOptions> options, IOptionsMonitor<GithubOptions> options,
HttpClientFactory httpClientFactory,
ILogger<GithubMetaProvider> logger) ILogger<GithubMetaProvider> logger)
{ {
this.options = options; this.options = options;
this.httpClientFactory = httpClientFactory;
this.logger = logger; this.logger = logger;
} }
@ -49,7 +52,7 @@ namespace FastGithub.Scanner.DomainAddressProviders
try try
{ {
using var httpClient = new HttpClient(); using var httpClient = this.httpClientFactory.Create();
var meta = await this.GetMetaAsync(httpClient, setting.MetaUri); var meta = await this.GetMetaAsync(httpClient, setting.MetaUri);
if (meta != null) if (meta != null)
{ {
@ -91,6 +94,9 @@ namespace FastGithub.Scanner.DomainAddressProviders
[JsonPropertyName("web")] [JsonPropertyName("web")]
public string[] Web { get; set; } = Array.Empty<string>(); public string[] Web { get; set; } = Array.Empty<string>();
[JsonPropertyName("api")]
public string[] Api { get; set; } = Array.Empty<string>();
/// <summary> /// <summary>
/// 转换为域名与ip关系 /// 转换为域名与ip关系
/// </summary> /// </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);
}
}
}
} }
} }
} }

View File

@ -18,6 +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 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");
@ -28,9 +29,11 @@ namespace FastGithub.Scanner.DomainAddressProviders
/// <param name="logger"></param> /// <param name="logger"></param>
public IPAddressComProvider( public IPAddressComProvider(
IOptionsMonitor<GithubOptions> options, IOptionsMonitor<GithubOptions> options,
HttpClientFactory httpClientFactory,
ILogger<IPAddressComProvider> logger) ILogger<IPAddressComProvider> logger)
{ {
this.options = options; this.options = options;
this.httpClientFactory = httpClientFactory;
this.logger = logger; this.logger = logger;
} }
@ -46,7 +49,7 @@ namespace FastGithub.Scanner.DomainAddressProviders
return Enumerable.Empty<DomainAddress>(); return Enumerable.Empty<DomainAddress>();
} }
using var httpClient = new HttpClient(); using var httpClient = this.httpClientFactory.Create();
var result = new HashSet<DomainAddress>(); var result = new HashSet<DomainAddress>();
foreach (var domain in setting.Domains) foreach (var domain in setting.Domains)
{ {

View 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;
}
}
}

View File

@ -18,6 +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 ILogger<HttpsScanMiddleware> logger; private readonly ILogger<HttpsScanMiddleware> logger;
/// <summary> /// <summary>
@ -27,9 +28,11 @@ namespace FastGithub.Scanner.ScanMiddlewares
/// <param name="logger"></param> /// <param name="logger"></param>
public HttpsScanMiddleware( public HttpsScanMiddleware(
IOptionsMonitor<GithubOptions> options, IOptionsMonitor<GithubOptions> options,
HttpClientFactory httpClientFactory,
ILogger<HttpsScanMiddleware> logger) ILogger<HttpsScanMiddleware> logger)
{ {
this.options = options; this.options = options;
this.httpClientFactory = httpClientFactory;
this.logger = logger; this.logger = logger;
} }
@ -52,18 +55,12 @@ namespace FastGithub.Scanner.ScanMiddlewares
}; };
request.Headers.Host = context.Domain; request.Headers.Host = context.Domain;
request.Headers.ConnectionClose = true; 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; var timeout = this.options.CurrentValue.Scan.HttpsScanTimeout;
using var cancellationTokenSource = new CancellationTokenSource(timeout); 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); this.VerifyHttpsResponse(context.Domain, response);
context.Available = true; context.Available = true;