mirror of
https://github.com/shadow1ng/fscan.git
synced 2026-09-22 03:10:42 +08:00
95 lines
1.8 KiB
Go
95 lines
1.8 KiB
Go
package common
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/shadow1ng/fscan/common/i18n"
|
|
)
|
|
|
|
/*
|
|
initialize.go - 统一初始化入口
|
|
|
|
简化后的流程:
|
|
命令行 → FlagVars → BuildConfig() → Config + State
|
|
*/
|
|
|
|
// InitResult 初始化结果
|
|
type InitResult struct {
|
|
Config *Config
|
|
State *State
|
|
Info *HostInfo
|
|
Session *ScanSession
|
|
}
|
|
|
|
// Initialize 统一初始化函数
|
|
// 封装 BuildConfig → InitOutput 流程
|
|
func Initialize(info *HostInfo) (*InitResult, error) {
|
|
// 1. 初始化日志系统
|
|
InitLogger()
|
|
|
|
// 2. 从 FlagVars 构建 Config 和 State
|
|
cfg, state, err := BuildConfig(GetFlagVars(), info)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%s: %w", i18n.GetText("config_build_failed"), err)
|
|
}
|
|
|
|
// 3. 设置全局实例
|
|
SetGlobalConfig(cfg)
|
|
SetGlobalState(state)
|
|
|
|
// 4. 初始化输出系统
|
|
if err := InitOutput(); err != nil {
|
|
return nil, fmt.Errorf("%s: %w", i18n.GetText("output_init_failed"), err)
|
|
}
|
|
|
|
session := NewScanSession(cfg, state, GetFlagVars())
|
|
|
|
return &InitResult{
|
|
Config: cfg,
|
|
State: state,
|
|
Info: info,
|
|
Session: session,
|
|
}, nil
|
|
}
|
|
|
|
// ValidateExclusiveParams 验证互斥参数
|
|
// 检查 -h、-u、-local 只能指定一个
|
|
func ValidateExclusiveParams(info *HostInfo) error {
|
|
paramCount := 0
|
|
var activeParam string
|
|
|
|
fv := GetFlagVars()
|
|
|
|
if info.Host != "" {
|
|
paramCount++
|
|
activeParam = "-h"
|
|
}
|
|
if fv.TargetURL != "" {
|
|
paramCount++
|
|
if activeParam != "" {
|
|
activeParam = i18n.Tr("param_join_and", activeParam, "-u")
|
|
} else {
|
|
activeParam = "-u"
|
|
}
|
|
}
|
|
if fv.LocalPlugin != "" {
|
|
paramCount++
|
|
if activeParam != "" {
|
|
activeParam = i18n.Tr("param_join_and", activeParam, "-local")
|
|
} else {
|
|
activeParam = "-local"
|
|
}
|
|
}
|
|
|
|
if paramCount > 1 {
|
|
return fmt.Errorf("%s", i18n.Tr("param_exclusive", activeParam))
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Cleanup 清理资源
|
|
func Cleanup() error {
|
|
return CloseOutput()
|
|
}
|