拆分Options
This commit is contained in:
parent
3004842a18
commit
960f4efe18
@ -12,7 +12,7 @@ namespace FastGithub
|
|||||||
sealed class GithubFullScanHostedService : BackgroundService
|
sealed class GithubFullScanHostedService : BackgroundService
|
||||||
{
|
{
|
||||||
private readonly GithubScanService githubScanService;
|
private readonly GithubScanService githubScanService;
|
||||||
private readonly IOptionsMonitor<GithubOptions> options;
|
private readonly IOptionsMonitor<GithubScanOptions> options;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 完整扫描后台服务
|
/// 完整扫描后台服务
|
||||||
@ -21,7 +21,7 @@ namespace FastGithub
|
|||||||
/// <param name="options"></param>
|
/// <param name="options"></param>
|
||||||
public GithubFullScanHostedService(
|
public GithubFullScanHostedService(
|
||||||
GithubScanService githubScanService,
|
GithubScanService githubScanService,
|
||||||
IOptionsMonitor<GithubOptions> options)
|
IOptionsMonitor<GithubScanOptions> options)
|
||||||
{
|
{
|
||||||
this.githubScanService = githubScanService;
|
this.githubScanService = githubScanService;
|
||||||
this.options = options;
|
this.options = options;
|
||||||
@ -37,7 +37,7 @@ namespace FastGithub
|
|||||||
while (stoppingToken.IsCancellationRequested == false)
|
while (stoppingToken.IsCancellationRequested == false)
|
||||||
{
|
{
|
||||||
await githubScanService.ScanAllAsync(stoppingToken);
|
await githubScanService.ScanAllAsync(stoppingToken);
|
||||||
await Task.Delay(this.options.CurrentValue.Scan.FullScanInterval, stoppingToken);
|
await Task.Delay(this.options.CurrentValue.FullScanInterval, stoppingToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
@ -10,17 +11,22 @@ namespace FastGithub.Scanner
|
|||||||
/// 域名与ip关系工厂
|
/// 域名与ip关系工厂
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Service(ServiceLifetime.Singleton)]
|
[Service(ServiceLifetime.Singleton)]
|
||||||
sealed class DomainAddressFacotry
|
sealed class GithubLookupFacotry
|
||||||
{
|
{
|
||||||
private readonly IEnumerable<IDomainAddressProvider> providers;
|
private readonly IEnumerable<IGithubLookupProvider> providers;
|
||||||
|
private readonly IOptionsMonitor<GithubLookupFactoryOptions> options;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 域名与ip关系工厂
|
/// 域名与ip关系工厂
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="providers"></param>
|
/// <param name="providers"></param>
|
||||||
public DomainAddressFacotry(IEnumerable<IDomainAddressProvider> providers)
|
/// <param name="options"></param>
|
||||||
|
public GithubLookupFacotry(
|
||||||
|
IEnumerable<IGithubLookupProvider> providers,
|
||||||
|
IOptionsMonitor<GithubLookupFactoryOptions> options)
|
||||||
{
|
{
|
||||||
this.providers = providers.OrderBy(item => item.Order);
|
this.providers = providers.OrderBy(item => item.Order);
|
||||||
|
this.options = options;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -30,9 +36,11 @@ namespace FastGithub.Scanner
|
|||||||
public async Task<IEnumerable<DomainAddress>> CreateDomainAddressesAsync(CancellationToken cancellationToken)
|
public async Task<IEnumerable<DomainAddress>> CreateDomainAddressesAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var hashSet = new HashSet<DomainAddress>();
|
var hashSet = new HashSet<DomainAddress>();
|
||||||
|
var domains = this.options.CurrentValue.Domains;
|
||||||
|
|
||||||
foreach (var provider in this.providers)
|
foreach (var provider in this.providers)
|
||||||
{
|
{
|
||||||
var domainAddresses = await provider.CreateDomainAddressesAsync(cancellationToken);
|
var domainAddresses = await provider.LookupAsync(domains, cancellationToken);
|
||||||
foreach (var item in domainAddresses)
|
foreach (var item in domainAddresses)
|
||||||
{
|
{
|
||||||
hashSet.Add(item);
|
hashSet.Add(item);
|
||||||
16
FastGithub.Scanner/GithubLookupFactoryOptions.cs
Normal file
16
FastGithub.Scanner/GithubLookupFactoryOptions.cs
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace FastGithub.Scanner
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 域名
|
||||||
|
/// </summary>
|
||||||
|
[Options("Github:Lookup")]
|
||||||
|
sealed class GithubLookupFactoryOptions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 反查的域名
|
||||||
|
/// </summary>
|
||||||
|
public string[] Domains { get; set; } = Array.Empty<string>();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,67 +0,0 @@
|
|||||||
using System;
|
|
||||||
|
|
||||||
namespace FastGithub.Scanner
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// github选项
|
|
||||||
/// </summary>
|
|
||||||
[Options("Github")]
|
|
||||||
sealed class GithubOptions
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 扫描配置
|
|
||||||
/// </summary>
|
|
||||||
public ScanSetting Scan { get; set; } = new ScanSetting();
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 域名与ip关系提供者配置
|
|
||||||
/// </summary>
|
|
||||||
public DomainAddressProvidersSetting DominAddressProviders { get; set; } = new DomainAddressProvidersSetting();
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 扫描配置
|
|
||||||
/// </summary>
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 域名与ip关系提供者配置
|
|
||||||
/// </summary>
|
|
||||||
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<string>();
|
|
||||||
}
|
|
||||||
public class PublicDnsProviderSetting
|
|
||||||
{
|
|
||||||
public bool Enable { get; set; } = true;
|
|
||||||
|
|
||||||
public string[] Dnss { get; set; } = Array.Empty<string>();
|
|
||||||
|
|
||||||
public string[] Domains { get; set; } = Array.Empty<string>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -12,7 +12,7 @@ namespace FastGithub
|
|||||||
sealed class GithubResultScanHostedService : BackgroundService
|
sealed class GithubResultScanHostedService : BackgroundService
|
||||||
{
|
{
|
||||||
private readonly GithubScanService githubScanService;
|
private readonly GithubScanService githubScanService;
|
||||||
private readonly IOptionsMonitor<GithubOptions> options;
|
private readonly IOptionsMonitor<GithubScanOptions> options;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 扫描结果轮询扫描后台服务
|
/// 扫描结果轮询扫描后台服务
|
||||||
@ -21,7 +21,7 @@ namespace FastGithub
|
|||||||
/// <param name="options"></param>
|
/// <param name="options"></param>
|
||||||
public GithubResultScanHostedService(
|
public GithubResultScanHostedService(
|
||||||
GithubScanService githubScanService,
|
GithubScanService githubScanService,
|
||||||
IOptionsMonitor<GithubOptions> options)
|
IOptionsMonitor<GithubScanOptions> options)
|
||||||
{
|
{
|
||||||
this.githubScanService = githubScanService;
|
this.githubScanService = githubScanService;
|
||||||
this.options = options;
|
this.options = options;
|
||||||
@ -36,9 +36,9 @@ namespace FastGithub
|
|||||||
{
|
{
|
||||||
while (stoppingToken.IsCancellationRequested == false)
|
while (stoppingToken.IsCancellationRequested == false)
|
||||||
{
|
{
|
||||||
await Task.Delay(this.options.CurrentValue.Scan.ResultScanInterval, stoppingToken);
|
await Task.Delay(this.options.CurrentValue.ResultScanInterval, stoppingToken);
|
||||||
await githubScanService.ScanResultAsync();
|
await githubScanService.ScanResultAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
22
FastGithub.Scanner/GithubScanOptions.cs
Normal file
22
FastGithub.Scanner/GithubScanOptions.cs
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace FastGithub.Scanner
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 扫描选项
|
||||||
|
/// </summary>
|
||||||
|
[Options("Github:Scan")]
|
||||||
|
sealed class GithubScanOptions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 完整扫描轮询时间间隔
|
||||||
|
/// </summary>
|
||||||
|
|
||||||
|
public TimeSpan FullScanInterval = TimeSpan.FromHours(2d);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 结果扫描轮询时间间隔
|
||||||
|
/// </summary>
|
||||||
|
public TimeSpan ResultScanInterval = TimeSpan.FromMinutes(1d);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -14,7 +14,7 @@ namespace FastGithub.Scanner
|
|||||||
[Service(ServiceLifetime.Singleton)]
|
[Service(ServiceLifetime.Singleton)]
|
||||||
sealed class GithubScanService
|
sealed class GithubScanService
|
||||||
{
|
{
|
||||||
private readonly DomainAddressFacotry domainAddressFactory;
|
private readonly GithubLookupFacotry domainAddressFactory;
|
||||||
private readonly GithubContextCollection scanResults;
|
private readonly GithubContextCollection scanResults;
|
||||||
private readonly ILogger<GithubScanService> logger;
|
private readonly ILogger<GithubScanService> logger;
|
||||||
|
|
||||||
@ -29,7 +29,7 @@ namespace FastGithub.Scanner
|
|||||||
/// <param name="appService"></param>
|
/// <param name="appService"></param>
|
||||||
/// <param name="logger"></param>
|
/// <param name="logger"></param>
|
||||||
public GithubScanService(
|
public GithubScanService(
|
||||||
DomainAddressFacotry domainAddressFactory,
|
GithubLookupFacotry domainAddressFactory,
|
||||||
GithubContextCollection scanResults,
|
GithubContextCollection scanResults,
|
||||||
IServiceProvider appService,
|
IServiceProvider appService,
|
||||||
ILogger<GithubScanService> logger)
|
ILogger<GithubScanService> logger)
|
||||||
|
|||||||
@ -7,7 +7,7 @@ namespace FastGithub.Scanner
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 定义域名的ip提值者
|
/// 定义域名的ip提值者
|
||||||
/// </summary>
|
/// </summary>
|
||||||
interface IDomainAddressProvider
|
interface IGithubLookupProvider
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取排序
|
/// 获取排序
|
||||||
@ -15,9 +15,11 @@ namespace FastGithub.Scanner
|
|||||||
int Order { get; }
|
int Order { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 创建域名与ip的关系
|
/// 查找域名与ip关系
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="domains"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<IEnumerable<DomainAddress>> CreateDomainAddressesAsync(CancellationToken cancellationToken);
|
Task<IEnumerable<DomainAddress>> LookupAsync(IEnumerable<string> domains, CancellationToken cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -11,15 +11,15 @@ using System.Text.Json.Serialization;
|
|||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace FastGithub.Scanner.DomainAddressProviders
|
namespace FastGithub.Scanner.LookupProviders
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Github公开的域名与ip关系提供者
|
/// Github公开的域名与ip关系提供者
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Service(ServiceLifetime.Singleton, ServiceType = typeof(IDomainAddressProvider))]
|
[Service(ServiceLifetime.Singleton, ServiceType = typeof(IGithubLookupProvider))]
|
||||||
sealed class GithubMetaProvider : IDomainAddressProvider
|
sealed class GithubMetaProvider : IGithubLookupProvider
|
||||||
{
|
{
|
||||||
private readonly IOptionsMonitor<GithubOptions> options;
|
private readonly IOptionsMonitor<GithubMetaProviderOptions> options;
|
||||||
private readonly IHttpClientFactory httpClientFactory;
|
private readonly IHttpClientFactory httpClientFactory;
|
||||||
private readonly ILogger<GithubMetaProvider> logger;
|
private readonly ILogger<GithubMetaProvider> logger;
|
||||||
private const string META_URI = "https://api.github.com/meta";
|
private const string META_URI = "https://api.github.com/meta";
|
||||||
@ -35,7 +35,7 @@ namespace FastGithub.Scanner.DomainAddressProviders
|
|||||||
/// <param name="options"></param>
|
/// <param name="options"></param>
|
||||||
/// <param name="logger"></param>
|
/// <param name="logger"></param>
|
||||||
public GithubMetaProvider(
|
public GithubMetaProvider(
|
||||||
IOptionsMonitor<GithubOptions> options,
|
IOptionsMonitor<GithubMetaProviderOptions> options,
|
||||||
IHttpClientFactory httpClientFactory,
|
IHttpClientFactory httpClientFactory,
|
||||||
ILogger<GithubMetaProvider> logger)
|
ILogger<GithubMetaProvider> logger)
|
||||||
{
|
{
|
||||||
@ -45,12 +45,14 @@ namespace FastGithub.Scanner.DomainAddressProviders
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 创建域名与ip的关系
|
/// 查找域名与ip关系
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="domains"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task<IEnumerable<DomainAddress>> CreateDomainAddressesAsync(CancellationToken cancellationToken)
|
public async Task<IEnumerable<DomainAddress>> LookupAsync(IEnumerable<string> domains, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var setting = this.options.CurrentValue.DominAddressProviders.GithubMetaProvider;
|
var setting = this.options.CurrentValue;
|
||||||
if (setting.Enable == false)
|
if (setting.Enable == false)
|
||||||
{
|
{
|
||||||
return Enumerable.Empty<DomainAddress>();
|
return Enumerable.Empty<DomainAddress>();
|
||||||
@ -62,7 +64,7 @@ namespace FastGithub.Scanner.DomainAddressProviders
|
|||||||
var meta = await GetMetaAsync(httpClient, setting.MetaUri, cancellationToken);
|
var meta = await GetMetaAsync(httpClient, setting.MetaUri, cancellationToken);
|
||||||
if (meta != null)
|
if (meta != null)
|
||||||
{
|
{
|
||||||
return meta.ToDomainAddresses();
|
return meta.ToDomainAddresses(domains);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@ -109,26 +111,35 @@ namespace FastGithub.Scanner.DomainAddressProviders
|
|||||||
/// 转换为域名与ip关系
|
/// 转换为域名与ip关系
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public IEnumerable<DomainAddress> ToDomainAddresses()
|
public IEnumerable<DomainAddress> ToDomainAddresses(IEnumerable<string> 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -0,0 +1,18 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace FastGithub.Scanner.LookupProviders
|
||||||
|
{
|
||||||
|
[Options("Github:Lookup:GithubMetaProvider")]
|
||||||
|
sealed class GithubMetaProviderOptions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 是否启用
|
||||||
|
/// </summary>
|
||||||
|
public bool Enable { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// meta请求uri
|
||||||
|
/// </summary>
|
||||||
|
public Uri MetaUri { get; set; } = new Uri("https://gitee.com/jiulang/fast-github/raw/master/FastGithub/meta.json");
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -10,15 +10,15 @@ using System.Text.RegularExpressions;
|
|||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace FastGithub.Scanner.DomainAddressProviders
|
namespace FastGithub.Scanner.LookupProviders
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// ipaddress.com的域名与ip关系提供者
|
/// ipaddress.com的域名与ip关系提供者
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Service(ServiceLifetime.Singleton, ServiceType = typeof(IDomainAddressProvider))]
|
[Service(ServiceLifetime.Singleton, ServiceType = typeof(IGithubLookupProvider))]
|
||||||
sealed class IPAddressComProvider : IDomainAddressProvider
|
sealed class IPAddressComProvider : IGithubLookupProvider
|
||||||
{
|
{
|
||||||
private readonly IOptionsMonitor<GithubOptions> options;
|
private readonly IOptionsMonitor<IPAddressComProviderOptions> options;
|
||||||
private readonly IHttpClientFactory httpClientFactory;
|
private readonly IHttpClientFactory httpClientFactory;
|
||||||
private readonly ILogger<IPAddressComProvider> logger;
|
private readonly ILogger<IPAddressComProvider> logger;
|
||||||
private readonly Uri lookupUri = new("https://www.ipaddress.com/ip-lookup");
|
private readonly Uri lookupUri = new("https://www.ipaddress.com/ip-lookup");
|
||||||
@ -34,7 +34,7 @@ namespace FastGithub.Scanner.DomainAddressProviders
|
|||||||
/// <param name="options"></param>
|
/// <param name="options"></param>
|
||||||
/// <param name="logger"></param>
|
/// <param name="logger"></param>
|
||||||
public IPAddressComProvider(
|
public IPAddressComProvider(
|
||||||
IOptionsMonitor<GithubOptions> options,
|
IOptionsMonitor<IPAddressComProviderOptions> options,
|
||||||
IHttpClientFactory httpClientFactory,
|
IHttpClientFactory httpClientFactory,
|
||||||
ILogger<IPAddressComProvider> logger)
|
ILogger<IPAddressComProvider> logger)
|
||||||
{
|
{
|
||||||
@ -44,12 +44,14 @@ namespace FastGithub.Scanner.DomainAddressProviders
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 创建域名与ip的关系
|
/// 查找域名与ip关系
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="domains"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task<IEnumerable<DomainAddress>> CreateDomainAddressesAsync(CancellationToken cancellationToken)
|
public async Task<IEnumerable<DomainAddress>> LookupAsync(IEnumerable<string> domains, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var setting = this.options.CurrentValue.DominAddressProviders.IPAddressComProvider;
|
var setting = this.options.CurrentValue;
|
||||||
if (setting.Enable == false)
|
if (setting.Enable == false)
|
||||||
{
|
{
|
||||||
return Enumerable.Empty<DomainAddress>();
|
return Enumerable.Empty<DomainAddress>();
|
||||||
@ -57,7 +59,7 @@ namespace FastGithub.Scanner.DomainAddressProviders
|
|||||||
|
|
||||||
var httpClient = this.httpClientFactory.CreateClient(nameof(FastGithub));
|
var httpClient = this.httpClientFactory.CreateClient(nameof(FastGithub));
|
||||||
var result = new HashSet<DomainAddress>();
|
var result = new HashSet<DomainAddress>();
|
||||||
foreach (var domain in setting.Domains)
|
foreach (var domain in domains)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
namespace FastGithub.Scanner.LookupProviders
|
||||||
|
{
|
||||||
|
[Options("Github:Lookup:IPAddressComProvider")]
|
||||||
|
sealed class IPAddressComProviderOptions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 是否启用
|
||||||
|
/// </summary>
|
||||||
|
public bool Enable { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -9,15 +9,15 @@ using System.Net.Sockets;
|
|||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace FastGithub.Scanner.DomainAddressProviders
|
namespace FastGithub.Scanner.LookupProviders
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 公共dns的域名与ip关系提供者
|
/// 公共dns的域名与ip关系提供者
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Service(ServiceLifetime.Singleton, ServiceType = typeof(IDomainAddressProvider))]
|
[Service(ServiceLifetime.Singleton, ServiceType = typeof(IGithubLookupProvider))]
|
||||||
sealed class PublicDnsProvider : IDomainAddressProvider
|
sealed class PublicDnsProvider : IGithubLookupProvider
|
||||||
{
|
{
|
||||||
private readonly IOptionsMonitor<GithubOptions> options;
|
private readonly IOptionsMonitor<PublicDnsProviderOptions> options;
|
||||||
private readonly ILogger<PublicDnsProvider> logger;
|
private readonly ILogger<PublicDnsProvider> logger;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -31,7 +31,7 @@ namespace FastGithub.Scanner.DomainAddressProviders
|
|||||||
/// <param name="options"></param>
|
/// <param name="options"></param>
|
||||||
/// <param name="logger"></param>
|
/// <param name="logger"></param>
|
||||||
public PublicDnsProvider(
|
public PublicDnsProvider(
|
||||||
IOptionsMonitor<GithubOptions> options,
|
IOptionsMonitor<PublicDnsProviderOptions> options,
|
||||||
ILogger<PublicDnsProvider> logger)
|
ILogger<PublicDnsProvider> logger)
|
||||||
{
|
{
|
||||||
this.options = options;
|
this.options = options;
|
||||||
@ -39,12 +39,14 @@ namespace FastGithub.Scanner.DomainAddressProviders
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 创建域名与ip的关系
|
/// 查找域名与ip关系
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="domains"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task<IEnumerable<DomainAddress>> CreateDomainAddressesAsync(CancellationToken cancellationToken)
|
public async Task<IEnumerable<DomainAddress>> LookupAsync(IEnumerable<string> domains, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var setting = this.options.CurrentValue.DominAddressProviders.PublicDnsProvider;
|
var setting = this.options.CurrentValue;
|
||||||
if (setting.Enable == false)
|
if (setting.Enable == false)
|
||||||
{
|
{
|
||||||
return Enumerable.Empty<DomainAddress>();
|
return Enumerable.Empty<DomainAddress>();
|
||||||
@ -53,7 +55,7 @@ namespace FastGithub.Scanner.DomainAddressProviders
|
|||||||
var result = new HashSet<DomainAddress>();
|
var result = new HashSet<DomainAddress>();
|
||||||
foreach (var dns in setting.Dnss)
|
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)
|
foreach (var item in domainAddresses)
|
||||||
{
|
{
|
||||||
result.Add(item);
|
result.Add(item);
|
||||||
@ -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<string>();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -17,7 +17,7 @@ namespace FastGithub.Scanner.ScanMiddlewares
|
|||||||
[Service(ServiceLifetime.Singleton)]
|
[Service(ServiceLifetime.Singleton)]
|
||||||
sealed class HttpsScanMiddleware : IMiddleware<GithubContext>
|
sealed class HttpsScanMiddleware : IMiddleware<GithubContext>
|
||||||
{
|
{
|
||||||
private readonly IOptionsMonitor<GithubOptions> options;
|
private readonly IOptionsMonitor<HttpsScanOptions> options;
|
||||||
private readonly IHttpClientFactory httpClientFactory;
|
private readonly IHttpClientFactory httpClientFactory;
|
||||||
private readonly ILogger<HttpsScanMiddleware> logger;
|
private readonly ILogger<HttpsScanMiddleware> logger;
|
||||||
|
|
||||||
@ -27,7 +27,7 @@ namespace FastGithub.Scanner.ScanMiddlewares
|
|||||||
/// <param name="options"></param>
|
/// <param name="options"></param>
|
||||||
/// <param name="logger"></param>
|
/// <param name="logger"></param>
|
||||||
public HttpsScanMiddleware(
|
public HttpsScanMiddleware(
|
||||||
IOptionsMonitor<GithubOptions> options,
|
IOptionsMonitor<HttpsScanOptions> options,
|
||||||
IHttpClientFactory httpClientFactory,
|
IHttpClientFactory httpClientFactory,
|
||||||
ILogger<HttpsScanMiddleware> logger)
|
ILogger<HttpsScanMiddleware> logger)
|
||||||
{
|
{
|
||||||
@ -55,7 +55,7 @@ namespace FastGithub.Scanner.ScanMiddlewares
|
|||||||
};
|
};
|
||||||
request.Headers.Host = context.Domain;
|
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 timeoutTokenSource = new CancellationTokenSource(timeout);
|
||||||
using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(timeoutTokenSource.Token, context.CancellationToken);
|
using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(timeoutTokenSource.Token, context.CancellationToken);
|
||||||
|
|
||||||
|
|||||||
10
FastGithub.Scanner/ScanMiddlewares/HttpsScanOptions.cs
Normal file
10
FastGithub.Scanner/ScanMiddlewares/HttpsScanOptions.cs
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -16,8 +16,7 @@ namespace FastGithub.Scanner.ScanMiddlewares
|
|||||||
sealed class TcpScanMiddleware : IMiddleware<GithubContext>
|
sealed class TcpScanMiddleware : IMiddleware<GithubContext>
|
||||||
{
|
{
|
||||||
private const int PORT = 443;
|
private const int PORT = 443;
|
||||||
private readonly TimeSpan cacheTimeSpan = TimeSpan.FromMinutes(20d);
|
private readonly IOptionsMonitor<TcpScanOptions> options;
|
||||||
private readonly IOptionsMonitor<GithubOptions> options;
|
|
||||||
private readonly IMemoryCache memoryCache;
|
private readonly IMemoryCache memoryCache;
|
||||||
private readonly ILogger<TcpScanMiddleware> logger;
|
private readonly ILogger<TcpScanMiddleware> logger;
|
||||||
|
|
||||||
@ -27,7 +26,7 @@ namespace FastGithub.Scanner.ScanMiddlewares
|
|||||||
/// <param name="options"></param>
|
/// <param name="options"></param>
|
||||||
/// <param name="logger"></param>
|
/// <param name="logger"></param>
|
||||||
public TcpScanMiddleware(
|
public TcpScanMiddleware(
|
||||||
IOptionsMonitor<GithubOptions> options,
|
IOptionsMonitor<TcpScanOptions> options,
|
||||||
IMemoryCache memoryCache,
|
IMemoryCache memoryCache,
|
||||||
ILogger<TcpScanMiddleware> logger)
|
ILogger<TcpScanMiddleware> logger)
|
||||||
{
|
{
|
||||||
@ -48,7 +47,7 @@ namespace FastGithub.Scanner.ScanMiddlewares
|
|||||||
if (this.memoryCache.TryGetValue<bool>(key, out var available) == false)
|
if (this.memoryCache.TryGetValue<bool>(key, out var available) == false)
|
||||||
{
|
{
|
||||||
available = await this.TcpScanAsync(context);
|
available = await this.TcpScanAsync(context);
|
||||||
this.memoryCache.Set(key, available, cacheTimeSpan);
|
this.memoryCache.Set(key, available, this.options.CurrentValue.CacheExpiration);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (available == true)
|
if (available == true)
|
||||||
@ -71,7 +70,7 @@ namespace FastGithub.Scanner.ScanMiddlewares
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var timeout = this.options.CurrentValue.Scan.TcpScanTimeout;
|
var timeout = this.options.CurrentValue.Timeout;
|
||||||
using var timeoutTokenSource = new CancellationTokenSource(timeout);
|
using var timeoutTokenSource = new CancellationTokenSource(timeout);
|
||||||
using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(timeoutTokenSource.Token, context.CancellationToken);
|
using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(timeoutTokenSource.Token, context.CancellationToken);
|
||||||
|
|
||||||
|
|||||||
18
FastGithub.Scanner/ScanMiddlewares/TcpScanOptions.cs
Normal file
18
FastGithub.Scanner/ScanMiddlewares/TcpScanOptions.cs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace FastGithub.Scanner.ScanMiddlewares
|
||||||
|
{
|
||||||
|
[Options("Github:Scan:TcpScan")]
|
||||||
|
sealed class TcpScanOptions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 扫描超时时长
|
||||||
|
/// </summary>
|
||||||
|
public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(1d);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 扫描结果缓存时长
|
||||||
|
/// </summary>
|
||||||
|
public TimeSpan CacheExpiration { get; set; } = TimeSpan.FromMinutes(20d);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -7,21 +7,26 @@
|
|||||||
"Scan": {
|
"Scan": {
|
||||||
"FullScanInterval": "02:00:00", // 完整扫描时间间隔
|
"FullScanInterval": "02:00:00", // 完整扫描时间间隔
|
||||||
"ResultScanInterval": "00:01:00", // 结果扫描时间间隔
|
"ResultScanInterval": "00:01:00", // 结果扫描时间间隔
|
||||||
"TcpScanTimeout": "00:00:01", // tcp扫描超时时间
|
"TcpScan": {
|
||||||
"HttpsScanTimeout": "00:00:05" // https扫描超时时间
|
"Timeout": "00:00:01", // tcp扫描超时时间
|
||||||
},
|
"CacheExpiration": "00:20:00" // 扫描结果缓存时长
|
||||||
"DominAddressProviders": {
|
|
||||||
"GithubMetaProvider": {
|
|
||||||
"Enable": true,
|
|
||||||
"MetaUri": "https://gitee.com/jiulang/fast-github/raw/master/FastGithub/meta.json"
|
|
||||||
},
|
},
|
||||||
|
"HttpsScan": {
|
||||||
|
"Timeout": "00:00:05" // https扫描超时时间
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Lookup": {
|
||||||
|
"Domains": [
|
||||||
|
"github.com",
|
||||||
|
"api.github.com",
|
||||||
|
"github.githubassets.com"
|
||||||
|
],
|
||||||
"IPAddressComProvider": {
|
"IPAddressComProvider": {
|
||||||
|
"Enable": true
|
||||||
|
},
|
||||||
|
"GithubMetaProvider": {
|
||||||
"Enable": true,
|
"Enable": true,
|
||||||
"Domains": [
|
"MetaUri": "https://gitee.com/jiulang/fast-github/raw/master/FastGithub/meta.json"
|
||||||
"github.com",
|
|
||||||
"api.github.com",
|
|
||||||
"github.githubassets.com"
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
"PublicDnsProvider": {
|
"PublicDnsProvider": {
|
||||||
"Enable": true,
|
"Enable": true,
|
||||||
@ -34,11 +39,6 @@
|
|||||||
"1.2.4.8",
|
"1.2.4.8",
|
||||||
"208.67.220.220",
|
"208.67.220.220",
|
||||||
"123.125.81.6"
|
"123.125.81.6"
|
||||||
],
|
|
||||||
"Domains": [
|
|
||||||
"github.com",
|
|
||||||
"api.github.com",
|
|
||||||
"github.githubassets.com"
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user