diff --git a/core/BaseScanStrategy.go b/core/BaseScanStrategy.go index 21fc8f5..a0a86a8 100644 --- a/core/BaseScanStrategy.go +++ b/core/BaseScanStrategy.go @@ -122,14 +122,8 @@ func (b *BaseScanStrategy) IsPluginApplicableByName(pluginName string, targetHos // pluginExists 检查插件是否存在,不创建实例 func (b *BaseScanStrategy) pluginExists(pluginName string) bool { - // 使用All()获取所有注册插件名称,避免调用Get()创建实例 - allPlugins := plugins.All() - for _, name := range allPlugins { - if name == pluginName { - return true - } - } - return false + // 使用统一插件系统的Exists方法,避免创建实例和遍历 + return plugins.Exists(pluginName) } // getPluginPorts 获取插件端口列表 diff --git a/core/Scanner.go b/core/Scanner.go index 26ab631..0c3b323 100644 --- a/core/Scanner.go +++ b/core/Scanner.go @@ -122,7 +122,8 @@ func ExecuteScanTasks(targets []common.HostInfo, strategy ScanStrategy, ch *chan } for _, pluginName := range pluginsToRun { - if plugins.Get(pluginName) == nil { + // 使用Exists检查避免不必要的插件实例创建 + if !plugins.Exists(pluginName) { continue } @@ -144,7 +145,8 @@ func countApplicableTasks(targets []common.HostInfo, pluginsToRun []string, isCu } for _, pluginName := range pluginsToRun { - if plugins.Get(pluginName) != nil && + // 使用Exists检查避免不必要的插件实例创建 + if plugins.Exists(pluginName) && strategy.IsPluginApplicableByName(pluginName, target.Host, targetPort, isCustomMode) { count++ } diff --git a/plugins/init.go b/plugins/init.go index 5da694f..6f852a2 100644 --- a/plugins/init.go +++ b/plugins/init.go @@ -55,26 +55,31 @@ type Credential struct { KeyData []byte } +// PluginInfo 插件信息结构 - 合并工厂和端口信息 +type PluginInfo struct { + factory func() Plugin + ports []int +} + // 全局插件注册表 - 一个数据结构解决所有问题 var ( - plugins = make(map[string]func() Plugin) - pluginPorts = make(map[string][]int) // 存储插件端口信息 + plugins = make(map[string]*PluginInfo) mutex sync.RWMutex ) // Register 注册插件 - 一个函数统治所有注册 func Register(name string, factory func() Plugin) { - mutex.Lock() - defer mutex.Unlock() - plugins[name] = factory + RegisterWithPorts(name, factory, []int{}) } // RegisterWithPorts 注册带端口信息的插件 func RegisterWithPorts(name string, factory func() Plugin, ports []int) { mutex.Lock() defer mutex.Unlock() - plugins[name] = factory - pluginPorts[name] = ports + plugins[name] = &PluginInfo{ + factory: factory, + ports: ports, + } } // Get 获取插件实例 @@ -82,8 +87,8 @@ func Get(name string) Plugin { mutex.RLock() defer mutex.RUnlock() - if factory, exists := plugins[name]; exists { - return factory() + if info, exists := plugins[name]; exists { + return info.factory() } return nil } @@ -100,13 +105,22 @@ func All() []string { return names } +// Exists 检查插件是否存在,不创建实例 - 解决性能问题 +func Exists(name string) bool { + mutex.RLock() + defer mutex.RUnlock() + + _, exists := plugins[name] + return exists +} + // GetPluginPorts 获取插件端口列表 func GetPluginPorts(name string) []int { mutex.RLock() defer mutex.RUnlock() - if ports, exists := pluginPorts[name]; exists { - return ports + if info, exists := plugins[name]; exists { + return info.ports } return []int{} // 返回空列表表示适用于所有端口 }