mirror of
https://github.com/shadow1ng/fscan.git
synced 2026-02-09 10:19:19 +08:00
refactor: 删除 deadcode 检测出的未使用函数
- proxy/detector.go: 删除 IsSOCKS5Standard, IsProxyInitialized - findnet.go: 删除 NetworkInfo.OneLine, TreeFormat 方法 - port_scan.go: 删除 estimateScanTime 函数 - web_scanner.go: 删除 GetFingerprints 函数 - 清理相关测试代码
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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扫描策略
|
||||
// ===============================
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// 边界情况测试
|
||||
// =============================================================================
|
||||
|
||||
@@ -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 (
|
||||
|
||||
Reference in New Issue
Block a user