增加GithubService
This commit is contained in:
parent
a1ae1bbeca
commit
b39e50b53a
@ -1,74 +1,29 @@
|
|||||||
using FastGithub.Middlewares;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace FastGithub
|
namespace FastGithub
|
||||||
{
|
{
|
||||||
sealed class GithubHostedService : BackgroundService
|
sealed class GithubHostedService : IHostedService
|
||||||
{
|
{
|
||||||
private readonly GithubDelegate githubDelegate;
|
|
||||||
private readonly IServiceScopeFactory serviceScopeFactory;
|
private readonly IServiceScopeFactory serviceScopeFactory;
|
||||||
private readonly ILogger<GithubHostedService> logger;
|
|
||||||
|
|
||||||
public GithubHostedService(
|
public GithubHostedService(IServiceScopeFactory serviceScopeFactory)
|
||||||
IServiceProvider appServiceProvider,
|
|
||||||
IServiceScopeFactory serviceScopeFactory,
|
|
||||||
ILogger<GithubHostedService> logger)
|
|
||||||
{
|
{
|
||||||
this.githubDelegate = new GithubBuilder(appServiceProvider, ctx => Task.CompletedTask)
|
|
||||||
.Use<ConcurrentMiddleware>()
|
|
||||||
.Use<PortScanMiddleware>()
|
|
||||||
.Use<HttpTestMiddleware>()
|
|
||||||
.Build();
|
|
||||||
|
|
||||||
this.serviceScopeFactory = serviceScopeFactory;
|
this.serviceScopeFactory = serviceScopeFactory;
|
||||||
this.logger = logger;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
public Task StartAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
using var scope = this.serviceScopeFactory.CreateScope();
|
var scope = this.serviceScopeFactory.CreateScope();
|
||||||
var metaService = scope.ServiceProvider.GetRequiredService<MetaService>();
|
var service = scope.ServiceProvider.GetRequiredService<GithubService>();
|
||||||
|
return service.ScanAddressAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
var meta = await metaService.GetMetaAsync();
|
public Task StopAsync(CancellationToken cancellationToken)
|
||||||
|
|
||||||
if (meta != null)
|
|
||||||
{
|
|
||||||
var contexts = new List<GithubContext>();
|
|
||||||
var tasks = this.GetScanTasks(meta, contexts);
|
|
||||||
await Task.WhenAll(tasks);
|
|
||||||
|
|
||||||
var orderByContexts = contexts
|
|
||||||
.Where(item => item.HttpElapsed != null)
|
|
||||||
.OrderBy(item => item.HttpElapsed);
|
|
||||||
|
|
||||||
foreach (var context in orderByContexts)
|
|
||||||
{
|
|
||||||
this.logger.LogInformation($"{context.Address} {context.HttpElapsed}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this.logger.LogInformation("扫描结束");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private IEnumerable<Task> GetScanTasks(Meta meta, IList<GithubContext> contexts)
|
|
||||||
{
|
{
|
||||||
foreach (var address in meta.ToIPv4Address())
|
return Task.CompletedTask;
|
||||||
{
|
|
||||||
var context = new GithubContext
|
|
||||||
{
|
|
||||||
Address = address,
|
|
||||||
};
|
|
||||||
contexts.Add(context);
|
|
||||||
yield return this.githubDelegate(context);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,32 +3,33 @@ using Microsoft.Extensions.Options;
|
|||||||
using System;
|
using System;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Net.Http.Json;
|
using System.Net.Http.Json;
|
||||||
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace FastGithub
|
namespace FastGithub
|
||||||
{
|
{
|
||||||
sealed class MetaService
|
sealed class GithubMetaService
|
||||||
{
|
{
|
||||||
private readonly IHttpClientFactory httpClientFactory;
|
private readonly IHttpClientFactory httpClientFactory;
|
||||||
private readonly IOptionsMonitor<GithubOptions> options;
|
private readonly IOptionsMonitor<GithubOptions> options;
|
||||||
private readonly ILogger<MetaService> logger;
|
private readonly ILogger<GithubMetaService> logger;
|
||||||
|
|
||||||
public MetaService(
|
public GithubMetaService(
|
||||||
IHttpClientFactory httpClientFactory,
|
IHttpClientFactory httpClientFactory,
|
||||||
IOptionsMonitor<GithubOptions> options,
|
IOptionsMonitor<GithubOptions> options,
|
||||||
ILogger<MetaService> logger)
|
ILogger<GithubMetaService> logger)
|
||||||
{
|
{
|
||||||
this.httpClientFactory = httpClientFactory;
|
this.httpClientFactory = httpClientFactory;
|
||||||
this.options = options;
|
this.options = options;
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Meta?> GetMetaAsync()
|
public async Task<Meta?> GetMetaAsync(CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var httpClient = this.httpClientFactory.CreateClient();
|
var httpClient = this.httpClientFactory.CreateClient();
|
||||||
return await httpClient.GetFromJsonAsync<Meta>(this.options.CurrentValue.MetaUri);
|
return await httpClient.GetFromJsonAsync<Meta>(this.options.CurrentValue.MetaUri, cancellationToken);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
61
FastGithub/GithubService.cs
Normal file
61
FastGithub/GithubService.cs
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace FastGithub
|
||||||
|
{
|
||||||
|
sealed class GithubService
|
||||||
|
{
|
||||||
|
private readonly GithubMetaService githubMetaService;
|
||||||
|
private readonly GithubDelegate githubDelegate;
|
||||||
|
private readonly ILogger<GithubService> logger;
|
||||||
|
|
||||||
|
public GithubService(
|
||||||
|
GithubMetaService githubMetaService,
|
||||||
|
GithubDelegate githubDelegate,
|
||||||
|
ILogger<GithubService> logger)
|
||||||
|
{
|
||||||
|
this.githubMetaService = githubMetaService;
|
||||||
|
this.githubDelegate = githubDelegate;
|
||||||
|
this.logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task ScanAddressAsync(CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
var meta = await this.githubMetaService.GetMetaAsync(cancellationToken);
|
||||||
|
if (meta != null)
|
||||||
|
{
|
||||||
|
var contexts = new List<GithubContext>();
|
||||||
|
var scanTasks = this.GetMetaScanTasks(meta, contexts);
|
||||||
|
await Task.WhenAll(scanTasks);
|
||||||
|
|
||||||
|
var sortedContexts = contexts
|
||||||
|
.Where(item => item.HttpElapsed != null)
|
||||||
|
.OrderBy(item => item.HttpElapsed);
|
||||||
|
|
||||||
|
foreach (var context in sortedContexts)
|
||||||
|
{
|
||||||
|
this.logger.LogInformation($"{context.Address} {context.HttpElapsed}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.logger.LogInformation("扫描结束");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private IEnumerable<Task> GetMetaScanTasks(Meta meta, IList<GithubContext> contexts)
|
||||||
|
{
|
||||||
|
foreach (var address in meta.ToIPv4Address())
|
||||||
|
{
|
||||||
|
var context = new GithubContext
|
||||||
|
{
|
||||||
|
Address = address,
|
||||||
|
};
|
||||||
|
contexts.Add(context);
|
||||||
|
yield return this.githubDelegate(context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,6 +1,7 @@
|
|||||||
using FastGithub.Middlewares;
|
using FastGithub.Middlewares;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace FastGithub
|
namespace FastGithub
|
||||||
{
|
{
|
||||||
@ -29,10 +30,20 @@ namespace FastGithub
|
|||||||
services
|
services
|
||||||
.Configure<GithubOptions>(ctx.Configuration.GetSection("Github"))
|
.Configure<GithubOptions>(ctx.Configuration.GetSection("Github"))
|
||||||
.AddHttpClient()
|
.AddHttpClient()
|
||||||
.AddTransient<MetaService>()
|
.AddTransient<GithubMetaService>()
|
||||||
|
.AddTransient<GithubService>()
|
||||||
|
|
||||||
.AddSingleton<PortScanMiddleware>()
|
.AddSingleton<PortScanMiddleware>()
|
||||||
.AddSingleton<HttpTestMiddleware>()
|
.AddSingleton<HttpTestMiddleware>()
|
||||||
.AddSingleton<ConcurrentMiddleware>()
|
.AddSingleton<ConcurrentMiddleware>()
|
||||||
|
.AddSingleton(serviceProvider =>
|
||||||
|
{
|
||||||
|
return new GithubBuilder(serviceProvider, ctx => Task.CompletedTask)
|
||||||
|
.Use<ConcurrentMiddleware>()
|
||||||
|
.Use<PortScanMiddleware>()
|
||||||
|
.Use<HttpTestMiddleware>()
|
||||||
|
.Build();
|
||||||
|
})
|
||||||
.AddHostedService<GithubHostedService>()
|
.AddHostedService<GithubHostedService>()
|
||||||
;
|
;
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user