refactor: 4项架构优化 — CEL缓存/POC隔离/服务缓存/结果统一

1. CEL 表达式编译缓存
   - 新增 CelProgCache,同一 POC 的所有规则/参数组合共享编译后的 Program
   - clusterpoc 热路径上消除重复的 Compile+Program 调用

2. POC 全局状态消除
   - allPocs/pocLoaded 全局变量改为 pocStore 按 PocPath 缓存
   - 不同 PocPath 的扫描独立加载,Web API 并发场景不再互相覆盖

3. serviceCache 下沉到 per-session State
   - 服务识别缓存从包级全局 map 迁移到 State.serviceCache (sync.Map)
   - BaseScanStrategy 通过 SetState 注入 session state
   - 消除多个并发扫描之间的服务识别缓存串台

4. POC 结果输出路径统一
   - 提取 buildVulnDetails/buildVulnLogMsg/saveVulnResult 三个公共函数
   - CheckMultiPoc 和 recordVulnerabilityResult 共用统一的结果构造逻辑
   - 消除 details 字段名不一致和日志格式差异
This commit is contained in:
ZacharyZcR
2026-06-14 22:23:49 +08:00
parent a115499793
commit d4f4e65dec
10 changed files with 261 additions and 230 deletions
+63 -32
View File
@@ -208,12 +208,9 @@ func (w *WebPortDetector) tryHTTP(ctx context.Context, client *http.Client, sess
// 基于服务指纹的Web服务识别
// ===============================
// 服务识别缓存 - 存储所有识别到的服务(不仅限于 Web
// 端口扫描阶段写入,插件匹配阶段读取
var (
serviceCache = make(map[string]*ServiceInfo)
serviceCacheMutex sync.RWMutex
)
// globalState 全局 State 兼容指针(向后兼容不接受 State 的旧调用方
// 新代码应通过 State 方法访问服务缓存
var globalState *common.State
// IsWebServiceByFingerprint 基于服务指纹判断Web服务 - 保持API兼容
// 服务识别规则 - 编译期常量,避免运行时分配
@@ -279,50 +276,84 @@ func isDefinitelyNonWeb(serviceInfo *ServiceInfo) bool {
return false
}
// CacheServiceInfo 缓存识别到的服务信息
func CacheServiceInfo(host string, port int, serviceInfo *ServiceInfo) {
cacheKey := net.JoinHostPort(host, strconv.Itoa(port))
serviceCacheMutex.Lock()
defer serviceCacheMutex.Unlock()
serviceCache[cacheKey] = serviceInfo
// SetGlobalState 设置全局 StateRunScan 入口调用,兼容旧代码路径)
func SetGlobalState(state *common.State) {
globalState = state
}
// MarkAsWebService 标记 Web 服务(兼容旧调用)
func resolveState(state *common.State) *common.State {
if state != nil {
return state
}
return globalState
}
// CacheServiceInfoWithState 缓存服务信息到指定 State
func CacheServiceInfoWithState(state *common.State, host string, port int, serviceInfo *ServiceInfo) {
s := resolveState(state)
if s == nil {
return
}
key := net.JoinHostPort(host, strconv.Itoa(port))
s.CacheService(key, serviceInfo)
}
// CacheServiceInfo 兼容旧调用(使用全局 State)
func CacheServiceInfo(host string, port int, serviceInfo *ServiceInfo) {
CacheServiceInfoWithState(nil, host, port, serviceInfo)
}
// MarkAsWebService 标记 Web 服务
func MarkAsWebService(host string, port int, serviceInfo *ServiceInfo) {
CacheServiceInfo(host, port, serviceInfo)
}
// GetCachedServiceInfo 获取缓存的服务信息
func GetCachedServiceInfo(host string, port int) (*ServiceInfo, bool) {
cacheKey := net.JoinHostPort(host, strconv.Itoa(port))
serviceCacheMutex.RLock()
defer serviceCacheMutex.RUnlock()
serviceInfo, exists := serviceCache[cacheKey]
return serviceInfo, exists
}
// GetWebServiceInfo 获取 Web 服务信息(兼容旧调用)
func GetWebServiceInfo(host string, port int) (*ServiceInfo, bool) {
info, exists := GetCachedServiceInfo(host, port)
if !exists {
// GetCachedServiceInfoWithState 从指定 State 获取缓存的服务信息
func GetCachedServiceInfoWithState(state *common.State, host string, port int) (*ServiceInfo, bool) {
s := resolveState(state)
if s == nil {
return nil, false
}
if !IsWebServiceByFingerprint(info) {
key := net.JoinHostPort(host, strconv.Itoa(port))
val, ok := s.GetCachedService(key)
if !ok {
return nil, false
}
info, ok := val.(*ServiceInfo)
return info, ok
}
// GetCachedServiceInfo 兼容旧调用
func GetCachedServiceInfo(host string, port int) (*ServiceInfo, bool) {
return GetCachedServiceInfoWithState(nil, host, port)
}
// GetWebServiceInfo 获取 Web 服务信息
func GetWebServiceInfo(host string, port int) (*ServiceInfo, bool) {
return GetWebServiceInfoWithState(nil, host, port)
}
// GetWebServiceInfoWithState 从指定 State 获取 Web 服务信息
func GetWebServiceInfoWithState(state *common.State, host string, port int) (*ServiceInfo, bool) {
info, exists := GetCachedServiceInfoWithState(state, host, port)
if !exists || !IsWebServiceByFingerprint(info) {
return nil, false
}
return info, true
}
// IsMarkedWebService 检查是否为 Web 服务
// IsMarkedWebService 检查是否为 Web 服务(使用全局 State
func IsMarkedWebService(host string, port int) bool {
_, exists := GetWebServiceInfo(host, port)
return exists
}
// IsMarkedWebServiceWithState 检查是否为 Web 服务(指定 State)
func IsMarkedWebServiceWithState(state *common.State, host string, port int) bool {
_, exists := GetWebServiceInfoWithState(state, host, port)
return exists
}
// ===============================
// Web扫描策略
// ===============================