diff --git a/core/BaseScanStrategy.go b/core/BaseScanStrategy.go index c443d36..5fe078c 100644 --- a/core/BaseScanStrategy.go +++ b/core/BaseScanStrategy.go @@ -194,15 +194,15 @@ func (b *BaseScanStrategy) isLocalPlugin(pluginName string) bool { // isWebServicePort 使用智能检测判断端口是否运行Web服务(含预定义端口) func (b *BaseScanStrategy) isWebServicePort(host string, port int) bool { - // 创建Web端口检测器实例 - detector := NewWebPortDetector() + // 使用全局单例Web端口检测器 + detector := GetWebPortDetector() return detector.IsWebService(host, port) } // isWebServicePortByProtocol 仅通过HTTP协议检测判断端口是否运行Web服务(不使用预定义端口列表) func (b *BaseScanStrategy) isWebServicePortByProtocol(host string, port int) bool { - // 创建Web端口检测器实例 - detector := NewWebPortDetector() + // 使用全局单例Web端口检测器 + detector := GetWebPortDetector() // 直接调用协议检测,跳过预定义端口检查 return detector.DetectHTTPServiceOnly(host, port) } diff --git a/core/WebDetection.go b/core/WebDetection.go index 1494e36..8627aa2 100644 --- a/core/WebDetection.go +++ b/core/WebDetection.go @@ -25,12 +25,28 @@ type WebPortDetector struct { httpsClient *http.Client // 检测结果缓存(避免重复检测) detectionCache map[string]bool + // 协议检测缓存(避免重复TCP连接) + protocolCache map[string]bool // 缓存互斥锁 cacheMutex sync.RWMutex } -// NewWebPortDetector 创建Web端口检测器 -func NewWebPortDetector() *WebPortDetector { +var ( + // 全局单例实例 + webDetectorInstance *WebPortDetector + webDetectorOnce sync.Once +) + +// GetWebPortDetector 获取Web端口检测器单例 +func GetWebPortDetector() *WebPortDetector { + webDetectorOnce.Do(func() { + webDetectorInstance = newWebPortDetector() + }) + return webDetectorInstance +} + +// newWebPortDetector 创建Web端口检测器(私有方法) +func newWebPortDetector() *WebPortDetector { timeout := 3 * time.Second // 只保留最基本的Web端口用于快速路径优化 @@ -82,6 +98,7 @@ func NewWebPortDetector() *WebPortDetector { httpClient: httpClient, httpsClient: httpsClient, detectionCache: make(map[string]bool), + protocolCache: make(map[string]bool), } } @@ -350,6 +367,28 @@ func (w *WebPortDetector) isNonWebHTTPService(serverHeader, contentType string) // quickProtocolCheck 快速协议检查,避免向非HTTP服务发送HTTP请求 func (w *WebPortDetector) quickProtocolCheck(ctx context.Context, host string, port int) bool { + // 检查协议缓存 + protocolKey := fmt.Sprintf("%s:%d:protocol", host, port) + w.cacheMutex.RLock() + if cached, exists := w.protocolCache[protocolKey]; exists { + w.cacheMutex.RUnlock() + return cached + } + w.cacheMutex.RUnlock() + + // 执行实际的协议检测 + result := w.doProtocolCheck(ctx, host, port) + + // 缓存结果 + w.cacheMutex.Lock() + w.protocolCache[protocolKey] = result + w.cacheMutex.Unlock() + + return result +} + +// doProtocolCheck 执行实际的协议检测 +func (w *WebPortDetector) doProtocolCheck(ctx context.Context, host string, port int) bool { // 建立TCP连接 conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", host, port), 2*time.Second) if err != nil { diff --git a/fscan-lite/main.o b/fscan-lite/main.o new file mode 100644 index 0000000..821513a Binary files /dev/null and b/fscan-lite/main.o differ diff --git a/fscan-lite/platform.o b/fscan-lite/platform.o new file mode 100644 index 0000000..540ca79 Binary files /dev/null and b/fscan-lite/platform.o differ diff --git a/fscan-lite/scanner.o b/fscan-lite/scanner.o new file mode 100644 index 0000000..149790f Binary files /dev/null and b/fscan-lite/scanner.o differ