#!/usr/bin/env bash
# Real demos for P180 "Tailscale: Full Tutorial for Beginners". Made on render01 (Docker).
# A small lab internet (198.51.100.0/24, a documentation range) with three NAT routers on it, like three home or
# office routers: the Maple Court apartment building (192.168.50.0/24: the basement server and the front-door
# camera's recorder), a cafe where John works on his laptop (10.20.0.0/24), and Jen's office (10.30.0.0/24, whose
# router picks a random outside port for every connection, the kind of NAT that stops a direct connection). On the
# lab internet: the coordination server, Headscale (the open-source server the real Tailscale app can log in to, so
# no Tailscale account is needed; it also runs the lab's relay), and a tiny website that prints the visitor's
# address. Every Tailscale command is the real Tailscale client, typed into a real interactive bash in a pty
# (demo_driver.py); every prompt, address, ping and error is real.
set -euo pipefail
cd "$(dirname "$0")"
P=ce-p180
WORK=${CE_LAB_DIR:-$HOME/ce-lab-p180}
BOXES="headscale whoami basement doorcam laptop jen-laptop"
cleanup() {
  for b in $BOXES building-router cafe-router office-router; do docker rm -f -v $P-$b >/dev/null 2>&1 || true; done
  for n in internet building cafe office; do docker network rm $P-$n >/dev/null 2>&1 || true; done
}
cleanup
[ -n "${KEEP:-}" ] || trap cleanup EXIT
mkdir -p "$WORK"
# The lab's own certificate authority, so the coordination server can use https like the real one (off camera).
if [ ! -f "$WORK/ca.crt" ]; then
  openssl req -x509 -newkey rsa:2048 -nodes -keyout "$WORK/ca.key" -out "$WORK/ca.crt" -days 60 -subj "/CN=Maple Court lab CA" 2>/dev/null
  openssl req -newkey rsa:2048 -nodes -keyout "$WORK/tls.key" -out "$WORK/tls.csr" -subj /CN=headscale.lab 2>/dev/null
  printf "subjectAltName=DNS:headscale.lab" > "$WORK/ext"
  openssl x509 -req -in "$WORK/tls.csr" -CA "$WORK/ca.crt" -CAkey "$WORK/ca.key" -CAcreateserial -out "$WORK/tls.crt" -days 60 -extfile "$WORK/ext" 2>/dev/null
fi
for r in $BOXES; do docker build -q --build-arg ROLE=$r -t $P-$r lab >/dev/null; done
docker network create --subnet 198.51.100.0/24 $P-internet >/dev/null
docker network create --internal --subnet 192.168.50.0/24 --gateway 192.168.50.254 $P-building >/dev/null
docker network create --internal --subnet 10.20.0.0/24 --gateway 10.20.0.254 $P-cafe >/dev/null
docker network create --internal --subnet 10.30.0.0/24 --gateway 10.30.0.254 $P-office >/dev/null
# Routers: forward and NAT like a home router; drop unasked-for packets from outside.
router() {   # name outside-ip lan lan-ip lan-subnet [masquerade options]
  docker run -d --name $P-$1 --hostname $1 --network $P-internet --ip $2 --cap-add NET_ADMIN \
    --sysctl net.ipv4.ip_forward=1 alpine:3.22 sleep infinity >/dev/null
  docker network connect --ip $4 $P-$3 $P-$1
  docker exec $P-$1 sh -c "apk add -q iptables conntrack-tools >/dev/null && \
    iptables -t nat -A POSTROUTING -s $5 -o eth0 -j MASQUERADE ${6:-} && \
    iptables -A INPUT -i eth0 -m conntrack --ctstate NEW -j DROP && \
    iptables -A FORWARD -i eth0 -m conntrack --ctstate NEW -j DROP"
}
router building-router 198.51.100.10 building 192.168.50.1 192.168.50.0/24
router cafe-router 198.51.100.20 cafe 10.20.0.1 10.20.0.0/24
router office-router 198.51.100.30 office 10.30.0.1 10.30.0.0/24 --random-fully
box() {   # name network ip [gateway]
  docker run -d --name $P-$1 --hostname $1 --network $P-$2 --ip $3 --privileged --cgroupns=private \
    --tmpfs /run --tmpfs /run/lock --add-host headscale.lab:198.51.100.5 --add-host whatismyip.lab:198.51.100.80 \
    -e LANG=C.UTF-8 $P-$1 >/dev/null
  if [ -n "${4:-}" ]; then docker exec $P-$1 sh -c "ip route replace default via $4 && echo 'nameserver 1.1.1.1' > /etc/resolv.conf"; fi
}
box headscale internet 198.51.100.5
box whoami internet 198.51.100.80
box basement building 192.168.50.10 192.168.50.1
box doorcam building 192.168.50.20 192.168.50.1
box laptop cafe 10.20.0.50 10.20.0.1
box jen-laptop office 10.30.0.40 10.30.0.1
docker exec $P-headscale mkdir -p /etc/headscale/tls
docker cp "$WORK/tls.crt" $P-headscale:/etc/headscale/tls/tls.crt
docker cp "$WORK/tls.key" $P-headscale:/etc/headscale/tls/tls.key
docker exec $P-headscale sh -c 'chown -R headscale:headscale /etc/headscale/tls && chmod 600 /etc/headscale/tls/tls.key'
for m in $BOXES; do
  for i in $(seq 60); do docker exec $P-$m systemctl is-system-running 2>/dev/null | grep -qE 'running|degraded' && break; sleep 1; done
  docker cp "$WORK/ca.crt" $P-$m:/usr/local/share/ca-certificates/maple-court-lab.crt
  docker exec $P-$m update-ca-certificates >/dev/null
done
docker exec $P-headscale systemctl restart headscale
for i in $(seq 30); do docker exec $P-basement curl -sf -o /dev/null https://headscale.lab/health && break; sleep 1; done
for m in basement laptop jen-laptop; do docker exec $P-$m curl -sf -o /dev/null https://headscale.lab/health; done   # trusted
[ -n "${NODRIVER:-}" ] || python3 demo_driver.py $P
