| 1 | { ... }: |
| 2 | let |
| 3 | box = builtins.fromJSON (builtins.readFile ../config/ext-mantikafasi.json); |
| 4 | keys = "/var/lib/snowglobe/ext-mantikafasi"; |
| 5 | # The guest reaches the internet only through Snow Globe, never the networks behind it. |
| 6 | 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" ]; |
| 7 | in |
| 8 | { |
| 9 | networking.wireguard.interfaces.wg-ext = { |
| 10 | ips = [ "${box.gatewayAddress}/24" ]; |
| 11 | listenPort = box.wireguardPort; |
| 12 | mtu = 1200; |
| 13 | privateKeyFile = "${keys}/wg.key"; |
| 14 | peers = [{ |
| 15 | publicKey = box.guestPublicKey; |
| 16 | presharedKeyFile = "${keys}/wg.psk"; |
| 17 | allowedIPs = [ "${box.guestAddress}/32" ]; |
| 18 | }]; |
| 19 | }; |
| 20 | |
| 21 | networking.firewall.interfaces.tailscale0.allowedUDPPorts = [ box.wireguardPort ]; |
| 22 | |
| 23 | networking.nat = { |
| 24 | enable = true; |
| 25 | externalInterface = "enp4s0"; |
| 26 | internalInterfaces = [ "wg-ext" ]; |
| 27 | forwardPorts = [ |
| 28 | { sourcePort = box.sshPort; destination = "${box.guestAddress}:22"; proto = "tcp"; } |
| 29 | { sourcePort = box.vncPort; destination = "${box.guestAddress}:5900"; proto = "tcp"; } |
| 30 | ]; |
| 31 | }; |
| 32 | |
| 33 | networking.firewall.extraCommands = '' |
| 34 | iptables -N ext-mantikafasi 2>/dev/null || iptables -F ext-mantikafasi |
| 35 | iptables -A ext-mantikafasi -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT |
| 36 | ${builtins.concatStringsSep "\n" (map (range: "iptables -A ext-mantikafasi -d ${range} -j DROP") privateRanges)} |
| 37 | iptables -A ext-mantikafasi -p tcp --dport 25 -j DROP |
| 38 | iptables -A ext-mantikafasi -j ACCEPT |
| 39 | iptables -I FORWARD -i wg-ext -j ext-mantikafasi |
| 40 | iptables -t mangle -A FORWARD -o wg-ext -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu |
| 41 | iptables -t mangle -A FORWARD -i wg-ext -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu |
| 42 | iptables -I nixos-fw 1 -i wg-ext -j DROP |
| 43 | ''; |
| 44 | networking.firewall.extraStopCommands = '' |
| 45 | iptables -D FORWARD -i wg-ext -j ext-mantikafasi 2>/dev/null || true |
| 46 | iptables -F ext-mantikafasi 2>/dev/null || true |
| 47 | iptables -X ext-mantikafasi 2>/dev/null || true |
| 48 | 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 |
| 49 | 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 |
| 50 | ''; |
| 51 | } |