开放IGithubScanResults接口
This commit is contained in:
parent
8a5dcee4b2
commit
f0d0fc7fd2
@ -16,17 +16,17 @@ namespace FastGithub.Dns
|
||||
[Service(ServiceLifetime.Singleton)]
|
||||
sealed class GithubRequestResolver : IRequestResolver
|
||||
{
|
||||
private readonly IGithubScanService githubScanService;
|
||||
private readonly IGithubScanResults githubScanResults;
|
||||
private readonly IMemoryCache memoryCache;
|
||||
private readonly ILogger<GithubRequestResolver> logger;
|
||||
private readonly TimeSpan TTL = TimeSpan.FromMinutes(10d);
|
||||
|
||||
public GithubRequestResolver(
|
||||
IGithubScanService githubScanService,
|
||||
IGithubScanResults githubScanResults,
|
||||
IMemoryCache memoryCache,
|
||||
ILogger<GithubRequestResolver> logger)
|
||||
{
|
||||
this.githubScanService = githubScanService;
|
||||
this.githubScanResults = githubScanResults;
|
||||
this.memoryCache = memoryCache;
|
||||
this.logger = logger;
|
||||
}
|
||||
@ -69,14 +69,14 @@ namespace FastGithub.Dns
|
||||
var key = $"ttl:{domain}";
|
||||
if (this.memoryCache.TryGetValue<IPAddress>(key, out var address))
|
||||
{
|
||||
if (this.githubScanService.IsAvailable(domain, address))
|
||||
if (this.githubScanResults.IsAvailable(domain, address))
|
||||
{
|
||||
return address;
|
||||
}
|
||||
this.memoryCache.Remove(key);
|
||||
}
|
||||
|
||||
address = this.githubScanService.FindBestAddress(domain);
|
||||
address = this.githubScanResults.FindBestAddress(domain);
|
||||
if (address != null)
|
||||
{
|
||||
this.memoryCache.Set(key, address, ttl);
|
||||
|
||||
@ -1,11 +1,12 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
|
||||
namespace FastGithub.Scanner
|
||||
{
|
||||
sealed class GithubContextCollection
|
||||
[Service(ServiceLifetime.Singleton)]
|
||||
sealed class GithubContextCollection : IGithubScanResults
|
||||
{
|
||||
private readonly object syncRoot = 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()
|
||||
{
|
||||
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>
|
||||
/// 查找又稳又快的ip
|
||||
/// </summary>
|
||||
|
||||
@ -8,11 +8,11 @@ namespace FastGithub
|
||||
{
|
||||
sealed class GithubFullScanHostedService : BackgroundService
|
||||
{
|
||||
private readonly IGithubScanService githubScanService;
|
||||
private readonly GithubScanService githubScanService;
|
||||
private readonly IOptionsMonitor<GithubOptions> options;
|
||||
|
||||
public GithubFullScanHostedService(
|
||||
IGithubScanService githubScanService,
|
||||
GithubScanService githubScanService,
|
||||
IOptionsMonitor<GithubOptions> options)
|
||||
{
|
||||
this.githubScanService = githubScanService;
|
||||
|
||||
@ -8,11 +8,11 @@ namespace FastGithub
|
||||
{
|
||||
sealed class GithubResultScanHostedService : BackgroundService
|
||||
{
|
||||
private readonly IGithubScanService githubScanService;
|
||||
private readonly GithubScanService githubScanService;
|
||||
private readonly IOptionsMonitor<GithubOptions> options;
|
||||
|
||||
public GithubResultScanHostedService(
|
||||
IGithubScanService githubScanService,
|
||||
GithubScanService githubScanService,
|
||||
IOptionsMonitor<GithubOptions> options)
|
||||
{
|
||||
this.githubScanService = githubScanService;
|
||||
|
||||
@ -1,33 +1,34 @@
|
||||
using FastGithub.Scanner.Middlewares;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FastGithub.Scanner
|
||||
{
|
||||
[Service(ServiceLifetime.Singleton, ServiceType = typeof(IGithubScanService))]
|
||||
sealed class GithubScanService : IGithubScanService
|
||||
[Service(ServiceLifetime.Singleton)]
|
||||
sealed class GithubScanService
|
||||
{
|
||||
private readonly GithubMetaService metaService;
|
||||
private readonly ILogger<GithubScanService> logger;
|
||||
private readonly GithubContextCollection results = new();
|
||||
private readonly GithubContextCollection contextCollection;
|
||||
|
||||
private readonly InvokeDelegate<GithubContext> fullScanDelegate;
|
||||
private readonly InvokeDelegate<GithubContext> resultScanDelegate;
|
||||
|
||||
public GithubScanService(
|
||||
GithubMetaService metaService,
|
||||
ILogger<GithubScanService> logger,
|
||||
IPipelineBuilder<GithubContext> pipelineBuilder)
|
||||
GithubContextCollection contextCollection,
|
||||
IServiceProvider appService,
|
||||
ILogger<GithubScanService> logger)
|
||||
{
|
||||
this.metaService = metaService;
|
||||
this.contextCollection = contextCollection;
|
||||
this.logger = logger;
|
||||
|
||||
this.fullScanDelegate = pipelineBuilder
|
||||
.New()
|
||||
;
|
||||
this.fullScanDelegate = new PipelineBuilder<GithubContext>(appService, ctx => Task.CompletedTask)
|
||||
.Use<ConcurrentMiddleware>()
|
||||
.Use<ScanOkLogMiddleware>()
|
||||
.Use<StatisticsMiddleware>()
|
||||
@ -35,8 +36,7 @@ namespace FastGithub.Scanner
|
||||
.Use<HttpsScanMiddleware>()
|
||||
.Build();
|
||||
|
||||
this.resultScanDelegate = pipelineBuilder
|
||||
.New()
|
||||
this.resultScanDelegate = new PipelineBuilder<GithubContext>(appService, ctx => Task.CompletedTask)
|
||||
.Use<ScanOkLogMiddleware>()
|
||||
.Use<StatisticsMiddleware>()
|
||||
.Use<PortScanMiddleware>()
|
||||
@ -61,7 +61,7 @@ namespace FastGithub.Scanner
|
||||
await this.fullScanDelegate(context);
|
||||
if (context.Available == true)
|
||||
{
|
||||
this.results.Add(context);
|
||||
this.contextCollection.Add(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -70,7 +70,7 @@ namespace FastGithub.Scanner
|
||||
{
|
||||
this.logger.LogInformation("结果扫描开始");
|
||||
|
||||
var contexts = this.results.ToArray();
|
||||
var contexts = this.contextCollection.ToArray();
|
||||
foreach (var context in contexts)
|
||||
{
|
||||
await this.resultScanDelegate(context);
|
||||
@ -78,16 +78,5 @@ namespace FastGithub.Scanner
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
11
FastGithub.Scanner/IGithubScanResults.cs
Normal file
11
FastGithub.Scanner/IGithubScanResults.cs
Normal file
@ -0,0 +1,11 @@
|
||||
using System.Net;
|
||||
|
||||
namespace FastGithub.Scanner
|
||||
{
|
||||
public interface IGithubScanResults
|
||||
{
|
||||
bool IsAvailable(string domain, IPAddress address);
|
||||
|
||||
IPAddress? FindBestAddress(string domain);
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
@ -1,7 +1,6 @@
|
||||
using FastGithub.Scanner;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FastGithub
|
||||
{
|
||||
@ -24,11 +23,8 @@ namespace FastGithub
|
||||
.AddServiceAndOptions(assembly, configuration)
|
||||
.AddHostedService<GithubFullScanHostedService>()
|
||||
.AddHostedService<GithubResultScanHostedService>()
|
||||
.AddSingleton<IPipelineBuilder<GithubContext>>(appService =>
|
||||
{
|
||||
return new PipelineBuilder<GithubContext>(appService, ctx => Task.CompletedTask);
|
||||
})
|
||||
;
|
||||
.AddSingleton<IGithubScanResults>(appService => appService.GetRequiredService<GithubContextCollection>());
|
||||
;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user