#!/bin/sh
# Writes the Cloudflare address ranges Apache should trust as proxies, then reloads Apache. Run weekly from cron.
set -e
OUT=/etc/apache2/cloudflare-ips.conf
TMP=$(mktemp)
for f in ips-v4 ips-v6; do curl -fsS "https://www.cloudflare.com/$f" >> "$TMP"; echo >> "$TMP"; done
grep -q '/' "$TMP" || { echo "no ranges downloaded" >&2; exit 1; }
sed '/^$/d; s/^/RemoteIPTrustedProxy /' "$TMP" > "$OUT"
rm -f "$TMP"
apachectl configtest && systemctl reload apache2
