miaopanel
1#!/usr/bin/env bash
2# Verify trust, force-HTTPS + ACME path, disable/enable and cleanup.
3set -uo pipefail
4ENTRY="${1:-ipdduiyy}"
5DOMAIN="${2:-186-241-74-221.sslip.io}"
6BASE="http://127.0.0.1:8888/$ENTRY"
7PASS=$(awk -F': ' '/^password:/{print $2}' /www/server/panel/data/initial_password.txt)
8ORIGIN="http://127.0.0.1:8888"
9
10curl -s -c /tmp/ck.txt -H 'Content-Type: application/json' -H "Origin: $ORIGIN" \
11 -d "{\"username\":\"admin\",\"password\":\"$PASS\"}" "$BASE/api/v1/auth/login" >/dev/null
12CSRF=$(curl -s -b /tmp/ck.txt "$BASE/api/v1/auth/session" | python3 -c "import json,sys;print(json.load(sys.stdin)['data']['csrf'])")
13H=(-b /tmp/ck.txt -H "X-CSRF-Token: $CSRF" -H 'Content-Type: application/json' -H "Origin: $ORIGIN")
14
15SID=$(curl -s -b /tmp/ck.txt "$BASE/api/v1/sites" | python3 -c "
16import json,sys
17print([s['id'] for s in json.load(sys.stdin)['data'] if '$DOMAIN' in s['domains']][0])")
18echo "site=$SID"
19
20echo "== 系统信任链校验(不加 -k) =="
21curl -s -o /dev/null -w "https trusted: %{http_code}\n" "https://$DOMAIN/"
22
23echo "== 开启强制 HTTPS =="
24curl -s -b /tmp/ck.txt "$BASE/api/v1/sites/$SID" > /tmp/site-full.json
25python3 - <<'PY'
26import json
27site = json.load(open('/tmp/site-full.json'))['data']
28payload = {k: site[k] for k in ('name','kind','domains','root','port','php_version','run_user','start_cmd','env','upstream','remark')}
29opts = site['options']; opts['https_redirect'] = True
30payload['options'] = opts
31json.dump(payload, open('/tmp/site-put.json','w'))
32PY
33curl -s "${H[@]}" -X PUT -d @/tmp/site-put.json "$BASE/api/v1/sites/$SID" >/dev/null
34sleep 1
35printf 'http / -> %s\n' "$(curl -s -o /dev/null -w '%{http_code}' -H "Host: $DOMAIN" http://127.0.0.1/)"
36mkdir -p /www/wwwroot/*/.well-known/acme-challenge 2>/dev/null || true
37ROOT=$(python3 -c "import json;print(json.load(open('/tmp/site-full.json'))['data']['root'])")
38printf 'acme-probe' > "$ROOT/.well-known/acme-challenge/probe"
39printf 'http acme probe -> %s (%s)\n' \
40 "$(curl -s -o /dev/null -w '%{http_code}' -H "Host: $DOMAIN" http://127.0.0.1/.well-known/acme-challenge/probe)" \
41 "$(curl -s -H "Host: $DOMAIN" http://127.0.0.1/.well-known/acme-challenge/probe)"
42rm -f "$ROOT/.well-known/acme-challenge/probe"
43
44echo "== 关闭 / 启用 HTTPS =="
45curl -s "${H[@]}" -d '{}' "$BASE/api/v1/sites/$SID/ssl/disable" >/dev/null
46sleep 1
47printf 'after disable https -> %s\n' "$(curl -sk -o /dev/null -w '%{http_code}' --max-time 4 "https://$DOMAIN/")"
48curl -s "${H[@]}" -d '{}' "$BASE/api/v1/sites/$SID/ssl/enable" >/dev/null
49sleep 1
50printf 'after enable https -> %s\n' "$(curl -s -o /dev/null -w '%{http_code}' "https://$DOMAIN/")"
51
52echo "== SSL 状态 =="
53curl -s -b /tmp/ck.txt "$BASE/api/v1/sites/$SID/ssl"
54
55echo
56echo "== 删除站点并检查证书清理 =="
57curl -s "${H[@]}" -X DELETE "$BASE/api/v1/sites/$SID?remove_files=1" >/dev/null
58ls -d /www/server/panel/vhost/cert/186-241-74-221-sslip-io 2>/dev/null || echo "cert dir removed"
59nginx -t 2>&1 | tail -1