mirror of
https://github.com/eduardogsilva/wireguard_webadmin.git
synced 2026-11-22 04:43:18 +01:00
Add distributed cluster mode and sync peer network routes on reload
This commit is contained in:
+11
-1
@@ -3,7 +3,8 @@ import re
|
||||
from django import forms
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from firewall.models import FirewallRule, FirewallSettings, RedirectRule
|
||||
from cluster.models import ClusterSettings
|
||||
from firewall.models import INSTANCE_INTERFACE_REGEX, FirewallRule, FirewallSettings, RedirectRule
|
||||
from wgwadmlibrary.tools import list_network_interfaces
|
||||
from wireguard.models import Peer, WireGuardInstance
|
||||
|
||||
@@ -143,9 +144,18 @@ class FirewallRuleForm(forms.ModelForm):
|
||||
firewall_chain = cleaned_data.get('firewall_chain')
|
||||
rule_action = cleaned_data.get('rule_action')
|
||||
in_interface = cleaned_data.get('in_interface')
|
||||
out_interface = cleaned_data.get('out_interface')
|
||||
destination_port = self.cleaned_data.get('destination_port')
|
||||
protocol = cleaned_data.get('protocol')
|
||||
|
||||
if ClusterSettings.is_distributed() and any(
|
||||
interface and re.match(INSTANCE_INTERFACE_REGEX, interface) for interface in (in_interface, out_interface)
|
||||
):
|
||||
raise forms.ValidationError(_(
|
||||
"The distributed cluster mode does not allow rules bound to a specific WireGuard instance. "
|
||||
"Use wg+ or IP addresses instead."
|
||||
))
|
||||
|
||||
if firewall_chain == 'forward' and rule_action not in ['accept', 'drop', 'reject']:
|
||||
raise forms.ValidationError("Invalid rule action for firewall chain 'forward'. Allowed actions are 'accept', 'drop', and 'reject'.")
|
||||
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
import uuid
|
||||
|
||||
from django.db import models
|
||||
from django.db.models import Q
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from wireguard.models import NETMASK_CHOICES
|
||||
from wireguard.models import Peer, WireGuardInstance
|
||||
|
||||
# Interface of a single WireGuard instance (wg0, wg1...). wg+ matches every instance.
|
||||
INSTANCE_INTERFACE_REGEX = r'^wg[0-9]+$'
|
||||
|
||||
|
||||
class RedirectRule(models.Model):
|
||||
description = models.CharField(max_length=100, blank=True, null=True)
|
||||
@@ -69,6 +73,13 @@ class FirewallRule(models.Model):
|
||||
def __str__(self):
|
||||
return str(self.uuid)
|
||||
|
||||
@classmethod
|
||||
def with_instance_interface(cls):
|
||||
"""Rules bound to the interface of a specific instance, not allowed in the distributed cluster mode."""
|
||||
return cls.objects.filter(
|
||||
Q(in_interface__regex=INSTANCE_INTERFACE_REGEX) | Q(out_interface__regex=INSTANCE_INTERFACE_REGEX)
|
||||
)
|
||||
|
||||
|
||||
class FirewallSettings(models.Model):
|
||||
name = models.CharField(max_length=6, default='global', unique=True)
|
||||
|
||||
+11
-4
@@ -3,6 +3,7 @@ from django.db.models import Q, Prefetch
|
||||
from django.utils import timezone
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from cluster.models import ClusterSettings
|
||||
from firewall.models import FirewallRule, FirewallSettings, RedirectRule
|
||||
from wireguard.models import PeerAllowedIP, WireGuardInstance, Peer
|
||||
|
||||
@@ -281,12 +282,17 @@ def generate_firewall_footer():
|
||||
return footer
|
||||
|
||||
|
||||
def generate_port_forward_firewall(server_hostname=PRIMARY_HOSTNAME):
|
||||
def generate_port_forward_firewall(server_hostname=PRIMARY_HOSTNAME, worker=None):
|
||||
firewall_settings, firewall_settings_created = FirewallSettings.objects.get_or_create(name='global')
|
||||
redirect_firewall = ''
|
||||
wan_interface = firewall_settings.wan_interface
|
||||
distributed = ClusterSettings.is_distributed()
|
||||
server_worker_id = worker.id if worker else None
|
||||
|
||||
for redirect_rule in RedirectRule.objects.all().order_by('port'):
|
||||
for redirect_rule in RedirectRule.objects.select_related('wireguard_instance').order_by('port'):
|
||||
# In the distributed cluster mode only the server that runs the instance forwards the port
|
||||
if distributed and redirect_rule.wireguard_instance and redirect_rule.wireguard_instance.worker_id != server_worker_id:
|
||||
continue
|
||||
description = f" - {redirect_rule.description} " if redirect_rule.description else ""
|
||||
rule_destination = redirect_rule.ip_address
|
||||
try:
|
||||
@@ -319,16 +325,17 @@ def generate_port_forward_firewall(server_hostname=PRIMARY_HOSTNAME):
|
||||
|
||||
return redirect_firewall
|
||||
|
||||
def generate_firewall_script(cluster_node=False):
|
||||
def generate_firewall_script(cluster_node=False, worker=None):
|
||||
"""
|
||||
Build wg-firewall.sh for the primary server or for a cluster node. On cluster nodes dnsmasq shares the
|
||||
network namespace of the node, so DNS is answered directly on the instance address without DNAT.
|
||||
The worker selects the port forwards of the instances it runs in the distributed cluster mode.
|
||||
"""
|
||||
firewall_content = generate_firewall_header(cluster_node=cluster_node)
|
||||
if not cluster_node:
|
||||
firewall_content += generate_redirect_dns_rules()
|
||||
firewall_content += generate_port_forward_firewall(
|
||||
server_hostname=CLUSTER_NODE_HOSTNAME if cluster_node else PRIMARY_HOSTNAME
|
||||
server_hostname=CLUSTER_NODE_HOSTNAME if cluster_node else PRIMARY_HOSTNAME, worker=worker
|
||||
)
|
||||
firewall_content += generate_route_policy_rules()
|
||||
firewall_content += export_user_firewall()
|
||||
|
||||
Reference in New Issue
Block a user