Skip to content

PasteFerry (隔空贴): clipboard and file sync across macOS, Windows and Linux. Direct-first, end-to-end encrypted. Optional official access: no server to host. Independent self-hosting remains available.

tools: add a network speed test script

baiyuzedb88f78fea3e
diff --git a/scripts/net-speedtest.md b/scripts/net-speedtest.md
new file mode 100644
index 0000000..cb5681d
--- /dev/null
+++ b/scripts/net-speedtest.md
@@ -0,0 +1,35 @@
+# 国内 / 海外一键测速
+
+适用于 macOS 和 Linux,不需要管理员权限。依赖 Bash、curl、awk,安装了 ping 时也会显示 ICMP 延迟和丢包率。脚本可单独复制到其他电脑运行。
+
+```bash
+bash scripts/net-speedtest.sh
+```
+
+依次测试清华北京镜像、阿里云 CDN、新加坡、美国东部和法国节点,输出 Ping 平均延迟、丢包率、TCP 连接耗时、HTTP 首字节耗时、下载 Mbps 和 MB/s。默认每节点请求 16 MiB,HTTP 最多 10 秒,总请求下载量最多 80 MiB,下载数据直接丢弃。
+
+```bash
+# 快速检查:每节点 4 MiB,HTTP 最多 4 秒
+bash scripts/net-speedtest.sh --quick
+
+# 只测延迟,不下载测速文件
+bash scripts/net-speedtest.sh --latency-only
+
+# 更长的下载采样:每节点最多 32 MiB / 15 秒
+bash scripts/net-speedtest.sh --size 32 --seconds 15
+
+# 通过本机代理测试
+bash scripts/net-speedtest.sh --proxy http://127.0.0.1:1087
+bash scripts/net-speedtest.sh --proxy socks5h://127.0.0.1:1080
+
+# 保存可分享的文本结果
+bash scripts/net-speedtest.sh | tee speedtest.txt
+```
+
+默认强制 IPv4 直连,忽略代理环境变量和 curl 配置文件;系统 VPN 或 TUN 路由仍然生效。代理模式跳过 Ping,因为普通 ICMP 不会经过 HTTP / SOCKS 代理;此时 TCP 显示的是连接代理的耗时,首字节耗时反映整条 HTTP 请求链路。
+
+测速逐个运行,避免节点争抢带宽。“限时采样”表示达到时间限制后,用已收到的数据计算均速;不足 64 KiB 不报告速度。HTTP 错误或返回 HTML 拦截页面不会被当作测速结果。全部节点失败时退出码为 1,参数错误为 2。
+
+这些是公共下载端点的单连接短时测速,不测上传,不代表宽带的最高容量。首字节耗时包括 DNS、TCP、TLS 与服务端处理;Ping 在下载结束后测量,不代表下载满载时的延迟。阿里云 CDN 会就近调度,在境外运行时不保证连接到国内节点。脚本使用 HTTP Range 和 curl 的文件大小限制;旧版 curl 对无 Content-Length 且忽略 Range 的服务器可能无法严格限制流量,但仍有总超时。
+
+公共服务可能更换地址、限速或屏蔽探测。需要更换节点时,编辑脚本顶部的 `NODES` 数组,填入支持 HTTPS Range 的二进制文件地址;地域标签应与服务器实际部署位置一致。
diff --git a/scripts/net-speedtest.sh b/scripts/net-speedtest.sh
new file mode 100755
index 0000000..df76293
--- /dev/null
+++ b/scripts/net-speedtest.sh
@@ -0,0 +1,172 @@
+#!/usr/bin/env bash
+# Standalone macOS / Linux download and latency benchmark. No root required.
+set -euo pipefail
+export LC_ALL=C
+
+usage() {
+ cat <<'EOF'
+国内 / 海外测速(macOS / Linux,需要 bash、curl、awk;ping 可选)
+
+用法:bash scripts/net-speedtest.sh [选项]
+ --quick 快测:每节点 4 MiB、最多 4 秒,Ping 2 次
+ --latency-only 只测延迟,不下载测速文件
+ --seconds N 每节点 HTTP 总超时,默认 10 秒(含建连)
+ --size N 每节点请求下载 N MiB,默认 16,范围 1–100
+ --count N 每节点 Ping 次数,默认 4,范围 1–20
+ --proxy URL HTTP(S) / SOCKS 代理,如 http://127.0.0.1:1087
+ -h, --help 显示帮助
+
+默认 IPv4 直连,忽略代理环境变量和 ~/.curlrc;系统 VPN/TUN 仍会影响路由。
+指定代理时仅测 HTTP,跳过不会经过该代理的 Ping;TCP 指连接代理的耗时。
+结果为单连接下载均速,不含上传测试。可用 “| tee speedtest.txt” 保存结果。
+后面的选项覆盖前面的选项。节点可在脚本的 NODES 数组中更换。
+EOF
+}
+
+die() { printf '错误:%s\n' "$*" >&2; exit 2; }
+integer() { [[ $1 =~ ^[1-9][0-9]{0,3}$ ]]; }
+seconds=10
+size_mib=16
+count=4
+latency_only=0
+proxy=''
+while [[ $# -gt 0 ]]; do
+ case "$1" in
+ --quick) seconds=4; size_mib=4; count=2; shift ;;
+ --latency-only) latency_only=1; shift ;;
+ --seconds|--size|--count|--proxy)
+ [[ $# -ge 2 && -n $2 ]] || die "$1 缺少参数"
+ case "$1" in
+ --seconds) seconds=$2 ;;
+ --size) size_mib=$2 ;;
+ --count) count=$2 ;;
+ --proxy) proxy=$2 ;;
+ esac
+ shift 2 ;;
+ -h|--help) usage; exit 0 ;;
+ *) die "未知选项:$1(用 --help 查看用法)" ;;
+ esac
+done
+integer "$seconds" && (( seconds <= 120 )) || die '--seconds 必须是 1–120 的整数'
+integer "$size_mib" && (( size_mib <= 100 )) || die '--size 必须是 1–100 的整数'
+integer "$count" && (( count <= 20 )) || die '--count 必须是 1–20 的整数'
+if [[ -n $proxy ]]; then
+ case "$proxy" in
+ http://*|https://*|socks5://*|socks5h://*) ;;
+ *) die '--proxy 需要 http://、https://、socks5:// 或 socks5h:// URL' ;;
+ esac
+fi
+for dependency in curl awk; do
+ command -v "$dependency" >/dev/null 2>&1 || die "请先安装 $dependency"
+done
+os=$(uname -s)
+case "$os" in Darwin|Linux) ;; *) die '本脚本支持 macOS / Linux' ;; esac
+
+# name|URL: regional endpoints avoid a global CDN selecting a nearby node.
+# Aliyun is a CDN and may select a different region when run outside mainland China.
+NODES=(
+ '国内·清华北京|https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ls-lR.gz'
+ '国内·阿里云CDN|https://mirrors.aliyun.com/ubuntu/ls-lR.gz'
+ '海外·新加坡|https://sgp-ping.vultr.com/vultr.com.100MB.bin'
+ '海外·美国东部|https://was1-vin.speedtest.network.ovh.net/files/100Mb.dat'
+ '海外·法国|https://lil2-gra.speedtest.network.ovh.net/files/100Mb.dat'
+)
+curl_args=(-q -4 --silent --show-error --fail --location --max-redirs 3
+ --connect-timeout 4 --max-time "$seconds" --proto '=https' --proto-redir '=https')
+if [[ -n $proxy ]]; then
+ curl_args+=(--proxy "$proxy" --noproxy '')
+else
+ curl_args+=(--noproxy '*')
+fi
+tmpdir=$(mktemp -d "${TMPDIR:-/tmp}/net-speedtest.XXXXXX")
+trap 'rm -rf "$tmpdir"' EXIT
+trap 'exit 130' INT
+trap 'exit 143' TERM
+bytes=$((size_mib * 1024 * 1024))
+total_bytes=0
+successes=0
+
+printf '国内 / 海外网络测速 %s\n' "$(date '+%Y-%m-%d %H:%M:%S %Z')"
+if [[ -n $proxy ]]; then
+ printf '模式:指定代理;Ping 跳过,TCP 为连接代理耗时。\n'
+else
+ printf '模式:IPv4 直连(忽略代理环境变量;系统 VPN/TUN 仍生效)。\n'
+fi
+if (( latency_only )); then
+ printf '仅测延迟;每节点 HTTP 最多 %s 秒,Ping %s 次。\n' "$seconds" "$count"
+else
+ printf '每节点请求 %s MiB、HTTP 最多 %s 秒;总请求下载量最多 %s MiB。\n' \
+ "$size_mib" "$seconds" "$((size_mib * ${#NODES[@]}))"
+fi
+printf '逐个测量以避免相互抢带宽;海外节点不通时会自动跳过。\n\n'
+printf '节点\tPing均值(ms)\t丢包\tTCP(ms)\t首字节(ms)\t下载(Mbps)\tMB/s\t状态\n'
+
+for node in "${NODES[@]}"; do
+ name=${node%%|*}
+ url=${node#*|}
+ request_args=()
+ if (( latency_only )); then
+ request_args+=(--head)
+ else
+ request_args+=(--range "0-$((bytes - 1))" --max-filesize "$bytes")
+ fi
+ # Keep errors separate from the machine-readable result, including on timeout.
+ rc=0
+ metrics=$(curl "${curl_args[@]}" "${request_args[@]}" --output /dev/null \
+ --write-out '%{http_code}|%{remote_ip}|%{time_namelookup}|%{time_connect}|%{time_starttransfer}|%{speed_download}|%{size_download}|%{num_redirects}|%{content_type}' \
+ "$url" 2>"$tmpdir/curl.err") || rc=$?
+ IFS='|' read -r http ip dns connect first rate received redirects content_type <<< "$metrics"
+ total_bytes=$(awk -v a="$total_bytes" -v b="${received:-0}" 'BEGIN {printf "%.0f", a+b}')
+ tcp='-'; ttfb='-'; mbps='-'; mbytes='-'
+ # curl aggregates timings across redirects, so do not label them as one RTT.
+ if [[ ${redirects:-0} == 0 ]]; then
+ tcp=$(awk -v t="${connect:-0}" -v d="${dns:-0}" \
+ 'BEGIN {if (t>0 && t>=d) printf "%.1f", (t-d)*1000; else printf "-"}')
+ fi
+ ttfb=$(awk -v t="${first:-0}" 'BEGIN {if (t>0) printf "%.1f", t*1000; else printf "-"}')
+ status="失败(curl=$rc,HTTP=${http:-000})"
+ if [[ $http == 200 || $http == 206 ]]; then
+ if (( latency_only && rc == 0 )); then
+ status='正常'; successes=$((successes + 1))
+ elif (( !latency_only )) && [[ $rc == 0 || $rc == 28 ]]; then
+ case "${content_type:-}" in
+ application/octet-stream*|application/gzip*|application/x-gzip*)
+ if awk -v n="${received:-0}" 'BEGIN {exit !(n>=65536)}'; then
+ mbps=$(awk -v v="$rate" 'BEGIN {printf "%.2f", v*8/1000000}')
+ mbytes=$(awk -v v="$rate" 'BEGIN {printf "%.2f", v/1000000}')
+ status='完成'
+ [[ $rc == 28 ]] && status='限时采样'
+ successes=$((successes + 1))
+ else
+ status='样本不足(<64KiB)'
+ fi ;;
+ *) status='非测速文件(可能被拦截)' ;;
+ esac
+ fi
+ fi
+ ping_avg='-'; loss='-'
+ if [[ -z $proxy && $ip =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]] && command -v ping >/dev/null 2>&1; then
+ # Ping the IP curl actually reached; macOS -W uses ms, Linux uses seconds.
+ if [[ $os == Darwin ]]; then
+ ping -n -q -c "$count" -W 1000 -t "$((count + 2))" "$ip" >"$tmpdir/ping.txt" 2>&1 || true
+ else
+ ping -n -q -c "$count" -W 1 -w "$((count + 2))" "$ip" >"$tmpdir/ping.txt" 2>&1 || true
+ fi
+ ping_avg=$(awk -F= '/^(rtt|round-trip)/ {split($2,a,"/"); gsub(/ /,"",a[2]); print a[2]; found=1} END {if (!found) print "-"}' "$tmpdir/ping.txt")
+ loss=$(awk '/packet loss/ {for(i=1;i<=NF;i++) if($i=="packet") {print $(i-1); found=1; break}} END {if (!found) print "-"}' "$tmpdir/ping.txt")
+ fi
+ printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n' \
+ "$name" "$ping_avg" "$loss" "$tcp" "$ttfb" "$mbps" "$mbytes" "$status"
+ if [[ $status == 失败* ]]; then
+ awk 'NR==1 {print " " $0}' "$tmpdir/curl.err"
+ fi
+done
+
+printf '\n成功节点:%s/%s;收到的 HTTP 正文:' "$successes" "${#NODES[@]}"
+awk -v n="$total_bytes" 'BEGIN {printf "%.2f MiB(不含协议开销)\n", n/1048576}'
+printf '%s\n' \
+ 'Ping 与 HTTP 分开测量;丢包只代表这几次探测,禁 Ping 不代表断网。' \
+ 'TCP 不含 DNS;首字节包含 DNS、建连、TLS 和服务端响应,不等于 Ping。' \
+ '下载是单连接短时均速(含建连开销),受节点限速和路由影响,不代表宽带上限。' \
+ '阿里云由 CDN 就近调度,不保证落在国内;大文件请求由 Range 和 curl 限制。'
+(( successes > 0 ))