From 8c3039506f2e2ac4501fa07da2461eb426033ae3 Mon Sep 17 00:00:00 2001 From: ZacharyZcR Date: Thu, 7 Aug 2025 06:31:05 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E5=91=BD=E5=90=8Dcommon/c?= =?UTF-8?q?ore=E4=B8=BAcommon/base=E5=B9=B6=E7=A7=BB=E9=99=A4LegacyParser?= =?UTF-8?q?=E4=B8=AD=E9=97=B4=E5=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 重命名common/core包为common/base,解决与根目录core包名冲突问题 - 移除common/parsers/LegacyParser.go向后兼容层,简化代码架构 - 将SimpleParseIP/SimpleParsePort等函数重命名为ParseIP/ParsePort - 更新所有相关引用和导入路径 - 删除41行冗余代码,提升代码可维护性 --- Common/Ports.go | 6 +- Common/{Core => base}/Constants.go | 2 +- Common/{Core => base}/Manager.go | 2 +- Common/{Core => base}/Plugin.go | 2 +- Common/common.go | 22 ++--- Common/globals.go | 100 ++++++++++---------- Common/parsers/LegacyParser.go | 41 -------- Common/parsers/LegacyParser_test.go | 142 ---------------------------- Common/parsers/Simple.go | 16 ++-- 9 files changed, 75 insertions(+), 258 deletions(-) rename Common/{Core => base}/Constants.go (99%) rename Common/{Core => base}/Manager.go (99%) rename Common/{Core => base}/Plugin.go (99%) delete mode 100644 Common/parsers/LegacyParser.go delete mode 100644 Common/parsers/LegacyParser_test.go diff --git a/Common/Ports.go b/Common/Ports.go index 7172aab..fb6600c 100644 --- a/Common/Ports.go +++ b/Common/Ports.go @@ -1,6 +1,6 @@ package common -import "github.com/shadow1ng/fscan/common/core" +import "github.com/shadow1ng/fscan/common/base" /* Ports.go - 端口常量(向后兼容层) @@ -10,6 +10,6 @@ Ports.go - 端口常量(向后兼容层) // 向后兼容的端口常量 - 引用Core包中的定义 var ( - WebPorts = core.WebPorts // Web服务端口组 - MainPorts = core.MainPorts // 主要服务端口组 + WebPorts = base.WebPorts // Web服务端口组 + MainPorts = base.MainPorts // 主要服务端口组 ) diff --git a/Common/Core/Constants.go b/Common/base/Constants.go similarity index 99% rename from Common/Core/Constants.go rename to Common/base/Constants.go index 0c6df78..bc5cf59 100644 --- a/Common/Core/Constants.go +++ b/Common/base/Constants.go @@ -1,4 +1,4 @@ -package core +package base /* Constants.go - 核心常量定义 diff --git a/Common/Core/Manager.go b/Common/base/Manager.go similarity index 99% rename from Common/Core/Manager.go rename to Common/base/Manager.go index b66b50e..b36cb99 100644 --- a/Common/Core/Manager.go +++ b/Common/base/Manager.go @@ -1,4 +1,4 @@ -package core +package base import ( "sync" diff --git a/Common/Core/Plugin.go b/Common/base/Plugin.go similarity index 99% rename from Common/Core/Plugin.go rename to Common/base/Plugin.go index edb9965..a4ac429 100644 --- a/Common/Core/Plugin.go +++ b/Common/base/Plugin.go @@ -1,4 +1,4 @@ -package core +package base import ( "fmt" diff --git a/Common/common.go b/Common/common.go index e7a93a4..060fc8d 100644 --- a/Common/common.go +++ b/Common/common.go @@ -15,7 +15,7 @@ import ( "sync" "time" - "github.com/shadow1ng/fscan/common/core" + "github.com/shadow1ng/fscan/common/base" "github.com/shadow1ng/fscan/common/logging" "github.com/shadow1ng/fscan/common/output" ) @@ -24,21 +24,21 @@ import ( // 核心类型导出 - 直接从core模块导出 // ============================================================================= -type HostInfo = core.HostInfo -type ScanPlugin = core.ScanPlugin +type HostInfo = base.HostInfo +type ScanPlugin = base.ScanPlugin // 插件类型常量 const ( - PluginTypeService = core.PluginTypeService - PluginTypeWeb = core.PluginTypeWeb - PluginTypeLocal = core.PluginTypeLocal - PluginTypeBrute = core.PluginTypeBrute - PluginTypePoc = core.PluginTypePoc - PluginTypeScan = core.PluginTypeScan + PluginTypeService = base.PluginTypeService + PluginTypeWeb = base.PluginTypeWeb + PluginTypeLocal = base.PluginTypeLocal + PluginTypeBrute = base.PluginTypeBrute + PluginTypePoc = base.PluginTypePoc + PluginTypeScan = base.PluginTypeScan ) // 全局插件管理器 -var PluginManager = core.LegacyPluginManager +var PluginManager = base.LegacyPluginManager // ============================================================================= // 核心功能导出 - 直接调用对应模块 @@ -46,7 +46,7 @@ var PluginManager = core.LegacyPluginManager // 插件系统 func RegisterPlugin(name string, plugin ScanPlugin) { - if err := core.RegisterPlugin(name, plugin); err != nil { + if err := base.RegisterPlugin(name, plugin); err != nil { LogError("Failed to register plugin " + name + ": " + err.Error()) } } diff --git a/Common/globals.go b/Common/globals.go index 8ad7c0e..afb4fd8 100644 --- a/Common/globals.go +++ b/Common/globals.go @@ -5,7 +5,7 @@ import ( "time" "github.com/schollz/progressbar/v3" - "github.com/shadow1ng/fscan/common/core" + "github.com/shadow1ng/fscan/common/base" "github.com/shadow1ng/fscan/common/logging" ) @@ -35,11 +35,11 @@ var startTimeInit = time.Now() // ============================================================================= var ( - ScanMode string // 直接映射到core.ScanMode - ThreadNum int // 直接映射到core.ThreadNum - Timeout int64 // 直接映射到core.Timeout - DisablePing bool // 直接映射到core.DisablePing - LocalMode bool // 直接映射到core.LocalMode + ScanMode string // 直接映射到base.ScanMode + ThreadNum int // 直接映射到base.ThreadNum + Timeout int64 // 直接映射到base.Timeout + DisablePing bool // 直接映射到base.DisablePing + LocalMode bool // 直接映射到base.LocalMode ) // ============================================================================= @@ -47,10 +47,10 @@ var ( // ============================================================================= var ( - Username string // 直接映射到core.Username - Password string // 直接映射到core.Password - Userdict map[string][]string // 直接映射到core.Userdict - Passwords []string // 直接映射到core.Passwords + Username string // 直接映射到base.Username + Password string // 直接映射到base.Password + Userdict map[string][]string // 直接映射到base.Userdict + Passwords []string // 直接映射到base.Passwords ) // ============================================================================= @@ -58,8 +58,8 @@ var ( // ============================================================================= var ( - HttpProxy string // 直接映射到core.HttpProxy - Socks5Proxy string // 直接映射到core.Socks5Proxy + HttpProxy string // 直接映射到base.HttpProxy + Socks5Proxy string // 直接映射到base.Socks5Proxy ) // ============================================================================= @@ -67,9 +67,9 @@ var ( // ============================================================================= var ( - NoColor bool // 直接映射到core.NoColor - Language string // 直接映射到core.Language - LogLevel string // 直接映射到core.LogLevel + NoColor bool // 直接映射到base.NoColor + Language string // 直接映射到base.Language + LogLevel string // 直接映射到base.LogLevel ) // ============================================================================= @@ -77,8 +77,8 @@ var ( // ============================================================================= var ( - PortMap map[int][]string // 直接映射到core.PortMap - DefaultMap []string // 直接映射到core.DefaultMap + PortMap map[int][]string // 直接映射到base.PortMap + DefaultMap []string // 直接映射到base.DefaultMap ) // ============================================================================= @@ -96,50 +96,50 @@ var ( // SyncFromCore 从core包同步配置到common包(读操作) func SyncFromCore() { - ScanMode = core.ScanMode - ThreadNum = core.ThreadNum - Timeout = core.Timeout - DisablePing = core.DisablePing - LocalMode = core.LocalMode + ScanMode = base.ScanMode + ThreadNum = base.ThreadNum + Timeout = base.Timeout + DisablePing = base.DisablePing + LocalMode = base.LocalMode - Username = core.Username - Password = core.Password - Userdict = core.Userdict - Passwords = core.Passwords + Username = base.Username + Password = base.Password + Userdict = base.Userdict + Passwords = base.Passwords - HttpProxy = core.HttpProxy - Socks5Proxy = core.Socks5Proxy + HttpProxy = base.HttpProxy + Socks5Proxy = base.Socks5Proxy - NoColor = core.NoColor - Language = core.Language - LogLevel = core.LogLevel + NoColor = base.NoColor + Language = base.Language + LogLevel = base.LogLevel - PortMap = core.PortMap - DefaultMap = core.DefaultMap + PortMap = base.PortMap + DefaultMap = base.DefaultMap } // SyncToCore 同步common包配置到core包(写操作) func SyncToCore() { - core.ScanMode = ScanMode - core.ThreadNum = ThreadNum - core.Timeout = Timeout - core.DisablePing = DisablePing - core.LocalMode = LocalMode + base.ScanMode = ScanMode + base.ThreadNum = ThreadNum + base.Timeout = Timeout + base.DisablePing = DisablePing + base.LocalMode = LocalMode - core.Username = Username - core.Password = Password - core.Userdict = Userdict - core.Passwords = Passwords + base.Username = Username + base.Password = Password + base.Userdict = Userdict + base.Passwords = Passwords - core.HttpProxy = HttpProxy - core.Socks5Proxy = Socks5Proxy + base.HttpProxy = HttpProxy + base.Socks5Proxy = Socks5Proxy - core.NoColor = NoColor - core.Language = Language - core.LogLevel = LogLevel + base.NoColor = NoColor + base.Language = Language + base.LogLevel = LogLevel - core.PortMap = PortMap - core.DefaultMap = DefaultMap + base.PortMap = PortMap + base.DefaultMap = DefaultMap } // ============================================================================= @@ -179,7 +179,7 @@ const ( func init() { // 初始化core包配置 - core.InitGlobalConfig() + base.InitGlobalConfig() // 从core包同步初始配置 SyncFromCore() diff --git a/Common/parsers/LegacyParser.go b/Common/parsers/LegacyParser.go deleted file mode 100644 index db7bf19..0000000 --- a/Common/parsers/LegacyParser.go +++ /dev/null @@ -1,41 +0,0 @@ -package parsers - -/* -LegacyParser.go - 简化的向后兼容解析器 - -现在直接使用简化的解析函数,大幅减少代码复杂度, -同时保持与现有代码的完全兼容性。 -*/ - -// ParseIP 解析各种格式的IP地址(兼容原函数签名) -// 参数: -// - host: 主机地址(可以是单个IP、IP范围、CIDR或常用网段简写) -// - filename: 包含主机地址的文件名 -// - nohosts: 需要排除的主机地址列表 -// -// 返回: -// - []string: 解析后的IP地址列表 -// - error: 解析过程中的错误 -func ParseIP(host string, filename string, nohosts ...string) ([]string, error) { - return SimpleParseIP(host, filename, nohosts...) -} - -// ParsePort 解析端口配置字符串为端口号列表(兼容原函数签名) -// 参数: -// - ports: 端口配置字符串 -// -// 返回: -// - []int: 解析后的端口号列表 -func ParsePort(ports string) []int { - return SimpleParsePort(ports) -} - -// ParsePortsFromString 从字符串解析端口列表(兼容原函数签名) -// 参数: -// - portsStr: 端口字符串,支持单个端口、端口范围、端口组 -// -// 返回: -// - []int: 解析后的端口号列表,已去重并排序 -func ParsePortsFromString(portsStr string) []int { - return SimpleParsePortsFromString(portsStr) -} \ No newline at end of file diff --git a/Common/parsers/LegacyParser_test.go b/Common/parsers/LegacyParser_test.go deleted file mode 100644 index 0745378..0000000 --- a/Common/parsers/LegacyParser_test.go +++ /dev/null @@ -1,142 +0,0 @@ -package parsers - -import ( - "reflect" - "testing" -) - -// TestParsePort 测试端口解析功能 -func TestParsePort(t *testing.T) { - tests := []struct { - name string - input string - expected []int - }{ - { - name: "Simple ports", - input: "80,443,22", - expected: []int{22, 80, 443}, // sorted - }, - { - name: "Port range", - input: "80-85", - expected: []int{80, 81, 82, 83, 84, 85}, - }, - { - name: "Mixed ports and ranges", - input: "22,80-82,443", - expected: []int{22, 80, 81, 82, 443}, // sorted - }, - { - name: "Predefined group", - input: "web", - expected: []int{80, 443, 8080, 8443}, // subset of web ports - }, - { - name: "Empty string", - input: "", - expected: nil, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - result := ParsePort(tt.input) - - // For predefined groups, we just check if common ports are included - if tt.name == "Predefined group" { - hasExpectedPorts := false - for _, expectedPort := range tt.expected { - for _, resultPort := range result { - if resultPort == expectedPort { - hasExpectedPorts = true - break - } - } - if !hasExpectedPorts { - break - } - } - if !hasExpectedPorts { - t.Errorf("ParsePort(%s) does not contain expected common web ports %v", tt.input, tt.expected) - } - } else { - if !reflect.DeepEqual(result, tt.expected) { - t.Errorf("ParsePort(%s) = %v, expected %v", tt.input, result, tt.expected) - } - } - }) - } -} - -// TestParseIP 测试IP解析功能 -func TestParseIP(t *testing.T) { - tests := []struct { - name string - input string - expected int // expected number of IPs - }{ - { - name: "Single IP", - input: "192.168.1.1", - expected: 1, - }, - { - name: "CIDR notation", - input: "192.168.1.0/30", - expected: 4, // 192.168.1.0-3 - }, - { - name: "IP range", - input: "192.168.1.1-192.168.1.3", - expected: 3, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - result, err := ParseIP(tt.input, "", "") - if err != nil { - t.Errorf("ParseIP(%s) returned error: %v", tt.input, err) - return - } - if len(result) != tt.expected { - t.Errorf("ParseIP(%s) returned %d IPs, expected %d", tt.input, len(result), tt.expected) - } - }) - } -} - -// TestParsePortsFromString 测试端口字符串解析功能 -func TestParsePortsFromString(t *testing.T) { - tests := []struct { - name string - input string - expected []int - }{ - { - name: "Valid ports", - input: "80,443,22", - expected: []int{22, 80, 443}, // sorted - }, - { - name: "Empty string", - input: "", - expected: []int{}, - }, - { - name: "With spaces", - input: " 80 , 443 , 22 ", - expected: []int{22, 80, 443}, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - result := ParsePortsFromString(tt.input) - if !reflect.DeepEqual(result, tt.expected) { - t.Errorf("ParsePortsFromString(%s) = %v, expected %v", tt.input, result, tt.expected) - } - }) - } -} \ No newline at end of file diff --git a/Common/parsers/Simple.go b/Common/parsers/Simple.go index 60efc5d..066b5d7 100644 --- a/Common/parsers/Simple.go +++ b/Common/parsers/Simple.go @@ -21,9 +21,9 @@ Simple.go - 简化版本的解析器函数 // 简化的IP/主机解析函数 // ============================================================================= -// SimpleParseIP 简化版本的IP解析函数 -// 保持与 ParseIP 的接口兼容性,但使用更简单的实现 -func SimpleParseIP(host string, filename string, nohosts ...string) ([]string, error) { +// ParseIP 解析各种格式的IP地址 +// 支持单个IP、IP范围、CIDR和文件输入 +func ParseIP(host string, filename string, nohosts ...string) ([]string, error) { var hosts []string // 如果提供了文件名,从文件读取主机列表 @@ -68,9 +68,9 @@ func SimpleParseIP(host string, filename string, nohosts ...string) ([]string, e // 简化的端口解析函数 // ============================================================================= -// SimpleParsePort 简化版本的端口解析函数 +// ParsePort 解析端口配置字符串为端口号列表 // 保持与 ParsePort 的接口兼容性 -func SimpleParsePort(ports string) []int { +func ParsePort(ports string) []int { if ports == "" { return nil } @@ -108,10 +108,10 @@ func SimpleParsePort(ports string) []int { return result } -// SimpleParsePortsFromString 简化版本的端口字符串解析 +// ParsePortsFromString 从字符串解析端口列表 // 保持与 ParsePortsFromString 的接口兼容性 -func SimpleParsePortsFromString(portsStr string) []int { - return SimpleParsePort(portsStr) +func ParsePortsFromString(portsStr string) []int { + return ParsePort(portsStr) } // =============================================================================