# mmalmi/nostr-vpn architecture

> Architecture diagram of the GitHub repository mmalmi/nostr-vpn, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mmalmi/nostr-vpn
- Repository: https://github.com/mmalmi/nostr-vpn

## Overview

1. **Repository shape**
   - This is a Rust workspace with one shared core library and several front ends / platform wrappers:
     - `nostr-vpn-core`: protocol, config, crypto, NAT, DNS, diagnostics, WireGuard helpers
     - `nostr-vpn-cli`: the `nvpn` command-line client and daemon
     - `nostr-vpn-gui`: Tauri + Svelte UI for desktop plus Android/iOS targets
     - `nostr-vpn-relay`: local test relay and UDP reflector
     - `nostr-vpn-wintun`: Windows tunnel integration support

2. **Primary control-plane/data-plane split**
   - The control plane is built around Nostr signaling and config/state management.
   - The data plane is userspace WireGuard, implemented through `boringtun` and platform tunnel adapters.
   - In a diagram, treat “Nostr relays / signaling” and “WireGuard tunnel traffic” as two separate flows that meet in the core session logic.

3. **Shared core as the architectural center**
   - `nostr-vpn-core` owns the reusable business logic:
     - configuration model and path handling
     - identity/crypto key generation
     - node records, presence, and roster logic
     - signaling message handling
     - NAT discovery / hole punching support
     - MagicDNS / name mapping
     - diagnostics and status reporting
     - WireGuard configuration/rendering helpers
   - Both CLI and GUI depend on this core, so it is the main boundary between UI/platform code and network/protocol logic.

4. **CLI runtime and daemon lifecycle**
   - `nostr-vpn-cli` is the operational entry point for headless use.
   - It handles init, connect/up/down, daemon start/stop/pause/resume, service installation, status, netcheck, doctor bundles, and lower-level networking commands.
   - The CLI is also the process that actually orchestrates tunnel bring-up on desktop/server systems, often as a background service on macOS, Linux, or Windows.

5. **GUI architecture**
   - `nostr-vpn-gui` is a Tauri app with a Svelte frontend and a Rust backend.
   - On desktop, the GUI shells out to the `nvpn` sidecar/CLI for service control and networking.
   - On mobile, it uses platform-specific VPN runtime code in `src-tauri` instead of relying on the desktop sidecar pattern.
   - The GUI is therefore a presentation and orchestration layer, not the owner of the core networking protocol.

6. **Platform-specific tunnel boundaries**
   - The repository has explicit platform adapters for VPN lifecycle and tunnel interfaces:
     - Windows: `wintun` support plus Windows service integration
     - Android: native service/plugin code under `src-tauri/gen/android`
     - iOS/macOS: Packet Tunnel and Apple project files under `src-tauri/gen/apple`
   - These adapters isolate OS-specific tunnel creation, permissions, and lifecycle from the shared protocol logic.

7. **External network dependencies**
   - Nostr relays are the main distributed signaling dependency; peers publish and consume private peer announcements through relays.
   - NAT traversal uses STUN/reflector-style discovery and UDP hole punching.
   - WireGuard traffic flows directly between peers once endpoints are discovered and the tunnel is established.
   - Default relays are bundled as fallbacks when a network config does not specify its own relay list.

8. **Configuration and identity model**
   - The app persists a single config file containing global settings and one or more named networks.
   - Each network has its own stable `network_id`, participant allowlist, and active/inactive state.
   - The config also stores Nostr relay URLs, keys, NAT discovery settings, tunnel IP, endpoint, and advertised routes.
   - The design supports multiple saved networks, but only one active network participates in the live runtime.

9. **Session and routing features**
   - Once connected, the system tracks peer presence, endpoints, and health, then configures the WireGuard session accordingly.
   - The core supports route advertisement and exit-node selection, so the VPN can act as a routed mesh, not just a point-to-point tunnel.
   - MagicDNS and diagnostics are part of the same operational surface, which means name resolution and health status are first-class features of the control plane.

10. **Local test infrastructure**
   - `nostr-vpn-relay` provides a minimal websocket relay for integration tests and e2e scenarios.
   - `nvpn-reflector` provides a minimal UDP reflector for NAT discovery and hole-punch testing.
   - These are not production services; they exist to validate signaling and traversal behavior in controlled environments.

11. **End-to-end validation environment**
   - The repo contains Docker-based e2e scripts that exercise relay connectivity, mesh formation, NAT traversal, exit-node routing, and GUI smoke flows.
   - This indicates the project expects real tunnel devices and container networking privileges during validation.
   - For an architecture diagram, it is useful to show an explicit “test harness / local infrastructure” boundary separate from production components.

12. **Release and distribution boundaries**
   - Releases are multi-platform and include:
     - desktop CLI archives
     - signed macOS and Windows GUI artifacts
     - Android APK/AAB artifacts
   - The GUI and CLI are built and validated in CI, but the runtime architecture remains centered on the shared core plus platform wrappers.

Main entry points: [`crates/nostr-vpn-cli/src/main.rs`](https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-cli/src/main.rs) (nvpn), [`crates/nostr-vpn-gui/src-tauri/src/lib.rs`](https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-gui/src-tauri/src/lib.rs) (Backend), [`scripts/e2e-docker.sh`](https://github.com/mmalmi/nostr-vpn/blob/master/scripts/e2e-docker.sh) (E2E harness), [`crates/nostr-vpn-core/src/signaling.rs`](https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-core/src/signaling.rs) (Signaling)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_core["Core"]
  node_core_config["Config<br/>core state<br/>[config.rs]"]
  node_core_identity["Identity<br/>crypto/keys<br/>[crypto.rs]"]
  node_core_signal["Signaling<br/>nostr control<br/>[signaling.rs]"]
  node_core_session["Session<br/>peer state<br/>[presence.rs]"]
  node_core_nat["NAT<br/>traversal<br/>[nat.rs]"]
  node_core_dns["MagicDNS<br/>name mapping<br/>[magic_dns.rs]"]
  node_core_wg["WireGuard<br/>session config<br/>[wireguard.rs]"]
  node_core_diag["Diagnostics<br/>health/status<br/>[diagnostics.rs]"]
end

subgraph group_group_cli["CLI"]
  node_cli_main["nvpn<br/>CLI daemon<br/>[main.rs]"]
  node_cli_diag["Ops tools<br/>diagnostics<br/>[diagnostics.rs]"]
  node_cli_wg["Userspace WG<br/>tunnel runtime<br/>[userspace_wg.rs]"]
end

subgraph group_group_gui["GUI"]
  node_gui_app["Tauri app<br/>desktop/mobile UI<br/>[App.svelte]"]
  node_gui_backend["Backend<br/>tauri runtime<br/>[lib.rs]"]
  node_gui_sidecar["Sidecar<br/>desktop control"]
end

subgraph group_group_platform["Platform Adapters"]
  node_cli_win["Windows tunnel<br/>platform integration<br/>[windows_tunnel.rs]"]
  node_gui_mobile["Mobile VPN<br/>platform runtime<br/>[mobile_wg.rs]"]
end

subgraph group_group_test["Test Infra"]
  node_relay_test["Test relay<br/>local relay<br/>[nvpn-udp-relay.rs]"]
  node_reflector["Reflector<br/>nat test<br/>[nvpn-reflector.rs]"]
  node_e2e_harness["E2E harness<br/>validation<br/>[e2e-docker.sh]"]
end

node_repo_root["Workspace"]

node_repo_root -->|"contains"| node_core_config
node_repo_root -->|"contains"| node_cli_main
node_repo_root -->|"contains"| node_gui_app
node_repo_root -->|"contains"| node_relay_test
node_repo_root -->|"contains"| node_reflector
node_repo_root -->|"contains"| node_e2e_harness
node_cli_main -->|"uses"| node_core_config
node_cli_main -->|"uses"| node_core_signal
node_cli_main -->|"uses"| node_core_wg
node_cli_main -->|"exposes"| node_cli_diag
node_cli_main -->|"orchestrates"| node_cli_wg
node_cli_main -->|"adapts"| node_cli_win
node_gui_app -->|"drives"| node_gui_backend
node_gui_backend -->|"uses"| node_core_config
node_gui_backend -->|"uses"| node_core_signal
node_gui_backend -->|"uses"| node_core_diag
node_gui_backend -.->|"desktop path"| node_gui_sidecar
node_gui_backend -.->|"mobile path"| node_gui_mobile
node_core_identity -->|"signs"| node_core_signal
node_core_session -->|"configures"| node_core_wg
node_core_nat -->|"tests against"| node_reflector
node_core_nat -->|"feeds endpoints"| node_core_wg
node_core_dns -->|"names peers"| node_core_session
node_core_signal -->|"talks to"| node_relay_test
node_e2e_harness -->|"spins up"| node_relay_test
node_e2e_harness -->|"spins up"| node_reflector
node_e2e_harness -->|"smokes"| node_gui_app
node_e2e_harness -->|"exercises"| node_cli_main

click node_core_config "https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-core/src/config.rs"
click node_core_identity "https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-core/src/crypto.rs"
click node_core_signal "https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-core/src/signaling.rs"
click node_core_session "https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-core/src/presence.rs"
click node_core_nat "https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-core/src/nat.rs"
click node_core_dns "https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-core/src/magic_dns.rs"
click node_core_wg "https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-core/src/wireguard.rs"
click node_core_diag "https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-core/src/diagnostics.rs"
click node_cli_main "https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-cli/src/main.rs"
click node_cli_diag "https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-cli/src/diagnostics.rs"
click node_cli_wg "https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-cli/src/userspace_wg.rs"
click node_cli_win "https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-cli/src/windows_tunnel.rs"
click node_gui_app "https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-gui/src/App.svelte"
click node_gui_backend "https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-gui/src-tauri/src/lib.rs"
click node_gui_sidecar "https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-gui/scripts/prepare-sidecar.mjs"
click node_gui_mobile "https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-gui/src-tauri/src/mobile_wg.rs"
click node_relay_test "https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-relay/src/bin/nvpn-udp-relay.rs"
click node_reflector "https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-relay/src/bin/nvpn-reflector.rs"
click node_e2e_harness "https://github.com/mmalmi/nostr-vpn/blob/master/scripts/e2e-docker.sh"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_core_config,node_core_identity,node_core_signal,node_core_session,node_core_nat,node_core_dns,node_core_wg,node_core_diag toneBlue
class node_cli_main,node_cli_diag,node_cli_wg toneAmber
class node_gui_app,node_gui_backend,node_gui_sidecar toneMint
class node_cli_win,node_gui_mobile toneRose
class node_relay_test,node_reflector,node_e2e_harness toneIndigo
class node_repo_root toneNeutral
```

## Components

### Core

Shared protocol, state, and tunnel logic used by all front ends.

- [Config](https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-core/src/config.rs): core state · Persistent network model, paths, relay settings, and active-network selection. · `crates/nostr-vpn-core/src/config.rs`
- [Identity](https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-core/src/crypto.rs): crypto/keys · Key generation and cryptographic identity used for peers and signaling. · `crates/nostr-vpn-core/src/crypto.rs`
- [Signaling](https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-core/src/signaling.rs): nostr control · Nostr relay messaging, join requests, service signaling, and roster coordination. · `crates/nostr-vpn-core/src/signaling.rs`
- [Session](https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-core/src/presence.rs): peer state · Presence, node records, roster state, and session lifecycle coordination. · `crates/nostr-vpn-core/src/presence.rs`
- [NAT](https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-core/src/nat.rs): traversal · Endpoint discovery and hole-punch support for peer connectivity. · `crates/nostr-vpn-core/src/nat.rs`
- [MagicDNS](https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-core/src/magic_dns.rs): name mapping · Mesh name resolution and peer name mapping. · `crates/nostr-vpn-core/src/magic_dns.rs`
- [WireGuard](https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-core/src/wireguard.rs): session config · Userspace WireGuard configuration and rendering helpers for tunnel bring-up. · `crates/nostr-vpn-core/src/wireguard.rs`
- [Diagnostics](https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-core/src/diagnostics.rs): health/status · Shared status, health, and diagnostic reporting surface. · `crates/nostr-vpn-core/src/diagnostics.rs`

### CLI

Headless command-line client and daemon runtime.

- [nvpn](https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-cli/src/main.rs): CLI daemon · Headless command-line entry point for connect, daemon lifecycle, service control, and networking commands. · `crates/nostr-vpn-cli/src/main.rs`
- [Ops tools](https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-cli/src/diagnostics.rs): diagnostics · Doctor bundles, netcheck, status, and other operational commands. · `crates/nostr-vpn-cli/src/diagnostics.rs`
- [Userspace WG](https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-cli/src/userspace_wg.rs): tunnel runtime · Desktop/server userspace tunnel orchestration built around boringtun. · `crates/nostr-vpn-cli/src/userspace_wg.rs`

### GUI

Tauri desktop/mobile application and platform backends.

- [Tauri app](https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-gui/src/App.svelte): desktop/mobile UI · Svelte frontend plus Rust backend for desktop and mobile orchestration. · `crates/nostr-vpn-gui/src/App.svelte`
- [Backend](https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-gui/src-tauri/src/lib.rs): tauri runtime · Rust-side orchestration layer for UI commands and platform bridging. · `crates/nostr-vpn-gui/src-tauri/src/lib.rs`
- [Sidecar](https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-gui/scripts/prepare-sidecar.mjs): desktop control · Desktop path that shells out to nvpn for service control and networking. · `crates/nostr-vpn-gui/scripts/prepare-sidecar.mjs`

### Platform Adapters

OS-specific tunnel and service integrations.

- [Windows tunnel](https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-cli/src/windows_tunnel.rs): platform integration · Windows-specific tunnel and service integration used by the CLI. · `crates/nostr-vpn-cli/src/windows_tunnel.rs`
- [Mobile VPN](https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-gui/src-tauri/src/mobile_wg.rs): platform runtime · Android and iOS VPN session code used instead of the desktop sidecar path. · `crates/nostr-vpn-gui/src-tauri/src/mobile_wg.rs`

### Test Infra

Local relay, reflector, and end-to-end validation harnesses.

- [Test relay](https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-relay/src/bin/nvpn-udp-relay.rs): local relay · Minimal websocket relay for integration and end-to-end signaling tests. · `crates/nostr-vpn-relay/src/bin/nvpn-udp-relay.rs`
- [Reflector](https://github.com/mmalmi/nostr-vpn/blob/master/crates/nostr-vpn-relay/src/bin/nvpn-reflector.rs): nat test · Minimal UDP reflector used to validate NAT discovery and traversal. · `crates/nostr-vpn-relay/src/bin/nvpn-reflector.rs`
- [E2E harness](https://github.com/mmalmi/nostr-vpn/blob/master/scripts/e2e-docker.sh): validation · Docker-based scripts and Tauri driver flows for full-stack validation. · `scripts/e2e-docker.sh`

### Other components

- **Workspace**: Rust workspace · Top-level multi-crate repository coordinating shared core and front ends.

## Connections

- Workspace → Config: contains
- Workspace → nvpn: contains
- Workspace → Tauri app: contains
- Workspace → Test relay: contains
- Workspace → Reflector: contains
- Workspace → E2E harness: contains
- nvpn → Config: uses
- nvpn → Signaling: uses
- nvpn → WireGuard: uses
- nvpn → Ops tools: exposes
- nvpn → Userspace WG: orchestrates
- nvpn → Windows tunnel: adapts
- Tauri app → Backend: drives
- Backend → Config: uses
- Backend → Signaling: uses
- Backend → Diagnostics: uses
- Backend → Sidecar: desktop path
- Backend → Mobile VPN: mobile path
- Identity → Signaling: signs
- Session → WireGuard: configures
- NAT → Reflector: tests against
- NAT → WireGuard: feeds endpoints
- MagicDNS → Session: names peers
- Signaling → Test relay: talks to
- E2E harness → Test relay: spins up
- E2E harness → Reflector: spins up
- E2E harness → Tauri app: smokes
- E2E harness → nvpn: exercises
