From 79a2ad8e78c99f2f2ea589020278e6d4b64f9ef8 Mon Sep 17 00:00:00 2001 From: svefnz Date: Thu, 19 Mar 2026 16:15:15 +0800 Subject: [PATCH] update hy2.sh --- hy2.sh | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/hy2.sh b/hy2.sh index 8eba2c6..5a4e626 100644 --- a/hy2.sh +++ b/hy2.sh @@ -24,6 +24,12 @@ require_cmd() { } } +require_python3_json() { + python3 - <<'PY' >/dev/null 2>&1 +import json +PY +} + random_subdomain_prefix() { openssl rand -hex 8 | cut -c1-8 } @@ -113,7 +119,7 @@ get_server_ip() { } apt_update_and_install_base() { - if ! confirm_yn "即将执行 apt update 并安装基础依赖 curl sed ufw openssl,是否继续?"; then + if ! confirm_yn "即将执行 apt update 并安装基础依赖 curl sed ufw openssl python3,是否继续?"; then red "已取消。" exit 1 fi @@ -126,7 +132,7 @@ apt_update_and_install_base() { blue "==> 安装基础依赖" wait_for_apt_lock 300 || { red "APT 被占用,无法继续。"; exit 1; } - apt install -y curl sed ufw openssl + apt install -y curl sed ufw openssl python3 } disable_existing_firewalls() { @@ -250,10 +256,30 @@ cf_api_request() { fi } +cf_extract_zone_id() { + python3 -c ' +import json, sys +data = json.load(sys.stdin) +result = data.get("result") or [] +if result and isinstance(result[0], dict): + print(result[0].get("id", ""), end="") +' +} + +cf_extract_dns_record_id() { + python3 -c ' +import json, sys +data = json.load(sys.stdin) +result = data.get("result") or [] +if result and isinstance(result[0], dict): + print(result[0].get("id", ""), end="") +' +} + get_cf_zone_id() { local zone="$1" cf_api_request GET "https://api.cloudflare.com/client/v4/zones?name=${zone}" \ - | sed -n 's/.*"id":"\([^"]*\)".*/\1/p' | head -n 1 + | cf_extract_zone_id } delete_existing_cf_dns_record() { @@ -264,7 +290,7 @@ delete_existing_cf_dns_record() { record_id="$( cf_api_request GET "https://api.cloudflare.com/client/v4/zones/${zone_id}/dns_records?name=${full_domain}&type=${type}" \ - | sed -n 's/.*"id":"\([^"]*\)".*/\1/p' | head -n 1 + | cf_extract_dns_record_id )" if [[ -n "$record_id" ]]; then @@ -480,6 +506,8 @@ main() { require_cmd systemctl require_cmd ufw require_cmd openssl + require_cmd python3 + require_python3_json || { red "python3 缺少 json 模块,无法解析 Cloudflare API 返回值。"; exit 1; } local email zone subdomain proxy_url password ip_info ipv4 ipv6 domain