From c6bfb5f064f09a8706c9c11d8e0ab2acd31bcfd8 Mon Sep 17 00:00:00 2001 From: ZacharyZcR Date: Tue, 12 Aug 2025 16:05:20 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E6=99=BA=E8=83=BDWeb?= =?UTF-8?q?=E6=A3=80=E6=B5=8B=E6=80=A7=E8=83=BD=EF=BC=8C=E9=81=BF=E5=85=8D?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加检测结果缓存机制,避免同一端口重复HTTP探测 - 使用线程安全的读写锁保护缓存访问 - 显著减少网络请求次数,提升扫描速度 - 支持多主机扫描的缓存键设计 - 保持架构兼容性的同时优化性能表现 --- core/WebDetection.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/core/WebDetection.go b/core/WebDetection.go index 464d6d9..37a099c 100644 --- a/core/WebDetection.go +++ b/core/WebDetection.go @@ -9,6 +9,7 @@ import ( "regexp" "strconv" "strings" + "sync" "time" "github.com/shadow1ng/fscan/common" @@ -24,6 +25,10 @@ type WebPortDetector struct { httpClient *http.Client // HTTPS客户端 httpsClient *http.Client + // 检测结果缓存(避免重复检测) + detectionCache map[string]bool + // 缓存互斥锁 + cacheMutex sync.RWMutex } // NewWebPortDetector 创建Web端口检测器 @@ -119,6 +124,7 @@ func NewWebPortDetector() *WebPortDetector { httpTimeout: timeout, httpClient: httpClient, httpsClient: httpsClient, + detectionCache: make(map[string]bool), } } @@ -130,9 +136,25 @@ func (w *WebPortDetector) IsWebService(host string, port int) bool { return true } - // 2. 智能路径:对非常见端口进行HTTP协议探测 + // 2. 缓存检查:避免重复检测同一端口 + cacheKey := fmt.Sprintf("%s:%d", host, port) + w.cacheMutex.RLock() + if cached, exists := w.detectionCache[cacheKey]; exists { + w.cacheMutex.RUnlock() + common.LogDebug(fmt.Sprintf("端口 %d 使用缓存结果: %v", port, cached)) + return cached + } + w.cacheMutex.RUnlock() + + // 3. 智能路径:对非常见端口进行HTTP协议探测 common.LogDebug(fmt.Sprintf("对端口 %d 进行智能Web检测", port)) result := w.detectHTTPService(host, port) + + // 4. 缓存结果 + w.cacheMutex.Lock() + w.detectionCache[cacheKey] = result + w.cacheMutex.Unlock() + common.LogDebug(fmt.Sprintf("端口 %d 智能检测结果: %v", port, result)) return result }