catcrafts.net/deploy/catcrafts-analytics

139 lines
5.7 KiB
Text
Raw Normal View History

#!/bin/sh
# Rebuilds the analytics reports from Caddy's access logs, in two tiers:
#
# /var/www/analytics/index.html PUBLIC - censored. IPs are
# anonymized at ingest, there is no HOSTS or full-URL REFERRERS panel,
# and log lines matching $CENSOR_RE never reach its DB at all: the
# public tier cannot leak what it never ingested.
# /var/www/analytics-private/index.html PRIVATE (basic auth in Caddy) -
# uncensored: full IPs, all panels.
#
# Each tier has its own persistent DB and ingest ledger: rotated logs are
# ingested exactly once per tier; the live file is layered on at report time
# WITHOUT --persist, so its lines never double-count when Caddy rolls it.
#
# Filters live at INGEST: the DBs store aggregated, filtered data, so a
# filter change only applies to new lines. To re-filter history:
# rm -rf /var/lib/goaccess/db-* /var/lib/goaccess/ingested-*
# systemctl start catcrafts-analytics # rebuilds from retained raw logs
#
# Runs hourly as the caddy user (owner of the 0600 logs) via
# catcrafts-analytics.timer.
set -eu
LOG_DIR=/var/log/caddy
LIVE=$LOG_DIR/catcrafts.net.log
STATE_DIR=/var/lib/goaccess
OUT_PUBLIC=/var/www/analytics/index.html
OUT_PRIVATE=/var/www/analytics-private/index.html
2026-08-13 23:34:19 +02:00
# Log lines whose URI matches this never enter the public tier.
#
# /order MUST be here. An order token is not an identifier, it is the whole
# capability: /order/<token> is the buyer's status page and
# /order/<token>/invoice.md is their name, street, postal code and city. The
# public report lists requested paths (only the HOSTS and REFERRERS panels are
# suppressed, and --no-query-string does nothing for a token that lives in the
# PATH), so an uncensored public tier would have published a harvestable index
# of every buyer's address within an hour of the first sale.
#
deploy: the bank callback's controls are in the Caddy example The README says a bank callback is not signed and that three controls in Caddy "all have to stay in place": a source-IP allowlist, a path of which only the non-secret prefix is in the web server's config, and log_skip. Caddyfile.example had none of them. A host set up from it proxied the callback to the backend from anyone and wrote the secret path to the access log, which feeds the public analytics page. The example now has the block: /hooks/bunq/* answers 403 to anything outside the bank's range, is kept out of the log, and is proxied. It adapts with Caddy 2.11.7 and sorts ahead of the catch-all. Both proxy blocks carry a 64 KiB request_body cap, the same as the backend's. catcrafts-analytics drops /hooks lines from the public tier as well, so the public page does not rest on that one directive. The README no longer says the backend binds loopback, because it does not: ListenerHTTP1 takes a port and no address and binds every IPv4 interface, while the server logs 127.0.0.1 (Crafter.Network issue 11 asks for the address). The firewall on 8081 is what the per-peer limit, the callback's allowlist and the request-size bound rest on, with the two commands that check it. It also documents what the earlier commits changed: the claims file and what to do with a payment that named two orders, the rule that a reference names one order, a payment being counted once whatever the credits file holds, and the ledger being held in memory. The server's copies of the Caddyfile and of the analytics script are installed by hand and are not changed by a deploy. Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
2026-11-26 11:05:35 +01:00
# /hooks is here as the second of two layers. /hooks/bunq/<secret> is where the
# bank posts its callbacks, and that path is the endpoint's shared secret. The
# Caddyfile keeps it out of the access log altogether (log_skip on the
# prefix), so normally there is no such line to drop. But that is one
# directive in a file that is merged by hand, and the cost of it going missing
# would be the secret on a public page: with this, the public tier does not
# depend on it. The raw log and the private tier still do, so log_skip stays.
#
2026-08-13 23:34:19 +02:00
# Filters apply at INGEST, so this must be correct BEFORE the shop opens.
# Should an order URL ever reach the public DB, changing this line is not
# enough — follow the re-filter procedure at the top of this file to rebuild
# from the retained raw logs.
deploy: the bank callback's controls are in the Caddy example The README says a bank callback is not signed and that three controls in Caddy "all have to stay in place": a source-IP allowlist, a path of which only the non-secret prefix is in the web server's config, and log_skip. Caddyfile.example had none of them. A host set up from it proxied the callback to the backend from anyone and wrote the secret path to the access log, which feeds the public analytics page. The example now has the block: /hooks/bunq/* answers 403 to anything outside the bank's range, is kept out of the log, and is proxied. It adapts with Caddy 2.11.7 and sorts ahead of the catch-all. Both proxy blocks carry a 64 KiB request_body cap, the same as the backend's. catcrafts-analytics drops /hooks lines from the public tier as well, so the public page does not rest on that one directive. The README no longer says the backend binds loopback, because it does not: ListenerHTTP1 takes a port and no address and binds every IPv4 interface, while the server logs 127.0.0.1 (Crafter.Network issue 11 asks for the address). The firewall on 8081 is what the per-peer limit, the callback's allowlist and the request-size bound rest on, with the two commands that check it. It also documents what the earlier commits changed: the claims file and what to do with a payment that named two orders, the rule that a reference names one order, a payment being counted once whatever the credits file holds, and the ledger being held in memory. The server's copies of the Caddyfile and of the analytics script are installed by hand and are not changed by a deploy. Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
2026-11-26 11:05:35 +01:00
CENSOR_RE='"uri":"/(api|order|hooks)'
# Serialize runs: a manual run racing the hourly timer once ingested the same
# rotated log twice (both processes passed the ledger check before either
# appended). Skip quietly if another run holds the lock - the timer comes
# around hourly anyway.
exec 9>$STATE_DIR/.lock
flock -n 9 || exit 0
# goaccess refuses a missing --db-path; recreate after a re-filter wipe.
mkdir -p "$STATE_DIR/db-private" "$STATE_DIR/db-public"
# Own IPs to keep out of the numbers (data quality, both tiers). The IPs
# live in /etc/goaccess/exclude-ips (one per line, # comments allowed) - ON
# THE HOST ONLY, never in this public repo.
exclude_args() {
[ -r /etc/goaccess/exclude-ips ] || return 0
while IFS= read -r ip; do
case "$ip" in ''|'#'*) continue ;; esac
printf -- '--exclude-ip=%s\n' "$ip"
done < /etc/goaccess/exclude-ips
}
# Shared quality filters, both tiers:
# --ignore-crawlers drops self-declared bots (ClaudeBot alone was 18% of
# traffic); --unknowns-as-crawlers additionally drops the headerless
# vulnerability scanners (57% of traffic, no User-Agent at all); the
# browsers-file catches known fake-browser scrapers.
# --ignore-referrer drops referrer values that can never carry real source
# information: '*catcrafts.*' covers self-referrals AND the parked redirect
# TLDs (a 301 never sets the redirecting host as referrer, so those are
# always crawlers stamping their pre-redirect URL); '*localhost*' is Android
# fediverse apps whose WebView UI lives on an embedded localhost page.
# $(exclude_args) is unquoted on purpose: one word per --exclude-ip flag.
common() {
goaccess "$@" \
--log-format=CADDY \
--ignore-crawlers \
--unknowns-as-crawlers \
--browsers-file=/etc/goaccess/browsers.list \
$(exclude_args) \
--ignore-referrer='*catcrafts.*' \
--ignore-referrer='*localhost*' \
--no-query-string \
--tz=Europe/Amsterdam
}
run_private() {
common "$@" --db-path="$STATE_DIR/db-private"
}
# Public extras: --anonymize-ip zeroes the last octet before anything is
# stored; visitor IPs are personal data and stay out of the public page.
run_public() {
common "$@" --db-path="$STATE_DIR/db-public" \
--anonymize-ip \
--ignore-panel=HOSTS \
--ignore-panel=REFERRERS
}
# grep -v exits 1 when it outputs nothing - not an error here.
censor() {
grep -vE "$CENSOR_RE" || true
}
ingest() { # $1 = tier
ledger=$STATE_DIR/ingested-$1
touch "$ledger"
for f in "$LOG_DIR"/catcrafts.net-*.log.gz; do
[ -e "$f" ] || continue
grep -qxF "$f" "$ledger" && continue
case "$1" in
private) zcat "$f" | run_private - --persist --restore --process-and-exit ;;
public) zcat "$f" | censor | run_public - --persist --restore --process-and-exit ;;
esac
printf '%s\n' "$f" >>"$ledger"
done
}
ingest private
ingest public
run_private "$LIVE" --restore \
--html-report-title="catcrafts.net (private)" \
-o "$OUT_PRIVATE"
censor <"$LIVE" | run_public - --restore \
--html-report-title="catcrafts.net" \
-o "$OUT_PUBLIC"