完善扫描机制

This commit is contained in:
陈国伟 2021-06-15 16:40:45 +08:00
parent d2189462b0
commit 52083c459b
5 changed files with 73 additions and 24 deletions

View File

@ -44,18 +44,16 @@ namespace FastGithub
var domain = question.Name.ToString();
if (domain.Contains("github", StringComparison.OrdinalIgnoreCase))
{
var contexts = this.githubScanService.Result
.Where(item => item.Domain == domain)
.OrderBy(item => item.HttpElapsed);
foreach (var context in contexts)
var ttl = TimeSpan.FromMinutes(2d);
var addressArray = this.githubScanService.FindAddress(domain);
foreach (var address in addressArray)
{
var record = new IPAddressResourceRecord(question.Name, context.Address, TimeSpan.FromMinutes(2d));
var record = new IPAddressResourceRecord(question.Name, address, ttl);
response.AnswerRecords.Add(record);
this.logger.LogWarning(record.ToString());
}
return Task.FromResult<IResponse>(response);
var addressString = string.Join(',', addressArray.Select(a => a.ToString()));
this.logger.LogInformation($"{domain}=>{addressString}");
}
}

View File

@ -3,15 +3,15 @@ using System.Net;
namespace FastGithub
{
class GithubContext
class GithubContext : IEquatable<GithubContext>
{
public string Domain { get; }
public string Domain { get; }
public IPAddress Address { get; }
public IPAddress Address { get; }
public TimeSpan? HttpElapsed { get; set; }
public GithubContext(string domain,IPAddress address)
public GithubContext(string domain, IPAddress address)
{
this.Domain = domain;
this.Address = address;
@ -21,5 +21,20 @@ namespace FastGithub
{
return $"{Address}\t{Domain}\t# {HttpElapsed}";
}
public override bool Equals(object? obj)
{
return obj is GithubContext other && this.Equals(other);
}
public bool Equals(GithubContext? other)
{
return other != null && other.Address.Equals(this.Address) && other.Domain == this.Domain;
}
public override int GetHashCode()
{
return HashCode.Combine(this.Domain, this.Address);
}
}
}

View File

@ -0,0 +1,9 @@
using System.Collections.Generic;
namespace FastGithub
{
class GithubContextHashSet : HashSet<GithubContext>
{
public readonly object SyncRoot = new();
}
}

View File

@ -1,5 +1,6 @@
using System.Collections.Concurrent;
using Microsoft.Extensions.Logging;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
@ -9,15 +10,17 @@ namespace FastGithub
{
private readonly GithubMetaService metaService;
private readonly GithubScanDelegate scanDelegate;
public ConcurrentQueue<GithubContext> Result { get; } = new();
private readonly ILogger<GithubScanService> logger;
private readonly GithubContextHashSet results = new();
public GithubScanService(
GithubMetaService metaService,
GithubScanDelegate scanDelegate)
GithubScanDelegate scanDelegate,
ILogger<GithubScanService> logger)
{
this.metaService = metaService;
this.scanDelegate = scanDelegate;
this.logger = logger;
}
public async Task ScanAllAsync(CancellationToken cancellationToken = default)
@ -25,26 +28,50 @@ namespace FastGithub
var meta = await this.metaService.GetMetaAsync(cancellationToken);
if (meta != null)
{
this.Result.Clear();
var scanTasks = meta.ToGithubContexts().Select(ctx => ScanAsync(ctx));
await Task.WhenAll(scanTasks);
}
this.logger.LogInformation("完全扫描完成");
async Task ScanAsync(GithubContext context)
{
await this.scanDelegate(context);
if (context.HttpElapsed != null)
{
lock (this.results.SyncRoot)
{
this.results.Add(context);
}
}
}
}
public async Task ScanResultAsync()
{
while (this.Result.TryDequeue(out var context))
GithubContext[] contexts;
lock (this.results.SyncRoot)
{
await this.ScanAsync(context);
contexts = this.results.ToArray();
}
foreach (var context in contexts)
{
await this.scanDelegate(context);
}
this.logger.LogInformation("结果扫描完成");
}
private async Task ScanAsync(GithubContext context)
public IPAddress[] FindAddress(string domain)
{
await this.scanDelegate(context);
if (context.HttpElapsed != null)
lock (this.results.SyncRoot)
{
this.Result.Enqueue(context);
return this.results
.Where(item => item.Domain == domain && item.HttpElapsed != null)
.OrderBy(item => item.HttpElapsed)
.Select(item => item.Address)
.ToArray();
}
}
}

View File

@ -7,7 +7,7 @@
"launchBrowser": true,
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Development",
"Logging__LogLevel__Default": "Information"
"Logging__LogLevel__Default": "Trace"
}
}
}