增加https扫描规则

This commit is contained in:
xljiulang 2021-06-19 14:10:27 +08:00
parent 960f4efe18
commit e04456878c
10 changed files with 77 additions and 14 deletions

View File

@ -30,10 +30,10 @@ namespace FastGithub.Scanner
} }
/// <summary> /// <summary>
/// 创建域名与ip的关系 /// 查找域名与ip关系
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public async Task<IEnumerable<DomainAddress>> CreateDomainAddressesAsync(CancellationToken cancellationToken) public async Task<IEnumerable<DomainAddress>> LookupAsync(CancellationToken cancellationToken)
{ {
var hashSet = new HashSet<DomainAddress>(); var hashSet = new HashSet<DomainAddress>();
var domains = this.options.CurrentValue.Domains; var domains = this.options.CurrentValue.Domains;

View File

@ -14,7 +14,7 @@ namespace FastGithub.Scanner
[Service(ServiceLifetime.Singleton)] [Service(ServiceLifetime.Singleton)]
sealed class GithubScanService sealed class GithubScanService
{ {
private readonly GithubLookupFacotry domainAddressFactory; private readonly GithubLookupFacotry lookupFactory;
private readonly GithubContextCollection scanResults; private readonly GithubContextCollection scanResults;
private readonly ILogger<GithubScanService> logger; private readonly ILogger<GithubScanService> logger;
@ -24,17 +24,17 @@ namespace FastGithub.Scanner
/// <summary> /// <summary>
/// github扫描服务 /// github扫描服务
/// </summary> /// </summary>
/// <param name="domainAddressFactory"></param> /// <param name="lookupFactory"></param>
/// <param name="scanResults"></param> /// <param name="scanResults"></param>
/// <param name="appService"></param> /// <param name="appService"></param>
/// <param name="logger"></param> /// <param name="logger"></param>
public GithubScanService( public GithubScanService(
GithubLookupFacotry domainAddressFactory, GithubLookupFacotry lookupFactory,
GithubContextCollection scanResults, GithubContextCollection scanResults,
IServiceProvider appService, IServiceProvider appService,
ILogger<GithubScanService> logger) ILogger<GithubScanService> logger)
{ {
this.domainAddressFactory = domainAddressFactory; this.lookupFactory = lookupFactory;
this.scanResults = scanResults; this.scanResults = scanResults;
this.logger = logger; this.logger = logger;
@ -58,7 +58,7 @@ namespace FastGithub.Scanner
public async Task ScanAllAsync(CancellationToken cancellationToken) public async Task ScanAllAsync(CancellationToken cancellationToken)
{ {
this.logger.LogInformation("完整扫描开始.."); this.logger.LogInformation("完整扫描开始..");
var domainAddresses = await this.domainAddressFactory.CreateDomainAddressesAsync(cancellationToken); var domainAddresses = await this.lookupFactory.LookupAsync(cancellationToken);
var scanTasks = domainAddresses var scanTasks = domainAddresses
.Select(item => new GithubContext(item.Domain, item.Address, cancellationToken)) .Select(item => new GithubContext(item.Domain, item.Address, cancellationToken))

View File

@ -2,6 +2,9 @@
namespace FastGithub.Scanner.LookupProviders namespace FastGithub.Scanner.LookupProviders
{ {
/// <summary>
/// Github公开的域名与ip关系提供者选项
/// </summary>
[Options("Github:Lookup:GithubMetaProvider")] [Options("Github:Lookup:GithubMetaProvider")]
sealed class GithubMetaProviderOptions sealed class GithubMetaProviderOptions
{ {

View File

@ -1,5 +1,8 @@
namespace FastGithub.Scanner.LookupProviders namespace FastGithub.Scanner.LookupProviders
{ {
/// <summary>
/// ipaddress.com的域名与ip关系提供者选项
/// </summary>
[Options("Github:Lookup:IPAddressComProvider")] [Options("Github:Lookup:IPAddressComProvider")]
sealed class IPAddressComProviderOptions sealed class IPAddressComProviderOptions
{ {

View File

@ -2,11 +2,20 @@
namespace FastGithub.Scanner.LookupProviders namespace FastGithub.Scanner.LookupProviders
{ {
/// <summary>
/// 公共dns的域名与ip关系提供者选项
/// </summary>
[Options("Github:Lookup:PublicDnsProvider")] [Options("Github:Lookup:PublicDnsProvider")]
sealed class PublicDnsProviderOptions sealed class PublicDnsProviderOptions
{ {
/// <summary>
/// 是否启用
/// </summary>
public bool Enable { get; set; } = true; public bool Enable { get; set; } = true;
/// <summary>
/// dns列表
/// </summary>
public string[] Dnss { get; set; } = Array.Empty<string>(); public string[] Dnss { get; set; } = Array.Empty<string>();
} }
} }

View File

@ -48,11 +48,15 @@ namespace FastGithub.Scanner.ScanMiddlewares
{ {
context.Available = false; context.Available = false;
var request = new HttpRequestMessage var setting = this.options.CurrentValue;
if (setting.Rules.TryGetValue(context.Domain, out var rule) == false)
{ {
Method = HttpMethod.Head, rule = new HttpsScanOptions.ScanRule();
RequestUri = new Uri($"https://{context.Address}"), }
};
using var request = new HttpRequestMessage();
request.Method = new HttpMethod(rule.Method);
request.RequestUri = new Uri(new Uri($"https://{context.Address}"), rule.Path);
request.Headers.Host = context.Domain; request.Headers.Host = context.Domain;
var timeout = this.options.CurrentValue.Timeout; var timeout = this.options.CurrentValue.Timeout;

View File

@ -1,10 +1,38 @@
using System; using System;
using System.Collections.Generic;
namespace FastGithub.Scanner.ScanMiddlewares namespace FastGithub.Scanner.ScanMiddlewares
{ {
/// <summary>
/// https扫描选项
/// </summary>
[Options("Github:Scan:HttpsScan")] [Options("Github:Scan:HttpsScan")]
sealed class HttpsScanOptions sealed class HttpsScanOptions
{ {
/// <summary>
/// 扫描超时时长
/// </summary>
public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(5d); public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(5d);
/// <summary>
/// 各域名扫描规则
/// </summary>
public Dictionary<string, ScanRule> Rules { get; set; } = new Dictionary<string, ScanRule>();
/// <summary>
/// 扫描规则
/// </summary>
public class ScanRule
{
/// <summary>
/// 请求方式
/// </summary>
public string Method { get; set; } = "HEAD";
/// <summary>
/// 请求路径
/// </summary>
public string Path { get; set; } = "/";
}
} }
} }

View File

@ -74,7 +74,7 @@ namespace FastGithub.Scanner.ScanMiddlewares
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);
using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); using var socket = new Socket(context.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
await socket.ConnectAsync(context.Address, PORT, linkedTokenSource.Token); await socket.ConnectAsync(context.Address, PORT, linkedTokenSource.Token);
return true; return true;
} }

View File

@ -2,6 +2,9 @@
namespace FastGithub.Scanner.ScanMiddlewares namespace FastGithub.Scanner.ScanMiddlewares
{ {
/// <summary>
/// tcp扫描选项
/// </summary>
[Options("Github:Scan:TcpScan")] [Options("Github:Scan:TcpScan")]
sealed class TcpScanOptions sealed class TcpScanOptions
{ {

View File

@ -12,14 +12,27 @@
"CacheExpiration": "00:20:00" // "CacheExpiration": "00:20:00" //
}, },
"HttpsScan": { "HttpsScan": {
"Timeout": "00:00:05" // https "Timeout": "00:00:05", // https
"Rules": {
"github.githubassets.com": {
"Path": "/favicons/favicon.png"
},
"avatars.githubusercontent.com": {
"Path": "/u/8308014?s=40&v=4"
},
"raw.githubusercontent.com": {
"Path": "/xljiulang/FastGithub/master/README.md"
}
}
} }
}, },
"Lookup": { "Lookup": {
"Domains": [ "Domains": [
"github.com", "github.com",
"api.github.com", "api.github.com",
"github.githubassets.com" "github.githubassets.com",
"raw.githubusercontent.com",
"avatars.githubusercontent.com"
], ],
"IPAddressComProvider": { "IPAddressComProvider": {
"Enable": true "Enable": true