From c0824d99c0907c3db497093ecd4a663a555035cf Mon Sep 17 00:00:00 2001 From: Olia Lisa Date: Thu, 28 May 2026 15:08:00 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81,=20update?= =?UTF-8?q?=20=E7=AB=AF=E5=8F=A3=E7=9B=B8=E5=85=B3=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/create_config_hysteria.sh | 14 +++++++------ bin/create_config_tuic.sh | 9 +++++---- bin/port.sh | 38 +++++++++++++++++++++++------------ 3 files changed, 38 insertions(+), 23 deletions(-) diff --git a/bin/create_config_hysteria.sh b/bin/create_config_hysteria.sh index 2737778..cddc7b1 100644 --- a/bin/create_config_hysteria.sh +++ b/bin/create_config_hysteria.sh @@ -27,15 +27,16 @@ create_hysteria_config(){ create_hysteria_self_tls_config() { local SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # 脚本文件夹绝对路径 - local CONFIG_FILE="$SCRIPT_DIR/../config/config.json" + local CONFIG_DIR="$SCRIPT_DIR/../config" + local CONFIG_FILE="$CONFIG_DIR/config.json" - cp "$SCRIPT_DIR/../config/template/hysteria/hysteria_self_tls_config.json" "$CONFIG_FILE" + cp "$CONFIG_DIR/template/hysteria/hysteria_self_tls_config.json" "$CONFIG_FILE" # 生成自签名证书和设置域名 update_self_tls "$CONFIG_FILE" "bing.com" # 设置端口 - update_port + update_port "$CONFIG_FILE" # 设置密码 update_password "$CONFIG_FILE" @@ -48,14 +49,15 @@ create_hysteria_self_tls_config() { create_hysteria_acme_tls_config() { local SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # 脚本文件夹绝对路径 - local CONFIG_FILE="$SCRIPT_DIR/../config/config.json" + local CONFIG_DIR="$SCRIPT_DIR/../config" + local CONFIG_FILE="$CONFIG_DIR/config.json" - cp "$SCRIPT_DIR/../config/template/hysteria/hysteria_acme_tls_config.json" "$CONFIG_FILE" + cp "$CONFIG_DIR/template/hysteria/hysteria_acme_tls_config.json" "$CONFIG_FILE" update_acme_tls "$CONFIG_FILE" # 设置端口 - update_port + update_port "$CONFIG_FILE" # 设置密码 update_password "$CONFIG_FILE" diff --git a/bin/create_config_tuic.sh b/bin/create_config_tuic.sh index d83fe2b..0624b81 100644 --- a/bin/create_config_tuic.sh +++ b/bin/create_config_tuic.sh @@ -35,7 +35,7 @@ create_tuic_acme_tls_config() { update_acme_tls "$CONFIG_FILE" # 设置端口 - update_port + update_port "$CONFIG_FILE" # 设置密码 update_password "$CONFIG_FILE" @@ -48,15 +48,16 @@ create_tuic_acme_tls_config() { create_tuic_self_tls_config() { local SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # 脚本文件夹绝对路径 - local CONFIG_FILE="$SCRIPT_DIR/../config/config.json" + local CONFIG_DIR="$SCRIPT_DIR/../config" + local CONFIG_FILE="$CONFIG_DIR/config.json" - cp "$SCRIPT_DIR/../config/template/tuic/tuic_self_tls_config.json" "$CONFIG_FILE" + cp "$CONFIG_DIR/template/tuic/tuic_self_tls_config.json" "$CONFIG_FILE" # 生成自签名证书和设置域名 update_self_tls "$CONFIG_FILE" "bing.com" # 设置端口 - update_port + update_port "$CONFIG_FILE" # 设置密码 update_password "$CONFIG_FILE" diff --git a/bin/port.sh b/bin/port.sh index c3ccdb6..4989d92 100644 --- a/bin/port.sh +++ b/bin/port.sh @@ -2,21 +2,25 @@ # ========================================== # 更新 sing-box 配置文件中的监听端口,并开放对应防火墙规则 -# 参数: 端口号(可选,不传则设置一个5000 到 15000的空闲端口) +# 参数1: 配置文件路径 +# 参数2: 端口号(可选,不传则设置一个5000 到 15000的空闲端口) # --- 使用示例 --- # 指定端口 -# update_port 8080 +# update_port "$CONFIG_FILE" 8080 # ========================================== update_port(){ - local PORT=$1 + local CONFIG_FILE="$1" + local PORT="$2" + if [[ -z "$CONFIG_FILE" ]]; then + echo "[Error] config_file配置文件路径 不能为空" + return 1 + fi # 如果没有提供端口参数,则自动查找一个随机空闲端口 if [[ -z "$PORT" ]]; then PORT=$(find_free_port 5000 15000) - fi + fi - local SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # 脚本文件夹绝对路径 - local CONFIG_DIR="$SCRIPT_DIR/../config" - modify_json_file "$CONFIG_DIR/config.json" ".inbounds[0].listen_port" "$PORT" + modify_json_file "$CONFIG_FILE" ".inbounds[0].listen_port" "$PORT" port_manage allow "$PORT" echo "设置端口成功" } @@ -187,18 +191,26 @@ port_manage() { # 通过交互方式更新端口 ask_update_port(){ - local SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" - local CONFIG_FILE="$SCRIPT_DIR/../config/config.json" + local CONFIG_FILE="${1}" - local CURRENT_PORT=$(get_listen_port) + if [[ -z "$CONFIG_FILE" ]]; then + echo "[Error] config_file配置文件路径 不能为空" + return 1 + fi + + local CURRENT_PORT=$(get_listen_port "$CONFIG_FILE") local NEW_PORT=$(ask_update "请输入新的端口号" "$CURRENT_PORT") - update_port "$NEW_PORT" + update_port "$CONFIG_FILE" "$NEW_PORT" } # 获取当前配置的监听端口 get_listen_port(){ - local SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" - local CONFIG_FILE="$SCRIPT_DIR/../config/config.json" + local CONFIG_FILE="${1}" + + if [[ -z "$CONFIG_FILE" ]]; then + echo "[Error] config_file配置文件路径 不能为空" + return 1 + fi local PORT=$(jq -r ".inbounds[0].listen_port" "$CONFIG_FILE") echo $PORT