From 960f4efe180cda161545ea7928724b7530cad7e4 Mon Sep 17 00:00:00 2001 From: xljiulang <366193849@qq.com> Date: Sat, 19 Jun 2021 13:27:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8B=86=E5=88=86Options?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../GithubFullScanHostedService.cs | 6 +- ...dressFacotry.cs => GithubLookupFacotry.cs} | 16 +++-- .../GithubLookupFactoryOptions.cs | 16 +++++ FastGithub.Scanner/GithubOptions.cs | 67 ------------------- .../GithubResultScanHostedService.cs | 8 +-- FastGithub.Scanner/GithubScanOptions.cs | 22 ++++++ FastGithub.Scanner/GithubScanService.cs | 4 +- ...ssProvider.cs => IGithubLookupProvider.cs} | 8 ++- .../GithubMetaProvider.cs | 47 ++++++++----- .../GithubMetaProviderOptions.cs | 18 +++++ .../IPAddressComProvider.cs | 20 +++--- .../IPAddressComProviderOptions.cs | 11 +++ .../PublicDnsProvider.cs | 20 +++--- .../PublicDnsProviderOptions.cs | 12 ++++ .../ScanMiddlewares/HttpsScanMiddleware.cs | 6 +- .../ScanMiddlewares/HttpsScanOptions.cs | 10 +++ .../ScanMiddlewares/TcpScanMiddleware.cs | 9 ++- .../ScanMiddlewares/TcpScanOptions.cs | 18 +++++ FastGithub/appsettings.json | 34 +++++----- 19 files changed, 208 insertions(+), 144 deletions(-) rename FastGithub.Scanner/{DomainAddressFacotry.cs => GithubLookupFacotry.cs} (63%) create mode 100644 FastGithub.Scanner/GithubLookupFactoryOptions.cs delete mode 100644 FastGithub.Scanner/GithubOptions.cs create mode 100644 FastGithub.Scanner/GithubScanOptions.cs rename FastGithub.Scanner/{IDomainAddressProvider.cs => IGithubLookupProvider.cs} (57%) rename FastGithub.Scanner/{DomainAddressProviders => LookupProviders}/GithubMetaProvider.cs (67%) create mode 100644 FastGithub.Scanner/LookupProviders/GithubMetaProviderOptions.cs rename FastGithub.Scanner/{DomainAddressProviders => LookupProviders}/IPAddressComProvider.cs (84%) create mode 100644 FastGithub.Scanner/LookupProviders/IPAddressComProviderOptions.cs rename FastGithub.Scanner/{DomainAddressProviders => LookupProviders}/PublicDnsProvider.cs (80%) create mode 100644 FastGithub.Scanner/LookupProviders/PublicDnsProviderOptions.cs create mode 100644 FastGithub.Scanner/ScanMiddlewares/HttpsScanOptions.cs create mode 100644 FastGithub.Scanner/ScanMiddlewares/TcpScanOptions.cs diff --git a/FastGithub.Scanner/GithubFullScanHostedService.cs b/FastGithub.Scanner/GithubFullScanHostedService.cs index 93d112c..88bd628 100644 --- a/FastGithub.Scanner/GithubFullScanHostedService.cs +++ b/FastGithub.Scanner/GithubFullScanHostedService.cs @@ -12,7 +12,7 @@ namespace FastGithub sealed class GithubFullScanHostedService : BackgroundService { private readonly GithubScanService githubScanService; - private readonly IOptionsMonitor options; + private readonly IOptionsMonitor options; /// /// 完整扫描后台服务 @@ -21,7 +21,7 @@ namespace FastGithub /// public GithubFullScanHostedService( GithubScanService githubScanService, - IOptionsMonitor options) + IOptionsMonitor options) { this.githubScanService = githubScanService; this.options = options; @@ -37,7 +37,7 @@ namespace FastGithub while (stoppingToken.IsCancellationRequested == false) { await githubScanService.ScanAllAsync(stoppingToken); - await Task.Delay(this.options.CurrentValue.Scan.FullScanInterval, stoppingToken); + await Task.Delay(this.options.CurrentValue.FullScanInterval, stoppingToken); } } } diff --git a/FastGithub.Scanner/DomainAddressFacotry.cs b/FastGithub.Scanner/GithubLookupFacotry.cs similarity index 63% rename from FastGithub.Scanner/DomainAddressFacotry.cs rename to FastGithub.Scanner/GithubLookupFacotry.cs index 3aef79a..398410f 100644 --- a/FastGithub.Scanner/DomainAddressFacotry.cs +++ b/FastGithub.Scanner/GithubLookupFacotry.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; using System.Collections.Generic; using System.Linq; using System.Threading; @@ -10,17 +11,22 @@ namespace FastGithub.Scanner /// 域名与ip关系工厂 /// [Service(ServiceLifetime.Singleton)] - sealed class DomainAddressFacotry + sealed class GithubLookupFacotry { - private readonly IEnumerable providers; + private readonly IEnumerable providers; + private readonly IOptionsMonitor options; /// /// 域名与ip关系工厂 /// /// - public DomainAddressFacotry(IEnumerable providers) + /// + public GithubLookupFacotry( + IEnumerable providers, + IOptionsMonitor options) { this.providers = providers.OrderBy(item => item.Order); + this.options = options; } /// @@ -30,9 +36,11 @@ namespace FastGithub.Scanner public async Task> CreateDomainAddressesAsync(CancellationToken cancellationToken) { var hashSet = new HashSet(); + var domains = this.options.CurrentValue.Domains; + foreach (var provider in this.providers) { - var domainAddresses = await provider.CreateDomainAddressesAsync(cancellationToken); + var domainAddresses = await provider.LookupAsync(domains, cancellationToken); foreach (var item in domainAddresses) { hashSet.Add(item); diff --git a/FastGithub.Scanner/GithubLookupFactoryOptions.cs b/FastGithub.Scanner/GithubLookupFactoryOptions.cs new file mode 100644 index 0000000..dca3ec6 --- /dev/null +++ b/FastGithub.Scanner/GithubLookupFactoryOptions.cs @@ -0,0 +1,16 @@ +using System; + +namespace FastGithub.Scanner +{ + /// + /// 域名 + /// + [Options("Github:Lookup")] + sealed class GithubLookupFactoryOptions + { + /// + /// 反查的域名 + /// + public string[] Domains { get; set; } = Array.Empty(); + } +} diff --git a/FastGithub.Scanner/GithubOptions.cs b/FastGithub.Scanner/GithubOptions.cs deleted file mode 100644 index 6af58ca..0000000 --- a/FastGithub.Scanner/GithubOptions.cs +++ /dev/null @@ -1,67 +0,0 @@ -using System; - -namespace FastGithub.Scanner -{ - /// - /// github选项 - /// - [Options("Github")] - sealed class GithubOptions - { - /// - /// 扫描配置 - /// - public ScanSetting Scan { get; set; } = new ScanSetting(); - - /// - /// 域名与ip关系提供者配置 - /// - public DomainAddressProvidersSetting DominAddressProviders { get; set; } = new DomainAddressProvidersSetting(); - - /// - /// 扫描配置 - /// - public class ScanSetting - { - public TimeSpan FullScanInterval = TimeSpan.FromHours(2d); - - public TimeSpan ResultScanInterval = TimeSpan.FromMinutes(1d); - - public TimeSpan TcpScanTimeout { get; set; } = TimeSpan.FromSeconds(1d); - - public TimeSpan HttpsScanTimeout { get; set; } = TimeSpan.FromSeconds(5d); - } - - /// - /// 域名与ip关系提供者配置 - /// - public class DomainAddressProvidersSetting - { - public GithubMetaProviderSetting GithubMetaProvider { get; set; } = new GithubMetaProviderSetting(); - public IPAddressComProviderSetting IPAddressComProvider { get; set; } = new IPAddressComProviderSetting(); - public PublicDnsProviderSetting PublicDnsProvider { get; set; } = new PublicDnsProviderSetting(); - - public class GithubMetaProviderSetting - { - public bool Enable { get; set; } = true; - - public Uri MetaUri { get; set; } = new Uri("https://gitee.com/jiulang/fast-github/raw/master/FastGithub/meta.json"); - } - - public class IPAddressComProviderSetting - { - public bool Enable { get; set; } = true; - - public string[] Domains { get; set; } = Array.Empty(); - } - public class PublicDnsProviderSetting - { - public bool Enable { get; set; } = true; - - public string[] Dnss { get; set; } = Array.Empty(); - - public string[] Domains { get; set; } = Array.Empty(); - } - } - } -} diff --git a/FastGithub.Scanner/GithubResultScanHostedService.cs b/FastGithub.Scanner/GithubResultScanHostedService.cs index d4c9409..7957c43 100644 --- a/FastGithub.Scanner/GithubResultScanHostedService.cs +++ b/FastGithub.Scanner/GithubResultScanHostedService.cs @@ -12,7 +12,7 @@ namespace FastGithub sealed class GithubResultScanHostedService : BackgroundService { private readonly GithubScanService githubScanService; - private readonly IOptionsMonitor options; + private readonly IOptionsMonitor options; /// /// 扫描结果轮询扫描后台服务 @@ -21,7 +21,7 @@ namespace FastGithub /// public GithubResultScanHostedService( GithubScanService githubScanService, - IOptionsMonitor options) + IOptionsMonitor options) { this.githubScanService = githubScanService; this.options = options; @@ -36,9 +36,9 @@ namespace FastGithub { while (stoppingToken.IsCancellationRequested == false) { - await Task.Delay(this.options.CurrentValue.Scan.ResultScanInterval, stoppingToken); + await Task.Delay(this.options.CurrentValue.ResultScanInterval, stoppingToken); await githubScanService.ScanResultAsync(); } - } + } } } diff --git a/FastGithub.Scanner/GithubScanOptions.cs b/FastGithub.Scanner/GithubScanOptions.cs new file mode 100644 index 0000000..6d1b284 --- /dev/null +++ b/FastGithub.Scanner/GithubScanOptions.cs @@ -0,0 +1,22 @@ +using System; + +namespace FastGithub.Scanner +{ + /// + /// 扫描选项 + /// + [Options("Github:Scan")] + sealed class GithubScanOptions + { + /// + /// 完整扫描轮询时间间隔 + /// + + public TimeSpan FullScanInterval = TimeSpan.FromHours(2d); + + /// + /// 结果扫描轮询时间间隔 + /// + public TimeSpan ResultScanInterval = TimeSpan.FromMinutes(1d); + } +} diff --git a/FastGithub.Scanner/GithubScanService.cs b/FastGithub.Scanner/GithubScanService.cs index ab215d7..97502f3 100644 --- a/FastGithub.Scanner/GithubScanService.cs +++ b/FastGithub.Scanner/GithubScanService.cs @@ -14,7 +14,7 @@ namespace FastGithub.Scanner [Service(ServiceLifetime.Singleton)] sealed class GithubScanService { - private readonly DomainAddressFacotry domainAddressFactory; + private readonly GithubLookupFacotry domainAddressFactory; private readonly GithubContextCollection scanResults; private readonly ILogger logger; @@ -29,7 +29,7 @@ namespace FastGithub.Scanner /// /// public GithubScanService( - DomainAddressFacotry domainAddressFactory, + GithubLookupFacotry domainAddressFactory, GithubContextCollection scanResults, IServiceProvider appService, ILogger logger) diff --git a/FastGithub.Scanner/IDomainAddressProvider.cs b/FastGithub.Scanner/IGithubLookupProvider.cs similarity index 57% rename from FastGithub.Scanner/IDomainAddressProvider.cs rename to FastGithub.Scanner/IGithubLookupProvider.cs index 8ec4a16..b21c04f 100644 --- a/FastGithub.Scanner/IDomainAddressProvider.cs +++ b/FastGithub.Scanner/IGithubLookupProvider.cs @@ -7,7 +7,7 @@ namespace FastGithub.Scanner /// /// 定义域名的ip提值者 /// - interface IDomainAddressProvider + interface IGithubLookupProvider { /// /// 获取排序 @@ -15,9 +15,11 @@ namespace FastGithub.Scanner int Order { get; } /// - /// 创建域名与ip的关系 + /// 查找域名与ip关系 /// + /// + /// /// - Task> CreateDomainAddressesAsync(CancellationToken cancellationToken); + Task> LookupAsync(IEnumerable domains, CancellationToken cancellationToken); } } diff --git a/FastGithub.Scanner/DomainAddressProviders/GithubMetaProvider.cs b/FastGithub.Scanner/LookupProviders/GithubMetaProvider.cs similarity index 67% rename from FastGithub.Scanner/DomainAddressProviders/GithubMetaProvider.cs rename to FastGithub.Scanner/LookupProviders/GithubMetaProvider.cs index 2604437..a54416b 100644 --- a/FastGithub.Scanner/DomainAddressProviders/GithubMetaProvider.cs +++ b/FastGithub.Scanner/LookupProviders/GithubMetaProvider.cs @@ -11,15 +11,15 @@ using System.Text.Json.Serialization; using System.Threading; using System.Threading.Tasks; -namespace FastGithub.Scanner.DomainAddressProviders +namespace FastGithub.Scanner.LookupProviders { /// /// Github公开的域名与ip关系提供者 /// - [Service(ServiceLifetime.Singleton, ServiceType = typeof(IDomainAddressProvider))] - sealed class GithubMetaProvider : IDomainAddressProvider + [Service(ServiceLifetime.Singleton, ServiceType = typeof(IGithubLookupProvider))] + sealed class GithubMetaProvider : IGithubLookupProvider { - private readonly IOptionsMonitor options; + private readonly IOptionsMonitor options; private readonly IHttpClientFactory httpClientFactory; private readonly ILogger logger; private const string META_URI = "https://api.github.com/meta"; @@ -35,7 +35,7 @@ namespace FastGithub.Scanner.DomainAddressProviders /// /// public GithubMetaProvider( - IOptionsMonitor options, + IOptionsMonitor options, IHttpClientFactory httpClientFactory, ILogger logger) { @@ -45,12 +45,14 @@ namespace FastGithub.Scanner.DomainAddressProviders } /// - /// 创建域名与ip的关系 + /// 查找域名与ip关系 /// + /// + /// /// - public async Task> CreateDomainAddressesAsync(CancellationToken cancellationToken) + public async Task> LookupAsync(IEnumerable domains, CancellationToken cancellationToken) { - var setting = this.options.CurrentValue.DominAddressProviders.GithubMetaProvider; + var setting = this.options.CurrentValue; if (setting.Enable == false) { return Enumerable.Empty(); @@ -62,7 +64,7 @@ namespace FastGithub.Scanner.DomainAddressProviders var meta = await GetMetaAsync(httpClient, setting.MetaUri, cancellationToken); if (meta != null) { - return meta.ToDomainAddresses(); + return meta.ToDomainAddresses(domains); } } catch (Exception ex) @@ -109,26 +111,35 @@ namespace FastGithub.Scanner.DomainAddressProviders /// 转换为域名与ip关系 /// /// - public IEnumerable ToDomainAddresses() + public IEnumerable ToDomainAddresses(IEnumerable domains) { - foreach (var range in IPAddressRange.From(this.Web).OrderBy(item => item.Size)) + const string github = "github.com"; + const string apiGithub = "api.github.com"; + + if (domains.Contains(github) == true) { - if (range.AddressFamily == AddressFamily.InterNetwork) + foreach (var range in IPAddressRange.From(this.Web).OrderBy(item => item.Size)) { - foreach (var address in range) + if (range.AddressFamily == AddressFamily.InterNetwork) { - yield return new DomainAddress("github.com", address); + foreach (var address in range) + { + yield return new DomainAddress(github, address); + } } } } - foreach (var range in IPAddressRange.From(this.Api).OrderBy(item => item.Size)) + if (domains.Contains(apiGithub) == true) { - if (range.AddressFamily == AddressFamily.InterNetwork) + foreach (var range in IPAddressRange.From(this.Api).OrderBy(item => item.Size)) { - foreach (var address in range) + if (range.AddressFamily == AddressFamily.InterNetwork) { - yield return new DomainAddress("api.github.com", address); + foreach (var address in range) + { + yield return new DomainAddress(apiGithub, address); + } } } } diff --git a/FastGithub.Scanner/LookupProviders/GithubMetaProviderOptions.cs b/FastGithub.Scanner/LookupProviders/GithubMetaProviderOptions.cs new file mode 100644 index 0000000..489b2db --- /dev/null +++ b/FastGithub.Scanner/LookupProviders/GithubMetaProviderOptions.cs @@ -0,0 +1,18 @@ +using System; + +namespace FastGithub.Scanner.LookupProviders +{ + [Options("Github:Lookup:GithubMetaProvider")] + sealed class GithubMetaProviderOptions + { + /// + /// 是否启用 + /// + public bool Enable { get; set; } + + /// + /// meta请求uri + /// + public Uri MetaUri { get; set; } = new Uri("https://gitee.com/jiulang/fast-github/raw/master/FastGithub/meta.json"); + } +} diff --git a/FastGithub.Scanner/DomainAddressProviders/IPAddressComProvider.cs b/FastGithub.Scanner/LookupProviders/IPAddressComProvider.cs similarity index 84% rename from FastGithub.Scanner/DomainAddressProviders/IPAddressComProvider.cs rename to FastGithub.Scanner/LookupProviders/IPAddressComProvider.cs index f07d9c6..7c4a5b6 100644 --- a/FastGithub.Scanner/DomainAddressProviders/IPAddressComProvider.cs +++ b/FastGithub.Scanner/LookupProviders/IPAddressComProvider.cs @@ -10,15 +10,15 @@ using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; -namespace FastGithub.Scanner.DomainAddressProviders +namespace FastGithub.Scanner.LookupProviders { /// /// ipaddress.com的域名与ip关系提供者 /// - [Service(ServiceLifetime.Singleton, ServiceType = typeof(IDomainAddressProvider))] - sealed class IPAddressComProvider : IDomainAddressProvider + [Service(ServiceLifetime.Singleton, ServiceType = typeof(IGithubLookupProvider))] + sealed class IPAddressComProvider : IGithubLookupProvider { - private readonly IOptionsMonitor options; + private readonly IOptionsMonitor options; private readonly IHttpClientFactory httpClientFactory; private readonly ILogger logger; private readonly Uri lookupUri = new("https://www.ipaddress.com/ip-lookup"); @@ -34,7 +34,7 @@ namespace FastGithub.Scanner.DomainAddressProviders /// /// public IPAddressComProvider( - IOptionsMonitor options, + IOptionsMonitor options, IHttpClientFactory httpClientFactory, ILogger logger) { @@ -44,12 +44,14 @@ namespace FastGithub.Scanner.DomainAddressProviders } /// - /// 创建域名与ip的关系 + /// 查找域名与ip关系 /// + /// + /// /// - public async Task> CreateDomainAddressesAsync(CancellationToken cancellationToken) + public async Task> LookupAsync(IEnumerable domains, CancellationToken cancellationToken) { - var setting = this.options.CurrentValue.DominAddressProviders.IPAddressComProvider; + var setting = this.options.CurrentValue; if (setting.Enable == false) { return Enumerable.Empty(); @@ -57,7 +59,7 @@ namespace FastGithub.Scanner.DomainAddressProviders var httpClient = this.httpClientFactory.CreateClient(nameof(FastGithub)); var result = new HashSet(); - foreach (var domain in setting.Domains) + foreach (var domain in domains) { try { diff --git a/FastGithub.Scanner/LookupProviders/IPAddressComProviderOptions.cs b/FastGithub.Scanner/LookupProviders/IPAddressComProviderOptions.cs new file mode 100644 index 0000000..acda328 --- /dev/null +++ b/FastGithub.Scanner/LookupProviders/IPAddressComProviderOptions.cs @@ -0,0 +1,11 @@ +namespace FastGithub.Scanner.LookupProviders +{ + [Options("Github:Lookup:IPAddressComProvider")] + sealed class IPAddressComProviderOptions + { + /// + /// 是否启用 + /// + public bool Enable { get; set; } + } +} diff --git a/FastGithub.Scanner/DomainAddressProviders/PublicDnsProvider.cs b/FastGithub.Scanner/LookupProviders/PublicDnsProvider.cs similarity index 80% rename from FastGithub.Scanner/DomainAddressProviders/PublicDnsProvider.cs rename to FastGithub.Scanner/LookupProviders/PublicDnsProvider.cs index 1c26b27..2298f06 100644 --- a/FastGithub.Scanner/DomainAddressProviders/PublicDnsProvider.cs +++ b/FastGithub.Scanner/LookupProviders/PublicDnsProvider.cs @@ -9,15 +9,15 @@ using System.Net.Sockets; using System.Threading; using System.Threading.Tasks; -namespace FastGithub.Scanner.DomainAddressProviders +namespace FastGithub.Scanner.LookupProviders { /// /// 公共dns的域名与ip关系提供者 /// - [Service(ServiceLifetime.Singleton, ServiceType = typeof(IDomainAddressProvider))] - sealed class PublicDnsProvider : IDomainAddressProvider + [Service(ServiceLifetime.Singleton, ServiceType = typeof(IGithubLookupProvider))] + sealed class PublicDnsProvider : IGithubLookupProvider { - private readonly IOptionsMonitor options; + private readonly IOptionsMonitor options; private readonly ILogger logger; /// @@ -31,7 +31,7 @@ namespace FastGithub.Scanner.DomainAddressProviders /// /// public PublicDnsProvider( - IOptionsMonitor options, + IOptionsMonitor options, ILogger logger) { this.options = options; @@ -39,12 +39,14 @@ namespace FastGithub.Scanner.DomainAddressProviders } /// - /// 创建域名与ip的关系 + /// 查找域名与ip关系 /// + /// + /// /// - public async Task> CreateDomainAddressesAsync(CancellationToken cancellationToken) + public async Task> LookupAsync(IEnumerable domains, CancellationToken cancellationToken) { - var setting = this.options.CurrentValue.DominAddressProviders.PublicDnsProvider; + var setting = this.options.CurrentValue; if (setting.Enable == false) { return Enumerable.Empty(); @@ -53,7 +55,7 @@ namespace FastGithub.Scanner.DomainAddressProviders var result = new HashSet(); foreach (var dns in setting.Dnss) { - var domainAddresses = await this.LookupAsync(dns, setting.Domains, cancellationToken); + var domainAddresses = await this.LookupAsync(dns, domains, cancellationToken); foreach (var item in domainAddresses) { result.Add(item); diff --git a/FastGithub.Scanner/LookupProviders/PublicDnsProviderOptions.cs b/FastGithub.Scanner/LookupProviders/PublicDnsProviderOptions.cs new file mode 100644 index 0000000..bbe2892 --- /dev/null +++ b/FastGithub.Scanner/LookupProviders/PublicDnsProviderOptions.cs @@ -0,0 +1,12 @@ +using System; + +namespace FastGithub.Scanner.LookupProviders +{ + [Options("Github:Lookup:PublicDnsProvider")] + sealed class PublicDnsProviderOptions + { + public bool Enable { get; set; } = true; + + public string[] Dnss { get; set; } = Array.Empty(); + } +} diff --git a/FastGithub.Scanner/ScanMiddlewares/HttpsScanMiddleware.cs b/FastGithub.Scanner/ScanMiddlewares/HttpsScanMiddleware.cs index e5911f3..f6d1e1c 100644 --- a/FastGithub.Scanner/ScanMiddlewares/HttpsScanMiddleware.cs +++ b/FastGithub.Scanner/ScanMiddlewares/HttpsScanMiddleware.cs @@ -17,7 +17,7 @@ namespace FastGithub.Scanner.ScanMiddlewares [Service(ServiceLifetime.Singleton)] sealed class HttpsScanMiddleware : IMiddleware { - private readonly IOptionsMonitor options; + private readonly IOptionsMonitor options; private readonly IHttpClientFactory httpClientFactory; private readonly ILogger logger; @@ -27,7 +27,7 @@ namespace FastGithub.Scanner.ScanMiddlewares /// /// public HttpsScanMiddleware( - IOptionsMonitor options, + IOptionsMonitor options, IHttpClientFactory httpClientFactory, ILogger logger) { @@ -55,7 +55,7 @@ namespace FastGithub.Scanner.ScanMiddlewares }; request.Headers.Host = context.Domain; - var timeout = this.options.CurrentValue.Scan.HttpsScanTimeout; + var timeout = this.options.CurrentValue.Timeout; using var timeoutTokenSource = new CancellationTokenSource(timeout); using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(timeoutTokenSource.Token, context.CancellationToken); diff --git a/FastGithub.Scanner/ScanMiddlewares/HttpsScanOptions.cs b/FastGithub.Scanner/ScanMiddlewares/HttpsScanOptions.cs new file mode 100644 index 0000000..945d06b --- /dev/null +++ b/FastGithub.Scanner/ScanMiddlewares/HttpsScanOptions.cs @@ -0,0 +1,10 @@ +using System; + +namespace FastGithub.Scanner.ScanMiddlewares +{ + [Options("Github:Scan:HttpsScan")] + sealed class HttpsScanOptions + { + public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(5d); + } +} diff --git a/FastGithub.Scanner/ScanMiddlewares/TcpScanMiddleware.cs b/FastGithub.Scanner/ScanMiddlewares/TcpScanMiddleware.cs index 0534872..e132c5e 100644 --- a/FastGithub.Scanner/ScanMiddlewares/TcpScanMiddleware.cs +++ b/FastGithub.Scanner/ScanMiddlewares/TcpScanMiddleware.cs @@ -16,8 +16,7 @@ namespace FastGithub.Scanner.ScanMiddlewares sealed class TcpScanMiddleware : IMiddleware { private const int PORT = 443; - private readonly TimeSpan cacheTimeSpan = TimeSpan.FromMinutes(20d); - private readonly IOptionsMonitor options; + private readonly IOptionsMonitor options; private readonly IMemoryCache memoryCache; private readonly ILogger logger; @@ -27,7 +26,7 @@ namespace FastGithub.Scanner.ScanMiddlewares /// /// public TcpScanMiddleware( - IOptionsMonitor options, + IOptionsMonitor options, IMemoryCache memoryCache, ILogger logger) { @@ -48,7 +47,7 @@ namespace FastGithub.Scanner.ScanMiddlewares if (this.memoryCache.TryGetValue(key, out var available) == false) { available = await this.TcpScanAsync(context); - this.memoryCache.Set(key, available, cacheTimeSpan); + this.memoryCache.Set(key, available, this.options.CurrentValue.CacheExpiration); } if (available == true) @@ -71,7 +70,7 @@ namespace FastGithub.Scanner.ScanMiddlewares { try { - var timeout = this.options.CurrentValue.Scan.TcpScanTimeout; + var timeout = this.options.CurrentValue.Timeout; using var timeoutTokenSource = new CancellationTokenSource(timeout); using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(timeoutTokenSource.Token, context.CancellationToken); diff --git a/FastGithub.Scanner/ScanMiddlewares/TcpScanOptions.cs b/FastGithub.Scanner/ScanMiddlewares/TcpScanOptions.cs new file mode 100644 index 0000000..10d0290 --- /dev/null +++ b/FastGithub.Scanner/ScanMiddlewares/TcpScanOptions.cs @@ -0,0 +1,18 @@ +using System; + +namespace FastGithub.Scanner.ScanMiddlewares +{ + [Options("Github:Scan:TcpScan")] + sealed class TcpScanOptions + { + /// + /// 扫描超时时长 + /// + public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(1d); + + /// + /// 扫描结果缓存时长 + /// + public TimeSpan CacheExpiration { get; set; } = TimeSpan.FromMinutes(20d); + } +} diff --git a/FastGithub/appsettings.json b/FastGithub/appsettings.json index dc2e7bf..a1afa55 100644 --- a/FastGithub/appsettings.json +++ b/FastGithub/appsettings.json @@ -7,21 +7,26 @@ "Scan": { "FullScanInterval": "02:00:00", // ɨʱ "ResultScanInterval": "00:01:00", // ɨʱ - "TcpScanTimeout": "00:00:01", // tcpɨ賬ʱʱ - "HttpsScanTimeout": "00:00:05" // httpsɨ賬ʱʱ - }, - "DominAddressProviders": { - "GithubMetaProvider": { - "Enable": true, - "MetaUri": "https://gitee.com/jiulang/fast-github/raw/master/FastGithub/meta.json" + "TcpScan": { + "Timeout": "00:00:01", // tcpɨ賬ʱʱ + "CacheExpiration": "00:20:00" // ɨʱ }, + "HttpsScan": { + "Timeout": "00:00:05" // httpsɨ賬ʱʱ + } + }, + "Lookup": { + "Domains": [ + "github.com", + "api.github.com", + "github.githubassets.com" + ], "IPAddressComProvider": { + "Enable": true + }, + "GithubMetaProvider": { "Enable": true, - "Domains": [ - "github.com", - "api.github.com", - "github.githubassets.com" - ] + "MetaUri": "https://gitee.com/jiulang/fast-github/raw/master/FastGithub/meta.json" }, "PublicDnsProvider": { "Enable": true, @@ -34,11 +39,6 @@ "1.2.4.8", "208.67.220.220", "123.125.81.6" - ], - "Domains": [ - "github.com", - "api.github.com", - "github.githubassets.com" ] } }