1# With FORGE_MIRROR_TOKEN set (a Fly secret), the edge sends it to the2# mirror bucket as X-Forge-Mirror-Token on each request for the offline3# copy, so a rule in front of the bucket can refuse requests without it.4. "$(dirname "$0")/../lib.sh"5command -v caddy >/dev/null 2>&1 || { note "caddy is not installed: skipped running it"; exit 0; }6# The bucket: answers every request, and writes down the token it came7# with. It takes a port of its own and says which.8node -e '9const fs = require("fs");10const s = require("http").createServer((req, res) => {11 fs.appendFileSync(process.argv[1], `${req.url} ${req.headers["x-forge-mirror-token"] ?? "-"}\n`);12 res.writeHead(200, { "Content-Type": "text/html" }).end("<p>copy</p>");13}).listen(0, "127.0.0.1", () => fs.writeFileSync(process.argv[2], String(s.address().port)));14' "$T/seen" "$T/mport" &15T_PIDS="$T_PIDS $!"16n=017until [ -s "$T/mport" ] || [ "$n" -ge 50 ]; do sleep 0.1; n=$((n + 1)); done18mport=$(cat "$T/mport")19eport=$(free_port)20dead=$(free_port)21# On 127.0.0.1 only, so no other scenario's server can take the port there.22sed "s/^:8080 {/:$eport {/; s/127\.0\.0\.1:8081/127.0.0.1:$dead/" "$T_HOME/deploy/edge/Caddyfile" |23 awk '{ print } $0 == "\tadmin off" { print "\tdefault_bind 127.0.0.1" }' >"$T/Caddyfile.edge"24FORGE_DOMAIN=src.test FORGE_MIRROR_URL=http://127.0.0.1:$mport FORGE_MIRROR_TOKEN=s3cret-token XDG_DATA_HOME=$T/xdg XDG_CONFIG_HOME=$T/xdg \25 caddy run --config "$T/Caddyfile.edge" --adapter caddyfile >"$T/caddy.edge.log" 2>&1 &26T_PIDS="$T_PIDS $!"27wait_http "http://127.0.0.1:$mport/"28wait_http "http://127.0.0.1:$eport/"29http -s -i -H 'Host: src.test' "http://127.0.0.1:$eport/dami/greet/" | tr -d '\r' >"$T/res"30has "$T/res" '^X-Forge-Offline: mirror'31has "$T/res" '<p>copy</p>'32has "$T/seen" '^/dami/greet/index\.html s3cret-token$'33lacks "$T/res" 's3cret-token'