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"
+ }
+ }
+}