# WGTools Architecture ## Components ### Web application `app/main.py` runs a FastAPI application under Uvicorn. It provides: - server-rendered HTML pages; - authenticated JSON API endpoints; - local user/session handling; - orchestration of privileged operations through the helper. ### Privileged helper `scripts/wgtools-helper` contains the privileged operating-system integration. It reads and changes WireGuard state, routes, firewall/NAT, backups and server provisioning. Keeping this code in a separate readable file makes privileged behavior easier to audit. ### Telegram service `app/telegram_bot.py` runs as a separate systemd service. It reads Telegram configuration from the same local WGTools state and uses the Telegram Bot API only when enabled. ### Built-in HTTPS The normal `wgtools.service` starts `scripts/wgtools-web`. By default it launches Uvicorn over HTTP on the configured WGTools port. When built-in HTTPS is enabled, the same launcher serves TLS directly on TCP `443` with the Let’s Encrypt `fullchain.pem` and `privkey.pem`; no reverse proxy is bundled. Certificate issuance uses the Certbot client from the WGTools virtual environment with standalone HTTP-01 validation on TCP `80`. A dedicated `wgtools-certbot-renew.timer` checks renewal automatically while built-in HTTPS is active, and its deploy hook restarts the WGTools web service after a successful renewal so Uvicorn reloads the certificate. `/etc/letsencrypt` remains host-level state and is deliberately not removed by the normal WGTools uninstaller. ### WGTools Nodes Nodes use a dedicated hidden `wgnode0` WireGuard transport and do not depend on the WGTools web listener. - The HUB listens on one numeric Nodes port over **UDP** for WireGuard and independently over **TCP** for the Node control socket. - Initial pairing uses pinned TLS on the TCP socket. A one-time join code contains a random secret plus the SHA-256 fingerprint of the HUB Node-control certificate, so the Spoke verifies the HUB identity before sending the secret. - The Spoke supplies the public HUB `host:port`; that same endpoint becomes the WireGuard endpoint. The HUB does not need to know its own public web or panel URL. - After `wgnode0` is up, sync/leave control requests are sent to the HUB transport address (normally `10.254.240.1`) through the WireGuard tunnel. The HUB accepts steady-state requests only from the transport IP assigned to that Node. - The optional panel URL is metadata only for the **Open panel** button and is never used for pairing, routing or synchronization. - Each Node automatically exports its configured interface VPN pools plus configured Site-to-Site peer remote networks. Export membership is configuration-driven rather than handshake-driven. - For traffic originating from a remote Node's ordinary VPN client pool and terminating in a locally published network, the receiving Node provides destination-scoped return-path MASQUERADE. Site-to-Site/custom LAN sources remain routed without this NAT. - The HUB control certificate/private key are mirrored into the SQLite Node state so a full `.wgtbackup` restore preserves the identity pinned by existing Spokes. ### Update job The administrator uploads a trusted `.bin` release archive from **Settings → About**. The helper validates the archive, version and required files, then schedules a one-shot systemd job outside the web-service cgroup. No upstream release server is queried. Before replacement, the updater creates a full state backup and a separate rollback archive containing the current `/opt/wgtools` application, helper, environment file and systemd units. On installation failure it attempts to restore the previous application. Packages are not cryptographically signed; their origin must be verified by the administrator. ### Storage - `/var/lib/wgtools/wgtools.db` — users, preferences, Telegram settings and WGTools-managed metadata. - `/var/lib/wgtools/session.key` — session-signing secret. - `/var/lib/wgtools/backups/` — automatic WireGuard configuration backups. - `/var/lib/wgtools/migration-backups/` — pre-restore safety backups. - `/var/lib/wgtools/update-backups/` — pre-update state backups and application rollback archives (last few copies retained). - `/var/lib/wgtools/update-status.json` — local progress/result of the latest update job. - `/etc/wireguard/*.conf` — actual WireGuard interface configurations. - `/etc/wgtools/https.env` and `/etc/wgtools/https.json` — built-in HTTPS runtime/state when configured. - `/etc/letsencrypt/live/wgtools/` — host-level Let’s Encrypt certificate/key lineage for built-in HTTPS. ## Configuration ownership WGTools distinguishes between: - **server-owned rules** — generated from the main Server settings; - **site-to-site routes** — owned by a specific peer and edited through Clients; - **WGTools-generated client policy** — for modes such as Full Tunnel except local networks; - **custom user rules** — explicitly created in the Server firewall/routes UI; - **advanced/raw configuration** — preserved for direct `wg0.conf` editing. This ownership model prevents a row from being edited in one screen while a different screen silently believes it owns the same configuration. ## Apply strategy Peer-only operations can use live WireGuard synchronization where possible. Changes involving interface addresses, routing, PostUp/PostDown or other `wg-quick` behavior can require a WireGuard restart. WGTools creates backups before risky operations and attempts rollback when apply fails. ## Trust boundary The browser is not trusted with filesystem/root access. Requests are authenticated by the web application, which then invokes a constrained helper action. The helper validates action arguments before running system changes. Because the services currently run as root, a vulnerability in the application can have high impact. Deploy WGTools only on trusted management networks and keep dependencies and WGTools updated.