Let peers connect to a remote WireGuard endpoint

This commit is contained in:
Eduardo Silva
2026-11-23 20:30:11 -03:00
parent a5ad9c4bae
commit 2fee9ebd6a
13 changed files with 337 additions and 43 deletions
+46
View File
@@ -16,6 +16,49 @@ WIREGUARD_CONFIG_DIR = '/etc/wireguard'
CLUSTER_WIREGUARD_CONFIG_DIR = '/etc/wireguard/cluster'
# Title of the configuration block of a peer with an endpoint. Mirror workers remove these blocks from the configuration
# of the primary server and receive only the endpoint peers they run.
ENDPOINT_PEER_TITLE = '# Endpoint peer: '
def peer_config_lines(peer) -> list:
"""[Peer] block of a peer, using its prefetched server allowed IPs. Ends with an empty line."""
allowed_ips = [ip for ip in peer.peerallowedip_set.all() if ip.config_file == 'server']
allowed_ips.sort(key=lambda ip: ip.priority)
if not allowed_ips:
prepend_line = "# "
peer_title = f"# WARNING: No Allowed IPs set for peer {peer}. "
allowed_ips_line = "# AllowedIPs = (missing)"
else:
prepend_line = ""
peer_title = f"{ENDPOINT_PEER_TITLE if peer.endpoint else '# Peer: '}{peer}"
allowed_ips_line = "AllowedIPs = " + ", ".join(f"{ip.allowed_ip}/{ip.netmask}" for ip in allowed_ips)
lines = [peer_title, f"{prepend_line}[Peer]", f"{prepend_line}PublicKey = {peer.public_key}"]
if peer.pre_shared_key:
lines.append(f"{prepend_line}PresharedKey = {peer.pre_shared_key}")
if peer.endpoint:
lines.append(f"{prepend_line}Endpoint = {peer.endpoint}")
lines.append(f"{prepend_line}PersistentKeepalive = {peer.persistent_keepalive}")
lines.append(allowed_ips_line)
lines.append("")
return lines
def remove_endpoint_peers(config_content: str) -> str:
"""Remove the blocks of endpoint peers from a configuration written by export_wireguard_configuration."""
lines = []
skipping = False
for line in config_content.split("\n"):
if line.startswith(ENDPOINT_PEER_TITLE):
skipping = True
if not skipping:
lines.append(line)
if skipping and line == "":
skipping = False
return "\n".join(lines)
def get_instance_config_dir(instance: WireGuardInstance) -> str:
return CLUSTER_WIREGUARD_CONFIG_DIR if instance.hosting_worker else WIREGUARD_CONFIG_DIR
@@ -159,6 +202,9 @@ def reload_wireguard_interface(target: Union[str, WireGuardInstance], config_dir
if target.hosting_worker:
# The worker applies the new configuration on its next synchronization
return True, f"wg{target.instance_id} runs on {target.hosting_worker} and is updated by the cluster"
if not ClusterSettings.primary_serves_clients():
# The interfaces of the primary server are stopped; the workers apply it on their next synchronization
return True, f"wg{target.instance_id} is updated by the cluster workers"
interface_name = f"wg{target.instance_id}"
conf_filename = f"{interface_name}.conf"
else:
+10 -25
View File
@@ -23,8 +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, stop_primary_interfaces,
sync_interface_routes,
CLUSTER_WIREGUARD_CONFIG_DIR, WIREGUARD_CONFIG_DIR, get_instance_config_dir, peer_config_lines,
stop_primary_interfaces, sync_interface_routes,
)
@@ -206,29 +206,10 @@ def export_wireguard_configuration(instance_only: WireGuardInstance = None):
)
for peer in peers:
allowed_ips = list(peer.peerallowedip_set.all())
if not allowed_ips:
prepend_line = "# "
peer_title = f"# WARNING: No Allowed IPs set for peer {peer}. "
allowed_ips_line = "# AllowedIPs = (missing)"
else:
prepend_line = ""
peer_title = f"# Peer: {peer}"
allowed_ips_line = prepend_line + "AllowedIPs = " + ", ".join(f"{ip.allowed_ip}/{ip.netmask}" for ip in allowed_ips)
peer_lines = [
peer_title,
f"{prepend_line}[Peer]",
f"{prepend_line}PublicKey = {peer.public_key}",
]
if peer.pre_shared_key:
peer_lines.append(f"{prepend_line}PresharedKey = {peer.pre_shared_key}")
peer_lines.append(f"{prepend_line}PersistentKeepalive = {peer.persistent_keepalive}")
peer_lines.append(allowed_ips_line)
config_lines.extend(peer_lines)
config_lines.append("")
# Only one server connects to the remote endpoint of a peer
if peer.endpoint and peer.endpoint_worker != instance.hosting_worker:
continue
config_lines.extend(peer_config_lines(peer))
config_content = "\n".join(config_lines)
config_path = os.path.join(config_dirs[instance.uuid], f"wg{instance.instance_id}.conf")
@@ -278,6 +259,10 @@ def download_config_or_qrcode(request):
if not user_has_access_to_peer(user_acl, peer):
raise Http404
# A peer with an endpoint is a remote server that this server connects to: it has no client configuration
if peer.endpoint:
raise Http404
format_type = request.GET.get('format', 'conf')
worker_uuid = request.GET.get('worker')
server_address = None