中间件重命名
This commit is contained in:
parent
ea70ddda61
commit
a2ea783c11
@ -8,6 +8,6 @@ namespace FastGithub
|
||||
|
||||
public TimeSpan PortScanTimeout { get; set; } = TimeSpan.FromSeconds(1d);
|
||||
|
||||
public TimeSpan HttpTestTimeout { get; set; } = TimeSpan.FromSeconds(5d);
|
||||
public TimeSpan HttpsScanTimeout { get; set; } = TimeSpan.FromSeconds(5d);
|
||||
}
|
||||
}
|
||||
|
||||
@ -19,7 +19,7 @@ namespace FastGithub
|
||||
{
|
||||
this.IPAddress = ipAddress;
|
||||
this.Mask = mask;
|
||||
this.Size = (int)(uint.MaxValue << mask >> mask);
|
||||
this.Size = Math.Abs((int)(uint.MaxValue << mask >> mask));
|
||||
}
|
||||
|
||||
public IEnumerable<IPAddress> GetAllIPAddress()
|
||||
@ -45,6 +45,17 @@ namespace FastGithub
|
||||
return new IPAddress(span);
|
||||
}
|
||||
|
||||
public static IEnumerable<IPv4CIDR> From(IEnumerable<string> cidrs)
|
||||
{
|
||||
foreach (var item in cidrs)
|
||||
{
|
||||
if (TryParse(item, out var value))
|
||||
{
|
||||
yield return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static bool TryParse(ReadOnlySpan<char> cidr, [MaybeNullWhen(false)] out IPv4CIDR value)
|
||||
{
|
||||
value = null;
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
@ -37,25 +38,19 @@ namespace FastGithub
|
||||
|
||||
public IEnumerable<DomainAddress> ToDomainAddress()
|
||||
{
|
||||
foreach (var item in this.Web)
|
||||
foreach (var cidr in IPv4CIDR.From(this.Web).OrderBy(item => item.Size))
|
||||
{
|
||||
if (IPv4CIDR.TryParse(item, out var cidr))
|
||||
foreach (var address in cidr.GetAllIPAddress())
|
||||
{
|
||||
foreach (var address in cidr.GetAllIPAddress())
|
||||
{
|
||||
yield return new DomainAddress("github.com", address);
|
||||
}
|
||||
yield return new DomainAddress("github.com", address);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var item in this.Api)
|
||||
foreach (var cidr in IPv4CIDR.From(this.Api).OrderBy(item => item.Size))
|
||||
{
|
||||
if (IPv4CIDR.TryParse(item, out var cidr))
|
||||
foreach (var address in cidr.GetAllIPAddress())
|
||||
{
|
||||
foreach (var address in cidr.GetAllIPAddress())
|
||||
{
|
||||
yield return new DomainAddress("api.github.com", address);
|
||||
}
|
||||
yield return new DomainAddress("api.github.com", address);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,60 +0,0 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FastGithub.Middlewares
|
||||
{
|
||||
sealed class HttpTestMiddleware : IGithubMiddleware
|
||||
{
|
||||
private readonly IOptionsMonitor<GithubOptions> options;
|
||||
private readonly ILogger<HttpTestMiddleware> logger;
|
||||
|
||||
public HttpTestMiddleware(
|
||||
IOptionsMonitor<GithubOptions> options,
|
||||
ILogger<HttpTestMiddleware> logger)
|
||||
{
|
||||
this.options = options;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
public async Task InvokeAsync(GithubContext context, Func<Task> next)
|
||||
{
|
||||
try
|
||||
{
|
||||
var request = new HttpRequestMessage
|
||||
{
|
||||
Method = HttpMethod.Get,
|
||||
RequestUri = new Uri($"https://{context.Address}/"),
|
||||
};
|
||||
request.Headers.Host = context.Domain;
|
||||
|
||||
using var httpClient = new HttpClient(new HttpClientHandler
|
||||
{
|
||||
Proxy = null,
|
||||
UseProxy = false,
|
||||
ServerCertificateCustomValidationCallback = (_, _, _, _) => true
|
||||
});
|
||||
|
||||
var startTime = DateTime.Now;
|
||||
using var cancellationTokenSource = new CancellationTokenSource(this.options.CurrentValue.HttpTestTimeout);
|
||||
var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationTokenSource.Token);
|
||||
var server = response.EnsureSuccessStatusCode().Headers.Server;
|
||||
if (server.Any(s => string.Equals("GitHub.com", s.Product?.Name, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
context.HttpElapsed = DateTime.Now.Subtract(startTime);
|
||||
this.logger.LogWarning(context.ToString());
|
||||
|
||||
await next();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this.logger.LogInformation($"{context.Domain} {context.Address} {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -34,14 +34,14 @@ namespace FastGithub
|
||||
.AddTransient<GithubService>()
|
||||
|
||||
.AddSingleton<PortScanMiddleware>()
|
||||
.AddSingleton<HttpTestMiddleware>()
|
||||
.AddSingleton<HttpsScanMiddleware>()
|
||||
.AddSingleton<ConcurrentMiddleware>()
|
||||
.AddSingleton(serviceProvider =>
|
||||
{
|
||||
return new GithubBuilder(serviceProvider, ctx => Task.CompletedTask)
|
||||
.Use<ConcurrentMiddleware>()
|
||||
.Use<PortScanMiddleware>()
|
||||
.Use<HttpTestMiddleware>()
|
||||
.Use<HttpsScanMiddleware>()
|
||||
.Build();
|
||||
})
|
||||
.AddHostedService<GithubHostedService>()
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
"Github": {
|
||||
"MetaUri": "https://gitee.com/jiulang/fast-github/raw/master/FastGithub/meta.json", // ip资源文件uri
|
||||
"PortScanTimeout": "00:00:01", // 端口扫描超时时间
|
||||
"HttpTestTimeout": "00:00:05" // http测试超时时间
|
||||
"HttpsScanTimeout": "00:00:05" // https扫描超时时间
|
||||
},
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user