pro-bing 在 Windows 上必须 SetPrivileged(true) 走原始套接字,普通账号 没有权限会直接失败。按平台拆分实现:unix 继续走 pro-bing,windows 调 系统 ping 并以 TTL= 作为跨语言锚点解析输出;同时校验 target 防止被 ping.exe 当作 flag 解析。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
80 lines
1.9 KiB
Go
80 lines
1.9 KiB
Go
//go:build windows
|
|
|
|
package collector
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"os/exec"
|
|
"regexp"
|
|
"strconv"
|
|
"syscall"
|
|
"time"
|
|
)
|
|
|
|
// "TTL=" 在 Windows ping.exe 各语言版本输出中都保留为英文字面量,
|
|
// 因此用作回包行的锚点比 "Reply from" / "来自" 之类本地化文案更稳妥
|
|
var (
|
|
winPingReplyAnchor = regexp.MustCompile(`(?i)TTL[=:]`)
|
|
winPingTimeRe = regexp.MustCompile(`(?i)[=<]\s*(\d+)\s*ms`)
|
|
)
|
|
|
|
func pingHost(target string, count, timeoutSec int) (*pingStats, error) {
|
|
if count <= 0 {
|
|
count = 4
|
|
}
|
|
if timeoutSec <= 0 {
|
|
timeoutSec = 5
|
|
}
|
|
|
|
perReplyMs := timeoutSec * 1000
|
|
|
|
// 进程级超时取每包超时 × 包数 + 余量;ping 自身在包间还会停 ~1s
|
|
overall := time.Duration(timeoutSec*count+5) * time.Second
|
|
ctx, cancel := context.WithTimeout(context.Background(), overall)
|
|
defer cancel()
|
|
|
|
cmd := exec.CommandContext(ctx, "ping",
|
|
"-n", strconv.Itoa(count),
|
|
"-w", strconv.Itoa(perReplyMs),
|
|
target,
|
|
)
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{
|
|
HideWindow: true,
|
|
CreationFlags: 0x08000000, // CREATE_NO_WINDOW,避免在前台 console 下闪窗
|
|
}
|
|
|
|
out, runErr := cmd.CombinedOutput()
|
|
if ctx.Err() == context.DeadlineExceeded {
|
|
return nil, fmt.Errorf("ping command timed out after %v", overall)
|
|
}
|
|
if runErr != nil && len(out) == 0 {
|
|
return nil, fmt.Errorf("ping command failed: %w", runErr)
|
|
}
|
|
|
|
stats := &pingStats{PacketsSent: count}
|
|
var totalRtt int64
|
|
for _, line := range bytes.Split(out, []byte("\n")) {
|
|
if !winPingReplyAnchor.Match(line) {
|
|
continue
|
|
}
|
|
m := winPingTimeRe.FindSubmatch(line)
|
|
if m == nil {
|
|
continue
|
|
}
|
|
rtt, err := strconv.Atoi(string(m[1]))
|
|
if err != nil {
|
|
continue
|
|
}
|
|
stats.PacketsRecv++
|
|
totalRtt += int64(rtt)
|
|
}
|
|
|
|
if stats.PacketsRecv > 0 {
|
|
stats.AvgRttMs = totalRtt / int64(stats.PacketsRecv)
|
|
}
|
|
stats.PacketLoss = float64(stats.PacketsSent-stats.PacketsRecv) * 100 / float64(stats.PacketsSent)
|
|
return stats, nil
|
|
}
|