完善扫描机制
This commit is contained in:
parent
d2189462b0
commit
52083c459b
@ -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}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -3,15 +3,15 @@ using System.Net;
|
|||||||
|
|
||||||
namespace FastGithub
|
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 TimeSpan? HttpElapsed { get; set; }
|
||||||
|
|
||||||
public GithubContext(string domain,IPAddress address)
|
public GithubContext(string domain, IPAddress address)
|
||||||
{
|
{
|
||||||
this.Domain = domain;
|
this.Domain = domain;
|
||||||
this.Address = address;
|
this.Address = address;
|
||||||
@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
9
FastGithub/GithubContextHashSet.cs
Normal file
9
FastGithub/GithubContextHashSet.cs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace FastGithub
|
||||||
|
{
|
||||||
|
class GithubContextHashSet : HashSet<GithubContext>
|
||||||
|
{
|
||||||
|
public readonly object SyncRoot = new();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
lock (this.results.SyncRoot)
|
||||||
if (context.HttpElapsed != null)
|
|
||||||
{
|
{
|
||||||
this.Result.Enqueue(context);
|
return this.results
|
||||||
|
.Where(item => item.Domain == domain && item.HttpElapsed != null)
|
||||||
|
.OrderBy(item => item.HttpElapsed)
|
||||||
|
.Select(item => item.Address)
|
||||||
|
.ToArray();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,7 +7,7 @@
|
|||||||
"launchBrowser": true,
|
"launchBrowser": true,
|
||||||
"environmentVariables": {
|
"environmentVariables": {
|
||||||
"DOTNET_ENVIRONMENT": "Development",
|
"DOTNET_ENVIRONMENT": "Development",
|
||||||
"Logging__LogLevel__Default": "Information"
|
"Logging__LogLevel__Default": "Trace"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user