Disimula – A Party Game of Secret Missions

Disimula – Cloudflare and infrastructure

A home server with no open ports, a Cloudflare Tunnel, Access, Workers and R2 on the free plan, all declared with OpenTofu, with two environments.

The shape of it

Disimula runs on a home server with Docker Compose, and on Cloudflare for everything that must be public or always up. The split is deliberate:

HostnameWhatRuns on
disimula.applanding pageWorkers static assets
organiza.disimula.appthe organizers' apphome server, through Access and a tunnel
jugar.disimula.apponline gamesWorker + Durable Objects

Each has a twin for the development environment, which runs on the same server as a separate compose project.

System overview

The home server

The production stack is one compose project (deploy/compose.prod.yaml):

ServiceImageNotes
postgrespostgres:18-alpine, pinned by digesthealthcheck with pg_isready
backendghcr.io/…/disimula-backendlayered, non-root JRE image; JSON logs in production
frontendghcr.io/…/disimula-frontendNext.js standalone server
automationghcr.io/…/disimula-automationthe AI worker; stop_grace_period: 60s to release its job
backupbuilt on the serverdaily pg_dump, encrypted copy to R2
cloudflaredpinned by digestthe only way in
  • No ports are published on the host. The only way in is the tunnel, which cloudflared opens outwards, so the router forwards nothing.
  • Every service keeps at most 50 MB of logs (5 × 10 MB), so logs never fill the disk.
  • Dev follows the app's latest images; production starts the exact digests dev is running, so what reaches it is what was checked. Pinning or rolling back is setting PROD_BACKEND_TAG=sha-1a2b3c4 (the deploy logs the digest of every image it starts).
  • Disimula is a compose project of its own, with its own database, volumes, tunnel and env file.

Cloudflare, piece by piece

Tunnel

A remotely managed tunnel (config_src = cloudflare): cloudflared needs only its token, and the ingress (organiza.disimula.app → http://frontend:3000, anything else 404) is declared in OpenTofu. The DNS record is a proxied CNAME to <tunnel-id>.cfargotunnel.com.

Access

A self-hosted Access application for the app's hostname only, never a wildcard: the landing page and the players' hostname must stay public. Policies are either Everyone (open sign-up, the app decides who is a user) or a list of emails. A second application with a Bypass policy covers only Paddle's webhook path, because Paddle cannot sign in; a WAF rule lets only Paddle's IPs reach it, and the backend checks Paddle's signature there instead.

Workers and Durable Objects

The online Worker binds one Durable Object class and nothing else. SQLite-backed Durable Objects are the only kind on the free plan, and on the free plan Workers stop answering at the daily limit instead of charging. The players' page is served as static assets, which do not count as Worker requests: only /api/* runs code (run_worker_first). Wrangler creates the custom domains and their DNS records.

WAF

  • Rate limiting on every hostname's /api/, per IP. Requests it blocks never run the Worker, so nobody can burn the daily quota from one address by hammering it.
  • Paddle's webhook path only accepts Paddle's published IP addresses (live ones for production, sandbox ones for dev); anything else is blocked before the tunnel.

R2

Two buckets: production's encrypted backups (30-day lifecycle rule; the server never needs delete rights) and the OpenTofu state. R2 is the one service billed by usage once enabled, so it is never bound to a Worker; both buckets sit far inside the free tier.

Infrastructure as code with OpenTofu

infra/
  modules/environment/   tunnel + ingress, the app's DNS record, Access application and policies,
                         the Paddle webhook bypass, and the stack's generated secrets
  envs/dev/              the development environment
  envs/prod/             production, plus its backups' bucket and the zone's WAF rules
  • Production waits for a human. Dev's changes apply on every merge; production's wait in a protected GitHub environment until someone approves them, with the list of resources about to change in the run's summary.

  • Generated secrets. The database password, the AI worker token and the online Worker's push token are random_id resources: nobody makes them up, they live in the state and leave it as an env output for the deploy.

  • Encrypted state in R2, one key per environment, encrypted by OpenTofu itself with a passphrase kept in GitHub secrets and a password manager.

  • The S3 credentials for the state bucket are derived from the Cloudflare API token (its id, and the SHA-256 of its value), so there is no second key to manage.

  • One custom API token with exactly the permissions needed: Tunnel, Access apps and policies, R2, Workers scripts, and on the zone DNS, Workers routes and WAF.

  • Preconditions catch mistakes at plan time (an Access application with no allow policy fails the plan: "otherwise nobody can open the app").

Settings without a list to maintain

Every repository secret or variable named <ENV>_<NAME> (PROD_PADDLE_API_KEY, DEV_ADMIN_EMAIL...) goes into that environment's env file without the prefix. A script merges them with the state's outputs (tunnel token, Access AUD, generated secrets) and writes ~/<project>/.env on every deploy. Adding a setting to the compose file means adding one GitHub secret, nothing else.

The script refuses to change DB_PASSWORD or BACKUP_PASSPHRASE once the file has them (the database would not open; the backups could not be decrypted), keeps the previous file as .env.previous, and has a dry-run mode that only lists the names that would appear or disappear.

Backups and restore

  • pg_dump every day at 03:30 UTC (and at start if the last good one is over 20 h old), checked with pg_restore --list, kept 7 days locally.
  • Copied to R2 with rclone crypt: contents encrypted with BACKUP_PASSPHRASE, kept off the server too.
  • The service turns unhealthy when the last good backup is over 26 h old, which fails the next deploy's --wait and fires an alert.
  • deploy/restore.sh restores in one go: picks a backup (local or R2), asks you to type restore, stops the app, saves the current database as pre-restore-<time>.dump, replaces it in one transaction and starts the stack. Flyway migrates an older dump forward at start.

The database also holds the players' photos, so they are in the backups.

Staying free

Everything on Cloudflare is on the free plan: Tunnel, Access (up to 50 seats, shared by all environments), DNS, WAF rules, Workers and Durable Objects. The only usage-billed service, R2, holds a few KB of state and a month of nightly dumps. A billing notification on the account is the safety net. The hosting cost of a new user is zero; AI usage is paid for by credits.