mirror of
https://github.com/eduardogsilva/wireguard_webadmin.git
synced 2026-11-27 07:06:03 +01:00
Add API v2 endpoints to manage cluster workers and the primary WireGuard
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import glob
|
||||
import ipaddress
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
from typing import Union
|
||||
|
||||
@@ -38,6 +40,40 @@ def get_config_servers(instance: WireGuardInstance) -> list:
|
||||
return servers
|
||||
|
||||
|
||||
def running_wireguard_interfaces() -> list:
|
||||
result = subprocess.run(['wg', 'show', 'interfaces'], capture_output=True, text=True)
|
||||
return [interface for interface in result.stdout.split() if re.fullmatch(r'wg[0-9]+', interface)]
|
||||
|
||||
|
||||
def stop_primary_interfaces() -> list:
|
||||
"""Stop every WireGuard interface running on this server. Returns (interface, error) for each failure."""
|
||||
errors = []
|
||||
for interface_name in running_wireguard_interfaces():
|
||||
if os.path.isfile(os.path.join(WIREGUARD_CONFIG_DIR, f'{interface_name}.conf')):
|
||||
command = ['wg-quick', 'down', interface_name]
|
||||
else:
|
||||
# Instances moved to a worker no longer have a configuration on this server
|
||||
command = ['ip', 'link', 'delete', interface_name]
|
||||
result = subprocess.run(command, capture_output=True, text=True)
|
||||
if result.returncode != 0:
|
||||
errors.append((interface_name, result.stderr.strip()))
|
||||
return errors
|
||||
|
||||
|
||||
def start_primary_interfaces() -> list:
|
||||
"""Start the configured WireGuard interfaces that are not running. Returns (interface, error) for each failure."""
|
||||
errors = []
|
||||
running_interfaces = running_wireguard_interfaces()
|
||||
for config_path in sorted(glob.glob(os.path.join(WIREGUARD_CONFIG_DIR, 'wg[0-9]*.conf'))):
|
||||
interface_name = os.path.basename(config_path)[:-len('.conf')]
|
||||
if interface_name in running_interfaces:
|
||||
continue
|
||||
result = subprocess.run(['wg-quick', 'up', interface_name], capture_output=True, text=True)
|
||||
if result.returncode != 0:
|
||||
errors.append((interface_name, result.stderr.strip()))
|
||||
return errors
|
||||
|
||||
|
||||
def sync_interface_routes(interface_name: str) -> list:
|
||||
"""
|
||||
Bring the IPv4 routes of a running interface in line with its allowed IPs, as wg-quick does on start.
|
||||
|
||||
@@ -23,7 +23,8 @@ from vpn_invite.models import PeerInvite
|
||||
from wgwadmlibrary.tools import user_has_access_to_peer
|
||||
from wireguard.models import Peer, PeerAllowedIP, WireGuardInstance
|
||||
from wireguard_tools.functions import (
|
||||
CLUSTER_WIREGUARD_CONFIG_DIR, WIREGUARD_CONFIG_DIR, get_instance_config_dir, sync_interface_routes
|
||||
CLUSTER_WIREGUARD_CONFIG_DIR, WIREGUARD_CONFIG_DIR, get_instance_config_dir, stop_primary_interfaces,
|
||||
sync_interface_routes,
|
||||
)
|
||||
|
||||
|
||||
@@ -341,11 +342,8 @@ def restart_wireguard_interfaces(request):
|
||||
if not (user_acl.enable_reload if mode == 'reload' else user_acl.enable_restart):
|
||||
return render(request, 'access_denied.html', {'page_title': 'Access Denied'})
|
||||
# WireGuard is disabled on the primary server in mirror mode: only the cluster workers run the instances
|
||||
for interface_name in running_interfaces:
|
||||
if re.fullmatch(r'wg[0-9]+', interface_name) and os.path.isfile(os.path.join(config_dir, f'{interface_name}.conf')):
|
||||
stop_result = subprocess.run(['wg-quick', 'down', interface_name], capture_output=True, text=True)
|
||||
if stop_result.returncode != 0:
|
||||
messages.warning(request, _("Error stopping") + f" {interface_name}|{stop_result.stderr}")
|
||||
for interface_name, error in stop_primary_interfaces():
|
||||
messages.warning(request, _("Error stopping") + f" {interface_name}|{error}")
|
||||
messages.info(request, _(
|
||||
"WireGuard disabled on the primary server|The interfaces of this server are stopped. Cluster workers "
|
||||
"keep serving the peers."
|
||||
|
||||
Reference in New Issue
Block a user