827 字
4 分钟
Linux SOCKS5代理延迟测试脚本

Linux SOCKS5 代理延迟测试脚本#

手上有多个代理节点时,想知道哪个连接目标网站最快,手动测试太麻烦。这里分享一个 Bash 脚本,自动批量测试并排序。

基础命令速查#

单次测试#

最简单的测试方法是用 curl:

Terminal window
curl -o /dev/null -s -w "
DNS解析: %{time_namelookup}s
TCP连接: %{time_connect}s
SSL握手: %{time_appconnect}s
首字节: %{time_starttransfer}s
总耗时: %{time_total}s
" -x socks5h://用户名:密码@IP:端口 https://api.groq.com
协议选择

使用 socks5h:// 而不是 socks5://,前者会让代理服务器进行 DNS 解析,绕过本地 DNS 污染,结果更准确。

批量测试脚本#

如果你有多个节点需要对比,用这个脚本:

test_proxies.sh
#!/bin/bash
# 目标URL
TARGET_URL="https://api.groq.com"
TIMEOUT=5
TEST_COUNT=12
# 节点列表
PROXIES=(
"HK|socks5h://user:[email protected]:58888"
"JP|socks5h://user:[email protected]:58888"
"SG|socks5h://user:[email protected]:58888"
"US|socks5h://user:[email protected]:58888"
)
echo "----------------------------------------------------------------"
echo "目标: $TARGET_URL"
echo "每个节点测 ${TEST_COUNT} 次,去掉最高/最低,取剩余 $(($TEST_COUNT-2)) 次平均"
echo "----------------------------------------------------------------"
printf "%-15s | %-12s | %-10s\n" "节点" "平均延迟" "失败次数"
echo "----------------------------------------------------------------"
RESULTS_FILE=$(mktemp)
for proxy_entry in "${PROXIES[@]}"; do
IFS='|' read -r name url <<< "$proxy_entry"
raw_times=""
fail_count=0
# 循环测试
for (( i=1; i<=TEST_COUNT; i++ )); do
printf "\r%-15s | 测试中... [%2d/%d]" "$name" "$i" "$TEST_COUNT"
output=$(curl -s -w "%{http_code},%{time_total}" -o /dev/null \
--connect-timeout $TIMEOUT -x "$url" "$TARGET_URL")
code=$(echo "$output" | cut -d',' -f1)
time=$(echo "$output" | cut -d',' -f2)
# 判断是否有效 (2xx/3xx 且耗时>0)
if [[ "$code" =~ ^[234] ]] && (( $(echo "$time > 0" | bc -l 2>/dev/null || echo 0) )); then
raw_times="$raw_times $time"
else
((fail_count++))
fi
done
# 数据处理:去头去尾取平均
sorted_times=$(echo "$raw_times" | tr ' ' '\n' | sort -n | grep -v '^$')
valid_count=$(echo "$sorted_times" | grep -c .)
if [ "$valid_count" -ge 3 ]; then
# 去掉最小和最大
trimmed_times=$(echo "$sorted_times" | sed '1d;$d')
# 计算平均值
avg_time=$(echo "$trimmed_times" | awk '{sum+=$1} END {printf "%.3f", sum/NR}')
printf "\r%-15s | %-10ss | %-10s\n" "$name" "$avg_time" "${fail_count}次"
echo "$avg_time $name $fail_count" >> "$RESULTS_FILE"
else
printf "\r%-15s | %-12s | %-10s\n" "$name" "N/A" "${fail_count}次"
fi
done
echo "----------------------------------------------------------------"
echo "最终排名 (越小越快):"
echo "----------------------------------------------------------------"
echo -e "排名\t延迟\t失败次数\t节点"
sort -n "$RESULTS_FILE" | awk '{printf "No.%d\t%ss\t%s\t%s\n", NR, $1, $3, $2}'
rm -f "$RESULTS_FILE"

脚本说明#

测试逻辑:

  • 每个节点测 12 次
  • 去掉 1 个最快(可能命中缓存)和 1 个最慢(网络抖动)
  • 剩余 10 次取平均

关键指标:

  • time_appconnect - SSL 握手时间,反映线路物理质量
  • time_total - 总耗时,包含 DNS、建连、握手、响应

使用方法#

Terminal window
chmod +x test_proxies.sh
./test_proxies.sh

输出示例:

节点 | 平均延迟 | 失败次数
----------------------------------------------------------------
US | 0.245s | 0次
JP | 0.312s | 0次
SG | 0.389s | 0次
HK | 0.456s | 2次
----------------------------------------------------------------
最终排名 (越小越快):
----------------------------------------------------------------
排名 延迟 失败次数 节点
No.1 0.245s 0 US
No.2 0.312s 0 JP
No.3 0.389s 0 SG
No.4 0.456s 2 HK

其他代理协议#

Terminal window
# HTTP 代理
curl -x http://127.0.0.1:8080 https://ip.sb
# HTTPS 代理
curl -x https://127.0.0.1:8443 https://ip.sb
# SOCKS4 代理
curl -x socks4://127.0.0.1:1080 https://ip.sb
# SOCKS4a 代理(远端 DNS)
curl -x socks4a://127.0.0.1:1080 https://ip.sb

使用环境变量#

设置临时环境变量(当前 shell 会话有效):

Terminal window
export ALL_PROXY=socks5h://127.0.0.1:40000
curl https://ip.sb
# 取消设置
unset ALL_PROXY

批量测试脚本#

如果你有多个节点需要对比,用这个脚本:

  1. 节点离线

    Terminal window
    nc -vz IP 端口
  2. 认证失败 - 用户名或密码错误

  3. IP被墙 - 连接被 RST

  4. 目标网站拒绝 - 某些网站会封代理IP

进阶:持续监控#

想持续观察节点稳定性?加个循环:

Terminal window
while true; do
./test_proxies.sh
echo "等待60秒后下一轮..."
sleep 60
done

参考链接#

Linux SOCKS5代理延迟测试脚本
https://im.awsl.app/posts/networking/037-socks5-latency-test/
作者
uu
发布于
2024-08-15
许可协议
CC0 1.0