I built another door for match days

I built another door for match days
ContenidoContents

In August I wrote that LaLiga had switched off my classroom. I complained and left it there. Yesterday it happened again, and this time I did something.

This is not another complaint. It is what I built: a second door that opens by itself when the Cloudflare one is bricked up, and closes when it comes back. With commands. If you would rather listen, it is in episode 30 of the podcast (in Spanish).

What happened on Saturday

On 7 November, roughly from 18:30 to 23:15 Madrid time, Orange users could not reach the Cloudflare IPs serving my domain: 188.114.96.5 and 188.114.97.5, in 188.114.96.0/20.

The Mini was fine. Apache answered, the classroom answered. It was not an outage. The entrance was blocked.

As I said in degraded is not down, telling those apart matters. Here it was everything.

Why turning off the orange cloud is not enough

The first idea is: set the records to grey and let people go straight in.

There is nowhere to go. My origin is reached through Cloudflare Tunnel (cloudflared). No port is open on the router, and I want it that way. Remove Cloudflare and nothing answers.

I needed another machine with a public IP that was not Cloudflare’s.

The machine

An e2-micro on Google Cloud in europe-west1 (Belgium), called web-failover. Debian 12, 1 GB of RAM, a 10 GB disk, a new static IP and its own network, isolated from the rest of the project.

I had another VM at hand, the test instance for the classroom’s “create a Jitsi server” button. I left it alone. It belongs to something else.

1gcloud compute instances describe web-failover \
2  --project=$PROJECT --zone=europe-west1-b \
3  --format='value(status,networkInterfaces[0].accessConfigs[0].natIP)'

The tunnel goes the other way

The VM does not get into my house. The Mini reaches out to the VM and opens a reverse SSH through Google IAP:

1gcloud compute ssh sergiocomeron@web-failover \
2  --project=$PROJECT --zone=europe-west1-b \
3  --tunnel-through-iap --ssh-key-file=~/web-failover/ssh_key -- \
4  -N -T -o ExitOnForwardFailure=yes -o ServerAliveInterval=20 \
5  -R 127.0.0.1:18080:127.0.0.1:80

On the VM, port 18080 listens on loopback only and leads to the Mini’s Apache. nginx terminates TLS with a Let’s Encrypt certificate for four names: sergiocomeron.com, aula, portal and onemore. The blog, the Jitsi docs and the LiftApp Api under /liftcoach-api/ live on the apex, so they are covered.

Client → nginx (Google Cloud) → reverse SSH over IAP → Mini's Apache (127.0.0.1:80)

A LaunchAgent (tunnel.py) keeps the tunnel up, only when the controller enables it.

How it detects the block

Another LaunchAgent runs controller.py every 60 seconds. It asks two questions:

  1. Can I reach Cloudflare? It resolves www.sergiocomeron.com and tries every Cloudflare IP it gets. Any TLS answer below 500 counts as yes.
  2. Is the origin healthy? It checks the services locally.

www always stays on Cloudflare. It is the canary: even while the other four names are grey, it tells me when Cloudflare is reachable again.

The rules:

  • Two failures in a row with a healthy origin: start the VM, enable the tunnel, renew and deploy certificates, test every route by IP, and only then change DNS.
  • Five checks in a row with Cloudflare reachable: restore the original DNS.
  • Ten more minutes for DNS caches to drain, then stop the VM. If the block comes back meanwhile, it re-activates.

If the origin is not healthy, it does nothing. Sending people to another door into the same broken house fixes nothing.

The DNS flip

On activation, the four tunnel CNAMEs become unproxied A records with TTL 60 pointing at the VM. On recovery, the exact original CNAMEs come back with the orange cloud. I keep a copy of the original DNS, and the controller does not overwrite changes it did not make.

Everything can be done by hand:

1cd ~/web-failover
2python3 controller.py status
3python3 controller.py check     # Cloudflare, origin and state
4python3 controller.py prepare   # start the VM and test, no DNS change
5python3 controller.py test      # test routes against the VM
6python3 controller.py activate  # switch DNS after verifying
7python3 controller.py recover   # back to Cloudflare, schedule shutdown

Before moving anything, I test the VM without DNS by forcing resolution:

1curl --resolve sergiocomeron.com:443:$FAILOVER_IP \
2  https://sergiocomeron.com/liftcoach-api/health

I tested it for real: a real DNS change, the way back, the automatic shutdown and a cold start. From a stopped VM, the tunnel and seven HTTPS routes were answering again in about 47 seconds.

Without Cloudflare in front, lock it down

While the fallback is in use there is no Cloudflare WAF, cache or DDoS protection. The VM is exposed as is, so I hardened it.

Firewall. On Google Cloud, only 80 and 443 are public. SSH only from the IAP range (35.235.240.0/20), plus a rule denying it from anywhere else. Inside, UFW does the same:

1ufw default deny incoming
2ufw allow from 35.235.240.0/20 to any port 22 proto tcp
3ufw allow 80/tcp
4ufw allow 443/tcp

nginx. 60 requests per second per IP, burst 240. Excess gets a 429:

1limit_req_zone $binary_remote_addr zone=web_failover_per_ip:20m rate=60r/s;
2server_tokens off;

Requests to the bare IP or with a Host that is not mine are closed with no content (444 over HTTP; over HTTPS, no valid SNI means no handshake).

fail2ban. An IP that hits the limit 40 times in 2 minutes is out for 10 minutes:

1[nginx-limit-req]
2enabled  = true
3port     = http,https
4logpath  = /var/log/nginx/error.log
5findtime = 120
6maxretry = 40
7bantime  = 600

This stops application abuse. It will not stop a volumetric attack and it does not replace the WAF.

What it costs

About 5 dollars a month before VAT, assuming 40–60 hours powered on. Most of it is the static IP, which is billed even while the VM is off. Disk and compute are cents.

What I ruled out

Ideally only Spain would be diverted, and the rest of the world would stay on Cloudflare. That is geo-DNS: with Cloudflare’s load balancer, about 5 dollars more a month.

I ruled it out. With my traffic it is not worth it, and DNS geolocation sees the resolver, not the person. Someone in Spain using 8.8.8.8 or 1.1.1.1 could still get the blocked IPs.

Limits

  • It is not instant: one or two minutes to detect, the boot, and DNS caches. Proxied records have a TTL of about 300 seconds that I cannot lower.
  • www is not covered: it is the canary.
  • It still depends on the Mini and my home connection. If the power goes out at home, no door helps.
  • I have tested the flip, not a real LaLiga block with this in place. No IP is safe from future blocks.

I should not have to do this for a classroom to work on a Saturday. But it is done.

CompartirShare