开放IGithubScanResults接口

This commit is contained in:
xljiulang 2021-06-16 19:57:20 +08:00
parent 8a5dcee4b2
commit f0d0fc7fd2
8 changed files with 50 additions and 69 deletions

View File

@ -16,17 +16,17 @@ namespace FastGithub.Dns
[Service(ServiceLifetime.Singleton)] [Service(ServiceLifetime.Singleton)]
sealed class GithubRequestResolver : IRequestResolver sealed class GithubRequestResolver : IRequestResolver
{ {
private readonly IGithubScanService githubScanService; private readonly IGithubScanResults githubScanResults;
private readonly IMemoryCache memoryCache; private readonly IMemoryCache memoryCache;
private readonly ILogger<GithubRequestResolver> logger; private readonly ILogger<GithubRequestResolver> logger;
private readonly TimeSpan TTL = TimeSpan.FromMinutes(10d); private readonly TimeSpan TTL = TimeSpan.FromMinutes(10d);
public GithubRequestResolver( public GithubRequestResolver(
IGithubScanService githubScanService, IGithubScanResults githubScanResults,
IMemoryCache memoryCache, IMemoryCache memoryCache,
ILogger<GithubRequestResolver> logger) ILogger<GithubRequestResolver> logger)
{ {
this.githubScanService = githubScanService; this.githubScanResults = githubScanResults;
this.memoryCache = memoryCache; this.memoryCache = memoryCache;
this.logger = logger; this.logger = logger;
} }
@ -69,14 +69,14 @@ namespace FastGithub.Dns
var key = $"ttl:{domain}"; var key = $"ttl:{domain}";
if (this.memoryCache.TryGetValue<IPAddress>(key, out var address)) if (this.memoryCache.TryGetValue<IPAddress>(key, out var address))
{ {
if (this.githubScanService.IsAvailable(domain, address)) if (this.githubScanResults.IsAvailable(domain, address))
{ {
return address; return address;
} }
this.memoryCache.Remove(key); this.memoryCache.Remove(key);
} }
address = this.githubScanService.FindBestAddress(domain); address = this.githubScanResults.FindBestAddress(domain);
if (address != null) if (address != null)
{ {
this.memoryCache.Set(key, address, ttl); this.memoryCache.Set(key, address, ttl);

View File

@ -1,11 +1,12 @@
using System.Collections.Generic; using Microsoft.Extensions.DependencyInjection;
using System.Diagnostics.CodeAnalysis; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
namespace FastGithub.Scanner namespace FastGithub.Scanner
{ {
sealed class GithubContextCollection [Service(ServiceLifetime.Singleton)]
sealed class GithubContextCollection : IGithubScanResults
{ {
private readonly object syncRoot = new(); private readonly object syncRoot = new();
private readonly List<GithubContext> contextList = new(); private readonly List<GithubContext> contextList = new();
@ -24,16 +25,6 @@ namespace FastGithub.Scanner
} }
public bool TryGet(string domain, IPAddress address, [MaybeNullWhen(false)] out GithubContext context)
{
lock (this.syncRoot)
{
var target = new GithubContext(domain, address);
context = this.contextList.Find(item => item.Equals(target));
return context != null;
}
}
public GithubContext[] ToArray() public GithubContext[] ToArray()
{ {
lock (this.syncRoot) lock (this.syncRoot)
@ -42,6 +33,17 @@ namespace FastGithub.Scanner
} }
} }
public bool IsAvailable(string domain, IPAddress address)
{
lock (this.syncRoot)
{
var target = new GithubContext(domain, address);
var context = this.contextList.Find(item => item.Equals(target));
return context != null && context.Available;
}
}
/// <summary> /// <summary>
/// 查找又稳又快的ip /// 查找又稳又快的ip
/// </summary> /// </summary>

View File

@ -8,11 +8,11 @@ namespace FastGithub
{ {
sealed class GithubFullScanHostedService : BackgroundService sealed class GithubFullScanHostedService : BackgroundService
{ {
private readonly IGithubScanService githubScanService; private readonly GithubScanService githubScanService;
private readonly IOptionsMonitor<GithubOptions> options; private readonly IOptionsMonitor<GithubOptions> options;
public GithubFullScanHostedService( public GithubFullScanHostedService(
IGithubScanService githubScanService, GithubScanService githubScanService,
IOptionsMonitor<GithubOptions> options) IOptionsMonitor<GithubOptions> options)
{ {
this.githubScanService = githubScanService; this.githubScanService = githubScanService;

View File

@ -8,11 +8,11 @@ namespace FastGithub
{ {
sealed class GithubResultScanHostedService : BackgroundService sealed class GithubResultScanHostedService : BackgroundService
{ {
private readonly IGithubScanService githubScanService; private readonly GithubScanService githubScanService;
private readonly IOptionsMonitor<GithubOptions> options; private readonly IOptionsMonitor<GithubOptions> options;
public GithubResultScanHostedService( public GithubResultScanHostedService(
IGithubScanService githubScanService, GithubScanService githubScanService,
IOptionsMonitor<GithubOptions> options) IOptionsMonitor<GithubOptions> options)
{ {
this.githubScanService = githubScanService; this.githubScanService = githubScanService;

View File

@ -1,33 +1,34 @@
using FastGithub.Scanner.Middlewares; using FastGithub.Scanner.Middlewares;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System;
using System.Linq; using System.Linq;
using System.Net;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace FastGithub.Scanner namespace FastGithub.Scanner
{ {
[Service(ServiceLifetime.Singleton, ServiceType = typeof(IGithubScanService))] [Service(ServiceLifetime.Singleton)]
sealed class GithubScanService : IGithubScanService sealed class GithubScanService
{ {
private readonly GithubMetaService metaService; private readonly GithubMetaService metaService;
private readonly ILogger<GithubScanService> logger; private readonly ILogger<GithubScanService> logger;
private readonly GithubContextCollection results = new(); private readonly GithubContextCollection contextCollection;
private readonly InvokeDelegate<GithubContext> fullScanDelegate; private readonly InvokeDelegate<GithubContext> fullScanDelegate;
private readonly InvokeDelegate<GithubContext> resultScanDelegate; private readonly InvokeDelegate<GithubContext> resultScanDelegate;
public GithubScanService( public GithubScanService(
GithubMetaService metaService, GithubMetaService metaService,
ILogger<GithubScanService> logger, GithubContextCollection contextCollection,
IPipelineBuilder<GithubContext> pipelineBuilder) IServiceProvider appService,
ILogger<GithubScanService> logger)
{ {
this.metaService = metaService; this.metaService = metaService;
this.contextCollection = contextCollection;
this.logger = logger; this.logger = logger;
;
this.fullScanDelegate = pipelineBuilder this.fullScanDelegate = new PipelineBuilder<GithubContext>(appService, ctx => Task.CompletedTask)
.New()
.Use<ConcurrentMiddleware>() .Use<ConcurrentMiddleware>()
.Use<ScanOkLogMiddleware>() .Use<ScanOkLogMiddleware>()
.Use<StatisticsMiddleware>() .Use<StatisticsMiddleware>()
@ -35,8 +36,7 @@ namespace FastGithub.Scanner
.Use<HttpsScanMiddleware>() .Use<HttpsScanMiddleware>()
.Build(); .Build();
this.resultScanDelegate = pipelineBuilder this.resultScanDelegate = new PipelineBuilder<GithubContext>(appService, ctx => Task.CompletedTask)
.New()
.Use<ScanOkLogMiddleware>() .Use<ScanOkLogMiddleware>()
.Use<StatisticsMiddleware>() .Use<StatisticsMiddleware>()
.Use<PortScanMiddleware>() .Use<PortScanMiddleware>()
@ -61,7 +61,7 @@ namespace FastGithub.Scanner
await this.fullScanDelegate(context); await this.fullScanDelegate(context);
if (context.Available == true) if (context.Available == true)
{ {
this.results.Add(context); this.contextCollection.Add(context);
} }
} }
} }
@ -70,7 +70,7 @@ namespace FastGithub.Scanner
{ {
this.logger.LogInformation("结果扫描开始"); this.logger.LogInformation("结果扫描开始");
var contexts = this.results.ToArray(); var contexts = this.contextCollection.ToArray();
foreach (var context in contexts) foreach (var context in contexts)
{ {
await this.resultScanDelegate(context); await this.resultScanDelegate(context);
@ -78,16 +78,5 @@ namespace FastGithub.Scanner
this.logger.LogInformation("结果扫描结束"); this.logger.LogInformation("结果扫描结束");
} }
public IPAddress? FindBestAddress(string domain)
{
return this.results.FindBestAddress(domain);
}
public bool IsAvailable(string domain, IPAddress address)
{
return this.results.TryGet(domain, address, out var context) && context.Available;
}
} }
} }

View File

@ -0,0 +1,11 @@
using System.Net;
namespace FastGithub.Scanner
{
public interface IGithubScanResults
{
bool IsAvailable(string domain, IPAddress address);
IPAddress? FindBestAddress(string domain);
}
}

View File

@ -1,17 +0,0 @@
using System.Net;
using System.Threading;
using System.Threading.Tasks;
namespace FastGithub.Scanner
{
public interface IGithubScanService
{
Task ScanAllAsync(CancellationToken cancellationToken);
Task ScanResultAsync();
bool IsAvailable(string domain, IPAddress address);
IPAddress? FindBestAddress(string domain);
}
}

View File

@ -1,7 +1,6 @@
using FastGithub.Scanner; using FastGithub.Scanner;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using System.Threading.Tasks;
namespace FastGithub namespace FastGithub
{ {
@ -24,11 +23,8 @@ namespace FastGithub
.AddServiceAndOptions(assembly, configuration) .AddServiceAndOptions(assembly, configuration)
.AddHostedService<GithubFullScanHostedService>() .AddHostedService<GithubFullScanHostedService>()
.AddHostedService<GithubResultScanHostedService>() .AddHostedService<GithubResultScanHostedService>()
.AddSingleton<IPipelineBuilder<GithubContext>>(appService => .AddSingleton<IGithubScanResults>(appService => appService.GetRequiredService<GithubContextCollection>());
{ ;
return new PipelineBuilder<GithubContext>(appService, ctx => Task.CompletedTask);
})
;
} }
} }
} }