Diffinery

homeflare/diffinerypublic

Open-source, agent-first GitHub and GitHub Actions alternative that runs on your own Cloudflare account: git hosting, pull requests, code review and drop-in GitHub Actions YAML on Workers, Durable Objects, R2 and Containers.

diffinery/0001-homeflare.patch
367 lines · 22.4 KBRaw History
1diff --git a/HOMEFLARE.md b/HOMEFLARE.md
2new file mode 100644
3index 0000000..9ea4afd
4--- /dev/null
5+++ b/HOMEFLARE.md
6@@ -0,0 +1,47 @@
7+# Diffinery on Homeflare
8+
9+[Homeflare](https://github.com/jason-s13r/homeflare) runs Workers on your own machines (celld) behind a
10+Cloudflare-compatible API. These scripts deploy to it when `CLOUDFLARE_API_BASE_URL` points at that API,
11+which `homeflare-wrangler run` sets, along with `CLOUDFLARE_CONTAINER_REGISTRY` (Homeflare's registry) and a
12+`docker` command on your Podman.
13+
14+On another API than Cloudflare's, `scripts/setup.sh` and `scripts/bootstrap.sh` skip what needs Cloudflare
15+itself:
16+
17+| Skipped | What it means |
18+| --- | --- |
19+| Zone check | The hostname is a Homeflare custom domain, not a route in a Cloudflare zone. |
20+| R2 lifecycle rules | Logs, artifacts, archives and exports don't age out by themselves. |
21+| Email Sending setup and its event subscription | Onboard the sending domain in Cloudflare yourself (Homeflare relays email through it with `HOMEFLARE_EMAIL=cloudflare`). Bounces and complaints don't reach the suppression list. |
22+| Turnstile widget | Make one in the Cloudflare dashboard and pass its keys: `TURNSTILE_SITE_KEY` and `TURNSTILE_SECRET`. Without them open sign-up and anonymous security reports stay off. |
23+| Cloudflare Access | Email recovery and the `/auth/access` first sign-in stay off. Passkeys work. |
24+
25+## Deploy
26+
27+Put the settings in a file outside the repository (it holds the Turnstile secret), for example
28+`~/.config/homeflare/diffinery.env`, readable by you only:
29+
30+```sh
31+CLOUDFLARE_ACCOUNT_ID=<Homeflare's account id>
32+DIFFINERY_DOMAIN=git.homeflare.example.com
33+MAIL_DOMAIN=example.com
34[email protected]
35+TURNSTILE_SITE_KEY=0x4AAA...
36+TURNSTILE_SECRET=0x4AAA...
37+```
38+
39+`MAIL_DOMAIN` is a domain onboarded to Email Sending in your Cloudflare account. Homeflare's account id is
40+in the Dashboard, or from `GET /client/v4/accounts`. Then, from the repository:
41+
42+```sh
43+homeflare-wrangler --env-file ~/.config/homeflare/diffinery.env run scripts/setup.sh --yes
44+```
45+
46+`homeflare-wrangler` reads the Homeflare API token from `~/.config/homeflare/wrangler.env` and sets
47+`CLOUDFLARE_API_BASE_URL` itself. Run it again to update.
48+
49+## The first administrator
50+
51+Without Access, the first account comes from open sign-up, which needs Turnstile and email. Deploy once with
52+`"SIGNUP": "open"` under `vars` in `apps/diffinery/wrangler.jsonc`, sign up at `https://<host>/signup` with an
53+address in `BOOTSTRAP_ADMINS`, add a passkey, then remove `SIGNUP` and deploy again.
54diff --git a/scripts/bootstrap-admins.sh b/scripts/bootstrap-admins.sh
55index 989078a..a6a54d9 100755
56--- a/scripts/bootstrap-admins.sh
57+++ b/scripts/bootstrap-admins.sh
58@@ -59,7 +59,7 @@ FROM_FILE="$(trimmed "$FROM_FILE")"
59 if [[ -z "${CLOUDFLARE_API_TOKEN:-}" || -z "${CLOUDFLARE_ACCOUNT_ID:-}" ]]; then
60 none "it isn't in the environment or wrangler.jsonc, and without CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID the deployed Worker's list can't be read"
61 fi
62-SETTINGS="$(curl -sS --retry 4 --retry-all-errors --retry-delay 3 "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/workers/scripts/$WORKER/settings" -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" || true)"
63+SETTINGS="$(curl -sS --retry 4 --retry-all-errors --retry-delay 3 "${CLOUDFLARE_API_BASE_URL:-https://api.cloudflare.com/client/v4}/accounts/$CLOUDFLARE_ACCOUNT_ID/workers/scripts/$WORKER/settings" -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" || true)"
64 if jq -e '.success == true' >/dev/null 2>&1 <<<"$SETTINGS"; then
65 LIVE="$(jq -r '[.result.bindings[]? | select(.type == "plain_text" and .name == "BOOTSTRAP_ADMINS") | .text][0] // empty' <<<"$SETTINGS")"
66 LIVE="$(trimmed "$LIVE")"
67diff --git a/scripts/bootstrap.sh b/scripts/bootstrap.sh
68index aea32f9..29d0c52 100755
69--- a/scripts/bootstrap.sh
70+++ b/scripts/bootstrap.sh
71@@ -25,7 +25,11 @@ DOMAIN="${DIFFINERY_DOMAIN:-diffinery.com}"
72 ROOT="$(cd "$(dirname "$0")/.." && pwd)"
73 APP="$ROOT/apps/diffinery"
74 WRANGLER=(pnpm --dir "$APP" exec wrangler)
75-API="https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID"
76+CF_API="${CLOUDFLARE_API_BASE_URL:-https://api.cloudflare.com/client/v4}"
77+API="$CF_API/accounts/$CLOUDFLARE_ACCOUNT_ID"
78+# 0 on another Cloudflare-compatible API (Homeflare): the steps that need Cloudflare itself are skipped.
79+ON_CLOUDFLARE=1
80+[[ "$CF_API" == https://api.cloudflare.com/* ]] || ON_CLOUDFLARE=0
81
82 cf() { # cf METHOD PATH [JSON]
83 local method="$1" path="$2" body="${3:-}"
84@@ -74,6 +78,9 @@ LIFECYCLE='{"rules":[
85 {"id":"action-mirror-30d","enabled":true,"conditions":{"prefix":"action-mirror/"},"deleteObjectsTransition":{"condition":{"type":"Age","maxAge":2592000}}}
86 ]}'
87 # Applied on every deploy; a failure stops it, since a wrong rule can delete data.
88+if ((ON_CLOUDFLARE == 0)); then
89+ warn "R2 lifecycle rules are skipped on $CF_API: transient objects (logs, artifacts, archives, exports) don't age out by themselves."
90+else
91 RESULT="$(cf PUT "/r2/buckets/diffinery-data/lifecycle" "$LIFECYCLE")"
92 if ! jq -e '.success' >/dev/null <<<"$RESULT"; then
93 echo "could not set the R2 lifecycle rules (token has R2: Edit?): $(jq -c '.errors // .' <<<"$RESULT" 2>/dev/null || echo "$RESULT")" >&2
94@@ -85,6 +92,7 @@ if jq -e '[.result.rules[]? | select(.deleteObjectsTransition != null and ((.con
95 exit 1
96 fi
97 echo "R2 lifecycle rules set"
98+fi
99 endlog
100
101 # ------------------------------------------------------------------ Queues
102@@ -127,7 +135,7 @@ subscribe_mail_events() {
103 if [[ -z "$qid" ]]; then warn "Could not find the diffinery-mail-events queue, so it isn't subscribed to Email Sending's bounce and complaint events."; return; fi
104 # The zone that holds the sending domain (the domain itself or a parent), as scripts/email-sending.sh finds it.
105 while [[ "$name" == *.* ]]; do
106- zone="$(curl -sS "https://api.cloudflare.com/client/v4/zones?name=$name" -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" | jq -r '.result[0].id // empty' || true)"
107+ zone="$(curl -sS "${CLOUDFLARE_API_BASE_URL:-https://api.cloudflare.com/client/v4}/zones?name=$name" -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" | jq -r '.result[0].id // empty' || true)"
108 [[ -n "$zone" ]] && break
109 name="${name#*.}"
110 done
111@@ -162,12 +170,16 @@ subscribe_mail_events() {
112 warn "Could not subscribe diffinery-mail-events to Email Sending's bounce and complaint events for $SEND_DOMAIN. The token needs $SUB_PERMISSION (and $SEND_DOMAIN onboarded to Email Sending). Cloudflare said: $(api_errors "$res")"
113 fi
114 }
115-subscribe_mail_events
116+if ((ON_CLOUDFLARE)); then subscribe_mail_events; else echo "skipped on $CF_API: bounces and complaints don't reach the suppression list by themselves"; fi
117 endlog
118
119 # ------------------------------------------------------------------ Email Sending
120 # notifications@<MAIL_DOMAIN or DIFFINERY_DOMAIN>; warns (and email is skipped) until the domain is onboarded.
121-DIFFINERY_DOMAIN="$DOMAIN" "$ROOT/scripts/email-sending.sh" || warn "Email Sending check failed; email may not be delivered."
122+if ((ON_CLOUDFLARE)); then
123+ DIFFINERY_DOMAIN="$DOMAIN" "$ROOT/scripts/email-sending.sh" || warn "Email Sending check failed; email may not be delivered."
124+else
125+ echo "Email Sending setup skipped on $CF_API: the sending domain is set up wherever that API sends from."
126+fi
127
128 # ------------------------------------------------------------------ Turnstile
129 # The widget "diffinery <DIFFINERY_DOMAIN>", created when missing, so people who aren't signed in can
130@@ -177,19 +189,28 @@ DIFFINERY_DOMAIN="$DOMAIN" "$ROOT/scripts/email-sending.sh" || warn "Email Sendi
131 # Account → Turnstile → Edit; without it turnstile.sh warns, naming it, and the deploy goes on with
132 # anonymous reports refused. Sign-up stays as SIGNUP says: keys alone don't open it.
133 log "Turnstile"
134-TURNSTILE_SITE_KEY="$(DIFFINERY_DOMAIN="$DOMAIN" "$ROOT/scripts/turnstile.sh")" || { warn "scripts/turnstile.sh failed; anonymous security reports stay refused."; TURNSTILE_SITE_KEY=""; }
135+if ((ON_CLOUDFLARE)); then
136+ TURNSTILE_SITE_KEY="$(DIFFINERY_DOMAIN="$DOMAIN" "$ROOT/scripts/turnstile.sh")" || { warn "scripts/turnstile.sh failed; anonymous security reports stay refused."; TURNSTILE_SITE_KEY=""; }
137+else
138+ # No Turnstile API here: a widget made in the Cloudflare dashboard, its keys in the environment.
139+ TURNSTILE_SITE_KEY="${TURNSTILE_SITE_KEY:-}"
140+ [[ -n "$TURNSTILE_SITE_KEY" ]] || warn "No TURNSTILE_SITE_KEY: open sign-up and anonymous security reports stay off."
141+fi
142 echo "site key: ${TURNSTILE_SITE_KEY:-none}"
143 endlog
144
145 # ------------------------------------------------------------------ Access
146 log "Cloudflare Access"
147 TEAM="${ACCESS_TEAM_DOMAIN:-}"
148-if [[ -z "$TEAM" ]]; then
149+if [[ -z "$TEAM" ]] && ((ON_CLOUDFLARE)); then
150 TEAM="$(cf GET /access/organizations | jq -r '.result.auth_domain // empty' || true)"
151 fi
152-APPS="$(cf GET "/access/apps?per_page=100" || echo '{}')"
153 AUD=""
154-if ! jq -e '.success' >/dev/null <<<"$APPS"; then
155+APPS='{}'
156+((ON_CLOUDFLARE == 0)) || APPS="$(cf GET "/access/apps?per_page=100" || echo '{}')"
157+if ((ON_CLOUDFLARE == 0)); then
158+ echo "Access skipped on $CF_API: passkeys work; email recovery and the /auth/access first sign-in stay off."
159+elif ! jq -e '.success' >/dev/null <<<"$APPS"; then
160 warn "Could not read Access applications (token needs Access: Apps and Policies Edit). Passkeys work; email recovery stays off."
161 else
162 # People sign in with passkeys. Access guards only /auth/access, the email-based
163@@ -216,11 +237,11 @@ else
164 cf DELETE "/access/apps/$BYPASS_ID" | jq -e '.success' >/dev/null && echo "removed the old Access bypass app" || warn "Could not remove the old Access bypass application."
165 fi
166 fi
167-if [[ -z "$TEAM" ]]; then
168+if [[ -z "$TEAM" ]] && ((ON_CLOUDFLARE)); then
169 # Fall back to the sign-in redirect, which names the team domain.
170 TEAM="$(curl -sS -o /dev/null -w '%{redirect_url}' "https://$DOMAIN/auth/access" | sed -nE 's#^https://([^/]+\.cloudflareaccess\.com)/.*#\1#p' || true)"
171 fi
172-[[ -n "$TEAM" ]] || warn "Access team domain unknown; set the ACCESS_TEAM_DOMAIN repository variable (Zero Trust → Settings → Custom pages shows it)."
173+[[ -n "$TEAM" ]] || ((ON_CLOUDFLARE == 0)) || warn "Access team domain unknown; set the ACCESS_TEAM_DOMAIN repository variable (Zero Trust → Settings → Custom pages shows it)."
174 echo "team=${TEAM:-?} aud=${AUD:+set}"
175 endlog
176
177diff --git a/scripts/email-sending.sh b/scripts/email-sending.sh
178index 7e43dad..98dcaf3 100755
179--- a/scripts/email-sending.sh
180+++ b/scripts/email-sending.sh
181@@ -12,7 +12,7 @@ set -euo pipefail
182
183 : "${CLOUDFLARE_API_TOKEN:?set CLOUDFLARE_API_TOKEN}"
184 DOMAIN="${MAIL_DOMAIN:-${DIFFINERY_DOMAIN:-diffinery.com}}"
185-CF="https://api.cloudflare.com/client/v4"
186+CF="${CLOUDFLARE_API_BASE_URL:-https://api.cloudflare.com/client/v4}"
187
188 cf() { curl -sS "$CF$1" -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN"; }
189 warn() { echo "::warning::$*" >&2; }
190diff --git a/scripts/ensure-secrets.sh b/scripts/ensure-secrets.sh
191index 9900e79..4be2e19 100755
192--- a/scripts/ensure-secrets.sh
193+++ b/scripts/ensure-secrets.sh
194@@ -36,7 +36,11 @@ fi
195 # Without the Turnstile permission this warns (scripts/turnstile.sh names it) and the deploy goes on:
196 # people who aren't signed in can't send security reports until it's set.
197 SITE_KEY="$(tail -n +2 "$APP/wrangler.deploy.jsonc" | jq -r '.vars.TURNSTILE_SITE_KEY // empty')"
198-if [[ -n "$SITE_KEY" ]] && ! grep -qx TURNSTILE_SECRET <<<"$EXISTING"; then
199+if [[ -n "$SITE_KEY" && -n "${TURNSTILE_SECRET:-}" ]] && ! grep -qx TURNSTILE_SECRET <<<"$EXISTING"; then
200+ # A widget's secret from the environment (an API without Turnstile, such as Homeflare's).
201+ printf '%s' "$TURNSTILE_SECRET" | "${WRANGLER[@]}" secret put TURNSTILE_SECRET -c wrangler.deploy.jsonc >/dev/null
202+ echo "set TURNSTILE_SECRET"
203+elif [[ -n "$SITE_KEY" ]] && ! grep -qx TURNSTILE_SECRET <<<"$EXISTING"; then
204 if "$ROOT/scripts/turnstile.sh" secret "$SITE_KEY" -c wrangler.deploy.jsonc; then
205 echo "set TURNSTILE_SECRET"
206 else
207diff --git a/scripts/render-config.mjs b/scripts/render-config.mjs
208index fb7d756..595c9ce 100755
209--- a/scripts/render-config.mjs
210+++ b/scripts/render-config.mjs
211@@ -34,7 +34,9 @@ if (values["public-url"]) {
212 if (url.protocol !== "https:" || url.pathname !== "/") throw new Error("--public-url must be an https origin, like https://git.example.com");
213 cfg.vars.PUBLIC_URL = url.origin;
214 const zone = values.zone ?? url.hostname.split(".").slice(-2).join(".");
215- cfg.routes = [{ pattern: `${url.hostname}/*`, zone_name: zone }];
216+ // On another Cloudflare-compatible API (Homeflare), a custom domain: the zone isn't Cloudflare's.
217+ const onCloudflare = (process.env.CLOUDFLARE_API_BASE_URL ?? "https://api.cloudflare.com/").startsWith("https://api.cloudflare.com/");
218+ cfg.routes = onCloudflare ? [{ pattern: `${url.hostname}/*`, zone_name: zone }] : [{ pattern: url.hostname, custom_domain: true }];
219 cfg.vars.MAIL_FROM = `notifications@${url.hostname}`;
220 }
221 if (values["mail-from"]) cfg.vars.MAIL_FROM = values["mail-from"];
222diff --git a/scripts/runner-image.sh b/scripts/runner-image.sh
223index a121666..753b916 100755
224--- a/scripts/runner-image.sh
225+++ b/scripts/runner-image.sh
226@@ -20,7 +20,7 @@ tag=$(
227 } | sha256sum | cut -c1-24
228 )
229 : "${CLOUDFLARE_ACCOUNT_ID:?CLOUDFLARE_ACCOUNT_ID is required}"
230-image="registry.cloudflare.com/$CLOUDFLARE_ACCOUNT_ID/$name:$tag"
231+image="${CLOUDFLARE_CONTAINER_REGISTRY:-registry.cloudflare.com}/$CLOUDFLARE_ACCOUNT_ID/$name:$tag"
232 wrangler() { (cd apps/diffinery && pnpm exec wrangler "$@"); }
233
234 if ! listed=$(wrangler containers images list --json --filter "(^|/)$name\$" 2>&1); then
235diff --git a/scripts/setup.sh b/scripts/setup.sh
236index 7989e5c..1db5aea 100755
237--- a/scripts/setup.sh
238+++ b/scripts/setup.sh
239@@ -19,6 +19,12 @@
240 # GITHUB_APP_ID, GITHUB_APP_SLUG, GITHUB_APP_CLIENT_ID, GITHUB_APP_URL
241 # the GitHub App of GitHub runners (docs/self-hosting/configuration.md#github-runners)
242 # ROLLOUT_WAIT_MINUTES how long to wait for the container rollout (default 10)
243+# CLOUDFLARE_API_BASE_URL another Cloudflare-compatible API, such as Homeflare's (default Cloudflare's).
244+# Steps that need Cloudflare itself (the zone check, R2 lifecycle rules, Email
245+# Sending setup and events, Turnstile and Access) are skipped there; see
246+# HOMEFLARE.md.
247+# CLOUDFLARE_CONTAINER_REGISTRY the registry runner images go to (default registry.cloudflare.com)
248+# TURNSTILE_SITE_KEY, TURNSTILE_SECRET a Turnstile widget's keys, for an API without Turnstile
249 #
250 # Every step is idempotent: it creates only what is missing and keeps your data and secrets, so run
251 # it again to update. Secrets are never printed. Needs Node.js 22+, pnpm, jq, curl, openssl and Docker.
252@@ -26,8 +32,13 @@ set -euo pipefail
253
254 ROOT="$(cd "$(dirname "$0")/.." && pwd)"
255 APP="$ROOT/apps/diffinery"
256-CF_API="https://api.cloudflare.com/client/v4"
257+CF_API="${CLOUDFLARE_API_BASE_URL:-https://api.cloudflare.com/client/v4}"
258 PLACEHOLDER_D1="00000000-0000-0000-0000-000000000000"
259+# 1 on Cloudflare's own API; 0 on another Cloudflare-compatible one (Homeflare), which skips the
260+# steps that need Cloudflare itself.
261+ON_CLOUDFLARE=1
262+[[ "$CF_API" == https://api.cloudflare.com/* ]] || ON_CLOUDFLARE=0
263+export ON_CLOUDFLARE
264 DRY_RUN=0
265 ASK=1
266
267@@ -150,12 +161,16 @@ else
268 fi
269 jq -e '.success == true and .result.status == "active"' >/dev/null 2>&1 <<<"$verify" || die "Cloudflare doesn't accept the API token: $(errors_of "$verify")"
270 info "the token is valid and active"
271+ if ((ON_CLOUDFLARE == 0)); then
272+ info "API: $CF_API (not Cloudflare's: the zone check, R2 lifecycle rules, Email Sending setup, Turnstile and Access are skipped)"
273+ else
274 zones="$(cf_get "/zones?name=$ZONE&account.id=$CLOUDFLARE_ACCOUNT_ID" || true)"
275 jq -e '.success == true' >/dev/null 2>&1 <<<"$zones" || die "couldn't look up the zone $ZONE: $(errors_of "$zones")"
276 zone_status="$(jq -r '.result[0].status // empty' <<<"$zones")"
277 [[ -n "$zone_status" ]] || die "the zone $ZONE isn't in account $CLOUDFLARE_ACCOUNT_ID, or the token can't see it. Add the domain to the account, give the token Zone → Zone → Read on it, or set DIFFINERY_ZONE if the zone that holds $DIFFINERY_DOMAIN is another one."
278 [[ "$zone_status" == active ]] || warn "the zone $ZONE is $zone_status, not active: the route serves nothing until its nameservers point at Cloudflare"
279 info "zone $ZONE is in the account ($zone_status)"
280+ fi
281 fi
282
283 cd "$ROOT"
284@@ -173,7 +188,7 @@ if ((DRY_RUN)); then
285
286 step "Rendering the deploy config (placeholder D1 id) and bundling the Worker"
287 # A placeholder runner image: the dry run builds no container image (that needs Docker and the registry).
288- RUNNER_IMAGE="registry.cloudflare.com/${CLOUDFLARE_ACCOUNT_ID:-00000000000000000000000000000000}/diffinery-runner:dry-run" \
289+ RUNNER_IMAGE="${CLOUDFLARE_CONTAINER_REGISTRY:-registry.cloudflare.com}/${CLOUDFLARE_ACCOUNT_ID:-00000000000000000000000000000000}/diffinery-runner:dry-run" \
290 node "$ROOT/scripts/render-config.mjs" --d1 "$PLACEHOLDER_D1" ${ACCESS_TEAM_DOMAIN:+--team "$ACCESS_TEAM_DOMAIN"} \
291 --public-url "https://$DIFFINERY_DOMAIN" ${DIFFINERY_ZONE:+--zone "$DIFFINERY_ZONE"} --mail-from "$MAIL_FROM" --admins "$ADMINS"
292 OUT="$(mktemp -d "${TMPDIR:-/tmp}/diffinery-dry-run.XXXXXX")"
293@@ -205,7 +220,7 @@ image_log="$(mktemp "${TMPDIR:-/tmp}/diffinery-image.XXXXXX")"
294 env -u GITHUB_ENV "$ROOT/scripts/runner-image.sh" | tee "$image_log"
295 RUNNER_IMAGE="$(tail -n 1 "$image_log")"
296 rm -f "$image_log"
297-[[ "$RUNNER_IMAGE" == registry.cloudflare.com/* ]] || die "scripts/runner-image.sh didn't print the runner image"
298+[[ "$RUNNER_IMAGE" == ${CLOUDFLARE_CONTAINER_REGISTRY:-registry.cloudflare.com}/* ]] || die "scripts/runner-image.sh didn't print the runner image"
299 export RUNNER_IMAGE
300
301 step "Creating or updating Cloudflare resources (scripts/bootstrap.sh)"
302@@ -221,7 +236,7 @@ step "Waiting for new containers to get the new image"
303 step "Worker secrets (generated on the first deploy; never printed)"
304 "$ROOT/scripts/ensure-secrets.sh"
305
306-if [[ -z "$ACCESS_TEAM" ]]; then
307+if [[ -z "$ACCESS_TEAM" ]] && ((ON_CLOUDFLARE)); then
308 step "Finishing the Access setup (first deploy)"
309 : >"$OUTPUTS"
310 GITHUB_OUTPUT="$OUTPUTS" "$ROOT/scripts/bootstrap.sh"
311@@ -237,12 +252,21 @@ for _ in $(seq 1 30); do
312 [[ "$code" == 200 ]] && break
313 sleep 10
314 done
315-[[ "$code" == 200 ]] || die "https://$DIFFINERY_DOMAIN/healthz did not return 200 (last: ${code:-no answer}). Check that the $DIFFINERY_DOMAIN DNS record is proxied (orange cloud); a new certificate can take a few minutes, so run this again."
316+if ((ON_CLOUDFLARE)); then
317+ [[ "$code" == 200 ]] || die "https://$DIFFINERY_DOMAIN/healthz did not return 200 (last: ${code:-no answer}). Check that the $DIFFINERY_DOMAIN DNS record is proxied (orange cloud); a new certificate can take a few minutes, so run this again."
318+else
319+ [[ "$code" == 200 ]] || die "https://$DIFFINERY_DOMAIN/healthz did not return 200 (last: ${code:-no answer}). Check that $DIFFINERY_DOMAIN resolves to the host serving $CF_API and that the Worker started."
320+fi
321 info "/healthz -> 200"
322-curl -sS "https://$DIFFINERY_DOMAIN/login" | grep -q "Sign in with a passkey" || die "/login is not the Diffinery sign-in page: check DIFFINERY_DOMAIN, DIFFINERY_ZONE and the route in apps/diffinery/wrangler.deploy.jsonc"
323+# Read the whole page before matching: `curl | grep -q` fails under pipefail when grep stops
324+# early and curl can't finish writing (curl: (23) Failed writing body).
325+LOGIN_PAGE="$(curl -sS "https://$DIFFINERY_DOMAIN/login" || true)"
326+grep -q "Sign in with a passkey" <<<"$LOGIN_PAGE" || die "/login is not the Diffinery sign-in page: check DIFFINERY_DOMAIN, DIFFINERY_ZONE and the route in apps/diffinery/wrangler.deploy.jsonc"
327 info "/login serves the passkey sign-in page"
328 loc="$(curl -sS -o /dev/null -w '%{http_code} %{redirect_url}' "https://$DIFFINERY_DOMAIN/auth/access" || true)"
329-if [[ "$loc" == 302\ https://*.cloudflareaccess.com/* ]]; then
330+if ((ON_CLOUDFLARE == 0)); then
331+ info "no Cloudflare Access here: the first administrator signs up with SIGNUP=open (needs TURNSTILE_SITE_KEY and TURNSTILE_SECRET and email), see HOMEFLARE.md"
332+elif [[ "$loc" == 302\ https://*.cloudflareaccess.com/* ]]; then
333 info "/auth/access is behind Cloudflare Access"
334 else
335 warn "/auth/access is not behind Cloudflare Access yet ($loc), so email recovery and the first admin sign-in won't work. Set ACCESS_TEAM_DOMAIN and run this again."
336@@ -252,4 +276,4 @@ echo
337 echo "Diffinery is live at https://$DIFFINERY_DOMAIN."
338 echo "Sign in as the first administrator at https://$DIFFINERY_DOMAIN/auth/access with an email in BOOTSTRAP_ADMINS,"
339 echo "then add a passkey. Run scripts/setup.sh again at any time to update."
340-[[ -n "$ACCESS_TEAM" ]] || warn "the Access team domain is still unknown; set ACCESS_TEAM_DOMAIN (Zero Trust → Settings → Custom pages shows it) and run this again"
341+[[ -n "$ACCESS_TEAM" ]] || ((ON_CLOUDFLARE == 0)) || warn "the Access team domain is still unknown; set ACCESS_TEAM_DOMAIN (Zero Trust → Settings → Custom pages shows it) and run this again"
342diff --git a/scripts/turnstile.sh b/scripts/turnstile.sh
343index 22db2a1..6bf859f 100755
344--- a/scripts/turnstile.sh
345+++ b/scripts/turnstile.sh
346@@ -43,7 +43,7 @@ ROOT="$(cd "$(dirname "$0")/.." && pwd)"
347 APP="$ROOT/apps/diffinery"
348 WRANGLER=(pnpm --dir "$APP" exec wrangler)
349 WORKER=diffinery
350-API="https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID"
351+API="${CLOUDFLARE_API_BASE_URL:-https://api.cloudflare.com/client/v4}/accounts/$CLOUDFLARE_ACCOUNT_ID"
352 WIDGET_NAME="diffinery $DOMAIN"
353 PERMISSION="Account → Turnstile → Edit (\"Turnstile Sites Write\" in the API reference)"
354
355diff --git a/scripts/wait-container-rollout.sh b/scripts/wait-container-rollout.sh
356index 7a15eb6..59cae89 100755
357--- a/scripts/wait-container-rollout.sh
358+++ b/scripts/wait-container-rollout.sh
359@@ -10,7 +10,7 @@
360 # ROLLOUT_WAIT_MINUTES (default 10) rather than failing the deploy.
361 set -euo pipefail
362 : "${CLOUDFLARE_API_TOKEN:?}" "${CLOUDFLARE_ACCOUNT_ID:?}" "${RUNNER_IMAGE:?RUNNER_IMAGE is required (scripts/runner-image.sh)}"
363-api() { curl -fsS --retry 4 --retry-all-errors --retry-delay 3 "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/containers$1" -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN"; }
364+api() { curl -fsS --retry 4 --retry-all-errors --retry-delay 3 "${CLOUDFLARE_API_BASE_URL:-https://api.cloudflare.com/client/v4}/accounts/$CLOUDFLARE_ACCOUNT_ID/containers$1" -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN"; }
365 deadline=$(( $(date +%s) + ${ROLLOUT_WAIT_MINUTES:-10} * 60 ))
366 last=""
367 while :; do