From 5b9bcf3937d876d02af945dc786844869c0d5fc4 Mon Sep 17 00:00:00 2001 From: ZacharyZcR Date: Tue, 28 Apr 2026 11:44:00 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=B8=89=E9=98=B6=E6=AE=B5=E6=80=A7?= =?UTF-8?q?=E8=83=BD=E4=BC=98=E5=8C=96=EF=BC=8CICMP=20=E5=B9=B6=E5=8F=91?= =?UTF-8?q?=E6=8F=90=E5=8D=87+TCP=20=E5=B9=B6=E8=A1=8C=E6=8E=A2=E6=B5=8B?= =?UTF-8?q?=EF=BC=8C=E7=AB=AF=E5=8F=A3=E6=89=AB=E6=8F=8F=E9=80=80=E9=81=BF?= =?UTF-8?q?=E8=B0=83=E6=95=B4=EF=BC=8C=E6=9C=8D=E5=8A=A1=E6=8E=A2=E6=B5=8B?= =?UTF-8?q?=E8=B6=85=E6=97=B6=E5=87=8F=E5=8D=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/icmp.go | 44 ++++++++++++++++++++--------- core/port_scan.go | 8 +++--- core/service_probe.go | 2 +- core/service_probe_strategy_test.go | 4 +-- 4 files changed, 38 insertions(+), 20 deletions(-) diff --git a/core/icmp.go b/core/icmp.go index 8f4154a..554854e 100644 --- a/core/icmp.go +++ b/core/icmp.go @@ -473,8 +473,12 @@ func icmpalive(host string) bool { // RunPing 使用系统Ping命令并发探测主机存活 func RunPing(hostslist []string, chanHosts chan string, livewg *sync.WaitGroup) { var wg sync.WaitGroup - // 限制并发数为50 - limiter := make(chan struct{}, 50) + // 并发数根据主机数动态调整,上限 200 + concurrency := len(hostslist) + if concurrency > 200 { + concurrency = 200 + } + limiter := make(chan struct{}, concurrency) // 并发探测 for _, host := range hostslist { @@ -677,20 +681,34 @@ func ArrayCountValueTop(arrInit []string, length int, flag bool) (arrTop []strin var tcpProbeCommonPorts = []int{80, 443, 22, 445} // tcpProbeTimeout TCP 探测超时时间(较短,只做存活判断) -const tcpProbeTimeout = 2 * time.Second +const tcpProbeTimeout = 1 * time.Second // tcpProbeThreshold TCP 补充探测触发阈值 // 当 ICMP 响应率低于此值时,自动启用 TCP 补充探测 const tcpProbeThreshold = 0.1 // 10% -// tcpProbeAlive 使用 TCP 探测主机是否存活 -// 尝试连接常用端口,任一端口响应即认为存活 +// tcpProbeAlive 使用 TCP 并行探测主机是否存活 +// 同时连接所有常用端口,任一响应即返回 func tcpProbeAlive(ctx context.Context, session *common.ScanSession, host string) bool { + ctx, cancel := context.WithCancel(ctx) + defer cancel() + + result := make(chan bool, len(tcpProbeCommonPorts)) for _, port := range tcpProbeCommonPorts { - addr := fmt.Sprintf("%s:%d", host, port) - conn, err := session.DialTCP(ctx, "tcp", addr, tcpProbeTimeout) - if err == nil { - _ = conn.Close() + go func(p int) { + addr := fmt.Sprintf("%s:%d", host, p) + conn, err := session.DialTCP(ctx, "tcp", addr, tcpProbeTimeout) + if err == nil { + _ = conn.Close() + result <- true + return + } + result <- false + }(port) + } + + for range tcpProbeCommonPorts { + if <-result { return true } } @@ -709,10 +727,10 @@ func runTcpProbeForHosts(ctx context.Context, hosts []string, session *common.Sc var mu sync.Mutex aliveHosts := make([]string, 0) - // 并发控制,避免资源耗尽 - concurrency := 50 - if len(hosts) < concurrency { - concurrency = len(hosts) + // 并发控制,根据主机数动态调整,上限 200 + concurrency := len(hosts) + if concurrency > 200 { + concurrency = 200 } limiter := make(chan struct{}, concurrency) diff --git a/core/port_scan.go b/core/port_scan.go index 6459b7b..2d48d4c 100644 --- a/core/port_scan.go +++ b/core/port_scan.go @@ -286,9 +286,9 @@ func connectWithRetry(ctx context.Context, session *common.ScanSession, addr str // 记录资源耗尽错误 session.State.IncrementResourceExhaustedCount() - // 指数退避:第1次等50ms,第2次等150ms + // 指数退避:200ms → 600ms → 1200ms if attempt < maxRetries-1 { - waitTime := time.Duration(50*(attempt+1)) * time.Millisecond + waitTime := time.Duration(200*(1<