85 lines
3.7 KiB
Bash
Executable File
85 lines
3.7 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
#
|
|
# Disable Cloudflare compression for /videos/* so <video> stays byte-range
|
|
# serveable (Safari needs HTTP 206; a gzip/brotli stream can't be ranged, so
|
|
# Safari stalls). Idempotent: safe to run repeatedly — it only adds/updates
|
|
# our one rule and leaves any other compression rules untouched.
|
|
#
|
|
# Usage (token never lives in the repo — pass it at call time):
|
|
# CF_API_TOKEN=xxxxx CF_ZONE_ID=yyyyy ./scripts/cf-disable-video-compression.sh
|
|
#
|
|
# The token needs the "Zone > Config Rules / Rulesets: Edit" and
|
|
# "Zone > Cache Purge" permissions. Same secrets the deploy pipeline uses
|
|
# (CF_API_TOKEN / CF_ZONE_ID).
|
|
#
|
|
set -euo pipefail
|
|
|
|
: "${CF_API_TOKEN:?set CF_API_TOKEN (Cloudflare API token)}"
|
|
: "${CF_ZONE_ID:?set CF_ZONE_ID (Cloudflare zone id for hivegrid.de)}"
|
|
|
|
PATH_MATCH="${PATH_MATCH:-/videos/}"
|
|
PURGE_URL="${PURGE_URL:-https://hivegrid.de/videos/hero-test.mp4}"
|
|
DESC="hivegrid: no compression for ${PATH_MATCH} (Safari range playback)"
|
|
API="https://api.cloudflare.com/client/v4"
|
|
PHASE="http_response_compression"
|
|
|
|
auth=(-H "Authorization: Bearer ${CF_API_TOKEN}" -H "Content-Type: application/json")
|
|
|
|
say() { printf '\n\033[1m%s\033[0m\n' "$*"; }
|
|
die() { printf '\n\033[31mERROR:\033[0m %s\n' "$*" >&2; exit 1; }
|
|
|
|
# --- 1. read the current compression-phase ruleset (may not exist yet) --------
|
|
say "1/4 · Fetching current '${PHASE}' ruleset…"
|
|
resp="$(curl -s "${auth[@]}" \
|
|
"${API}/zones/${CF_ZONE_ID}/rulesets/phases/${PHASE}/entrypoint")"
|
|
|
|
if [ "$(jq -r '.success' <<<"$resp")" != "true" ]; then
|
|
# No entrypoint ruleset yet is fine — we'll create one. Any other error is fatal.
|
|
if jq -e '.errors[]? | select(.code==10007 or (.message|test("could not find";"i")))' <<<"$resp" >/dev/null 2>&1; then
|
|
existing_rules='[]'
|
|
else
|
|
die "Cloudflare API rejected the read: $(jq -c '.errors' <<<"$resp")"
|
|
fi
|
|
else
|
|
existing_rules="$(jq -c '.result.rules // []' <<<"$resp")"
|
|
fi
|
|
|
|
# --- 2. build the new rules array (drop any previous copy of ours, prepend) ---
|
|
say "2/4 · Building rules (keeping $(jq 'length' <<<"$existing_rules") existing, adding ours)…"
|
|
new_rule="$(jq -n --arg expr "starts_with(http.request.uri.path, \"${PATH_MATCH}\")" \
|
|
--arg desc "$DESC" '{
|
|
action: "compress_response",
|
|
expression: $expr,
|
|
description: $desc,
|
|
enabled: true,
|
|
action_parameters: { algorithms: [ { name: "none" } ] }
|
|
}')"
|
|
|
|
# Prepend ours; keep the others but strip CF's read-only fields (id/version/
|
|
# last_updated/ref) — Cloudflare rejects a PUT that echoes them back.
|
|
rules="$(jq -c --argjson r "$new_rule" --arg desc "$DESC" \
|
|
'[ $r ] + [ .[] | select(.description != $desc) | del(.id, .version, .last_updated, .ref) ]' \
|
|
<<<"$existing_rules")"
|
|
|
|
# --- 3. write it back ---------------------------------------------------------
|
|
say "3/4 · Updating ruleset…"
|
|
put="$(curl -s -X PUT "${auth[@]}" \
|
|
"${API}/zones/${CF_ZONE_ID}/rulesets/phases/${PHASE}/entrypoint" \
|
|
--data "$(jq -n --argjson rules "$rules" '{rules: $rules}')")"
|
|
|
|
[ "$(jq -r '.success' <<<"$put")" = "true" ] \
|
|
|| die "Ruleset update failed: $(jq -c '.errors' <<<"$put")"
|
|
echo " ✓ compression disabled for ${PATH_MATCH}"
|
|
|
|
# --- 4. purge the cached (gzipped) copy so the next hit re-fetches clean -------
|
|
say "4/4 · Purging cached copy of ${PURGE_URL}…"
|
|
purge="$(curl -s -X POST "${auth[@]}" \
|
|
"${API}/zones/${CF_ZONE_ID}/purge_cache" \
|
|
--data "$(jq -n --arg u "$PURGE_URL" '{files: [$u]}')")"
|
|
[ "$(jq -r '.success' <<<"$purge")" = "true" ] \
|
|
|| die "Cache purge failed: $(jq -c '.errors' <<<"$purge")"
|
|
echo " ✓ purged"
|
|
|
|
say "Done. Verify (expect 206, accept-ranges, NO content-encoding):"
|
|
echo " curl -sI -H 'Range: bytes=0-1023' ${PURGE_URL} | grep -iE 'HTTP|accept-ranges|content-encoding'"
|