From faf3fcf70afb01a410d21162a2c8e39586b4127d Mon Sep 17 00:00:00 2001 From: sindricn Date: Wed, 24 Sep 2025 16:17:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- install.sh | 28 ++++++++------------- scripts/common.sh | 48 ++++++++++++++++++++++++++++++++++-- scripts/firewall-manager.sh | 4 +-- scripts/outbound-manager.sh | 10 ++++---- scripts/post-deploy-check.sh | 4 +-- 5 files changed, 65 insertions(+), 29 deletions(-) diff --git a/install.sh b/install.sh index 77fe510..be01d90 100644 --- a/install.sh +++ b/install.sh @@ -3,8 +3,8 @@ # Hysteria2 安装脚本 (改进版本) # 作为 s-hy2 管理脚本的一部分 -# 严格错误处理 -set -euo pipefail +# 适度的错误处理 +set -uo pipefail # 加载公共库 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" @@ -79,31 +79,23 @@ check_system_compatibility() { # 检查网络连接 check_network_connection() { log_info "检查网络连接..." - + local test_urls=( - "github.com" - "get.hy2.sh" - "google.com" + "https://github.com" + "https://get.hy2.sh" + "https://www.google.com" + "https://raw.githubusercontent.com" ) - + local connected=false + # 直接使用 HTTP 连接测试,更可靠 for url in "${test_urls[@]}"; do - if ping -c 1 -W 5 "$url" &>/dev/null; then + if curl -s --connect-timeout 3 --max-time 8 --head "$url" >/dev/null 2>&1; then connected=true break fi done - if ! $connected; then - # 尝试 HTTP 连接测试 - for url in "${test_urls[@]}"; do - if curl -s --connect-timeout 5 --max-time 10 "https://$url" &>/dev/null; then - connected=true - break - fi - done - fi - if $connected; then log_success "网络连接正常" return 0 diff --git a/scripts/common.sh b/scripts/common.sh index 40509b8..1bf1f9c 100644 --- a/scripts/common.sh +++ b/scripts/common.sh @@ -3,8 +3,8 @@ # 公共函数库 - 统一错误处理和日志记录 # 为所有脚本提供标准化的错误处理、日志记录和工具函数 -# 严格错误处理 -set -euo pipefail +# 适度的错误处理 (不使用 -e 避免意外退出) +set -uo pipefail # 全局变量 (防止重复定义) if [[ -z "${SCRIPT_NAME:-}" ]]; then @@ -276,6 +276,50 @@ wait_for_user() { read -p "按回车键继续..." -r } +# 检查Hysteria2是否已安装和配置 +check_hysteria2_ready() { + local check_type="${1:-install}" # install, config, service + + case $check_type in + "install") + if ! command -v hysteria >/dev/null 2>&1; then + log_warn "Hysteria2 未安装" + echo "" + echo -e "${YELLOW}提示:${NC}请先安装 Hysteria2" + echo " 返回主菜单选择 '1. 安装 Hysteria2'" + echo "" + wait_for_user + return 1 + fi + ;; + "config") + if [[ ! -f "/etc/hysteria/config.yaml" ]]; then + log_warn "Hysteria2 配置文件不存在" + echo "" + echo -e "${YELLOW}提示:${NC}请先配置 Hysteria2" + echo " 1. 返回主菜单选择 '2. 快速配置' 或 '3. 手动配置'" + echo " 2. 如未安装,请先选择 '1. 安装 Hysteria2'" + echo "" + wait_for_user + return 1 + fi + ;; + "service") + if ! systemctl is-enabled hysteria-server >/dev/null 2>&1; then + log_warn "Hysteria2 服务未启用" + echo "" + echo -e "${YELLOW}提示:${NC}请先启用服务" + echo " 返回主菜单选择 '7. 服务管理'" + echo "" + wait_for_user + return 1 + fi + ;; + esac + + return 0 +} + # 显示进度条 show_progress() { local current="$1" diff --git a/scripts/firewall-manager.sh b/scripts/firewall-manager.sh index 770c8d8..5e099cd 100644 --- a/scripts/firewall-manager.sh +++ b/scripts/firewall-manager.sh @@ -3,8 +3,8 @@ # Hysteria2 防火墙管理模块 # 自动检测并管理 Linux 系统防火墙 -# 严格错误处理 -set -euo pipefail +# 适度的错误处理 +set -uo pipefail # 加载公共库 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" diff --git a/scripts/outbound-manager.sh b/scripts/outbound-manager.sh index 5a0213a..77f1be1 100644 --- a/scripts/outbound-manager.sh +++ b/scripts/outbound-manager.sh @@ -3,8 +3,8 @@ # Hysteria2 出站规则管理模块 # 用于配置和管理 Hysteria2 的出站规则 -# 严格错误处理 -set -euo pipefail +# 适度的错误处理 +set -uo pipefail # 加载公共库 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" @@ -130,9 +130,9 @@ show_outbound_menu() { view_current_outbound() { log_info "查看当前出站配置" - if [[ ! -f "$HYSTERIA_CONFIG" ]]; then - log_warn "Hysteria2 配置文件不存在" - return 1 + # 使用统一的检查函数 + if ! check_hysteria2_ready "config"; then + return 0 # 友好返回,不退出脚本 fi echo -e "${BLUE}=== 当前出站配置 ===${NC}" diff --git a/scripts/post-deploy-check.sh b/scripts/post-deploy-check.sh index 4b43fe9..19be259 100644 --- a/scripts/post-deploy-check.sh +++ b/scripts/post-deploy-check.sh @@ -3,8 +3,8 @@ # Hysteria2 部署后检查模块 # 确保节点部署完成后各项功能正常 -# 严格错误处理 -set -euo pipefail +# 适度的错误处理 +set -uo pipefail # 加载公共库 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"