diff --git a/README.md b/README.md index 594216e..5381042 100644 --- a/README.md +++ b/README.md @@ -25,11 +25,14 @@ sudo s-hy2 ### 安装问题修复 -如果安装后遇到问题(如"安装脚本不存在"),可以运行修复脚本: +如果安装后遇到问题,可以运行对应的修复脚本: ```bash -# 修复已安装的 s-hy2 +# 修复安装问题(如"安装脚本不存在") curl -fsSL https://raw.githubusercontent.com/sindricn/s-hy2/main/fix-installation.sh | sudo bash + +# 修复域名测试问题(如显示调试信息而非域名) +curl -fsSL https://raw.githubusercontent.com/sindricn/s-hy2/main/fix-domain-test.sh | sudo bash ``` ### 其他安装方式 diff --git a/fix-domain-test.sh b/fix-domain-test.sh new file mode 100644 index 0000000..dec5813 --- /dev/null +++ b/fix-domain-test.sh @@ -0,0 +1,107 @@ +#!/bin/bash + +# 修复域名测试功能脚本 + +# 颜色定义 +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +CYAN='\033[0;36m' +NC='\033[0m' + +INSTALL_DIR="/opt/s-hy2" +RAW_URL="https://raw.githubusercontent.com/sindricn/s-hy2/main" + +echo -e "${CYAN}修复域名测试功能${NC}" +echo "" + +# 检查权限 +if [[ $EUID -ne 0 ]]; then + echo -e "${RED}错误: 需要 root 权限${NC}" + echo "请使用: sudo bash" + exit 1 +fi + +# 检查安装目录 +if [[ ! -d "$INSTALL_DIR" ]]; then + echo -e "${RED}错误: S-Hy2 未安装${NC}" + echo "请先运行安装脚本" + exit 1 +fi + +echo -e "${BLUE}正在修复域名测试功能...${NC}" + +# 备份原文件 +if [[ -f "$INSTALL_DIR/scripts/domain-test.sh" ]]; then + cp "$INSTALL_DIR/scripts/domain-test.sh" "$INSTALL_DIR/scripts/domain-test.sh.backup.$(date +%Y%m%d_%H%M%S)" + echo -e "${GREEN}✓ 已备份原文件${NC}" +fi + +# 下载修复后的域名测试脚本 +echo -e "${BLUE}下载修复后的域名测试脚本...${NC}" +if curl -fsSL "$RAW_URL/scripts/domain-test.sh" -o "$INSTALL_DIR/scripts/domain-test.sh"; then + chmod +x "$INSTALL_DIR/scripts/domain-test.sh" + echo -e "${GREEN}✓ 域名测试脚本更新成功${NC}" +else + echo -e "${RED}✗ 域名测试脚本下载失败${NC}" + exit 1 +fi + +# 同时更新主脚本,确保路径问题也被修复 +echo -e "${BLUE}更新主脚本...${NC}" +if curl -fsSL "$RAW_URL/hy2-manager.sh" -o "$INSTALL_DIR/hy2-manager.sh"; then + chmod +x "$INSTALL_DIR/hy2-manager.sh" + echo -e "${GREEN}✓ 主脚本更新成功${NC}" +else + echo -e "${YELLOW}⚠ 主脚本更新失败,但域名测试功能已修复${NC}" +fi + +# 验证修复效果 +echo -e "${BLUE}验证修复效果...${NC}" + +# 加载修复后的脚本 +source "$INSTALL_DIR/scripts/domain-test.sh" + +# 测试静默版本 +echo "测试静默版本域名测试..." +if results=$(test_all_domains_silent 2>/dev/null | head -3); then + if [[ -n "$results" ]]; then + echo -e "${GREEN}✓ 静默版本测试成功${NC}" + + # 验证输出格式 + valid=true + while IFS= read -r line; do + if [[ -n "$line" ]]; then + latency=$(echo "$line" | awk '{print $1}') + domain=$(echo "$line" | awk '{print $2}') + + if [[ ! "$latency" =~ ^[0-9]+$ ]] || [[ -z "$domain" ]] || [[ "$domain" =~ [[:space:]] ]]; then + valid=false + break + fi + fi + done <<< "$results" + + if [[ "$valid" == "true" ]]; then + echo -e "${GREEN}✓ 输出格式验证通过${NC}" + else + echo -e "${YELLOW}⚠ 输出格式可能仍有问题${NC}" + fi + else + echo -e "${YELLOW}⚠ 静默版本无输出,可能是网络问题${NC}" + fi +else + echo -e "${RED}✗ 静默版本测试失败${NC}" +fi + +echo "" +echo -e "${CYAN}修复完成!${NC}" +echo "" +echo -e "${YELLOW}现在可以测试域名功能:${NC}" +echo "1. 运行: sudo s-hy2" +echo "2. 选择 '6. 测试伪装域名'" +echo "3. 选择 '2. 交互式选择域名'" +echo "" +echo -e "${YELLOW}如果仍有问题,可以运行验证脚本:${NC}" +echo "curl -fsSL https://raw.githubusercontent.com/sindricn/s-hy2/main/test-domain-fix.sh | bash" diff --git a/scripts/domain-test.sh b/scripts/domain-test.sh index abaa339..598697a 100644 --- a/scripts/domain-test.sh +++ b/scripts/domain-test.sh @@ -57,33 +57,51 @@ test_domain_latency() { fi } -# 测试所有域名并排序 -test_all_domains() { +# 测试所有域名并排序 (静默版本,只输出结果) +test_all_domains_silent() { local results=() - local total=${#DOMAINS[@]} - local current=0 - - echo -e "${BLUE}正在测试 $total 个域名的延迟...${NC}" - echo "" - + for domain in "${DOMAINS[@]}"; do - current=$((current + 1)) - printf "\r${BLUE}进度: $current/$total - 测试 $domain${NC}" - if result=$(test_domain_latency "$domain" 3); then results+=("$result") fi done - - echo "" - echo "" - + if [[ ${#results[@]} -eq 0 ]]; then - echo -e "${RED}所有域名测试失败,请检查网络连接${NC}" return 1 fi - - # 排序结果 + + # 排序结果并输出 + printf '%s\n' "${results[@]}" | sort -n +} + +# 测试所有域名并排序 (带进度显示) +test_all_domains() { + local results=() + local total=${#DOMAINS[@]} + local current=0 + + echo -e "${BLUE}正在测试 $total 个域名的延迟...${NC}" >&2 + echo "" >&2 + + for domain in "${DOMAINS[@]}"; do + current=$((current + 1)) + printf "\r${BLUE}进度: $current/$total - 测试 $domain${NC}" >&2 + + if result=$(test_domain_latency "$domain" 3); then + results+=("$result") + fi + done + + echo "" >&2 + echo "" >&2 + + if [[ ${#results[@]} -eq 0 ]]; then + echo -e "${RED}所有域名测试失败,请检查网络连接${NC}" >&2 + return 1 + fi + + # 排序结果并输出 printf '%s\n' "${results[@]}" | sort -n } @@ -114,7 +132,7 @@ get_best_domain() { # 获取最优域名名称 get_best_domain_name() { - local best_result=$(test_all_domains | head -n 1) + local best_result=$(test_all_domains_silent | head -n 1) if [[ -n "$best_result" ]]; then echo "$best_result" | awk '{print $2}' else @@ -126,29 +144,43 @@ get_best_domain_name() { interactive_domain_selection() { echo -e "${BLUE}域名延迟测试和选择${NC}" echo "" - - # 显示测试结果 - local results=$(test_all_domains | head -n 10) - + + # 显示进度信息 + echo -e "${BLUE}正在测试域名延迟,请稍候...${NC}" + + # 使用静默版本获取测试结果,避免进度信息混入 + local results=$(test_all_domains_silent | head -n 10) + if [[ -z "$results" ]]; then echo -e "${RED}域名测试失败,使用默认域名${NC}" echo "默认域名: news.ycombinator.com" read -p "按回车键继续..." return fi - + + echo "" echo -e "${CYAN}可用域名列表:${NC}" echo "" printf "%-5s %-30s %s\n" "编号" "域名" "延迟(ms)" echo "----------------------------------------" - + local domains_array=() local index=1 - - while read -r latency domain; do - printf "%-5d %-30s %d ms\n" "$index" "$domain" "$latency" - domains_array+=("$domain") - index=$((index + 1)) + + # 改进解析逻辑,过滤无效行 + while IFS= read -r line; do + if [[ -n "$line" ]]; then + # 提取延迟和域名 + local latency=$(echo "$line" | awk '{print $1}') + local domain=$(echo "$line" | awk '{print $2}') + + # 验证数据格式:延迟必须是数字,域名不能为空且不能包含空格 + if [[ "$latency" =~ ^[0-9]+$ ]] && [[ -n "$domain" ]] && [[ ! "$domain" =~ [[:space:]] ]]; then + printf "%-5d %-30s %d ms\n" "$index" "$domain" "$latency" + domains_array+=("$domain") + index=$((index + 1)) + fi + fi done <<< "$results" echo "" diff --git a/test-domain-fix.sh b/test-domain-fix.sh new file mode 100644 index 0000000..73dcdc6 --- /dev/null +++ b/test-domain-fix.sh @@ -0,0 +1,108 @@ +#!/bin/bash + +# 测试域名功能修复效果 + +# 颜色定义 +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +CYAN='\033[0;36m' +NC='\033[0m' + +echo -e "${CYAN}域名测试功能修复验证${NC}" +echo "" + +# 检查脚本是否存在 +if [[ ! -f "/opt/s-hy2/scripts/domain-test.sh" ]]; then + echo -e "${RED}错误: 域名测试脚本不存在${NC}" + echo "请先运行修复脚本:" + echo "curl -fsSL https://raw.githubusercontent.com/sindricn/s-hy2/main/fix-installation.sh | sudo bash" + exit 1 +fi + +# 加载域名测试脚本 +source /opt/s-hy2/scripts/domain-test.sh + +echo -e "${BLUE}测试1: 静默版本域名测试${NC}" +echo "这个测试应该只输出 '延迟 域名' 格式的数据,没有进度信息" +echo "" + +# 测试静默版本 +echo "--- 静默版本输出 ---" +results=$(test_all_domains_silent | head -5) +echo "$results" +echo "--- 输出结束 ---" +echo "" + +# 验证输出格式 +echo -e "${BLUE}验证输出格式:${NC}" +valid_lines=0 +total_lines=0 + +while IFS= read -r line; do + if [[ -n "$line" ]]; then + total_lines=$((total_lines + 1)) + latency=$(echo "$line" | awk '{print $1}') + domain=$(echo "$line" | awk '{print $2}') + + if [[ "$latency" =~ ^[0-9]+$ ]] && [[ -n "$domain" ]] && [[ ! "$domain" =~ [[:space:]] ]]; then + echo -e "${GREEN}✓ 有效: $latency ms - $domain${NC}" + valid_lines=$((valid_lines + 1)) + else + echo -e "${RED}✗ 无效: $line${NC}" + fi + fi +done <<< "$results" + +echo "" +echo "统计: $valid_lines/$total_lines 行有效" + +if [[ $valid_lines -eq $total_lines ]] && [[ $total_lines -gt 0 ]]; then + echo -e "${GREEN}✅ 静默版本测试通过${NC}" +else + echo -e "${RED}❌ 静默版本测试失败${NC}" +fi + +echo "" +echo -e "${BLUE}测试2: 带进度版本域名测试${NC}" +echo "这个测试应该显示进度信息,但不影响结果输出" +echo "" + +# 测试带进度版本 +echo "--- 带进度版本输出 ---" +results_with_progress=$(test_all_domains | head -5) +echo "$results_with_progress" +echo "--- 输出结束 ---" +echo "" + +# 比较两个版本的输出 +echo -e "${BLUE}比较两个版本的输出:${NC}" +if [[ "$results" == "$results_with_progress" ]]; then + echo -e "${GREEN}✅ 两个版本输出一致${NC}" +else + echo -e "${RED}❌ 两个版本输出不一致${NC}" + echo "" + echo "静默版本:" + echo "$results" + echo "" + echo "带进度版本:" + echo "$results_with_progress" +fi + +echo "" +echo -e "${BLUE}测试3: 获取最优域名${NC}" +best_domain=$(get_best_domain_name) +echo "最优域名: $best_domain" + +if [[ -n "$best_domain" ]] && [[ ! "$best_domain" =~ [[:space:]] ]]; then + echo -e "${GREEN}✅ 最优域名获取成功${NC}" +else + echo -e "${RED}❌ 最优域名获取失败${NC}" +fi + +echo "" +echo -e "${CYAN}修复验证完成${NC}" +echo "" +echo -e "${YELLOW}如果所有测试都通过,域名测试功能应该已经修复${NC}" +echo -e "${YELLOW}现在可以运行 'sudo s-hy2' 测试交互式域名选择功能${NC}"