System Architecture & Design Principles
Neutron is designed with a strictly decoupled architecture where all networking, security, and state logic exist in pure Rust modules that can be driven by any UI frontend (CLI, TUI, or GUI).
High-Level Architecture Diagram
┌─────────────────────────────────────────────────────────────────────────────┐
│ User Interface Layer │
│ ┌───────────────────────┐ ┌───────────────────────┐ ┌─────────────────┐ │
│ │ CLI (clap) │ │ GUI (Adwaita) │ │ TUI (ratatui) │ │
│ │ (Scripting & Headless)│ │ (GNOME Desktop Window)│ │ (Terminal UI) │ │
│ └───────────┬───────────┘ └───────────┬───────────┘ └────────┬────────┘ │
└──────────────┼──────────────────────────┼───────────────────────┼───────────┘
└──────────────────────────┼───────────────────────┘
▼
┌─────────────────────────────────────────────────────────────────────────────┐
│ Core Decoupled Engine │
│ ┌─────────────────────────┐ ┌─────────────────────────┐ ┌─────────────┐ │
│ │ NetworkManager (nm/) │ │ Firewall (firewall/) │ │ NAT-PMP │ │
│ │ • Profile Discovery │ │ • Lockdown Netfilter │ │ (portforward│ │
│ │ • Policy Kill Switch │ │ • Surgical Teardown │ │ • UDP Lease │ │
│ │ • Split Tunnel Routes │ │ • pkexec Orchestration │ │ • Auto-Renew│ │
│ └─────────────────────────┘ └─────────────────────────┘ └─────────────┘ │
│ ┌─────────────────────────┐ ┌─────────────────────────┐ │
│ │ Config (config/) │ │ Service (service/) │ │
│ │ • Atomic Persistence │ │ • Random Boot Selector │ │
│ │ • Unix Mode 0600 │ │ • Autostart Unit │ │
│ └─────────────────────────┘ └─────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────────────┘
Subsystem Responsibilities
1. nm/ — NetworkManager Control Plane
- Trait
NmClientprovides an abstract interface for listing, connecting, disconnecting, switching profiles, setting kill-switch properties, and applying split-tunneling routes. CliNmClientinteracts with NetworkManager vianmcliwith a strict 30-second execution deadline.- Submodule
nm::split_tunnelvalidates and normalizes CIDRs, resolves domain names to IP addresses, and formatsipv4.routes/ipv6.routes/never-defaultarguments. - Submodule
nm::kill_switchconfigures kernel policy routing (wireguard.ip4-auto-default-route) and negative DNS priorities (-1500).
2. firewall/ — Always-On Lockdown Netfilter Engine
- Trait
FirewallClientmanages permanent directOUTPUTchain rules infirewalld. - Uses mangle OUTPUT allow-list rules and a final DROP before filter-table established accepts; see Security & Kill Switch.
- All rules are tagged with a unique comment (
neutron-lockdown) ensuring surgical removal without modifying user-defined firewall rules. - Privilege escalation is consolidated into one
pkexecshell batch, with permanent fail-closed guards during rebuilds.
3. portforward/ — NAT-PMP Dynamic Port Leasing & App Integrations
- Implements RFC 6886 NAT-PMP client directly over
std::net::UdpSocket. - Derives the gateway address from the local tunnel IPv4 address (
10.x.x.x/100.x.x.x). - Acquires dynamic UDP/TCP port mappings and schedules automatic lease renewals before expiration.
- Integrates
portforward::qbittorrentWeb API bridge to automatically synchronize dynamic listening ports to qBittorrent (native, Flatpak, containerized).
4. config/ — Configuration & State Persistence
- Manages
AppConfigserialized as TOML in~/.config/neutron/config.toml. - Implements atomic file writes (
fs::renamewith fallback across filesystem boundaries) with strict0o600permissions. - Stores policy intent, startup eligibility, favorites, theme settings, and integration settings. Narrow updates are serialized by a sidecar file lock.
5. service/ — Boot-Time Automation
- Implements the one-shot random profile selector for login / boot.
- Manages XDG desktop autostart entries (
~/.config/autostart/io.github.pandabytez.neutron.desktop). - Prevents immediate profile repeats and respects user-defined eligibility exclusion sets.
Frontend & Resource Comparison Matrix
| Metric | GTK4 / Libadwaita (GUI)UNRELEASED | Pure Rust TUI (ratatui) | Background Daemon / CLI | Electron / Web Clients |
|---|---|---|---|---|
| Binary Size | ~15–30 MB (or AppImage bundle) | ~3–5 MB (Static musl binary) | ~3 MB | 150–250 MB |
| Active RAM (RSS) | ~70 – 110 MB | ~10 – 15 MB | ~3 – 6 MB | 250 – 450 MB |
| Idle CPU Usage | 0.1% – 0.5% | 0.0% (sleeps on epoll) | 0.0% | 0.5% – 2.0% |
| Startup Time | ~150–300 ms | < 10 ms (instantaneous) | < 2 ms | 1.5 – 3.0 seconds |
| System Dependencies | GTK4, Libadwaita, Mesa/Wayland | Zero (100% static musl) | Zero | Node, Chromium, X11/Wayland |
| Primary Environments | GNOME Desktop Workstations | Servers, SSH, Hyprland, Sway, i3 | Automation, Cron, Systemd | Legacy Cross-Platform |
| Distribution Channels | AppImage, Distro Packages | Homebrew, Cargo, AUR, Static Musl | Homebrew, System Package | Custom Installers |