Add distributed cluster mode and sync peer network routes on reload

This commit is contained in:
Eduardo Silva
2026-11-20 20:42:42 -03:00
parent 5f6e2cd61c
commit 0ee6c2b864
25 changed files with 993 additions and 70 deletions
+22
View File
@@ -1,3 +1,4 @@
import ipaddress
import subprocess
from typing import Any, Dict, Optional, Tuple
@@ -7,6 +8,27 @@ from routing_templates.models import RoutingTemplate
from wireguard.models import Peer, PeerAllowedIP, WireGuardInstance
def find_network_in_other_instance(wireguard_instance, allowed_ip, netmask):
"""
Server side network of a peer of another instance that is the same as the given one. Each instance routes its
peer networks to its own interface, so the same network on two instances cannot be routed: the second interface
fails to start.
"""
try:
network = ipaddress.ip_network(f'{allowed_ip}/{netmask}', strict=False)
except ValueError:
return None
other_networks = PeerAllowedIP.objects.filter(config_file='server').exclude(
peer__wireguard_instance=wireguard_instance
).select_related('peer__wireguard_instance')
for other_network in other_networks:
try:
if ipaddress.ip_network(f'{other_network.allowed_ip}/{other_network.netmask}', strict=False) == network:
return other_network
except ValueError:
continue
return None
def generate_peer_default(wireguard_instance):
private_key = subprocess.check_output('wg genkey', shell=True).decode('utf-8').strip()
public_key = subprocess.check_output(f'echo {private_key} | wg pubkey', shell=True).decode('utf-8').strip()