Torrents over a fail-closed VPN (#1026)

Torrents reach the Internet only through a fail-closed VPN (WireGuard or
OpenVPN, any provider), so peers never see a WROLPi's own address.

- A network namespace (Pi/Debian) or the vpn container (Docker) with an
  nftables kill switch; NAT-PMP or fixed port forwarding.
- qBittorrent inside it, sandboxed; torrents kept in config/torrents/.
- Torrent download form, Torrents page, Share as torrent, nav warning, and
  VPN settings in both Controller UIs.
- Help docs: Torrents, and The VPN for Torrents.

QA'd on a Raspberry Pi with a real provider over WireGuard and OpenVPN.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
lrnselfreliance 2026-11-28 14:25:24 -07:00 • committed by Roland
parent 633b125dc6
commit 2c3300b8b0
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
80 changed files with 7516 additions and 45 deletions

View file

@ -570,3 +570,66 @@ class OnboardingCommitResponse(BaseModel):
default=None,
description="Populated when needs_force='shadowed'.",
)
# ============================================================================
# VPN
# ============================================================================
class VpnStatusResponse(BaseModel):
"""The tunnel which torrents use."""
enabled: bool = Field(description="Whether the VPN is turned on in controller.yaml")
state: Optional[str] = Field(
default=None,
description="stopped, disabled, wrol_mode, waiting, connecting, connected, or error. Torrents can only reach the "
"Internet while connected.")
kind: Optional[str] = Field(default=None, description="wireguard or openvpn")
interface: Optional[str] = Field(default=None, description="The tunnel's network interface")
addresses: list[str] = Field(default_factory=list, description="The tunnel's addresses")
dns: list[str] = Field(default_factory=list, description="The VPN's DNS servers")
public_ip: Optional[str] = Field(default=None, description="The address peers see")
port_forwarding: str = Field(description="none, fixed, or natpmp")
forwarded_port: Optional[int] = Field(default=None, description="The port peers can connect to")
warnings: list[str] = Field(default_factory=list, description="Problems which do not stop the tunnel")
error: Optional[str] = Field(default=None, description="Why the tunnel is not up")
updated_at: Optional[float] = Field(default=None, description="When the tunnel last reported (Unix time)")
class VpnSettingsResponse(BaseModel):
"""The VPN settings and a summary of the uploaded config."""
enabled: bool = Field(description="Whether the VPN is turned on")
kind: Optional[str] = Field(default=None, description="wireguard or openvpn; None when no config is uploaded")
servers: list[str] = Field(default_factory=list, description="The VPN servers named by the config")
needs_auth: bool = Field(description="Whether the OpenVPN config needs a username and password")
has_auth: bool = Field(description="Whether an OpenVPN username and password are saved")
port_forwarding: str = Field(description="none, fixed, or natpmp")
forwarded_port: Optional[int] = Field(default=None, description="The port the provider forwards (fixed)")
public_ip_url: Optional[str] = Field(default=None, description="Fetched through the tunnel to show its address")
natpmp_gateway: Optional[str] = Field(
default=None, description="Where NAT-PMP asks for a port; None: the VPN's gateway or first DNS server")
class VpnSettingsRequest(BaseModel):
"""Change VPN settings; omitted fields are unchanged."""
enabled: Optional[bool] = Field(default=None, description="Turn the VPN on or off")
port_forwarding: Optional[str] = Field(default=None, description="none, fixed, or natpmp")
forwarded_port: Optional[int] = Field(default=None, description="The port the provider forwards (fixed)")
public_ip_url: Optional[str] = Field(default=None, description="Empty to stop checking the public address")
natpmp_gateway: Optional[str] = Field(default=None, description="An IPv4 address; empty for the default")
class VpnConfigUploadRequest(BaseModel):
"""A VPN config file's name and contents."""
filename: str = Field(description="Ends in .conf (WireGuard) or .ovpn (OpenVPN)")
content: str = Field(description="The file's contents")
class VpnAuthRequest(BaseModel):
"""The login an OpenVPN config asks for (auth-user-pass)."""
username: str = Field(description="VPN username")
password: str = Field(description="VPN password")

63
controller/api/vpn.py Normal file
View file

@ -0,0 +1,63 @@
"""
VPN API endpoints for WROLPi Controller. Torrents only reach the Internet through this VPN.
"""
import asyncio
from fastapi import APIRouter, HTTPException
from controller.api.schemas import (
VpnAuthRequest,
VpnConfigUploadRequest,
VpnSettingsRequest,
VpnSettingsResponse,
VpnStatusResponse,
)
from controller.lib import vpn
from controller.vpn.tunnel import VPNConfigError
router = APIRouter(tags=["vpn"])
async def _call(func, *args, **kwargs):
"""Run a settings change off the event loop (saving looks up the VPN servers), as a 400 when it is refused."""
try:
return await asyncio.to_thread(func, *args, **kwargs)
except VPNConfigError as e:
raise HTTPException(status_code=400, detail=str(e))
except RuntimeError as e:
# e.g. the primary drive is not mounted, so controller.yaml cannot be saved.
raise HTTPException(status_code=409, detail=str(e))
@router.get("/api/vpn/status", response_model=VpnStatusResponse)
async def vpn_status() -> VpnStatusResponse:
"""Whether torrents can reach the Internet, and the address peers see."""
return VpnStatusResponse(**vpn.get_vpn_status())
@router.get("/api/vpn/settings", response_model=VpnSettingsResponse)
async def vpn_settings() -> VpnSettingsResponse:
return VpnSettingsResponse(**vpn.get_vpn_settings_response())
@router.post("/api/vpn/settings", response_model=VpnSettingsResponse)
async def update_vpn_settings(request: VpnSettingsRequest) -> VpnSettingsResponse:
return VpnSettingsResponse(**await _call(vpn.update_vpn_settings, **request.model_dump()))
@router.post("/api/vpn/config", response_model=VpnSettingsResponse)
async def upload_vpn_config(request: VpnConfigUploadRequest) -> VpnSettingsResponse:
"""Store a WireGuard .conf or OpenVPN .ovpn, replacing the previous config."""
return VpnSettingsResponse(**await _call(vpn.save_vpn_config, request.filename, request.content))
@router.delete("/api/vpn/config", response_model=VpnSettingsResponse)
async def delete_vpn_config() -> VpnSettingsResponse:
"""Forget the VPN config and login, and turn the VPN off."""
return VpnSettingsResponse(**await _call(vpn.delete_vpn_config))
@router.post("/api/vpn/auth", response_model=VpnSettingsResponse)
async def save_vpn_auth(request: VpnAuthRequest) -> VpnSettingsResponse:
return VpnSettingsResponse(**await _call(vpn.save_openvpn_auth, request.username, request.password))