mirror of
https://github.com/shadow1ng/fscan.git
synced 2026-09-24 12:11:52 +08:00
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:
+31
-11
@@ -139,26 +139,46 @@ func MakeVarDecl(key, value string) *exprpb.Decl {
|
||||
}
|
||||
}
|
||||
|
||||
// Evaluate 评估 CEL 表达式
|
||||
// CelProgCache 缓存编译后的 CEL Program,避免同一 POC 内重复编译
|
||||
// 在 executePoc 中创建,同一个 POC 的所有规则/参数组合共享
|
||||
type CelProgCache map[string]cel.Program
|
||||
|
||||
// Evaluate 评估 CEL 表达式(无缓存,用于 Set/Sets 求值等低频路径)
|
||||
func Evaluate(env *cel.Env, expression string, params map[string]interface{}) (ref.Val, error) {
|
||||
// 空表达式默认返回 true
|
||||
return EvaluateCached(env, expression, params, nil)
|
||||
}
|
||||
|
||||
// EvaluateCached 评估 CEL 表达式(带编译缓存,用于规则执行热路径)
|
||||
func EvaluateCached(env *cel.Env, expression string, params map[string]interface{}, cache CelProgCache) (ref.Val, error) {
|
||||
if expression == "" {
|
||||
return types.Bool(true), nil
|
||||
}
|
||||
|
||||
// 编译表达式
|
||||
ast, issues := env.Compile(expression)
|
||||
if issues.Err() != nil {
|
||||
return nil, fmt.Errorf("%s: %w", i18n.GetText("webscan_expression_compile_failed"), issues.Err())
|
||||
var program cel.Program
|
||||
|
||||
if cache != nil {
|
||||
if cached, ok := cache[expression]; ok {
|
||||
program = cached
|
||||
}
|
||||
}
|
||||
|
||||
// 创建程序(使用缓存的程序选项)
|
||||
program, err := env.Program(ast, GetBaseProgramOptions()...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: %w", i18n.GetText("webscan_program_create_failed"), err)
|
||||
if program == nil {
|
||||
ast, issues := env.Compile(expression)
|
||||
if issues.Err() != nil {
|
||||
return nil, fmt.Errorf("%s: %w", i18n.GetText("webscan_expression_compile_failed"), issues.Err())
|
||||
}
|
||||
|
||||
var err error
|
||||
program, err = env.Program(ast, GetBaseProgramOptions()...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: %w", i18n.GetText("webscan_program_create_failed"), err)
|
||||
}
|
||||
|
||||
if cache != nil {
|
||||
cache[expression] = program
|
||||
}
|
||||
}
|
||||
|
||||
// 执行评估
|
||||
result, _, err := program.Eval(params)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: %w", i18n.GetText("webscan_expression_eval_failed"), err)
|
||||
|
||||
Reference in New Issue
Block a user