mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-04 21:59:58 -05:00
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:
parent
633b125dc6
commit
2c3300b8b0
80 changed files with 7516 additions and 45 deletions
|
|
@ -32,6 +32,54 @@ NON_VIEWABLE_CONTAINERS = {"redis", "memcached", "samba", "rsyncd", "tiles"}
|
|||
# Containers that should use HTTPS
|
||||
HTTPS_CONTAINERS = {"web"}
|
||||
|
||||
# What each docker-compose service is, for the Services list (systemd services have theirs in defaults.py).
|
||||
# `via_web`: the container publishes no port of its own; the web container (Caddy) serves it over HTTPS on this
|
||||
# container port (plain HTTP if `use_https` is False), so its View link uses the host port Docker publishes for that
|
||||
# port of the web container.
|
||||
DOCKER_SERVICES = {
|
||||
"api": {"description": "Python API (Sanic)"},
|
||||
"app": {"description": "React frontend", "via_web": 443},
|
||||
"archive": {"description": "Archiving (SingleFile and Readability)", "viewable": False},
|
||||
"controller": {"description": "Controller API (FastAPI)"},
|
||||
"help": {"description": "Help documentation", "via_web": 8086},
|
||||
"rsyncd": {"description": "Library for secondary WROLPis (rsync)"},
|
||||
"samba": {"description": "Samba file sharing"},
|
||||
# Tiles for ATAK and iTAK, which ask for them; no page to view.
|
||||
"tiles": {"description": "Map tiles for ATAK and iTAK", "via_web": 8088, "use_https": False, "viewable": False},
|
||||
"vpn": {"description": "VPN and torrents (qBittorrent WebUI)", "via_web": 8091},
|
||||
"web": {"description": "HTTPS proxy (Caddy) for the app, maps, Zims, help and torrents", "via_web": 443},
|
||||
"zim": {"description": "Kiwix/Zim server", "via_web": 8085},
|
||||
}
|
||||
WEB_SERVICE = "web"
|
||||
|
||||
|
||||
def _host_port(container, container_port: int = None):
|
||||
"""The host port Docker publishes for `container_port` of `container` (any published port, if None)."""
|
||||
if container is None:
|
||||
return None
|
||||
ports = container.attrs.get("NetworkSettings", {}).get("Ports", {}) or {}
|
||||
for port, host_bindings in ports.items():
|
||||
if host_bindings and (container_port is None or port == f"{container_port}/tcp"):
|
||||
return int(host_bindings[0].get("HostPort", 0))
|
||||
return None
|
||||
|
||||
|
||||
def _describe(name: str, container, web_container) -> dict:
|
||||
"""A container's description, the port its View link uses, and how."""
|
||||
service = DOCKER_SERVICES.get(name, {})
|
||||
if "via_web" in service:
|
||||
port = _host_port(web_container, service["via_web"])
|
||||
use_https = service.get("use_https", True)
|
||||
else:
|
||||
port = _host_port(container)
|
||||
use_https = name.endswith("_https") or name in HTTPS_CONTAINERS
|
||||
return {
|
||||
"description": service.get("description", ""),
|
||||
"port": port,
|
||||
"viewable": service.get("viewable", name not in NON_VIEWABLE_CONTAINERS),
|
||||
"use_https": use_https,
|
||||
}
|
||||
|
||||
|
||||
def can_manage_containers() -> bool:
|
||||
"""Check if we can manage Docker containers."""
|
||||
|
|
@ -91,23 +139,20 @@ def get_container_status(name: str) -> dict:
|
|||
else:
|
||||
status = docker_status
|
||||
|
||||
# Get port info from container
|
||||
ports = container.attrs.get("NetworkSettings", {}).get("Ports", {})
|
||||
port = None
|
||||
for container_port, host_bindings in ports.items():
|
||||
if host_bindings:
|
||||
port = int(host_bindings[0].get("HostPort", 0))
|
||||
break
|
||||
web_container = container
|
||||
if name != WEB_SERVICE and "via_web" in DOCKER_SERVICES.get(name, {}):
|
||||
try:
|
||||
web_container = client.containers.get(_get_container_name(WEB_SERVICE))
|
||||
except NotFound:
|
||||
web_container = None
|
||||
|
||||
return {
|
||||
"name": name,
|
||||
"container_name": container_name,
|
||||
"status": status,
|
||||
"docker_status": docker_status,
|
||||
"port": port,
|
||||
"available": True,
|
||||
"viewable": name not in NON_VIEWABLE_CONTAINERS,
|
||||
"use_https": name.endswith("_https") or name in HTTPS_CONTAINERS,
|
||||
**_describe(name, container, web_container),
|
||||
}
|
||||
except NotFound:
|
||||
return {
|
||||
|
|
@ -138,6 +183,7 @@ def get_all_containers_status() -> list[dict]:
|
|||
client = _get_client()
|
||||
containers = client.containers.list(all=True)
|
||||
|
||||
web_container = next((c for c in containers if c.name == _get_container_name(WEB_SERVICE)), None)
|
||||
results = []
|
||||
for container in containers:
|
||||
if container.name.startswith(CONTAINER_PREFIX):
|
||||
|
|
@ -148,22 +194,12 @@ def get_all_containers_status() -> list[dict]:
|
|||
else:
|
||||
service_name = container.name
|
||||
|
||||
# Get port info from container
|
||||
ports = container.attrs.get("NetworkSettings", {}).get("Ports", {})
|
||||
port = None
|
||||
for container_port, host_bindings in ports.items():
|
||||
if host_bindings:
|
||||
port = int(host_bindings[0].get("HostPort", 0))
|
||||
break
|
||||
|
||||
results.append({
|
||||
"name": service_name,
|
||||
"container_name": container.name,
|
||||
"status": "running" if container.status == "running" else "stopped",
|
||||
"docker_status": container.status,
|
||||
"port": port,
|
||||
"viewable": service_name not in NON_VIEWABLE_CONTAINERS,
|
||||
"use_https": service_name.endswith("_https") or service_name in HTTPS_CONTAINERS,
|
||||
**_describe(service_name, container, web_container),
|
||||
})
|
||||
|
||||
return results
|
||||
|
|
|
|||
|
|
@ -83,7 +83,9 @@ LOCAL_PATHS = (
|
|||
# This machine's settings and state.
|
||||
"/config/wrolpi.yaml", "/config/sync.yaml", "/config/controller.yaml", "/config/fstab.yaml",
|
||||
"/config/.fstab-migrated", "/config/smb.conf", f"/{STATE_FILE}", "/config/.wrol_mode", "/config/ssl",
|
||||
"/config/cookies.txt", "/config/cookies.txt.enc", "/config/backup",
|
||||
"/config/cookies.txt", "/config/cookies.txt.enc", "/config/backup", "/config/vpn",
|
||||
# This machine's torrents.
|
||||
"/config/torrents",
|
||||
# The database arrives as a snapshot, in its own step.
|
||||
"/config/wrolpi.db", "/config/wrolpi.db-wal", "/config/wrolpi.db-shm", "/config/wrolpi.db.lock",
|
||||
f"/{SYNC_DIRECTORY}",
|
||||
|
|
@ -94,7 +96,7 @@ LOCAL_PATHS = (
|
|||
# path is never deleted, so a secondary kept the write-ahead log of a map search database whose database the
|
||||
# primary had replaced, and SQLite would apply that log to the new database. (rsync keeps its own partial
|
||||
# directory itself.)
|
||||
UNFINISHED_PATTERNS = (".partial", "*.part", "*.aria2", "*.ytdl", "*.db-wal", "*.db-shm")
|
||||
UNFINISHED_PATTERNS = (".partial", "*.part", "*.aria2", "*.ytdl", "*.!qB", "*.db-wal", "*.db-shm")
|
||||
|
||||
|
||||
class SyncError(Exception):
|
||||
|
|
|
|||
252
controller/lib/vpn.py
Normal file
252
controller/lib/vpn.py
Normal file
|
|
@ -0,0 +1,252 @@
|
|||
"""
|
||||
VPN settings and status for the Controller.
|
||||
|
||||
The tunnel itself (controller/vpn/tunnel.py) runs in the `wrolpi-vpn` service (Raspberry Pi / Debian) or the `vpn`
|
||||
container (Docker); it follows the `vpn` section of controller.yaml on its own. The Controller saves the settings and
|
||||
the user's VPN config, resolves the config's server names (the tunnel's kill switch blocks DNS until the tunnel is
|
||||
up), and reports the status the tunnel writes.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import socket
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
from controller.lib.config import get_config_value, get_media_directory, save_config, update_config
|
||||
from controller.vpn import tunnel
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Relative to the config directory.
|
||||
WIREGUARD_CONFIG = 'vpn/wireguard.conf'
|
||||
OPENVPN_CONFIG = 'vpn/openvpn.ovpn'
|
||||
OPENVPN_AUTH = 'vpn/openvpn-auth.txt'
|
||||
CONFIG_FILES = {'wireguard': WIREGUARD_CONFIG, 'openvpn': OPENVPN_CONFIG}
|
||||
MAX_CONFIG_BYTES = 256 * 1024
|
||||
|
||||
# The tunnel writes its status every 15 seconds; older than this, it is not running.
|
||||
STALE_STATUS_SECONDS = 60
|
||||
# Provider host names rarely move; look them up again this often in case they do.
|
||||
ENDPOINTS_MAX_AGE = 6 * 60 * 60
|
||||
ENDPOINTS_LOOP_SECONDS = 60
|
||||
|
||||
|
||||
def get_run_dir() -> Path:
|
||||
return tunnel.RUN_DIR
|
||||
|
||||
|
||||
def get_config_directory() -> Path:
|
||||
return get_media_directory() / 'config'
|
||||
|
||||
|
||||
def get_vpn_settings() -> dict:
|
||||
return {**tunnel.VPN_DEFAULTS, **(get_config_value('vpn') or {})}
|
||||
|
||||
|
||||
def _write_private(path: Path, text: str):
|
||||
path.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
|
||||
tmp = path.with_name(f'.{path.name}.tmp')
|
||||
tunnel.write_private(tmp, text)
|
||||
os.replace(tmp, path)
|
||||
|
||||
|
||||
def read_vpn_config() -> tuple[str, str] | None:
|
||||
"""(kind, text) of the uploaded VPN config, or None."""
|
||||
config_file = get_vpn_settings().get('config_file')
|
||||
if not config_file:
|
||||
return None
|
||||
try:
|
||||
text = (get_config_directory() / config_file).read_text()
|
||||
except FileNotFoundError:
|
||||
return None
|
||||
return tunnel.config_kind(config_file), text
|
||||
|
||||
|
||||
def _save_settings(**values):
|
||||
for key, value in values.items():
|
||||
update_config(f'vpn.{key}', value)
|
||||
save_config()
|
||||
|
||||
|
||||
def save_vpn_config(filename: str, content: str) -> dict:
|
||||
"""Validate and store an uploaded WireGuard or OpenVPN config, replacing any previous one."""
|
||||
if len(content.encode()) > MAX_CONFIG_BYTES:
|
||||
raise tunnel.VPNConfigError('That file is too large to be a VPN config')
|
||||
kind = tunnel.config_kind(filename)
|
||||
tunnel.remotes_of(kind, content) # Raises when the config cannot be used.
|
||||
|
||||
config_directory = get_config_directory()
|
||||
_write_private(config_directory / CONFIG_FILES[kind], content)
|
||||
for other_kind, other in CONFIG_FILES.items():
|
||||
if other_kind != kind:
|
||||
(config_directory / other).unlink(missing_ok=True)
|
||||
_save_settings(config_file=CONFIG_FILES[kind])
|
||||
resolve_endpoints_safely()
|
||||
return get_vpn_settings_response()
|
||||
|
||||
|
||||
def save_openvpn_auth(username: str, password: str) -> dict:
|
||||
if not username or not password:
|
||||
raise tunnel.VPNConfigError('Enter the VPN username and password')
|
||||
if any(c in s for s in (username, password) for c in '\r\n'):
|
||||
raise tunnel.VPNConfigError('The VPN username and password cannot contain line breaks')
|
||||
_write_private(get_config_directory() / OPENVPN_AUTH, f'{username}\n{password}\n')
|
||||
_save_settings(auth_file=OPENVPN_AUTH)
|
||||
return get_vpn_settings_response()
|
||||
|
||||
|
||||
def update_vpn_settings(enabled: bool = None, port_forwarding: str = None, forwarded_port: int = None,
|
||||
public_ip_url: str = None, natpmp_gateway: str = None) -> dict:
|
||||
settings = get_vpn_settings()
|
||||
values = {}
|
||||
if port_forwarding is not None:
|
||||
if port_forwarding not in tunnel.PORT_FORWARDING_MODES:
|
||||
raise tunnel.VPNConfigError(f'Port forwarding must be one of {", ".join(tunnel.PORT_FORWARDING_MODES)}')
|
||||
values['port_forwarding'] = port_forwarding
|
||||
if forwarded_port is not None:
|
||||
if not 1 <= forwarded_port <= 65535:
|
||||
raise tunnel.VPNConfigError('The forwarded port must be between 1 and 65535')
|
||||
values['forwarded_port'] = forwarded_port
|
||||
if (values.get('port_forwarding', settings['port_forwarding']) == 'fixed'
|
||||
and not values.get('forwarded_port', settings['forwarded_port'])):
|
||||
raise tunnel.VPNConfigError('Enter the port your VPN provider forwards to you')
|
||||
if natpmp_gateway is not None:
|
||||
if natpmp_gateway and not (tunnel.is_ip(natpmp_gateway) and tunnel.ip_version(natpmp_gateway) == 4):
|
||||
raise tunnel.VPNConfigError('The port forwarding gateway must be an IPv4 address')
|
||||
values['natpmp_gateway'] = natpmp_gateway or None
|
||||
if public_ip_url is not None:
|
||||
if public_ip_url:
|
||||
tunnel.check_public_ip_url(public_ip_url)
|
||||
values['public_ip_url'] = public_ip_url or None
|
||||
if enabled is not None:
|
||||
if enabled:
|
||||
loaded = read_vpn_config()
|
||||
if not loaded:
|
||||
raise tunnel.VPNConfigError('Upload a VPN config before turning the VPN on')
|
||||
kind, text = loaded
|
||||
if (kind == 'openvpn' and tunnel.parse_openvpn(text).wants_auth
|
||||
and not (get_config_directory() / OPENVPN_AUTH).is_file()):
|
||||
raise tunnel.VPNConfigError('This OpenVPN config needs a username and password')
|
||||
values['enabled'] = enabled
|
||||
_save_settings(**values)
|
||||
resolve_endpoints_safely()
|
||||
return get_vpn_settings_response()
|
||||
|
||||
|
||||
def delete_vpn_config() -> dict:
|
||||
"""Forget the VPN config and login, and turn the VPN off."""
|
||||
config_directory = get_config_directory()
|
||||
for path in (*CONFIG_FILES.values(), OPENVPN_AUTH):
|
||||
(config_directory / path).unlink(missing_ok=True)
|
||||
_save_settings(enabled=False, config_file=None, auth_file=None)
|
||||
(get_run_dir() / tunnel.ENDPOINTS_FILE).unlink(missing_ok=True)
|
||||
return get_vpn_settings_response()
|
||||
|
||||
|
||||
def get_vpn_settings_response() -> dict:
|
||||
settings = get_vpn_settings()
|
||||
loaded = read_vpn_config()
|
||||
kind, remotes, wants_auth = None, [], False
|
||||
if loaded:
|
||||
kind, text = loaded
|
||||
try:
|
||||
remotes = [r.host for r in tunnel.remotes_of(kind, text)]
|
||||
wants_auth = kind == 'openvpn' and tunnel.parse_openvpn(text).wants_auth
|
||||
except tunnel.VPNConfigError:
|
||||
pass
|
||||
return {
|
||||
'enabled': bool(settings['enabled']),
|
||||
'kind': kind,
|
||||
'servers': list(dict.fromkeys(remotes)),
|
||||
'needs_auth': wants_auth,
|
||||
'has_auth': (get_config_directory() / OPENVPN_AUTH).is_file(),
|
||||
'port_forwarding': settings['port_forwarding'],
|
||||
'forwarded_port': settings['forwarded_port'],
|
||||
'public_ip_url': settings['public_ip_url'],
|
||||
'natpmp_gateway': settings['natpmp_gateway'],
|
||||
}
|
||||
|
||||
|
||||
def resolve_endpoints() -> dict[str, list[str]]:
|
||||
"""Look up the config's server names for the tunnel, which cannot use DNS until it is up."""
|
||||
run_dir = get_run_dir()
|
||||
loaded = read_vpn_config()
|
||||
if not loaded:
|
||||
(run_dir / tunnel.ENDPOINTS_FILE).unlink(missing_ok=True)
|
||||
return {}
|
||||
kind, text = loaded
|
||||
names = dict.fromkeys(r.host for r in tunnel.remotes_of(kind, text) if not tunnel.is_ip(r.host))
|
||||
hosts = {}
|
||||
for name in names:
|
||||
try:
|
||||
hosts[name] = tunnel.resolve_host(name)
|
||||
except socket.gaierror as e:
|
||||
logger.warning('Could not look up the VPN server %s: %s', name, e)
|
||||
tunnel.write_json(run_dir / tunnel.ENDPOINTS_FILE,
|
||||
{'config_sha256': tunnel.config_sha256(text), 'hosts': hosts, 'resolved_at': time.time()})
|
||||
return hosts
|
||||
|
||||
|
||||
def resolve_endpoints_safely():
|
||||
try:
|
||||
resolve_endpoints()
|
||||
except (OSError, tunnel.VPNConfigError) as e:
|
||||
logger.warning('Could not look up the VPN servers: %s', e)
|
||||
|
||||
|
||||
def endpoints_need_resolving() -> bool:
|
||||
if not get_vpn_settings()['enabled']:
|
||||
return False
|
||||
loaded = read_vpn_config()
|
||||
if not loaded:
|
||||
return False
|
||||
_, text = loaded
|
||||
try:
|
||||
data = json.loads((get_run_dir() / tunnel.ENDPOINTS_FILE).read_text())
|
||||
except (FileNotFoundError, json.JSONDecodeError):
|
||||
return True
|
||||
names = {r.host for r in tunnel.remotes_of(*loaded) if not tunnel.is_ip(r.host)}
|
||||
return (data.get('config_sha256') != tunnel.config_sha256(text)
|
||||
or not names.issubset(data.get('hosts') or {})
|
||||
or time.time() - data.get('resolved_at', 0) > ENDPOINTS_MAX_AGE)
|
||||
|
||||
|
||||
async def vpn_endpoints_loop():
|
||||
"""Keep the tunnel's server addresses current, e.g. when the Internet was down at boot."""
|
||||
while True:
|
||||
try:
|
||||
if await asyncio.to_thread(endpoints_need_resolving):
|
||||
await asyncio.to_thread(resolve_endpoints)
|
||||
except asyncio.CancelledError:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.warning('Could not look up the VPN servers: %s', e)
|
||||
await asyncio.sleep(ENDPOINTS_LOOP_SECONDS)
|
||||
|
||||
|
||||
def get_vpn_status() -> dict:
|
||||
settings = get_vpn_settings()
|
||||
try:
|
||||
status = json.loads((get_run_dir() / tunnel.STATUS_FILE).read_text())
|
||||
except (FileNotFoundError, json.JSONDecodeError):
|
||||
status = {}
|
||||
if not status or time.time() - status.get('updated_at', 0) > STALE_STATUS_SECONDS:
|
||||
# The tunnel is not running; its kill switch (if it ever ran) still blocks torrents.
|
||||
status = {'state': 'stopped'}
|
||||
return {
|
||||
'enabled': bool(settings['enabled']),
|
||||
'state': status.get('state'),
|
||||
'kind': status.get('kind'),
|
||||
'interface': status.get('interface'),
|
||||
'addresses': status.get('addresses') or [],
|
||||
'dns': status.get('dns') or [],
|
||||
'public_ip': status.get('public_ip'),
|
||||
'port_forwarding': settings['port_forwarding'],
|
||||
'forwarded_port': status.get('forwarded_port'),
|
||||
'warnings': status.get('warnings') or [],
|
||||
'error': status.get('error'),
|
||||
'updated_at': status.get('updated_at'),
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue