From a1ae1bbecab9875d2a590ddffbdc394518514413 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com> Date: Fri, 11 Jun 2021 14:02:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E8=BF=9C=E7=A8=8Bmeta?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub/FastGithub.csproj | 5 ++- FastGithub/GithubHostedService.cs | 16 +++++--- FastGithub/GithubOptions.cs | 15 +++++++ FastGithub/MetaService.cs | 40 +++++++++++++++++++ .../Middlewares/ConcurrentMiddleware.cs | 10 ++++- FastGithub/Middlewares/HttpTestMiddleware.cs | 12 ++++-- FastGithub/Middlewares/PortScanMiddleware.cs | 17 +++++--- FastGithub/Program.cs | 3 ++ FastGithub/appsettings.json | 16 ++++++++ 9 files changed, 115 insertions(+), 19 deletions(-) create mode 100644 FastGithub/GithubOptions.cs create mode 100644 FastGithub/MetaService.cs create mode 100644 FastGithub/appsettings.json diff --git a/FastGithub/FastGithub.csproj b/FastGithub/FastGithub.csproj index 7335c77..07fcd28 100644 --- a/FastGithub/FastGithub.csproj +++ b/FastGithub/FastGithub.csproj @@ -8,12 +8,13 @@ + - - PreserveNewest + + Always diff --git a/FastGithub/GithubHostedService.cs b/FastGithub/GithubHostedService.cs index 0e623cd..7067d44 100644 --- a/FastGithub/GithubHostedService.cs +++ b/FastGithub/GithubHostedService.cs @@ -1,11 +1,10 @@ using FastGithub.Middlewares; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; -using System.IO; using System.Linq; -using System.Text.Json; using System.Threading; using System.Threading.Tasks; @@ -14,10 +13,12 @@ namespace FastGithub sealed class GithubHostedService : BackgroundService { private readonly GithubDelegate githubDelegate; + private readonly IServiceScopeFactory serviceScopeFactory; private readonly ILogger logger; public GithubHostedService( IServiceProvider appServiceProvider, + IServiceScopeFactory serviceScopeFactory, ILogger logger) { this.githubDelegate = new GithubBuilder(appServiceProvider, ctx => Task.CompletedTask) @@ -26,13 +27,16 @@ namespace FastGithub .Use() .Build(); + this.serviceScopeFactory = serviceScopeFactory; this.logger = logger; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { - using var stream = File.OpenRead("meta.json"); - var meta = await JsonSerializer.DeserializeAsync(stream, cancellationToken: stoppingToken); + using var scope = this.serviceScopeFactory.CreateScope(); + var metaService = scope.ServiceProvider.GetRequiredService(); + + var meta = await metaService.GetMetaAsync(); if (meta != null) { @@ -49,8 +53,10 @@ namespace FastGithub this.logger.LogInformation($"{context.Address} {context.HttpElapsed}"); } } + this.logger.LogInformation("扫描结束"); - } + } + private IEnumerable GetScanTasks(Meta meta, IList contexts) { diff --git a/FastGithub/GithubOptions.cs b/FastGithub/GithubOptions.cs new file mode 100644 index 0000000..a748152 --- /dev/null +++ b/FastGithub/GithubOptions.cs @@ -0,0 +1,15 @@ +using System; + +namespace FastGithub +{ + class GithubOptions + { + public Uri MetaUri { get; set; } = new Uri("https://gitee.com/jiulang/fast-github/raw/master/FastGithub/meta.json"); + + public int Concurrent { get; set; } = 50; + + public TimeSpan PortScanTimeout { get; set; } = TimeSpan.FromSeconds(1d); + + public TimeSpan HttpTestTimeout { get; set; } = TimeSpan.FromSeconds(5d); + } +} diff --git a/FastGithub/MetaService.cs b/FastGithub/MetaService.cs new file mode 100644 index 0000000..646dcd0 --- /dev/null +++ b/FastGithub/MetaService.cs @@ -0,0 +1,40 @@ +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using System; +using System.Net.Http; +using System.Net.Http.Json; +using System.Threading.Tasks; + +namespace FastGithub +{ + sealed class MetaService + { + private readonly IHttpClientFactory httpClientFactory; + private readonly IOptionsMonitor options; + private readonly ILogger logger; + + public MetaService( + IHttpClientFactory httpClientFactory, + IOptionsMonitor options, + ILogger logger) + { + this.httpClientFactory = httpClientFactory; + this.options = options; + this.logger = logger; + } + + public async Task GetMetaAsync() + { + try + { + var httpClient = this.httpClientFactory.CreateClient(); + return await httpClient.GetFromJsonAsync(this.options.CurrentValue.MetaUri); + } + catch (Exception ex) + { + this.logger.LogError(ex, "获取meta.json文件失败"); + return default; + } + } + } +} diff --git a/FastGithub/Middlewares/ConcurrentMiddleware.cs b/FastGithub/Middlewares/ConcurrentMiddleware.cs index 9fa3fde..5941216 100644 --- a/FastGithub/Middlewares/ConcurrentMiddleware.cs +++ b/FastGithub/Middlewares/ConcurrentMiddleware.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.Extensions.Options; +using System; using System.Threading; using System.Threading.Tasks; @@ -6,7 +7,12 @@ namespace FastGithub.Middlewares { sealed class ConcurrentMiddleware : IGithubMiddleware { - private readonly SemaphoreSlim semaphoreSlim = new(50); + private readonly SemaphoreSlim semaphoreSlim; + + public ConcurrentMiddleware(IOptions options) + { + this.semaphoreSlim = new SemaphoreSlim(options.Value.Concurrent); + } public async Task InvokeAsync(GithubContext context, Func next) { diff --git a/FastGithub/Middlewares/HttpTestMiddleware.cs b/FastGithub/Middlewares/HttpTestMiddleware.cs index 7baca0a..c175b6d 100644 --- a/FastGithub/Middlewares/HttpTestMiddleware.cs +++ b/FastGithub/Middlewares/HttpTestMiddleware.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using System; using System.Net.Http; using System.Threading; @@ -8,11 +9,14 @@ namespace FastGithub.Middlewares { sealed class HttpTestMiddleware : IGithubMiddleware { - private readonly TimeSpan timeout = TimeSpan.FromSeconds(5d); + private readonly IOptionsMonitor options; private readonly ILogger logger; - public HttpTestMiddleware(ILogger logger) + public HttpTestMiddleware( + IOptionsMonitor options, + ILogger logger) { + this.options = options; this.logger = logger; } @@ -33,11 +37,11 @@ namespace FastGithub.Middlewares }); var startTime = DateTime.Now; - using var cancellationTokenSource = new CancellationTokenSource(this.timeout); + using var cancellationTokenSource = new CancellationTokenSource(this.options.CurrentValue.HttpTestTimeout); var response = await httpClient.SendAsync(request, cancellationTokenSource.Token); var media = response.EnsureSuccessStatusCode().Content.Headers.ContentType?.MediaType; - if (string.Equals(media, "application/manifest+json")) + if (string.Equals(media, "application/manifest+json", StringComparison.OrdinalIgnoreCase)) { context.HttpElapsed = DateTime.Now.Subtract(startTime); await next(); diff --git a/FastGithub/Middlewares/PortScanMiddleware.cs b/FastGithub/Middlewares/PortScanMiddleware.cs index 0e7a1c7..f59a69f 100644 --- a/FastGithub/Middlewares/PortScanMiddleware.cs +++ b/FastGithub/Middlewares/PortScanMiddleware.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using System; using System.Net.Sockets; using System.Threading; @@ -8,27 +9,31 @@ namespace FastGithub.Middlewares { sealed class PortScanMiddleware : IGithubMiddleware { - private readonly TimeSpan timeout = TimeSpan.FromSeconds(1d); + private const int PORT = 443; + private readonly IOptionsMonitor options; private readonly ILogger logger; - public PortScanMiddleware(ILogger logger) + public PortScanMiddleware( + IOptionsMonitor options, + ILogger logger) { + this.options = options; this.logger = logger; } public async Task InvokeAsync(GithubContext context, Func next) { try - { + { using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - using var cancellationTokenSource = new CancellationTokenSource(this.timeout); - await socket.ConnectAsync(context.Address, 443, cancellationTokenSource.Token); + using var cancellationTokenSource = new CancellationTokenSource(this.options.CurrentValue.PortScanTimeout); + await socket.ConnectAsync(context.Address, PORT, cancellationTokenSource.Token); await next(); } catch (Exception) { - this.logger.LogInformation($"{context.Address}的443端口未开放"); + this.logger.LogInformation($"{context.Address}的{PORT}端口未开放"); } } } diff --git a/FastGithub/Program.cs b/FastGithub/Program.cs index d8fd833..0f821fa 100644 --- a/FastGithub/Program.cs +++ b/FastGithub/Program.cs @@ -27,6 +27,9 @@ namespace FastGithub .ConfigureServices((ctx, services) => { services + .Configure(ctx.Configuration.GetSection("Github")) + .AddHttpClient() + .AddTransient() .AddSingleton() .AddSingleton() .AddSingleton() diff --git a/FastGithub/appsettings.json b/FastGithub/appsettings.json new file mode 100644 index 0000000..cfc9037 --- /dev/null +++ b/FastGithub/appsettings.json @@ -0,0 +1,16 @@ +{ + "Github": { + "MetaUri": "https://gitee.com/jiulang/fast-github/raw/master/FastGithub/meta.json", // ipԴļuri + "Concurrent": 50, // ҵҪЧ + "PortScanTimeout": "00:00:01", // ˿ɨ賬ʱʱ + "HttpTestTimeout": "00:00:05" // httpԳʱʱ + }, + "Logging": { + "LogLevel": { + "Default": "Information", + "System": "Warning", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +}