#!/bin/bash # 检查是否提供了URL参数 if [ $# -eq 0 ]; then echo "使用方法: $0 <要测试的URL>" exit 1 fi TARGET_URL="$1" LINKS_FILE="$(dirname "$0")/links.txt" HIGH_LATENCY_THRESHOLD=400 # 高延迟阈值,修改为400毫秒 TIMEOUT=10 # curl 超时时间,单位秒 MAX_RETRIES=1 # 失败重试次数 # 检查links.txt文件是否存在 if [ ! -f "$LINKS_FILE" ]; then echo "错误: $LINKS_FILE 文件不存在" exit 1 fi echo "开始测试代理速度,目标URL: $TARGET_URL" echo "--------------------------------------" # 初始化变量 total_latency=0 count=0 success_count=0 fail_count=0 retry_success_count=0 high_latency_proxies="" failed_proxies="" # 读取并测试每个代理 while IFS='|' read -r ip port username password || [ -n "$ip" ]; do # 跳过空行和注释行 [[ -z "$ip" || "$ip" =~ ^# ]] && continue # 去除可能的空白字符 ip=$(echo "$ip" | tr -d '[:space:]') port=$(echo "$port" | tr -d '[:space:]') username=$(echo "$username" | tr -d '[:space:]') password=$(echo "$password" | tr -d '[:space:]') proxy_url="http://$username:$password@$ip:$port" echo -n "测试代理: $ip:$port ... " # 测试逻辑封装成函数以便重试 test_proxy() { local start_time=$(date +%s%N) local response=$(curl -s -o /dev/null -w "%{http_code}" --connect-timeout $TIMEOUT --max-time $TIMEOUT -x "$proxy_url" "$TARGET_URL" 2>/dev/null) local end_time=$(date +%s%N) local latency=0 if [ "$response" = "200" ]; then latency=$(( (end_time - start_time) / 1000000 )) echo "$response $latency" else echo "$response 0" fi } # 第一次尝试 count=$((count + 1)) result=$(test_proxy) response=$(echo $result | cut -d ' ' -f 1) latency=$(echo $result | cut -d ' ' -f 2) # 判断是否需要重试 if [ "$response" != "200" ]; then echo -n "失败 - HTTP状态码: $response,正在重试... " # 重试 result=$(test_proxy) response=$(echo $result | cut -d ' ' -f 1) latency=$(echo $result | cut -d ' ' -f 2) if [ "$response" = "200" ]; then retry_success_count=$((retry_success_count + 1)) fi fi # 处理最终结果 if [ "$response" = "200" ]; then echo "成功 - 延迟: ${latency}ms" # 累加总延迟 total_latency=$((total_latency + latency)) success_count=$((success_count + 1)) # 检查是否为高延迟代理 if [ $latency -gt $HIGH_LATENCY_THRESHOLD ]; then high_latency_proxies="${high_latency_proxies}$ip:$port (${latency}ms)\n" fi else echo "最终失败 - HTTP状态码: $response" fail_count=$((fail_count + 1)) failed_proxies="${failed_proxies}$ip:$port (状态码: $response)\n" fi done < "$LINKS_FILE" echo "--------------------------------------" # 输出统计结果 echo "测试结果汇总:" echo "总测试代理数量: $count" echo "成功代理数量: $success_count (含重试成功: $retry_success_count)" echo "失败代理数量: $fail_count" echo "成功率: $(( success_count * 100 / count ))%" if [ $success_count -gt 0 ]; then average_latency=$(( total_latency / success_count )) echo "平均延迟: ${average_latency}ms" if [ -n "$high_latency_proxies" ]; then echo -e "\n高延迟代理 (>${HIGH_LATENCY_THRESHOLD}ms):" echo -e "$high_latency_proxies" else echo -e "\n没有发现高延迟代理。" fi fi if [ -n "$failed_proxies" ]; then echo -e "\n连接失败的代理:" echo -e "$failed_proxies" fi # 输出延迟分布情况 if [ $success_count -gt 0 ]; then echo -e "\n延迟分布统计:" # 重新统计以生成延迟分布 low_latency=0 # <100ms medium_latency=0 # 100-200ms high_latency=0 # 200-400ms very_high_latency=0 # >400ms while IFS='|' read -r ip port username password || [ -n "$ip" ]; do # 跳过空行和注释行 [[ -z "$ip" || "$ip" =~ ^# ]] && continue # 去除可能的空白字符 ip=$(echo "$ip" | tr -d '[:space:]') port=$(echo "$port" | tr -d '[:space:]') username=$(echo "$username" | tr -d '[:space:]') password=$(echo "$password" | tr -d '[:space:]') proxy_url="http://$username:$password@$ip:$port" # 只测量一次以获取延迟数据,不进行重试统计 start_time=$(date +%s%N) response=$(curl -s -o /dev/null -w "%{http_code}" --connect-timeout $TIMEOUT --max-time $TIMEOUT -x "$proxy_url" "$TARGET_URL" 2>/dev/null) end_time=$(date +%s%N) if [ "$response" = "200" ]; then latency=$(( (end_time - start_time) / 1000000 )) if [ $latency -lt 100 ]; then low_latency=$((low_latency + 1)) elif [ $latency -lt 200 ]; then medium_latency=$((medium_latency + 1)) elif [ $latency -lt 400 ]; then high_latency=$((high_latency + 1)) else very_high_latency=$((very_high_latency + 1)) fi fi done < "$LINKS_FILE" echo " < 100ms: $low_latency ($(( low_latency * 100 / success_count ))%)" echo "100-200ms: $medium_latency ($(( medium_latency * 100 / success_count ))%)" echo "200-400ms: $high_latency ($(( high_latency * 100 / success_count ))%)" echo " > 400ms: $very_high_latency ($(( very_high_latency * 100 / success_count ))%)" fi