From f18b51ca920fedb7a47b692c14ab7ef98063288e Mon Sep 17 00:00:00 2001 From: ZacharyZcR Date: Wed, 21 Jan 2026 23:39:47 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=88=A0=E9=99=A4=20deadcode=20?= =?UTF-8?q?=E6=A3=80=E6=B5=8B=E5=87=BA=E7=9A=84=E6=9C=AA=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - proxy/detector.go: 删除 IsSOCKS5Standard, IsProxyInitialized - findnet.go: 删除 NetworkInfo.OneLine, TreeFormat 方法 - port_scan.go: 删除 estimateScanTime 函数 - web_scanner.go: 删除 GetFingerprints 函数 - 清理相关测试代码 --- common/proxy/detector.go | 10 --- core/port_scan.go | 23 ------- core/port_scan_bench_test.go | 7 --- core/web_scanner.go | 11 ---- core/web_scanner_test.go | 116 ----------------------------------- plugins/services/findnet.go | 54 ---------------- 6 files changed, 221 deletions(-) diff --git a/common/proxy/detector.go b/common/proxy/detector.go index aab86f1..e296f44 100644 --- a/common/proxy/detector.go +++ b/common/proxy/detector.go @@ -41,16 +41,6 @@ func IsProxyEnabled() bool { return proxyEnabled.Load() } -// IsSOCKS5Standard 检查SOCKS5代理是否为标准代理 -func IsSOCKS5Standard() bool { - return socks5Standard.Load() -} - -// IsProxyInitialized 检查代理是否已初始化 -func IsProxyInitialized() bool { - return proxyInitialized.Load() -} - // SetProxyReliable 设置代理可靠性状态 func SetProxyReliable(reliable bool) { proxyReliable.Store(reliable) diff --git a/core/port_scan.go b/core/port_scan.go index 54c6db0..59ab31a 100644 --- a/core/port_scan.go +++ b/core/port_scan.go @@ -2,7 +2,6 @@ package core import ( "fmt" - "math" "net" "strings" "sync" @@ -166,28 +165,6 @@ func preResolveDomains(hosts []string, timeout time.Duration) []string { return result } -// estimateScanTime 估算扫描时间 -// 参数: totalTasks - 总任务数, threads - 线程数, timeout - 超时时间(秒) -// 返回: 估算的扫描时间(秒) -func estimateScanTime(totalTasks int, threads int, timeout int64) int64 { - if totalTasks == 0 || threads == 0 { - return 0 - } - - // 假设约50%的端口会快速返回关闭状态(平均耗时 timeout/4) - // 约50%的端口需要完整超时(耗时 timeout) - // 因此平均每个任务耗时 = timeout * 0.5 * (0.25 + 1.0) = timeout * 0.625 - avgTaskTime := float64(timeout) * 0.625 - - // 计算需要多少批次(向上取整) - parallelBatches := math.Ceil(float64(totalTasks) / float64(threads)) - - // 总时间 = 批次数 × 平均任务时间 - estimatedSeconds := int64(parallelBatches * avgTaskTime) - - return estimatedSeconds -} - // EnhancedPortScan 高性能端口扫描函数 // 使用滑动窗口调度 + 自适应线程池 + 流式迭代器 func EnhancedPortScan(hosts []string, ports string, timeout int64, config *common.Config, state *common.State) []string { diff --git a/core/port_scan_bench_test.go b/core/port_scan_bench_test.go index 2b92177..2ad3325 100644 --- a/core/port_scan_bench_test.go +++ b/core/port_scan_bench_test.go @@ -83,10 +83,3 @@ func BenchmarkFailedPortCollectorAdd(b *testing.B) { } } -// BenchmarkEstimateScanTime 测试扫描时间估算性能 -func BenchmarkEstimateScanTime(b *testing.B) { - b.ResetTimer() - for i := 0; i < b.N; i++ { - _ = estimateScanTime(10000, 600, 3) - } -} diff --git a/core/web_scanner.go b/core/web_scanner.go index a30f1da..f688e10 100644 --- a/core/web_scanner.go +++ b/core/web_scanner.go @@ -300,17 +300,6 @@ func SetFingerprints(host string, port int, fingerprints []string) { fingerprintCache[cacheKey] = fingerprints } -// GetFingerprints 获取目标的指纹信息 -func GetFingerprints(host string, port int) ([]string, bool) { - cacheKey := fmt.Sprintf("%s:%d", host, port) - - fingerprintCacheMutex.RLock() - defer fingerprintCacheMutex.RUnlock() - - fingerprints, exists := fingerprintCache[cacheKey] - return fingerprints, exists -} - // =============================== // Web扫描策略 // =============================== diff --git a/core/web_scanner_test.go b/core/web_scanner_test.go index ef8aee8..ee67d86 100644 --- a/core/web_scanner_test.go +++ b/core/web_scanner_test.go @@ -577,122 +577,6 @@ func TestWebServiceCache_Concurrent(t *testing.T) { // 指纹缓存测试 // ============================================================================= -// TestFingerprintCache 测试指纹缓存操作 -func TestFingerprintCache(t *testing.T) { - // 清空缓存 - fingerprintCacheMutex.Lock() - fingerprintCache = make(map[string][]string) - fingerprintCacheMutex.Unlock() - - t.Run("存储和读取指纹", func(t *testing.T) { - fingerprints := []string{"nginx", "http", "ssl"} - SetFingerprints("192.168.1.1", 80, fingerprints) - - result, exists := GetFingerprints("192.168.1.1", 80) - if !exists { - t.Error("GetFingerprints应返回exists=true") - } - if len(result) != 3 { - t.Errorf("指纹数量 = %d, 期望 3", len(result)) - } - for i, fp := range fingerprints { - if result[i] != fp { - t.Errorf("指纹[%d] = %q, 期望 %q", i, result[i], fp) - } - } - }) - - t.Run("空指纹列表不存储", func(t *testing.T) { - SetFingerprints("192.168.1.2", 80, []string{}) - - _, exists := GetFingerprints("192.168.1.2", 80) - if exists { - t.Error("空指纹列表不应被存储") - } - }) - - t.Run("不存在的指纹", func(t *testing.T) { - result, exists := GetFingerprints("192.168.1.3", 80) - if exists { - t.Error("不存在的指纹应返回exists=false") - } - if result != nil { - t.Errorf("不存在的指纹应返回nil, 实际 %v", result) - } - }) - - t.Run("覆盖写入指纹", func(t *testing.T) { - fingerprints1 := []string{"nginx"} - fingerprints2 := []string{"apache", "php"} - - SetFingerprints("192.168.1.4", 80, fingerprints1) - SetFingerprints("192.168.1.4", 80, fingerprints2) - - result, _ := GetFingerprints("192.168.1.4", 80) - if len(result) != 2 { - t.Errorf("覆盖后指纹数量 = %d, 期望 2", len(result)) - } - }) - - t.Run("不同端口独立存储指纹", func(t *testing.T) { - fp80 := []string{"http"} - fp443 := []string{"https"} - - SetFingerprints("192.168.1.5", 80, fp80) - SetFingerprints("192.168.1.5", 443, fp443) - - result80, _ := GetFingerprints("192.168.1.5", 80) - result443, _ := GetFingerprints("192.168.1.5", 443) - - if result80[0] != "http" { - t.Errorf("端口80指纹 = %v, 期望 ['http']", result80) - } - if result443[0] != "https" { - t.Errorf("端口443指纹 = %v, 期望 ['https']", result443) - } - }) -} - -// TestFingerprintCache_Concurrent 测试指纹缓存并发安全性 -func TestFingerprintCache_Concurrent(t *testing.T) { - // 清空缓存 - fingerprintCacheMutex.Lock() - fingerprintCache = make(map[string][]string) - fingerprintCacheMutex.Unlock() - - var wg sync.WaitGroup - numGoroutines := 100 - - // 并发写入 - for i := 0; i < numGoroutines; i++ { - wg.Add(1) - go func(id int) { - defer wg.Done() - fingerprints := []string{"test"} - SetFingerprints("192.168.1.1", id, fingerprints) - }(i) - } - - // 并发读取 - for i := 0; i < numGoroutines; i++ { - wg.Add(1) - go func(id int) { - defer wg.Done() - _, _ = GetFingerprints("192.168.1.1", id) - }(i) - } - - wg.Wait() - - // 验证数据完整性 - for i := 0; i < numGoroutines; i++ { - _, exists := GetFingerprints("192.168.1.1", i) - if !exists { - t.Errorf("端口 %d 指纹应存在", i) - } - } -} - // ============================================================================= // 边界情况测试 // ============================================================================= diff --git a/plugins/services/findnet.go b/plugins/services/findnet.go index 0b3f08e..2c66cf8 100644 --- a/plugins/services/findnet.go +++ b/plugins/services/findnet.go @@ -108,21 +108,6 @@ type NetworkInfo struct { IPv6Addrs []string } -// OneLine 返回单行格式(便于复制) -func (ni *NetworkInfo) OneLine() string { - if !ni.Valid { - return "" - } - var parts []string - if ni.Hostname != "" { - parts = append(parts, fmt.Sprintf("[%s]", ni.Hostname)) - } - if len(ni.IPv4Addrs) > 0 { - parts = append(parts, strings.Join(ni.IPv4Addrs, ",")) - } - return strings.Join(parts, " ") -} - // Summary 返回网络信息摘要 func (ni *NetworkInfo) Summary() string { if !ni.Valid { @@ -146,45 +131,6 @@ func (ni *NetworkInfo) Summary() string { return strings.Join(parts, ", ") } -// TreeFormat 返回tree格式的详细网络信息 -func (ni *NetworkInfo) TreeFormat() string { - if !ni.Valid { - return "网络发现失败" - } - - var result strings.Builder - - // 主机名信息 - if ni.Hostname != "" { - result.WriteString(fmt.Sprintf("主机名: %s\n", ni.Hostname)) - } - - // IPv4地址树形显示 - if len(ni.IPv4Addrs) > 0 { - result.WriteString(fmt.Sprintf("IPv4接口 (%d个):\n", len(ni.IPv4Addrs))) - for i, addr := range ni.IPv4Addrs { - if i == len(ni.IPv4Addrs)-1 { - result.WriteString(fmt.Sprintf(" └── %s\n", addr)) - } else { - result.WriteString(fmt.Sprintf(" ├── %s\n", addr)) - } - } - } - - // IPv6地址树形显示 - if len(ni.IPv6Addrs) > 0 { - result.WriteString(fmt.Sprintf("IPv6接口 (%d个):\n", len(ni.IPv6Addrs))) - for i, addr := range ni.IPv6Addrs { - if i == len(ni.IPv6Addrs)-1 { - result.WriteString(fmt.Sprintf(" └── %s\n", addr)) - } else { - result.WriteString(fmt.Sprintf(" ├── %s\n", addr)) - } - } - } - - return strings.TrimRight(result.String(), "\n") -} // RPC数据包定义 var (