缓存tcp扫描结果
This commit is contained in:
parent
7fccdba5cf
commit
01fac4fcb8
@ -8,6 +8,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="DNS" Version="6.1.0" />
|
<PackageReference Include="DNS" Version="6.1.0" />
|
||||||
<PackageReference Include="IPNetwork2" Version="2.5.320" />
|
<PackageReference Include="IPNetwork2" Version="2.5.320" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="5.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Http" Version="5.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Http" Version="5.0.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using System;
|
using System;
|
||||||
@ -15,7 +16,9 @@ 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<GithubOptions> options;
|
private readonly IOptionsMonitor<GithubOptions> options;
|
||||||
|
private readonly IMemoryCache memoryCache;
|
||||||
private readonly ILogger<TcpScanMiddleware> logger;
|
private readonly ILogger<TcpScanMiddleware> logger;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -25,9 +28,11 @@ namespace FastGithub.Scanner.ScanMiddlewares
|
|||||||
/// <param name="logger"></param>
|
/// <param name="logger"></param>
|
||||||
public TcpScanMiddleware(
|
public TcpScanMiddleware(
|
||||||
IOptionsMonitor<GithubOptions> options,
|
IOptionsMonitor<GithubOptions> options,
|
||||||
|
IMemoryCache memoryCache,
|
||||||
ILogger<TcpScanMiddleware> logger)
|
ILogger<TcpScanMiddleware> logger)
|
||||||
{
|
{
|
||||||
this.options = options;
|
this.options = options;
|
||||||
|
this.memoryCache = memoryCache;
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,6 +43,31 @@ namespace FastGithub.Scanner.ScanMiddlewares
|
|||||||
/// <param name="next"></param>
|
/// <param name="next"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task InvokeAsync(GithubContext context, Func<Task> next)
|
public async Task InvokeAsync(GithubContext context, Func<Task> next)
|
||||||
|
{
|
||||||
|
var key = $"tcp://{context.Address}";
|
||||||
|
if (this.memoryCache.TryGetValue<bool>(key, out var available) == false)
|
||||||
|
{
|
||||||
|
available = await this.TcpScanAsync(context);
|
||||||
|
this.memoryCache.Set(key, available, cacheTimeSpan);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (available == true)
|
||||||
|
{
|
||||||
|
await next();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this.logger.LogTrace($"{context.Domain} {context.Address}的{PORT}端口未开放");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// tcp扫描
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="context"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
private async Task<bool> TcpScanAsync(GithubContext context)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -45,12 +75,11 @@ namespace FastGithub.Scanner.ScanMiddlewares
|
|||||||
var timeout = this.options.CurrentValue.Scan.TcpScanTimeout;
|
var timeout = this.options.CurrentValue.Scan.TcpScanTimeout;
|
||||||
using var cancellationTokenSource = new CancellationTokenSource(timeout);
|
using var cancellationTokenSource = new CancellationTokenSource(timeout);
|
||||||
await socket.ConnectAsync(context.Address, PORT, cancellationTokenSource.Token);
|
await socket.ConnectAsync(context.Address, PORT, cancellationTokenSource.Token);
|
||||||
|
return true;
|
||||||
await next();
|
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
this.logger.LogTrace($"{context.Domain} {context.Address}的{PORT}端口未开放");
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -39,6 +39,7 @@ namespace FastGithub
|
|||||||
});
|
});
|
||||||
|
|
||||||
return services
|
return services
|
||||||
|
.AddMemoryCache()
|
||||||
.AddServiceAndOptions(assembly, configuration)
|
.AddServiceAndOptions(assembly, configuration)
|
||||||
.AddHostedService<GithubFullScanHostedService>()
|
.AddHostedService<GithubFullScanHostedService>()
|
||||||
.AddHostedService<GithubResultScanHostedService>()
|
.AddHostedService<GithubResultScanHostedService>()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user