#!/usr/bin/env bash # mquiz.tkrmagid.kr (from joke-app game-cert.sh): once DNS points at the home IP, get a Let's Encrypt cert in NPMplus (192.168.10.3) # and turn on Force SSL / HTTP2 / HSTS for the music_quiz proxy host. Safe to re-run. # Exit 0 + "DONE" = https works; exit 3 = DNS not ready yet; other = error. set -euo pipefail DOMAIN=mquiz.tkrmagid.kr WANT_IP=211.213.11.141 # Every authoritative NS must answer; Let's Encrypt fails on a single NXDOMAIN (and failures are rate-limited). for ns in ns1 ns2 ns3 ns4; do ip=$(dig +short +time=3 +tries=2 "$DOMAIN" A @$ns.hosting.co.kr | tail -1 || true) if [ "$ip" != "$WANT_IP" ]; then echo "DNS not ready ($DOMAIN @$ns -> '${ip}')"; exit 3; fi done ssh -o BatchMode=yes root@192.168.10.3 bash -s -- "$DOMAIN" <<'REMOTE' set -euo pipefail DOMAIN=$1 T=$(docker exec -w /app npmplus node --input-type=module -e 'import TokenModel from "./models/token.js"; const t = await TokenModel().create({iss:"api", attrs:{id:1}, scope:["user"], expiresIn:"30m"}); console.log(t.token);' | tail -1) api() { curl -sk -b "token=$T" -H 'Content-Type: application/json' "$@"; } HOST_ID=$(api https://127.0.0.1:81/api/nginx/proxy-hosts | python3 -c "import json,sys;print(next((h['id'] for h in json.load(sys.stdin) if '$DOMAIN' in h['domain_names']),0))") [ "$HOST_ID" = 0 ] && { echo "no proxy host for $DOMAIN"; exit 1; } host=$(api https://127.0.0.1:81/api/nginx/proxy-hosts/$HOST_ID) cid=$(echo "$host" | python3 -c 'import json,sys;print(json.load(sys.stdin).get("certificate_id") or 0)') if [ "$cid" = 0 ]; then cid=$(api https://127.0.0.1:81/api/nginx/certificates | python3 -c "import json,sys;print(next((c['id'] for c in json.load(sys.stdin) if '$DOMAIN' in c['domain_names']),0))") fi if [ "$cid" = 0 ]; then res=$(api -X POST https://127.0.0.1:81/api/nginx/certificates --data "{\"provider\":\"letsencrypt\",\"domain_names\":[\"$DOMAIN\"],\"meta\":{\"dns_challenge\":false}}") cid=$(echo "$res" | python3 -c 'import json,sys;print(json.load(sys.stdin).get("id") or 0)') [ "$cid" = 0 ] && { echo "cert request failed: $res" | head -c 600; exit 1; } fi api -X PUT https://127.0.0.1:81/api/nginx/proxy-hosts/$HOST_ID --data "{\"certificate_id\":$cid,\"ssl_forced\":true,\"http2_support\":true,\"hsts_enabled\":true,\"hsts_subdomains\":false}" | python3 -c 'import json,sys;d=json.load(sys.stdin);print("host", d.get("id"), "cert", d.get("certificate_id"), d.get("meta"), d.get("error"))' REMOTE code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 10 "https://$DOMAIN/healthz" || true) echo "https://$DOMAIN/healthz -> $code" [ "$code" = 200 ] && echo DONE