0
0
#!/usr/bin/env bash
# On the atproto VM, as stg, after dev up: the network's own atirc (plan 0040 step 8c), at the URL dev up published as
# the network value's atircUrl (--network-atirc-url). `atirc serve` resolves DIDs at the network's PLC and its service
# DID is did:web:<the URL's host and port>, the audience the network's requesters derive from atircUrl.
# atirc.sh up <url> start it (a live one is kept) and wait until <url>/chat answers
# atirc.sh down stop it
# An https URL: atirc serves plain HTTP on loopback and socat terminates TLS on the network's address with the run's
# leaf (guest-tls/<domain>.crt covers *.<domain>; the network's DNS answers every name under it; the leased 443 is the
# network's front, so the URL names another port). An http URL: atirc listens on the network's address itself.
set -euo pipefail
S=${SWC:-/home/stg/stg/socialweb-computer-linux-amd64} ST=${SWC_STATE:-/home/stg/stg/s} I=${SWC_INSTANCE:-stg}
D=${SWC_DIR:-/home/stg/stg}/atirc
XRPC=${ATIRC_LOOPBACK_XRPC_PORT:-18100} IRC=${ATIRC_IRC_PORT:-16667}
stop() {
for p in serve tls; 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: atirc.sh up <url>} ;;
*) echo "usage: atirc.sh up <url> | down" >&2; exit 2 ;;
esac
url=${url%/}
scheme=${url%%://*} hp=${url#*://}
host=${hp%%:*} port=${hp##*:}
case "$scheme:$port" in
https:*[!0-9]*|http:*[!0-9]*|https:|http:) echo "atirc.sh: $url: an http(s) URL with a port" >&2; exit 2 ;;
https:*|http:*) ;;
*) echo "atirc.sh: $url: an http(s) URL with a port" >&2; exit 2 ;;
esac
alive() { [ -f "$D/$1.pid" ] && kill -0 "$(cat "$D/$1.pid")" 2>/dev/null; }
answers() { curl -fsS -o /dev/null --max-time 5 "$url/chat" 2>/dev/null; }
if alive serve && { [ "$scheme" = http ] || alive tls; } && answers; then
echo "atirc.sh: $url already up"
exit 0
fi
stop
st=$($S dev status --instance "$I" --state-dir "$ST" 2>/dev/null)
plc=$(jq -r '.network.status.network.plcDirectoryUrl // empty' <<<"$st")
published=$(jq -r '.network.status.network.atircUrl // empty' <<<"$st")
front=$(jq -r '.network.status.network.xrpcFront // empty' <<<"$st")
addr=${front%:*}
[ -n "$plc" ] && [ -n "$addr" ] || { echo "atirc.sh: the network's status names no PLC or front (plc=${plc:-<none>} xrpcFront=${front:-<none>})" >&2; exit 1; }
[ "$published" = "$url" ] || { echo "atirc.sh: the network value's atircUrl is ${published:-<unset>}, not $url (dev up --network-atirc-url)" >&2; exit 1; }
bin=$(ls -t "$HOME"/.cache/socialweb-computer/runtime/*/atirc-x86_64 2>/dev/null | head -1)
[ -n "$bin" ] || { echo "atirc.sh: no staged atirc-x86_64 under ~/.cache/socialweb-computer/runtime" >&2; exit 1; }
install -d -m 0700 "$D" "$D/data"
if [ "$scheme" = https ]; then
tls=$ST/dev/$I/network/guest-tls leaf=${host#*.}
[ -f "$tls/$leaf.crt" ] && [ -f "$tls/$leaf.key" ] || { echo "atirc.sh: no leaf $tls/$leaf.crt for $host" >&2; exit 1; }
bind=127.0.0.1 xrpc=$XRPC
else
bind=$addr xrpc=$port
fi
ATIRC_DATA_DIR=$D/data ATIRC_PLC_DIRECTORY_URL=$plc setsid nohup "$bin" serve --bind "$bind" --endpoint "$url" \
--xrpc-port "$xrpc" --irc-port "$IRC" < /dev/null > "$D/serve.log" 2>&1 &
echo $! > "$D/serve.pid"
if [ "$scheme" = https ]; then
setsid nohup socat "OPENSSL-LISTEN:$port,bind=$addr,reuseaddr,fork,cert=$tls/$leaf.crt,key=$tls/$leaf.key,verify=0" \
"TCP:127.0.0.1:$xrpc" < /dev/null > "$D/tls.log" 2>&1 &
echo $! > "$D/tls.pid"
fi
for _ in $(seq 30); do
if answers; then
echo "atirc.sh: $url up (plc $plc, did:web:$host%3A$port)"
exit 0
fi
alive serve || break
sleep 1
done
echo "atirc.sh: $url did not answer; serve.log:" >&2
tail -n 20 "$D/serve.log" >&2 || true
[ -f "$D/tls.log" ] && tail -n 5 "$D/tls.log" >&2
exit 1