feat: 优化智能Web检测性能,避免重复调用

- 添加检测结果缓存机制,避免同一端口重复HTTP探测
- 使用线程安全的读写锁保护缓存访问
- 显著减少网络请求次数,提升扫描速度
- 支持多主机扫描的缓存键设计
- 保持架构兼容性的同时优化性能表现
This commit is contained in:
ZacharyZcR 2025-08-12 16:05:20 +08:00
parent b706fb46bb
commit c6bfb5f064

View File

@ -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
}