From cb89558ce6776b79c23326769c987a96e36c0af0 Mon Sep 17 00:00:00 2001 From: ZacharyZcR Date: Wed, 6 Aug 2025 07:23:17 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=B8=85=E7=90=86common/core?= =?UTF-8?q?=E5=8C=85=E4=B8=AD=E7=9A=84=E6=AD=BB=E4=BB=A3=E7=A0=81=E5=B9=B6?= =?UTF-8?q?=E7=B2=BE=E7=AE=80=E5=B8=B8=E9=87=8F=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 删除未使用的扫描模式、服务类型、输出格式等常量定义 - 移除Manager.go中的死代码访问器函数 - 清理Plugin.go中的未使用插件管理方法 - 简化配置管理架构,保留核心功能 - 总计删除约200行死代码,提升代码维护性 --- Common/Core/Constants.go | 89 ++-------------------- Common/Core/Manager.go | 53 +------------ Common/Core/Plugin.go | 161 ++------------------------------------- Common/common.go | 4 +- 4 files changed, 15 insertions(+), 292 deletions(-) diff --git a/Common/Core/Constants.go b/Common/Core/Constants.go index 2e0ed4a..0c6df78 100644 --- a/Common/Core/Constants.go +++ b/Common/Core/Constants.go @@ -21,12 +21,7 @@ var ( // ============================================================================= const ( - ScanModeAll = "all" // 全扫描模式 - ScanModePortScan = "portscan" // 端口扫描模式 - ScanModeTcpScan = "tcpscan" // TCP扫描模式 - ScanModeUdpScan = "udpscan" // UDP扫描模式 - ScanModeWebScan = "webscan" // Web扫描模式 - ScanModeNoPoc = "nopoc" // 无POC扫描模式 + ScanModeAll = "all" // 全扫描模式 ) // ============================================================================= @@ -34,81 +29,9 @@ const ( // ============================================================================= const ( - DefaultThreadNum = 600 // 默认线程数 - DefaultModuleThreadNum = 6 // 默认模块线程数 - DefaultTimeout = 3 // 默认超时时间(秒) - DefaultWebTimeout = 10 // 默认Web超时时间(秒) - DefaultGlobalTimeout = 7200 // 默认全局超时时间(秒) - DefaultLiveTop = 1000 // 默认存活探测端口数 - DefaultPocNum = 20 // 默认POC线程数 - DefaultScanMode = ScanModeAll // 默认扫描模式 - DefaultLanguage = "zh" // 默认语言 - DefaultLogLevel = "base" // 默认日志级别 - DefaultOutputFormat = "txt" // 默认输出格式 - DefaultUserAgent = "fscan/2.0.2" // 默认User-Agent - DefaultMaxTargets = 10000 // 默认最大目标数量 - DefaultMaxPorts = 5000 // 默认最大端口数量 - DefaultMaxCredentials = 1000 // 默认最大凭据数量 + DefaultThreadNum = 600 // 默认线程数 + DefaultTimeout = 3 // 默认超时时间(秒) + DefaultScanMode = ScanModeAll // 默认扫描模式 + DefaultLanguage = "zh" // 默认语言 + DefaultLogLevel = "base" // 默认日志级别 ) - -// ============================================================================= -// 服务类型常量 -// ============================================================================= - -const ( - ServiceTypeSSH = "ssh" // SSH服务 - ServiceTypeFTP = "ftp" // FTP服务 - ServiceTypeSMB = "smb" // SMB服务 - ServiceTypeMySQL = "mysql" // MySQL服务 - ServiceTypeMSSQL = "mssql" // MSSQL服务 - ServiceTypeOracle = "oracle" // Oracle服务 - ServiceTypeRedis = "redis" // Redis服务 - ServiceTypeMongo = "mongo" // MongoDB服务 - ServiceTypeHTTP = "http" // HTTP服务 - ServiceTypeHTTPS = "https" // HTTPS服务 - ServiceTypeTelnet = "telnet" // Telnet服务 - ServiceTypeLDAP = "ldap" // LDAP服务 - ServiceTypeVNC = "vnc" // VNC服务 - ServiceTypeRDP = "rdp" // RDP服务 -) - -// ============================================================================= -// 输出格式常量 -// ============================================================================= - -const ( - OutputFormatTXT = "txt" // 文本格式 - OutputFormatJSON = "json" // JSON格式 - OutputFormatCSV = "csv" // CSV格式 - OutputFormatXML = "xml" // XML格式 -) - -// ============================================================================= -// 错误常量 -// ============================================================================= - -const ( - ErrInvalidIP = "invalid_ip" // 无效IP地址 - ErrInvalidPort = "invalid_port" // 无效端口 - ErrInvalidURL = "invalid_url" // 无效URL - ErrFileNotFound = "file_not_found" // 文件未找到 - ErrConnectionTimeout = "connection_timeout" // 连接超时 - ErrConnectionRefused = "connection_refused" // 连接被拒绝 - ErrAuthenticationFail = "authentication_fail" // 认证失败 - ErrPermissionDenied = "permission_denied" // 权限不足 - ErrInvalidFormat = "invalid_format" // 格式无效 - ErrResourceNotFound = "resource_not_found" // 资源未找到 -) - -// ============================================================================= -// 版本信息常量 -// ============================================================================= - -const ( - Version = "2.0.2" // 版本号 - BuildTime = "2024-01-01 00:00:00" // 构建时间 - GitCommit = "unknown" // Git提交哈希 - Description = "Fast comprehensive scanner" // 项目描述 - Author = "shadow1ng" // 作者 - Repository = "github.com/shadow1ng/fscan" // 仓库地址 -) \ No newline at end of file diff --git a/Common/Core/Manager.go b/Common/Core/Manager.go index e8c0f1e..b66b50e 100644 --- a/Common/Core/Manager.go +++ b/Common/Core/Manager.go @@ -6,29 +6,6 @@ import ( "github.com/shadow1ng/fscan/common/config" ) -/* -Manager.go - 简化的核心管理器 - -移除了复杂的配置管理器架构,保留核心常量和必要的全局变量, -大幅简化代码结构,提高性能和可维护性。 -*/ - -// ============================================================================= -// 核心常量定义 -// ============================================================================= - -// 日志级别常量 -const ( - LogLevelAll = "all" - LogLevelError = "error" - LogLevelBase = "base" - LogLevelInfo = "info" - LogLevelSuccess = "success" - LogLevelDebug = "debug" - LogLevelInfoSuccess = "info,success" - LogLevelBaseInfoSuccess = "base,info,success" -) - // ============================================================================= // 全局配置变量 // ============================================================================= @@ -60,13 +37,6 @@ var ( PortMap map[int][]string // 端口映射 DefaultMap []string // 默认映射 - // 输出配置变量 - Outputfile string // 输出文件 - OutputFormat string // 输出格式 - - // 其他全局状态 - SlowLogOutput bool // 慢速日志输出 - // 初始化锁 initOnce sync.Once ) @@ -104,29 +74,10 @@ func InitGlobalConfig() { } // ============================================================================= -// 简化的访问函数(保持向后兼容) +// 访问器函数已移除(未使用的死代码) +// 直接使用全局变量进行访问 // ============================================================================= -// GetScanMode 获取扫描模式 -func GetScanMode() string { - return ScanMode -} - -// SetScanMode 设置扫描模式 -func SetScanMode(mode string) { - ScanMode = mode -} - -// GetThreadNum 获取线程数 -func GetThreadNum() int { - return ThreadNum -} - -// SetThreadNum 设置线程数 -func SetThreadNum(num int) { - ThreadNum = num -} - // init 自动初始化 func init() { InitGlobalConfig() diff --git a/Common/Core/Plugin.go b/Common/Core/Plugin.go index 6c514cf..edb9965 100644 --- a/Common/Core/Plugin.go +++ b/Common/Core/Plugin.go @@ -162,157 +162,11 @@ func (pm *PluginManager) RegisterPlugin(plugin *ScanPlugin) error { return nil } -// GetPlugin 获取指定名称的插件 -func (pm *PluginManager) GetPlugin(name string) (*ScanPlugin, bool) { - pm.mu.RLock() - defer pm.mu.RUnlock() - plugin, exists := pm.plugins[name] - return plugin, exists -} - -// GetPluginsByType 获取指定类型的所有插件 -func (pm *PluginManager) GetPluginsByType(pluginType string) []*ScanPlugin { - pm.mu.RLock() - defer pm.mu.RUnlock() - - plugins := make([]*ScanPlugin, 0) - if typePlugins, exists := pm.types[pluginType]; exists { - for _, plugin := range typePlugins { - if plugin.IsEnabled() { - plugins = append(plugins, plugin) - } - } - } - return plugins -} - -// GetPluginsByPort 获取支持指定端口的所有插件 -func (pm *PluginManager) GetPluginsByPort(port int) []*ScanPlugin { - pm.mu.RLock() - defer pm.mu.RUnlock() - - plugins := make([]*ScanPlugin, 0) - - // 检查按端口索引的插件 - if portPlugins, exists := pm.ports[port]; exists { - for _, plugin := range portPlugins { - if plugin.IsEnabled() { - plugins = append(plugins, plugin) - } - } - } - - // 检查支持所有端口的插件 - for _, plugin := range pm.plugins { - if len(plugin.Ports) == 0 && plugin.IsEnabled() { - plugins = append(plugins, plugin) - } - } - - return plugins -} - -// GetAllPlugins 获取所有插件 -func (pm *PluginManager) GetAllPlugins() map[string]*ScanPlugin { - pm.mu.RLock() - defer pm.mu.RUnlock() - - // 创建副本避免并发修改 - result := make(map[string]*ScanPlugin) - for name, plugin := range pm.plugins { - result[name] = plugin - } - return result -} - -// EnablePlugin 启用插件 -func (pm *PluginManager) EnablePlugin(name string) error { - pm.mu.Lock() - defer pm.mu.Unlock() - - plugin, exists := pm.plugins[name] - if !exists { - return fmt.Errorf("plugin %s not found", name) - } - - plugin.Enabled = true - return nil -} - -// DisablePlugin 禁用插件 -func (pm *PluginManager) DisablePlugin(name string) error { - pm.mu.Lock() - defer pm.mu.Unlock() - - plugin, exists := pm.plugins[name] - if !exists { - return fmt.Errorf("plugin %s not found", name) - } - - plugin.Enabled = false - return nil -} - -// UnregisterPlugin 注销插件 -func (pm *PluginManager) UnregisterPlugin(name string) error { - pm.mu.Lock() - defer pm.mu.Unlock() - - plugin, exists := pm.plugins[name] - if !exists { - return fmt.Errorf("plugin %s not found", name) - } - - // 从主索引删除 - delete(pm.plugins, name) - - // 从类型索引删除 - for _, pluginType := range plugin.Types { - if typePlugins, exists := pm.types[pluginType]; exists { - for i, p := range typePlugins { - if p.Name == name { - pm.types[pluginType] = append(typePlugins[:i], typePlugins[i+1:]...) - break - } - } - } - } - - // 从端口索引删除 - for _, port := range plugin.Ports { - if portPlugins, exists := pm.ports[port]; exists { - for i, p := range portPlugins { - if p.Name == name { - pm.ports[port] = append(portPlugins[:i], portPlugins[i+1:]...) - break - } - } - } - } - - return nil -} - -// GetPluginCount 获取插件总数 -func (pm *PluginManager) GetPluginCount() int { - pm.mu.RLock() - defer pm.mu.RUnlock() - return len(pm.plugins) -} - -// GetEnabledPluginCount 获取启用的插件数量 -func (pm *PluginManager) GetEnabledPluginCount() int { - pm.mu.RLock() - defer pm.mu.RUnlock() - - count := 0 - for _, plugin := range pm.plugins { - if plugin.IsEnabled() { - count++ - } - } - return count -} +// ======================================================================================== +// 未使用的插件管理器方法已删除(死代码清理) +// 包括:GetPlugin, GetPluginsByType, GetPluginsByPort, GetAllPlugins, +// EnablePlugin, DisablePlugin, UnregisterPlugin, GetPluginCount, GetEnabledPluginCount +// ======================================================================================== // ============================================================================= // 全局插件管理函数 (保持向后兼容) @@ -341,10 +195,7 @@ func RegisterPlugin(name string, plugin ScanPlugin) error { return nil } -// GetGlobalPluginManager 获取全局插件管理器 -func GetGlobalPluginManager() *PluginManager { - return globalPluginManager -} +// GetGlobalPluginManager 方法已删除(死代码清理) // 向后兼容的全局变量 (已废弃,建议使用PluginManager) var LegacyPluginManager = make(map[string]ScanPlugin) \ No newline at end of file diff --git a/Common/common.go b/Common/common.go index e72209d..a4c8e09 100644 --- a/Common/common.go +++ b/Common/common.go @@ -51,9 +51,7 @@ func RegisterPlugin(name string, plugin ScanPlugin) { } } -func GetGlobalPluginManager() *core.PluginManager { - return core.GetGlobalPluginManager() -} +// GetGlobalPluginManager 函数已删除(死代码清理) // ============================================================================= // 日志系统简化接口