#!/usr/bin/env bash # On the atproto VM, as stg, after dev up: the network's own QR session-transfer service (plan 0040 step 8g), the # did-key-associator of the suite's tree (did-key-associator/, typescript-helpers/lib), at the origin dev up published # as the network value's qrOrigin (--network-qr-origin). An oauthQr identity of the network shows /#oauth=..., # takes /oauth-client-metadata.json as its client id and polls /xrpc/com.fedfork.atprotoOauthQR with # the audience did:web: (no port: oauthclient.QRTransfer), which is the backend's SERVICE_DID. # qr.sh up build the site for the origin, start the backend and the edge (a live one is kept), wait # until the origin answers # qr.sh down stop it # The site is built here with the pinned deno (tools.sh): the origin in its client metadata, the network's PLC and # bidder PDS (their https names: the page is https) in place of plc.directory and bsky.social. The backend runs from # source with the same deno on loopback, resolving DIDs at the network's PLC and trusting the network CA; qr-front.py # is its edge (prod: Caddy) on the network's address with the run's leaf (guest-tls/.crt covers *.). # The backend keeps its checks: it verifies a posted session at the account's PDS only on https port 443 of a host # that resolves to no private address (hono-backend/identity.ts checkUrl), except the origins the operator names as # the network's own (NETWORK_ORIGINS: the bidder PDS's public URL, on the network's address), like its PLC. set -euo pipefail S=${SWC:-/home/stg/stg/socialweb-computer-linux-amd64} ST=${SWC_STATE:-/home/stg/stg/s} I=${SWC_INSTANCE:-stg} R=${SWC_REPO:-${SWC_DIR:-/home/stg/stg}/suite} D=${SWC_DIR:-/home/stg/stg}/qr XRPC=${QR_LOOPBACK_XRPC_PORT:-18110} stop() { for p in backend front; do [ -f "$D/$p.pid" ] && kill "$(cat "$D/$p.pid")" 2>/dev/null || true rm -f "$D/$p.pid" done } case "${1:-}" in down) stop; exit 0 ;; up) url=${2:?usage: qr.sh up } ;; *) echo "usage: qr.sh up | down" >&2; exit 2 ;; esac url=${url%/} hp=${url#https://} host=${hp%%:*} port=${hp##*:} case "$url" in https://*:*) ;; *) echo "qr.sh: $url: an https origin with a port" >&2; exit 2 ;; esac case "$port" in ''|*[!0-9]*) echo "qr.sh: $url: an https origin with a port" >&2; exit 2 ;; esac ca=$D/ca.pem alive() { [ -f "$D/$1.pid" ] && kill -0 "$(cat "$D/$1.pid")" 2>/dev/null; } answers() { [ "$(curl -sS -o /dev/null -w '%{http_code}' --cacert "$ca" --max-time 5 "$url/xrpc/com.fedfork.atprotoOauthQR" 2>/dev/null)" = 401 ] && [ "$(curl -fsS --cacert "$ca" --max-time 5 "$url/oauth-client-metadata.json" 2>/dev/null | jq -r .client_id)" = "$url/oauth-client-metadata.json" ] } if alive backend && alive front && answers; then echo "qr.sh: $url already up" exit 0 fi stop N=(--instance "$I" --state-dir "$ST") st=$($S dev status "${N[@]}" 2>/dev/null) published=$(jq -r '.network.status.network.qrOrigin // empty' <<<"$st") front=$(jq -r '.network.status.network.xrpcFront // empty' <<<"$st") pds=$(jq -r '.network.status.bidderPdsUrl // empty' <<<"$st") addr=${front%:*} [ -n "$addr" ] && [ -n "$pds" ] || { echo "qr.sh: the network's status names no front or bidder PDS (xrpcFront=${front:-} bidderPdsUrl=${pds:-})" >&2; exit 1; } [ "$published" = "$url" ] || { echo "qr.sh: the network value's qrOrigin is ${published:-}, not $url (dev up --network-qr-origin)" >&2; exit 1; } [ -f "$R/did-key-associator/hono-backend/mod.ts" ] && [ -d "$R/typescript-helpers/lib/serve" ] || { echo "qr.sh: the suite carries no did-key-associator and typescript-helpers/lib ($R)" >&2; exit 1; } tls=$ST/dev/$I/network/guest-tls leaf=${host#*.} [ -f "$tls/$leaf.crt" ] && [ -f "$tls/$leaf.key" ] || { echo "qr.sh: no leaf $tls/$leaf.crt for $host" >&2; exit 1; } install -d -m 0700 "$D" jq -r '.network.status.network.caBundle' <<<"$st" > "$ca" # the page's PLC and PDS: https names (an ingress Exposure's address, as target.sh reads them; the PDS's own public URL) plc=${QR_PLC_URL:-} if [ -z "$plc" ]; then eval "$($S dev env "${N[@]}" 2>/dev/null)" ktoken=$(awk '/ token:/{print $2; exit}' "$KUBECONFIG") kserver=$(awk '/ server:/{print $2; exit}' "$KUBECONFIG") plc=$(curl -fsSk -H "Authorization: Bearer $ktoken" "${kserver%%/clusters/*}/clusters/root/apis/exposure.socialweb.computer/v1alpha1/exposures/svc-plc-plc-ingress" | jq -er .status.address) fi pdsname=${QR_PDS_URL:-$(curl -fsS --cacert "$ca" "$pds/.well-known/oauth-protected-resource" | jq -er .resource)} case "$plc$pdsname" in https://*https://*) ;; *) echo "qr.sh: the page needs https names (plc=$plc pds=$pdsname)" >&2; exit 1 ;; esac T=$("$(dirname "$0")/tools.sh" deno) export PATH=$T/deno:$PATH DENO_DIR=$D/deno-dir rm -rf "$D/src" && mkdir -p "$D/src" cp -r "$R/did-key-associator" "$R/typescript-helpers" "$D/src/" cd "$D/src/did-key-associator" rm -rf dist DKA_ORIGIN=$url DKA_PLC_URL=$plc DKA_PDS_URL=$pdsname deno task build:static > "$D/build.log" 2>&1 || { echo "qr.sh: the site build failed; build.log:" >&2; tail -n 20 "$D/build.log" >&2; exit 1; } rm -f "$D/port" ADDR=127.0.0.1 PORT=$XRPC PORT_FILE=$D/port KV_PATH=$D/kv.db PLC_URL=$plc NETWORK_ORIGINS=$pdsname SERVICE_DID=did:web:$host DENO_CERT=$ca \ setsid nohup deno run --unstable-kv -A hono-backend/mod.ts < /dev/null > "$D/backend.log" 2>&1 & echo $! > "$D/backend.pid" setsid nohup python3 "$R/tests/compositions/staging/qr-front.py" "$addr" "$port" "$tls/$leaf.crt" "$tls/$leaf.key" "$D/src/did-key-associator/dist" "$XRPC" \ < /dev/null > "$D/front.log" 2>&1 & echo $! > "$D/front.pid" for _ in $(seq 120); do if answers; then echo "qr.sh: $url up (plc $plc, pds $pdsname, did:web:$host, deno $(deno --version | head -1 | cut -d' ' -f2))" exit 0 fi alive backend && alive front || break sleep 1 done echo "qr.sh: $url did not answer; backend.log:" >&2 tail -n 20 "$D/backend.log" >&2 || true tail -n 5 "$D/front.log" >&2 || true exit 1