完善扫描机制

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(); var domain = question.Name.ToString();
if (domain.Contains("github", StringComparison.OrdinalIgnoreCase)) if (domain.Contains("github", StringComparison.OrdinalIgnoreCase))
{ {
var contexts = this.githubScanService.Result var ttl = TimeSpan.FromMinutes(2d);
.Where(item => item.Domain == domain) var addressArray = this.githubScanService.FindAddress(domain);
.OrderBy(item => item.HttpElapsed); foreach (var address in addressArray)
foreach (var context in contexts)
{ {
var record = new IPAddressResourceRecord(question.Name, context.Address, TimeSpan.FromMinutes(2d)); var record = new IPAddressResourceRecord(question.Name, address, ttl);
response.AnswerRecords.Add(record); 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,7 +3,7 @@ using System.Net;
namespace FastGithub namespace FastGithub
{ {
class GithubContext class GithubContext : IEquatable<GithubContext>
{ {
public string Domain { get; } public string Domain { get; }
@ -21,5 +21,20 @@ namespace FastGithub
{ {
return $"{Address}\t{Domain}\t# {HttpElapsed}"; 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.Linq;
using System.Net;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -9,15 +10,17 @@ namespace FastGithub
{ {
private readonly GithubMetaService metaService; private readonly GithubMetaService metaService;
private readonly GithubScanDelegate scanDelegate; private readonly GithubScanDelegate scanDelegate;
private readonly ILogger<GithubScanService> logger;
public ConcurrentQueue<GithubContext> Result { get; } = new(); private readonly GithubContextHashSet results = new();
public GithubScanService( public GithubScanService(
GithubMetaService metaService, GithubMetaService metaService,
GithubScanDelegate scanDelegate) GithubScanDelegate scanDelegate,
ILogger<GithubScanService> logger)
{ {
this.metaService = metaService; this.metaService = metaService;
this.scanDelegate = scanDelegate; this.scanDelegate = scanDelegate;
this.logger = logger;
} }
public async Task ScanAllAsync(CancellationToken cancellationToken = default) public async Task ScanAllAsync(CancellationToken cancellationToken = default)
@ -25,26 +28,50 @@ namespace FastGithub
var meta = await this.metaService.GetMetaAsync(cancellationToken); var meta = await this.metaService.GetMetaAsync(cancellationToken);
if (meta != null) if (meta != null)
{ {
this.Result.Clear();
var scanTasks = meta.ToGithubContexts().Select(ctx => ScanAsync(ctx)); var scanTasks = meta.ToGithubContexts().Select(ctx => ScanAsync(ctx));
await Task.WhenAll(scanTasks); 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() 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();
}
} }
private async Task ScanAsync(GithubContext context) foreach (var context in contexts)
{ {
await this.scanDelegate(context); await this.scanDelegate(context);
if (context.HttpElapsed != null) }
this.logger.LogInformation("结果扫描完成");
}
public IPAddress[] FindAddress(string domain)
{ {
this.Result.Enqueue(context); lock (this.results.SyncRoot)
{
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, "launchBrowser": true,
"environmentVariables": { "environmentVariables": {
"DOTNET_ENVIRONMENT": "Development", "DOTNET_ENVIRONMENT": "Development",
"Logging__LogLevel__Default": "Information" "Logging__LogLevel__Default": "Trace"
} }
} }
} }