Files
shell-scripts/1-代理Xray/99-网络质量分析/使用说明.md
2025-12-08 08:56:23 +08:00

50 lines
2.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

go build -o netmonitor net_quality_monitor.go
#### 2. 部署与运行
**第一步:在主机 B 上启动服务端**
服务端将监听 TCP 28080 和 UDP 28081 端口,等待 A 的连接。
```bash
# Linux/Mac
./netmonitor -mode server -tcp 28080 -udp 28081
**第二步:在主机 A 上启动客户端**
客户端将每隔 5 秒测试一次到 140.238.52.228 的网络质量。
```bash
# 开启测试
./netmonitor -mode client -target 140.238.52.228 -tcp 28080 -udp 28081 -duration 10 -interval 60
# 德国
./netmonitor -mode client -target 43.154.83.213 -tcp 28080 -udp 28081 -duration 15 -interval 600
### 功能特点详解
1. **真实 TCP 交互**
* 代码中的 `testTCPLatency` 函数不使用 ICMP Ping而是通过 `net.DialTimeout` 建立完整的三次握手,并发送 Payload 数据。服务端接收并回写Echo
* 计算的时间包含了:`TCP握手时间` + `数据传输时间` + `ACK时间`。这比普通的 Ping 更能反映应用程序(如 HTTP/RPC的真实感受。
2. **UDP 丢包监测**
* `testPacketLoss` 采用 UDP 协议。UDP 是无连接的,不保证到达。
* 客户端连续快速发送 `LossTestCount` (默认20个) 包。如果接收端Echo模式下没有及时返回则判定为丢包。
* 这种方法能有效检测线路拥塞或防火墙限流情况。
3. **定时与报告**
* 程序使用 `time.Ticker` 保证精准的执行周期。
* 结果会同时输出到 **控制台** 和 **net_quality_report.log**。
* 另外生成 **net_report_data.jsonl**,每行一个 JSON 对象,方便后续通过脚本(如 Python/ELK进行图表分析。
4. **关于 Traceroute 的说明**
* 我在代码中预留了 `TraceRoute` 接口。
* *注意*:在 Go 语言中实现真正的 Traceroute修改 TTL需要引入 `golang.org/x/net/ipv4` 包并使用 Raw Socket这要求程序必须以 **root/管理员** 权限运行。为了保持代码作为一个简洁的“单文件”工具,且能保证在普通用户权限下运行,我没有包含 Raw Socket 代码。目前的实现是应用层层面的连通性检查。
### 报告样本
日志文件 (`net_report_data.jsonl`) 内容示例:
```json
{"timestamp":"2023-10-27 10:00:00","target":"192.168.1.200","tcp_latency_ms":12.5,"tcp_jitter_ms":1.2,"loss_rate_a_to_b":0.0,"loss_rate_b_to_a":0.0}
{"timestamp":"2023-10-27 10:00:10","target":"192.168.1.200","tcp_latency_ms":12.8,"tcp_jitter_ms":0.9,"loss_rate_a_to_b":0.05,"loss_rate_b_to_a":0.05}
你可以直接用 Excel 或 Python 读取这个文件来生成网络质量波动图。