Files
ucvl-zonghengjia-open-source/backend/host-traffic-sampler/README.md

29 lines
3.4 KiB
Markdown
Raw Permalink 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.
# 独立服务器 WAN 秒级采样
该进程与原有 `toplc-node-agent/1.1.0` 分开运行。它不修改 Xray、用户计费、代理配置、Tailscale、系统路由、防火墙或账号限速。
- 每 1 秒依据 monotonic 时钟读取 Linux `/proc/net/dev`。接口取主路由表的默认物理 WAN(先 IPv4,后 IPv6),排除 loopback、Tailscale、Docker、bridge、veth、tun/tap、WireGuard 等逻辑接口。
- `rx_bytes` 为服务器 WAN 入站;`tx_bytes` 为服务器 WAN 出站。语义为 `server_host_interface` / `linux_proc_net_dev_interface_bytes`。这包括该网口上的主机流量和协议开销,不能当作个人代理计费流量,也不能与 Tailscale 逻辑流量相加。
- 用原始 counter 差值除以实际 monotonic 时间间隔计算 B/s。首点、boot/接口变更、counter 复位、过长或无效间隔使用 `null`,不会伪造 0。真实零增量仍为 0。
- 单独上传线程每 5 秒把最多 60 条样本 POST 到固定 HTTPS `https://vpn.example.invalid/api/v1/node/host-traffic/`。TLS 验证始终开启,不跟随重定向、不使用环境代理。请求超时不阻塞采样。
- 内存队列最多 60 条;满时丢旧,在新样本标明 gap 和丢失数量。已排队样本不被修改,确保超时后重试数据仍幂等。丢失/延迟不是连续假曲线。
- 每次进程启动生成新 UUID counter epoch;boot/接口/计数器复位亦生成新 epoch,避免进程崩溃重启后 sequence 唯一键冲突。没有凭证缓存或持久化队列。
- 仅解析原 `node.env` 中 `NODE_SLUG`、`NODE_TOKEN` 两项,不执行该文件、不导出或记录其它密钥。上传凭证只保留内存。系统服务需要 root 读取原来的 0600 文件,capabilities 全部去除并启用只读系统沙箱。
输入契约 `contract: 1`:顶层 `node_slug/boot_id/interface/kind/counter_semantics/samples`;每样本 `sequence/observed_at/monotonic_ns/counter_epoch/interval_seconds/rx_bytes/tx_bytes/rx_bytes_per_second/tx_bytes_per_second/quality/gap_before/dropped_samples_before`。原始字节计数限制在 SQLite signed int64 范围。成功 2xx 表示整批持久化,失败不确认队列。后端未提供此端点时上传会安全失败,队列保持有界。
## 审核和安装
安装前必须先完成中央 ingest 端点。部署包仅包含本独立服务,可先执行:
```sh
python3 -B host_traffic_sampler.py --node-env /opt/toplc-vpn-node/node.env --check-config
```
`--check-config` 只读取本机接口和配置,既不发送网络请求,也不打印 token。由 root 审核冻结包 SHA256 后执行同包 `deploy_host_traffic_sampler.sh --install EXPECTED_NODE_SLUG`;该脚本会拒绝既有 sampler 安装及目标 slug 不一致,校验原主 agent/compose/env/service 文件前后 hash 相同,不修改原服务。安装过程失败时保留现场以供检查,不应盲目重试或删除旧现场。
离线测试:`python3 -m unittest discover -s . -p test_host_traffic_sampler.py -v`。测试不连接网络、不读生产凭证。
MIT source supplement: these files are published as a separate directory only. They do not replace the platform root, existing routes, source history or other licenses. Integration tests use an existing Django platform model fixture; production model snapshots, accounts, tokens, SQLite state and local output evidence are not included. Configure example endpoints for your deployment. Source publication does not assert production acceptance.