From a2ea783c11259d44b1e1737dc30f31f6b7129fca Mon Sep 17 00:00:00 2001 From: xljiulang <366193849@qq.com> Date: Sat, 12 Jun 2021 00:06:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=AD=E9=97=B4=E4=BB=B6=E9=87=8D=E5=91=BD?= =?UTF-8?q?=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub/GithubOptions.cs | 2 +- FastGithub/IPv4CIDR.cs | 13 ++++- FastGithub/Meta.cs | 19 +++---- FastGithub/Middlewares/HttpTestMiddleware.cs | 60 -------------------- FastGithub/Program.cs | 4 +- FastGithub/appsettings.json | 2 +- 6 files changed, 23 insertions(+), 77 deletions(-) delete mode 100644 FastGithub/Middlewares/HttpTestMiddleware.cs diff --git a/FastGithub/GithubOptions.cs b/FastGithub/GithubOptions.cs index 79c0793..ef46658 100644 --- a/FastGithub/GithubOptions.cs +++ b/FastGithub/GithubOptions.cs @@ -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); } } diff --git a/FastGithub/IPv4CIDR.cs b/FastGithub/IPv4CIDR.cs index 823f10a..cab17b4 100644 --- a/FastGithub/IPv4CIDR.cs +++ b/FastGithub/IPv4CIDR.cs @@ -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 GetAllIPAddress() @@ -45,6 +45,17 @@ namespace FastGithub return new IPAddress(span); } + public static IEnumerable From(IEnumerable cidrs) + { + foreach (var item in cidrs) + { + if (TryParse(item, out var value)) + { + yield return value; + } + } + } + public static bool TryParse(ReadOnlySpan cidr, [MaybeNullWhen(false)] out IPv4CIDR value) { value = null; diff --git a/FastGithub/Meta.cs b/FastGithub/Meta.cs index 8a8c5ba..a6c4d50 100644 --- a/FastGithub/Meta.cs +++ b/FastGithub/Meta.cs @@ -1,5 +1,6 @@ 锘縰sing System; using System.Collections.Generic; +using System.Linq; using System.Net; using System.Text.Json.Serialization; @@ -37,25 +38,19 @@ namespace FastGithub public IEnumerable 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); } } } diff --git a/FastGithub/Middlewares/HttpTestMiddleware.cs b/FastGithub/Middlewares/HttpTestMiddleware.cs deleted file mode 100644 index cb56beb..0000000 --- a/FastGithub/Middlewares/HttpTestMiddleware.cs +++ /dev/null @@ -1,60 +0,0 @@ -锘縰sing 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 options; - private readonly ILogger logger; - - public HttpTestMiddleware( - IOptionsMonitor options, - ILogger logger) - { - this.options = options; - this.logger = logger; - } - - public async Task InvokeAsync(GithubContext context, Func 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}"); - } - } - } -} diff --git a/FastGithub/Program.cs b/FastGithub/Program.cs index 9c08ae8..b22169c 100644 --- a/FastGithub/Program.cs +++ b/FastGithub/Program.cs @@ -34,14 +34,14 @@ namespace FastGithub .AddTransient() .AddSingleton() - .AddSingleton() + .AddSingleton() .AddSingleton() .AddSingleton(serviceProvider => { return new GithubBuilder(serviceProvider, ctx => Task.CompletedTask) .Use() .Use() - .Use() + .Use() .Build(); }) .AddHostedService() diff --git a/FastGithub/appsettings.json b/FastGithub/appsettings.json index bfb22b7..a6690fc 100644 --- a/FastGithub/appsettings.json +++ b/FastGithub/appsettings.json @@ -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": {