#!/usr/bin/env bash # Verify trust, force-HTTPS + ACME path, disable/enable and cleanup. set -uo pipefail ENTRY="${1:-ipdduiyy}" DOMAIN="${2:-186-241-74-221.sslip.io}" BASE="http://127.0.0.1:8888/$ENTRY" PASS=$(awk -F': ' '/^password:/{print $2}' /www/server/panel/data/initial_password.txt) ORIGIN="http://127.0.0.1:8888" curl -s -c /tmp/ck.txt -H 'Content-Type: application/json' -H "Origin: $ORIGIN" \ -d "{\"username\":\"admin\",\"password\":\"$PASS\"}" "$BASE/api/v1/auth/login" >/dev/null CSRF=$(curl -s -b /tmp/ck.txt "$BASE/api/v1/auth/session" | python3 -c "import json,sys;print(json.load(sys.stdin)['data']['csrf'])") H=(-b /tmp/ck.txt -H "X-CSRF-Token: $CSRF" -H 'Content-Type: application/json' -H "Origin: $ORIGIN") SID=$(curl -s -b /tmp/ck.txt "$BASE/api/v1/sites" | python3 -c " import json,sys print([s['id'] for s in json.load(sys.stdin)['data'] if '$DOMAIN' in s['domains']][0])") echo "site=$SID" echo "== 系统信任链校验(不加 -k) ==" curl -s -o /dev/null -w "https trusted: %{http_code}\n" "https://$DOMAIN/" echo "== 开启强制 HTTPS ==" curl -s -b /tmp/ck.txt "$BASE/api/v1/sites/$SID" > /tmp/site-full.json python3 - <<'PY' import json site = json.load(open('/tmp/site-full.json'))['data'] payload = {k: site[k] for k in ('name','kind','domains','root','port','php_version','run_user','start_cmd','env','upstream','remark')} opts = site['options']; opts['https_redirect'] = True payload['options'] = opts json.dump(payload, open('/tmp/site-put.json','w')) PY curl -s "${H[@]}" -X PUT -d @/tmp/site-put.json "$BASE/api/v1/sites/$SID" >/dev/null sleep 1 printf 'http / -> %s\n' "$(curl -s -o /dev/null -w '%{http_code}' -H "Host: $DOMAIN" http://127.0.0.1/)" mkdir -p /www/wwwroot/*/.well-known/acme-challenge 2>/dev/null || true ROOT=$(python3 -c "import json;print(json.load(open('/tmp/site-full.json'))['data']['root'])") printf 'acme-probe' > "$ROOT/.well-known/acme-challenge/probe" printf 'http acme probe -> %s (%s)\n' \ "$(curl -s -o /dev/null -w '%{http_code}' -H "Host: $DOMAIN" http://127.0.0.1/.well-known/acme-challenge/probe)" \ "$(curl -s -H "Host: $DOMAIN" http://127.0.0.1/.well-known/acme-challenge/probe)" rm -f "$ROOT/.well-known/acme-challenge/probe" echo "== 关闭 / 启用 HTTPS ==" curl -s "${H[@]}" -d '{}' "$BASE/api/v1/sites/$SID/ssl/disable" >/dev/null sleep 1 printf 'after disable https -> %s\n' "$(curl -sk -o /dev/null -w '%{http_code}' --max-time 4 "https://$DOMAIN/")" curl -s "${H[@]}" -d '{}' "$BASE/api/v1/sites/$SID/ssl/enable" >/dev/null sleep 1 printf 'after enable https -> %s\n' "$(curl -s -o /dev/null -w '%{http_code}' "https://$DOMAIN/")" echo "== SSL 状态 ==" curl -s -b /tmp/ck.txt "$BASE/api/v1/sites/$SID/ssl" echo echo "== 删除站点并检查证书清理 ==" curl -s "${H[@]}" -X DELETE "$BASE/api/v1/sites/$SID?remove_files=1" >/dev/null ls -d /www/server/panel/vhost/cert/186-241-74-221-sslip-io 2>/dev/null || echo "cert dir removed" nginx -t 2>&1 | tail -1