# VM test for qbittorrent-vpn.nix. Run: # nix build .#checks.x86_64-linux.qbittorrent-vpn -L # # vpn plays Proton: WireGuard peer at 10.2.0.1 with DNS, a fake NAT-PMP # gateway and an HTTP echo of the caller's address. client runs the module # with Qt offscreen. Keys are throwaway, test-only. { pkgs }: let clientKey = "sD38/QEAIwOEVNFlvNy7k0w/E/PJ9yA6twXF1Ef461Y="; clientPub = "apnW/TFK+Jx6LDCwSZUgPLYN9+U4b65CHdrGVqfJiEs="; serverKey = "IHQiVlBcfl2fKus+QiQ5Z6ubIFfg4lb0glszV0vIHnQ="; serverPub = "4esStqi/GKbgHQpWOwKLLtf3pc/7JvXgeTv+eJ+poE0="; mappedPort = 41234; natpmp = pkgs.writers.writePython3 "fake-natpmp" { } '' import socket import struct s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.bind(("0.0.0.0", 5351)) while True: req, addr = s.recvfrom(64) op = req[1] if op == 0: resp = struct.pack("!BBHI4s", 0, 128, 0, 1, socket.inet_aton("1.2.3.4")) else: priv = struct.unpack("!H", req[4:6])[0] resp = struct.pack("!BBHIHHI", 0, 128 + op, 0, 1, priv, ${toString mappedPort}, 60) s.sendto(resp, addr) ''; echo = pkgs.writers.writePython3 "echo-addr" { } '' from http.server import BaseHTTPRequestHandler, HTTPServer class H(BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.end_headers() self.wfile.write(self.client_address[0].encode()) HTTPServer(("0.0.0.0", 80), H).serve_forever() ''; in pkgs.testers.runNixOSTest { name = "qbittorrent-vpn"; nodes.vpn = { networking.wireguard.interfaces.wg0 = { ips = [ "10.2.0.1/24" ]; listenPort = 51820; privateKeyFile = "${pkgs.writeText "server.key" serverKey}"; peers = [{ publicKey = clientPub; allowedIPs = [ "10.2.0.2/32" ]; }]; }; networking.firewall.allowedUDPPorts = [ 51820 ]; networking.firewall.allowedTCPPorts = [ 80 ]; networking.firewall.trustedInterfaces = [ "wg0" ]; services.dnsmasq = { enable = true; resolveLocalQueries = false; settings = { interface = "wg0"; bind-dynamic = true; no-resolv = true; address = "/tracker.test/10.2.0.1"; }; }; systemd.services.fake-natpmp = { wantedBy = [ "multi-user.target" ]; serviceConfig.ExecStart = natpmp; }; systemd.services.echo-addr = { wantedBy = [ "multi-user.target" ]; serviceConfig.ExecStart = echo; }; }; nodes.client = { nodes, lib, ... }: { imports = [ ./qbittorrent-vpn.nix ]; users.users.darman = { isNormalUser = true; uid = 1000; }; environment.systemPackages = [ pkgs.curl pkgs.jq ]; environment.etc."wg/client.key" = { text = clientKey; mode = "0400"; }; homelab.qbittorrentVpn = { enable = true; downloadDir = "/mnt/data/Torrents"; privateKeyFile = "/etc/wg/client.key"; peerPublicKey = serverPub; endpoint = "${nodes.vpn.networking.primaryIPAddress}:51820"; }; systemd.services.qbittorrent-vpn.environment.QT_QPA_PLATFORM = lib.mkForce "offscreen"; # Things the sandbox must hide; the "Wayland socket" is a plain file, only # its bind is checked. systemd.tmpfiles.rules = [ "d /mnt/data 0755 darman users -" "f /mnt/data/other.txt 0644 darman users - other" "d /home/darman/.ssh 0700 darman users -" "f /home/darman/.ssh/id_ed25519 0600 darman users - key" "d /run/user/1000 0700 darman users -" "f /run/user/1000/wayland-1 0600 darman users -" "f /run/user/1000/bus 0600 darman users -" "f /run/pass-broker.sock 0666 root root -" "d /run/secrets 0755 root root -" "f /run/secrets/mine 0400 darman users - secret" ]; }; testScript = { nodes, ... }: let vpnIP = nodes.vpn.networking.primaryIPAddress; in '' import json start_all() vpn.wait_for_unit("wireguard-wg0.service") vpn.wait_for_unit("dnsmasq.service") vpn.wait_for_unit("fake-natpmp.service") vpn.wait_for_unit("echo-addr.service") client.wait_for_unit("multi-user.target") def ns(cmd): return client.succeed(f"ip netns exec proton {cmd}") def api(path): return ns(f"curl -sf http://127.0.0.1:8080/api/v2/{path}") with subtest("darman starts it via polkit, launcher queues a magnet"): client.succeed( "runuser -u darman -- qbittorrent-vpn " "'magnet:?xt=urn:btih:0123456789abcdef0123456789abcdef01234567&dn=t'" ) client.wait_for_unit("qbittorrent-vpn.service") client.wait_for_unit("proton-netns.service") client.wait_until_succeeds("ip netns exec proton curl -sf http://127.0.0.1:8080/api/v2/app/version") with subtest("sandbox: own state and downloads only"): pid = client.succeed("systemctl show -p MainPID --value qbittorrent-vpn").strip() def inside(cmd): return f"nsenter -t {pid} -m -S 1000 -G 100 sh -c '{cmd}'" client.succeed(inside("[ -e /run/qbittorrent-vpn/wayland ]")) client.succeed(inside("touch /mnt/data/Torrents/w /home/darman/.config/qBittorrent/w")) # InaccessiblePaths leaves a mode-000 node behind, so access, not # existence, is what's checked. for hidden in ["/home/darman/.ssh/id_ed25519", "/mnt/data/other.txt", "/run/user/1000/bus", "/run/pass-broker.sock", "/run/secrets/mine"]: client.fail(inside(f"cat {hidden}")) for hidden in ["/home/darman/.ssh", "/run/user/1000", "/run/secrets"]: client.fail(inside(f"ls {hidden}")) client.fail(inside("touch /home/darman/x.sh /etc/x")) client.succeed(f"! tr '\\0' '\\n' < /proc/{pid}/environ | grep -q DBUS_SESSION_BUS_ADDRESS") with subtest("magnet is picked up from the watch folder"): client.wait_until_succeeds( "ip netns exec proton curl -sf http://127.0.0.1:8080/api/v2/torrents/info" " | jq -e 'length == 1'" ) client.succeed("[ -z \"$(ls -A /home/darman/.local/share/qbittorrent-vpn/watch)\" ]") with subtest("NAT-PMP port lands in qBittorrent"): client.wait_until_succeeds( "ip netns exec proton curl -sf http://127.0.0.1:8080/api/v2/app/preferences" " | jq -e '.listen_port == ${toString mappedPort}'" ) prefs = json.loads(api("app/preferences")) assert prefs["current_network_interface"] == "wg-proton", prefs["current_network_interface"] assert prefs["save_path"].rstrip("/") == "/mnt/data/Torrents", prefs["save_path"] with subtest("traffic leaves through the tunnel only"): assert ns("curl -sf --max-time 5 http://${vpnIP}/") == "10.2.0.2" # Root namespace still goes direct. assert client.succeed("curl -sf --max-time 5 http://${vpnIP}/") != "10.2.0.2" with subtest("qBittorrent resolves via tunnel DNS, not host nscd"): pid = client.succeed("systemctl show -p MainPID --value qbittorrent-vpn").strip() out = client.succeed(f"nsenter -t {pid} -m -n getent hosts tracker.test") assert out.startswith("10.2.0.1"), out client.succeed(f"nsenter -t {pid} -m sh -c '! [ -S /run/nscd/socket ]'") client.fail("getent hosts tracker.test") with subtest("tunnel down = no connectivity, no fallback"): vpn.succeed("ip link set wg0 down") client.fail("ip netns exec proton curl -sf --max-time 5 http://${vpnIP}/") vpn.succeed("ip link set wg0 up && ip addr replace 10.2.0.1/24 dev wg0") client.wait_until_succeeds("ip netns exec proton curl -sf --max-time 5 http://${vpnIP}/") with subtest("stopping the GUI tears the namespace down"): client.succeed("runuser -u darman -- systemctl stop qbittorrent-vpn.service") client.wait_until_succeeds("! systemctl is-active proton-netns.service") client.succeed("! systemctl is-active proton-portforward.service") client.succeed("! [ -e /run/netns/proton ]") with subtest("polkit grants only this unit"): client.fail("runuser -u darman -- systemctl start proton-netns.service") ''; }