diff --git a/config/ext-mantikafasi.json b/config/ext-mantikafasi.json new file mode 100644 index 0000000000000000000000000000000000000000..9a23375d79aa710e9c4284a227b2eac0afebc6cc --- /dev/null +++ b/config/ext-mantikafasi.json @@ -0,0 +1,9 @@ +{ + "guestAddress": "10.100.0.2", + "gatewayAddress": "10.100.0.1", + "wireguardPort": 51820, + "snowglobePublicKey": "W1kcRxAkF7t/LRffd4vl9ocQgMYscaoEAHOKkBzXmGM=", + "sshPort": 2222, + "vncPort": 5900, + "guestPublicKey": "+AgCUT7xu8BXYNEnH8nR+qa7P/8cA7+69NH4DPZ0Cxw=" +} diff --git a/nixos/ext-mantikafasi.nix b/nixos/ext-mantikafasi.nix new file mode 100644 index 0000000000000000000000000000000000000000..15db0b0903862354ba81038df820b704cb2993b7 --- /dev/null +++ b/nixos/ext-mantikafasi.nix @@ -0,0 +1,51 @@ +{ ... }: +let + box = builtins.fromJSON (builtins.readFile ../config/ext-mantikafasi.json); + keys = "/var/lib/snowglobe/ext-mantikafasi"; + # The guest reaches the internet only through Snow Globe, never the networks behind it. + privateRanges = [ "10.0.0.0/8" "172.16.0.0/12" "192.168.0.0/16" "100.64.0.0/10" "169.254.0.0/16" ]; +in +{ + networking.wireguard.interfaces.wg-ext = { + ips = [ "${box.gatewayAddress}/24" ]; + listenPort = box.wireguardPort; + mtu = 1200; + privateKeyFile = "${keys}/wg.key"; + peers = [{ + publicKey = box.guestPublicKey; + presharedKeyFile = "${keys}/wg.psk"; + allowedIPs = [ "${box.guestAddress}/32" ]; + }]; + }; + + networking.firewall.interfaces.tailscale0.allowedUDPPorts = [ box.wireguardPort ]; + + networking.nat = { + enable = true; + externalInterface = "enp4s0"; + internalInterfaces = [ "wg-ext" ]; + forwardPorts = [ + { sourcePort = box.sshPort; destination = "${box.guestAddress}:22"; proto = "tcp"; } + { sourcePort = box.vncPort; destination = "${box.guestAddress}:5900"; proto = "tcp"; } + ]; + }; + + networking.firewall.extraCommands = '' + iptables -N ext-mantikafasi 2>/dev/null || iptables -F ext-mantikafasi + iptables -A ext-mantikafasi -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT + ${builtins.concatStringsSep "\n" (map (range: "iptables -A ext-mantikafasi -d ${range} -j DROP") privateRanges)} + iptables -A ext-mantikafasi -p tcp --dport 25 -j DROP + iptables -A ext-mantikafasi -j ACCEPT + iptables -I FORWARD -i wg-ext -j ext-mantikafasi + iptables -t mangle -A FORWARD -o wg-ext -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu + iptables -t mangle -A FORWARD -i wg-ext -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu + iptables -I nixos-fw 1 -i wg-ext -j DROP + ''; + networking.firewall.extraStopCommands = '' + iptables -D FORWARD -i wg-ext -j ext-mantikafasi 2>/dev/null || true + iptables -F ext-mantikafasi 2>/dev/null || true + iptables -X ext-mantikafasi 2>/dev/null || true + iptables -t mangle -D FORWARD -o wg-ext -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu 2>/dev/null || true + iptables -t mangle -D FORWARD -i wg-ext -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu 2>/dev/null || true + ''; +} diff --git a/nixos/snowglobe.nix b/nixos/snowglobe.nix index 69c2bce73b8665bd70d2c56252073d3fda306d09..81392202430f74dfbf021926a5ca10c75ae670e4 100644 --- a/nixos/snowglobe.nix +++ b/nixos/snowglobe.nix @@ -7,6 +7,7 @@ let xmppUDPPorts = [ xmppNetwork.turn xmppNetwork.turn_tls ]; irohNetwork = builtins.fromJSON (builtins.readFile ../service/iroh-relay/network.json); spacebarVoice = builtins.fromJSON (builtins.readFile ../service/spacebar/voice-network.json); + extMantikafasi = builtins.fromJSON (builtins.readFile ../config/ext-mantikafasi.json); udpPorts = xmppUDPPorts ++ [ irohNetwork.quic spacebarVoice.media spacebarVoice.nativeMedia ]; encryptionRoots = { clover = "clover"; @@ -17,7 +18,7 @@ let }; in { - imports = [ ./snowglobe-network.nix ./mac-studio-sandbox.nix ./knife-linux.nix ./mac-vm.nix ./vm-disks.nix ./zfs-read-cache.nix ./gpu-unlock.nix ]; + imports = [ ./snowglobe-network.nix ./mac-studio-sandbox.nix ./knife-linux.nix ./mac-vm.nix ./ext-mantikafasi.nix ./vm-disks.nix ./zfs-read-cache.nix ./gpu-unlock.nix ]; networking.hostName = "snowglobe"; environment.etc."machine-info".text = '' @@ -86,7 +87,7 @@ in script = '' description="$(${pkgs.python3}/bin/python3 ${./upnp-discover.py} ${lib.escapeShellArg config.networking.defaultGateway.address})" exec ${pkgs.miniupnpc}/bin/upnpc -u "$description" -e "Snow Globe services" -r \ - ${lib.concatMapStringsSep " " (port: "${toString port} TCP") xmppTCPPorts} \ + ${lib.concatMapStringsSep " " (port: "${toString port} TCP") (xmppTCPPorts ++ [ extMantikafasi.sshPort extMantikafasi.vncPort ])} \ ${lib.concatMapStringsSep " " (port: "${toString port} UDP") (udpPorts ++ lib.range xmppNetwork.relay_first xmppNetwork.relay_last)} ''; }; diff --git a/tools/ext-mantikafasi.md b/tools/ext-mantikafasi.md new file mode 100644 index 0000000000000000000000000000000000000000..8c1461296f020a7879d02c6c30cef978ba59c903 --- /dev/null +++ b/tools/ext-mantikafasi.md @@ -0,0 +1,34 @@ +# ext-mantikafasi + +`ext-mantikafasi` is a macOS 15 Tart VM (cloned from Cirrus's `macos-sequoia-base`, 8 vCPU, 16 GiB, +100 GB) for someone outside the house who has no Mac. It is reachable from the internet through Snow +Globe and cannot reach anything on the LAN. Its Tart home, `private/ext-mantikafasi`, holds the +account password (`password`), the WireGuard keys, and `run.log`. + +``` +internet ──UPnP──> Snow Globe :2222 ─DNAT─> 10.100.0.2:22 (sshd) + Snow Globe :5900 ─DNAT─> 10.100.0.2:5900 (Screen Sharing, Apple ARD login) + │ wg-ext 10.100.0.1 ▲ WireGuard over the tailnet, UDP 51820 on tailscale0 + ▼ │ + Mac NAT ── Softnet (only 100.64.0.1/32 allowed) ── guest utun 10.100.0.2 +``` + +- **Why a tunnel:** Tart's NAT on this Mac only leaves through Tailscale, so a guest otherwise has no internet. + Softnet lets the guest reach exactly Snow Globe's tailnet address; WireGuard (`ext-mantikafasi/up`, started by a + LaunchDaemon) carries all its traffic to Snow Globe, which NATs it out. Inbound SSH and VNC ride the same + tunnel, so the Mac needs no listeners and no host-to-guest route. +- **Isolation:** the guest is root on its own box but is enforced from outside it. Softnet limits what leaves + the Mac; `nixos/ext-mantikafasi.nix` drops new connections from `wg-ext` to `10.0.0.0/8`, `172.16.0.0/12`, + `192.168.0.0/16`, `100.64.0.0/10`, `169.254.0.0/16` and to port 25, and refuses all traffic to Snow Globe itself. + The guest's egress address is Snow Globe's public one. +- **Ports:** `config/ext-mantikafasi.json` owns the public ports; the `public-ports` unit maps them on the + router. VNC is unencrypted past login, so tell the user to tunnel it: + `ssh -p 2222 -L 5900:localhost:5900 admin@snowglobe.paperclover.net`, then connect to `localhost:5900`. +- **Run it:** `runtime/control/start-ext-mantikafasi` (copy of `ext-mantikafasi/start`) starts it; the + `net.paperclover.ext-mantikafasi-revive` LaunchAgent restarts it every 30 s unless + `private/ext-mantikafasi/stopped` exists (`tart stop` plus `touch` that file to stop it on purpose; `start` clears it). + Softnet cuts the Mac off from the guest, so maintain it over the public SSH port or `tart exec`. +- **Guest files:** `ext-mantikafasi/{up,net.paperclover.ext-wg.plist}` install to `/usr/local/libexec/ext-wg/` + and `/Library/LaunchDaemons`; the WireGuard config with the private key is `/etc/ext-wg.conf`. `wireguard-go` + and `wg` were built with nix for aarch64-darwin. Snow Globe's side of the keys is `/var/lib/snowglobe/ext-mantikafasi/`. +- **macOS VM limit:** Apple allows two macOS guests at once; `gui-agent` is the other, so `m3-phoenix-box` stays stopped. diff --git a/tools/ext-mantikafasi/net.paperclover.ext-mantikafasi-revive.plist b/tools/ext-mantikafasi/net.paperclover.ext-mantikafasi-revive.plist new file mode 100644 index 0000000000000000000000000000000000000000..af501eb234ca3a34b9292bd8fff4c2bcb3c8b486 --- /dev/null +++ b/tools/ext-mantikafasi/net.paperclover.ext-mantikafasi-revive.plist @@ -0,0 +1,32 @@ + + + + + Label + net.paperclover.ext-mantikafasi-revive + ProgramArguments + + /usr/bin/ssh + -F + /dev/null + -i + /Users/clo/.ssh/vm-localhost + -o + IdentitiesOnly=yes + -o + BatchMode=yes + -o + ConnectTimeout=10 + localhost + '/Volumes/Documents/VM/macOS Code Sandbox/runtime/control/start-ext-mantikafasi' revive + + RunAtLoad + + StandardErrorPath + /Users/clo/Library/Logs/ext-mantikafasi-revive.log + StandardOutPath + /Users/clo/Library/Logs/ext-mantikafasi-revive.log + StartInterval + 30 + + diff --git a/tools/ext-mantikafasi/net.paperclover.ext-wg.plist b/tools/ext-mantikafasi/net.paperclover.ext-wg.plist new file mode 100644 index 0000000000000000000000000000000000000000..65612f0e74de74a92157d67a535461035a50f3e1 --- /dev/null +++ b/tools/ext-mantikafasi/net.paperclover.ext-wg.plist @@ -0,0 +1,9 @@ + + + +Labelnet.paperclover.ext-wg +ProgramArguments/usr/local/libexec/ext-wg/up +RunAtLoad +KeepAlive +StandardErrorPath/var/log/ext-wg.log + diff --git a/tools/ext-mantikafasi/start b/tools/ext-mantikafasi/start new file mode 100755 index 0000000000000000000000000000000000000000..b3574e543ca719b5e5c41e45056facae4063efcb --- /dev/null +++ b/tools/ext-mantikafasi/start @@ -0,0 +1,15 @@ +#!/bin/bash +# Starts ext-mantikafasi headless. Softnet leaves the guest only Snow Globe's tailnet address, which carries its WireGuard tunnel. +# `revive` (the LaunchAgent) starts it only when its runner is gone and nothing stopped it on purpose. +set -euo pipefail +ROOT="/Volumes/Documents/VM/macOS Code Sandbox" +STOPPED="$ROOT/private/ext-mantikafasi/stopped" +LOG="$ROOT/private/ext-mantikafasi/run.log" +if [ "${1:-}" = revive ] && [ -e "$STOPPED" ]; then exit 0; fi +if pgrep -f "tart run ext-mantikafasi" >/dev/null; then exit 0; fi +if [ "${1:-}" = revive ]; then echo "$(date): ext-mantikafasi is down, starting"; fi +rm -f "$STOPPED" +: > "$LOG" +chmod 600 "$LOG" +# Closing an agent session kills its children, so sshd must own the VM. The loopback-only key works while the screen is locked. +ssh -F /dev/null -i "$HOME/.ssh/vm-localhost" -o IdentitiesOnly=yes -o BatchMode=yes -o ConnectTimeout=10 localhost "nohup /usr/bin/env PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin TART_HOME='$ROOT/state' /usr/bin/caffeinate -i '$ROOT/runtime/tart.app/Contents/MacOS/tart' run ext-mantikafasi --no-graphics --no-audio --no-usb-accessories --no-clipboard --suspendable --net-softnet --net-softnet-block=0.0.0.0/0 --net-softnet-allow=100.64.0.1/32 >'$LOG' 2>&1