From 01fac4fcb8d1c1f47fd355d7bff3bcbd04c96ae7 Mon Sep 17 00:00:00 2001
From: xljiulang <366193849@qq.com>
Date: Fri, 18 Jun 2021 20:01:19 +0800
Subject: [PATCH] =?UTF-8?q?=E7=BC=93=E5=AD=98tcp=E6=89=AB=E6=8F=8F?=
=?UTF-8?q?=E7=BB=93=E6=9E=9C?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
FastGithub.Scanner/FastGithub.Scanner.csproj | 1 +
.../ScanMiddlewares/TcpScanMiddleware.cs | 37 +++++++++++++++++--
.../ScannerServiceCollectionExtensions.cs | 1 +
3 files changed, 35 insertions(+), 4 deletions(-)
diff --git a/FastGithub.Scanner/FastGithub.Scanner.csproj b/FastGithub.Scanner/FastGithub.Scanner.csproj
index f0f0cd2..8c98877 100644
--- a/FastGithub.Scanner/FastGithub.Scanner.csproj
+++ b/FastGithub.Scanner/FastGithub.Scanner.csproj
@@ -8,6 +8,7 @@
+
diff --git a/FastGithub.Scanner/ScanMiddlewares/TcpScanMiddleware.cs b/FastGithub.Scanner/ScanMiddlewares/TcpScanMiddleware.cs
index d7a3698..c4f3138 100644
--- a/FastGithub.Scanner/ScanMiddlewares/TcpScanMiddleware.cs
+++ b/FastGithub.Scanner/ScanMiddlewares/TcpScanMiddleware.cs
@@ -1,4 +1,5 @@
-using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Caching.Memory;
+using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
@@ -15,7 +16,9 @@ namespace FastGithub.Scanner.ScanMiddlewares
sealed class TcpScanMiddleware : IMiddleware
{
private const int PORT = 443;
+ private readonly TimeSpan cacheTimeSpan = TimeSpan.FromMinutes(20d);
private readonly IOptionsMonitor options;
+ private readonly IMemoryCache memoryCache;
private readonly ILogger logger;
///
@@ -25,9 +28,11 @@ namespace FastGithub.Scanner.ScanMiddlewares
///
public TcpScanMiddleware(
IOptionsMonitor options,
+ IMemoryCache memoryCache,
ILogger logger)
{
this.options = options;
+ this.memoryCache = memoryCache;
this.logger = logger;
}
@@ -38,6 +43,31 @@ namespace FastGithub.Scanner.ScanMiddlewares
///
///
public async Task InvokeAsync(GithubContext context, Func next)
+ {
+ var key = $"tcp://{context.Address}";
+ if (this.memoryCache.TryGetValue(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}端口未开放");
+ }
+ }
+
+
+ ///
+ /// tcp扫描
+ ///
+ ///
+ ///
+ private async Task TcpScanAsync(GithubContext context)
{
try
{
@@ -45,12 +75,11 @@ namespace FastGithub.Scanner.ScanMiddlewares
var timeout = this.options.CurrentValue.Scan.TcpScanTimeout;
using var cancellationTokenSource = new CancellationTokenSource(timeout);
await socket.ConnectAsync(context.Address, PORT, cancellationTokenSource.Token);
-
- await next();
+ return true;
}
catch (Exception)
{
- this.logger.LogTrace($"{context.Domain} {context.Address}的{PORT}端口未开放");
+ return false;
}
}
}
diff --git a/FastGithub.Scanner/ScannerServiceCollectionExtensions.cs b/FastGithub.Scanner/ScannerServiceCollectionExtensions.cs
index 73a3c6d..e945c8a 100644
--- a/FastGithub.Scanner/ScannerServiceCollectionExtensions.cs
+++ b/FastGithub.Scanner/ScannerServiceCollectionExtensions.cs
@@ -39,6 +39,7 @@ namespace FastGithub
});
return services
+ .AddMemoryCache()
.AddServiceAndOptions(assembly, configuration)
.AddHostedService()
.AddHostedService()