No description
  • C 85.3%
  • Shell 14%
  • Makefile 0.7%
Find a file
2026-04-26 16:47:25 +08:00
.gitignore 0.0.1 2026-04-26 15:54:00 +08:00
auto_build.sh fix path 2026-04-26 16:47:25 +08:00
Makefile 0.0.1 2026-04-26 15:54:00 +08:00
README.md fix path 2026-04-26 16:47:25 +08:00
sdu_net-route-checker.c 0.0.1 2026-04-26 15:54:00 +08:00

校园网线路质量检测与优选工具

本项目用于在校园网多出口线路之间做自动质量对比,并可选自动切换到最优线路。

核心程序为 C 实现的 sdu_net-route-checker,依赖 fastrace 做链路探测。

功能特性

  • 自动获取校园网可用线路列表
  • 逐线路切换并检测到目标 IP/域名的连通质量
  • 输出每条线路的响应率、平均 RTT、抖动、样本数、综合评分
  • 默认仅检测并输出结果
  • 可使用 --auto-switch 在检测后自动切换到最优线路
  • 默认检测结束后恢复原始线路(可通过 --no-restore 关闭)
  • 内置 fastrace 权限预检(无权限时给出 sudo/setcap 提示)

目录说明

  • sdu_net-route-checker.c:主程序源码
  • Makefile:主程序构建脚本
  • fastrace/fastrace 源码与可执行文件
  • net_switcher.ps1PowerShell 版线路切换脚本

运行环境

  • Linux
  • gcc
  • curl
  • 可访问校园网接口
    • https://10.1.2.224/network_checker/get_network
    • https://10.1.2.224/network_checker/change_network
  • fastrace 可执行文件(默认路径:./fastrace/fastrace

构建

在项目根目录执行:

make clean && make

自动构建脚本

项目提供一键自动构建脚本:auto_build.sh

  • 项目仓库地址:https://git.zerohyh.top/zeroHYH/sdu_net-route-checker
  • fastrace 上游仓库:https://github.com/davidesantangelo/fastrace

网络一行构建(curl | bash

curl -fsSL https://git.zerohyh.top/zeroHYH/sdu_net-route-checker/raw/branch/main/auto_build.sh | bash

若需要构建后自动设置权限(需要 root

curl -fsSL https://git.zerohyh.top/zeroHYH/sdu_net-route-checker/raw/branch/main/auto_build.sh | sudo bash -s -- --setcap

更安全的本地执行方式(推荐):

curl -fsSL -o auto_build.sh https://git.zerohyh.top/zeroHYH/sdu_net-route-checker/raw/branch/main/auto_build.sh
bash auto_build.sh

默认行为:

  1. 检查 git/make/gcc 依赖
  2. 如果当前目录不是项目根目录,自动克隆项目仓库
  3. 自动同步 fastrace 到上游仓库最新状态(可关闭)
  4. 构建 fastracesdu_net-route-checker
  5. 可选设置 cap_net_raw

使用示例:

# 在项目根目录一键构建
./auto_build.sh

# 构建后自动设置 cap_net_raw需 root
sudo ./auto_build.sh --setcap

# 不同步 fastrace仅使用本地源码构建
./auto_build.sh --skip-fastrace-sync

先为脚本添加执行权限:

chmod +x ./auto_build.sh

生成可执行文件:

./sdu_net-route-checker

快速开始

仅检测并输出结果:

sudo ./sdu_net-route-checker 8.8.8.8

检测后自动切换到最优线路:

sudo ./sdu_net-route-checker --auto-switch 8.8.8.8

指定目标域名:

sudo ./sdu_net-route-checker www.baidu.com

如果 fastrace 不在默认位置:

sudo ./sdu_net-route-checker --fastrace-cmd "/path/to/fastrace" 8.8.8.8

权限说明

fastrace 需要 raw socket 权限。

若权限不足,程序会在启动时直接退出并提示处理方法:

  1. 以 root 运行(推荐最简单):
sudo ./sdu_net-route-checker 8.8.8.8
  1. 一次性赋予 fastrace 能力Linux
sudo setcap cap_net_raw+ep ./fastrace/fastrace

参数说明

Usage: ./sdu_net-route-checker [options] <ip-or-domain>

Options:
  --auto-switch            检测后自动切到最优线路
  --base-url <url>         校园网接口地址(默认: https://10.1.2.224/network_checker
  --fastrace-cmd <cmd>     fastrace 命令(默认: ./fastrace/fastrace
  --max-hops <n>           最大跳数(默认: 20
  --probes <n>             每跳探测次数(默认: 2
  --settle <sec>           切线后等待秒数(默认: 2
  --no-restore             检测结束不恢复原线路
  --verbose                输出详细过程
  -h, --help               显示帮助

输出状态解释

  • okfastrace 完整完成,结果可用
  • partial(rc=1)fastrace 未完整到达终点但返回了可解析 JSON结果仍可用于比较
  • switch failed:切换到该线路失败
  • no RTT samples:该次探测无 RTT 样本
  • fastrace failed(...)fastrace 执行失败且无可用结果

评分逻辑

当前综合评分为:

  • 更高响应率得分更高
  • 更低平均 RTT 得分更高
  • 更低抖动得分更高

程序内部计算:

score = response_rate * 3.0 - avg_rtt * 0.6 - jitter * 0.4

常见问题

  1. 为什么很多线路显示 partial(rc=1)

这通常表示该线路没有稳定到达目标终点,但途中已有足够回包用于估算质量。可换目标地址、增大 --settle,或在不同时间段多测几次。

  1. 为什么非 sudo 运行直接失败?

因为 fastrace 需要 raw socket 权限。请使用 sudo 或 setcap。

  1. 为什么默认会恢复原线路?

防止检测结束后长期停留在测试线路,避免影响日常网络使用。若希望保持最优线路,请用 --auto-switch

安全与注意事项

  • 本工具会主动调用校园网切线接口,请仅在授权环境中使用
  • 自动切线期间网络连接会短暂波动
  • 建议先在检测模式下观察多轮结果,再启用自动切换