2026-01-14 09:21:08 -03:00
from django.conf import settings
2025-08-14 22:43:18 -03:00
from django.contrib import messages
from django.contrib.auth.decorators import login_required
2026-11-20 20:42:42 -03:00
from django.db.models import ProtectedError
2025-08-14 22:43:18 -03:00
from django.shortcuts import get_object_or_404 , redirect , render
2026-11-20 11:56:30 -03:00
from django.urls import reverse
from django.utils.dateparse import parse_datetime
2025-08-14 22:43:18 -03:00
from django.utils.translation import gettext_lazy as _
2026-01-14 09:48:07 -03:00
from api.models import WireguardStatusCache
2026-11-20 11:56:30 -03:00
from app_gateway.setup_defaults import get_webadmin_hostnames
2025-08-14 22:43:18 -03:00
from user_manager.models import UserAcl
2026-11-20 20:42:42 -03:00
from wireguard.models import WireGuardInstance
from wireguard_tools.views import export_firewall_configuration , export_wireguard_configuration
2025-08-14 22:43:18 -03:00
from .forms import WorkerForm , ClusterSettingsForm
2026-11-20 11:56:30 -03:00
from .models import ClusterSettings , Worker , WorkerStatus
from .sync import SECTION_NAMES , get_worker_sync_state , get_worker_warnings
def get_worker_status ( worker ):
try :
return worker . workerstatus
except WorkerStatus . DoesNotExist :
return None
def describe_worker ( worker ):
"""Attach the synchronization state, warnings and reported data used by the cluster templates."""
worker . sync_state = get_worker_sync_state ( worker )
worker . unsynced_sections = [
section_name for section_name , section in worker . sync_state [ 'sections' ] . items () if section [ 'state' ] != 'synced'
]
worker . error_messages = [
f " { section_name } : { section [ 'message' ] } " for section_name , section in worker . sync_state [ 'sections' ] . items ()
if section [ 'state' ] == 'error' and section [ 'message' ]
]
worker . warnings = get_worker_warnings ( worker )
# Reported data of disabled, refused or offline workers is only the last known state
worker . operational = worker . enabled and not worker . error_status and worker . is_online
worker_status = get_worker_status ( worker )
worker . status = worker_status
worker . services = sorted (( worker_status . services_status or {}) . items ()) if worker_status else []
worker . runs_gateway = any ( service == 'caddy' for service , state in worker . services )
worker . outdated = bool ( worker_status and worker_status . worker_version != settings . CLUSTER_WORKER_CURRENT_VERSION )
2026-11-20 20:42:42 -03:00
# WireGuard instances that run only on this worker (distributed cluster mode)
worker . hosted_instances = (
list ( worker . wireguard_instances . order_by ( 'instance_id' )) if ClusterSettings . is_distributed () else []
)
2026-11-20 11:56:30 -03:00
return worker
2025-08-14 22:43:18 -03:00
@login_required
def cluster_main ( request ):
if not UserAcl . objects . filter ( user = request . user ) . filter ( user_level__gte = 50 ) . exists ():
return render ( request , 'access_denied.html' , { 'page_title' : _ ( 'Access Denied' )})
2026-01-14 09:48:07 -03:00
2026-11-20 11:56:30 -03:00
cluster_settings , cluster_settings_created = ClusterSettings . objects . get_or_create ( name = 'cluster_settings' )
workers = [ describe_worker ( worker ) for worker in Worker . objects . all () . select_related ( 'workerstatus' ) . order_by ( 'name' )]
enabled_workers = [ worker for worker in workers if worker . enabled ]
master_cache_times = ", " . join (
str ( cache . processing_time_ms ) for cache in WireguardStatusCache . objects . filter ( cache_type = 'master' ) . order_by ( '-created' )
)
cluster_cache_times = ", " . join (
str ( cache . processing_time_ms ) for cache in WireguardStatusCache . objects . filter ( cache_type = 'cluster' ) . order_by ( '-created' )
)
2026-01-14 09:48:07 -03:00
2025-12-31 11:52:25 -03:00
context = {
2026-11-20 11:56:30 -03:00
'page_title' : _ ( 'Cluster' ),
2026-01-14 09:48:07 -03:00
'workers' : workers ,
'cluster_settings' : cluster_settings ,
2026-11-20 11:56:30 -03:00
'current_worker_version' : settings . CLUSTER_WORKER_CURRENT_VERSION ,
2026-11-20 20:42:42 -03:00
'distributed' : ClusterSettings . is_distributed (),
'primary_instances' : WireGuardInstance . objects . filter ( worker__isnull = True ) . order_by ( 'instance_id' ),
# Instances that no server runs: their worker is disabled
'stopped_instances' : [
instance for worker in workers if not worker . enabled for instance in worker . hosted_instances
],
2026-11-20 11:56:30 -03:00
'summary' : {
'enabled' : len ( enabled_workers ),
'online' : sum ( 1 for worker in enabled_workers if worker . operational ),
'synced' : sum ( 1 for worker in enabled_workers if worker . operational and worker . sync_state [ 'state' ] == 'synced' ),
'attention' : sum (
1 for worker in enabled_workers
if worker . sync_state [ 'state' ] == 'error' or worker . error_status or worker . warnings [ 'stopped_services' ]
or worker . warnings [ 'certificate_issues' ] or not worker . is_online
),
},
2026-01-14 09:48:07 -03:00
'cache_refresh_interval' : settings . WIREGUARD_STATUS_CACHE_REFRESH_INTERVAL ,
'cache_enabled' : settings . WIREGUARD_STATUS_CACHE_ENABLED ,
'cache_web_load_previous_count' : settings . WIREGUARD_STATUS_CACHE_WEB_LOAD_PREVIOUS_COUNT ,
'master_cache_times' : master_cache_times ,
'cluster_cache_times' : cluster_cache_times ,
2025-12-31 11:52:25 -03:00
}
2025-08-14 22:43:18 -03:00
return render ( request , 'cluster/workers_list.html' , context )
@login_required
2026-11-20 11:56:30 -03:00
def worker_details ( request ):
2025-08-14 22:43:18 -03:00
if not UserAcl . objects . filter ( user = request . user ) . filter ( user_level__gte = 50 ) . exists ():
return render ( request , 'access_denied.html' , { 'page_title' : _ ( 'Access Denied' )})
2026-11-20 11:56:30 -03:00
worker = describe_worker ( get_object_or_404 ( Worker . objects . select_related ( 'workerstatus' ), uuid = request . GET . get ( 'uuid' )))
cluster_settings , cluster_settings_created = ClusterSettings . objects . get_or_create ( name = 'cluster_settings' )
sections = []
for section_name in SECTION_NAMES :
section_state = worker . sync_state [ 'sections' ] . get ( section_name , {})
reported = ( worker . status . section_status or {}) . get ( section_name , {}) if worker . status else {}
sections . append ({
'name' : section_name ,
'state' : section_state . get ( 'state' , 'pending' ),
'message' : section_state . get ( 'message' , '' ),
'updated' : parse_datetime ( reported . get ( 'updated' , '' )) if reported . get ( 'updated' ) else None ,
})
certificates = []
if worker . status :
for hostname , certificate in sorted (( worker . status . certificate_status or {}) . items ()):
certificates . append ({
'hostname' : hostname ,
'status' : certificate . get ( 'status' , '' ),
'issuer' : certificate . get ( 'issuer' , '' ),
'not_after' : parse_datetime ( certificate [ 'not_after' ]) if certificate . get ( 'not_after' ) else None ,
})
webadmin_hostnames = get_webadmin_hostnames ()
primary_address = f "https:// { webadmin_hostnames [ 0 ] if webadmin_hostnames else request . get_host () } "
context = {
'page_title' : worker . name ,
'worker' : worker ,
'cluster_settings' : cluster_settings ,
'current_worker_version' : settings . CLUSTER_WORKER_CURRENT_VERSION ,
2026-11-20 20:42:42 -03:00
'distributed' : ClusterSettings . is_distributed (),
2026-11-20 11:56:30 -03:00
'sections' : sections ,
'certificates' : certificates ,
'primary_address' : primary_address ,
'time_zone' : settings . TIME_ZONE ,
}
return render ( request , 'cluster/worker_details.html' , context )
@login_required
def worker_manage ( request ):
if not UserAcl . objects . filter ( user = request . user ) . filter ( user_level__gte = 50 ) . exists ():
return render ( request , 'access_denied.html' , { 'page_title' : _ ( 'Access Denied' )})
2025-08-14 22:43:18 -03:00
worker = None
if 'uuid' in request . GET :
worker = get_object_or_404 ( Worker , uuid = request . GET [ 'uuid' ])
page_title = _ ( 'Edit Worker: ' ) + worker . name
2026-11-20 11:56:30 -03:00
2025-08-14 22:43:18 -03:00
if request . GET . get ( 'action' ) == 'delete' :
worker_name = worker . name
if request . GET . get ( 'confirmation' ) == 'delete' :
2026-11-20 20:42:42 -03:00
try :
worker . delete ()
messages . success ( request , _ ( 'Worker deleted|Worker deleted: ' ) + worker_name )
except ProtectedError :
messages . warning ( request , _ (
'Worker not deleted|Move the WireGuard instances of this worker to another server first: '
) + ', ' . join ( str ( instance ) for instance in worker . wireguard_instances . all ()))
return redirect ( reverse ( 'worker_details' ) + f '?uuid= { worker . uuid } ' )
2025-08-14 22:43:18 -03:00
else :
messages . warning ( request , _ ( 'Worker not deleted|Invalid confirmation.' ))
2026-11-20 11:56:30 -03:00
return redirect ( reverse ( 'cluster_main' ))
2025-08-14 22:43:18 -03:00
else :
page_title = _ ( 'Add Worker' )
2026-11-20 11:56:30 -03:00
form = WorkerForm ( request . POST or None , instance = worker )
if form . is_valid ():
is_new_worker = worker is None
worker = form . save ()
if is_new_worker :
messages . success ( request , _ ( 'Worker created|Worker created: ' ) + worker . name )
2025-08-14 22:43:18 -03:00
else :
2026-11-20 11:56:30 -03:00
messages . success ( request , _ ( 'Worker updated|Worker updated: ' ) + worker . name )
return redirect ( reverse ( 'worker_details' ) + f '?uuid= { worker . uuid } ' )
2025-08-14 22:43:18 -03:00
form_description = {
'size' : 'col-lg-6' ,
'content' : _ ( '''
2026-11-20 11:56:30 -03:00
<h5>Worker</h5>
<p>A worker is a cluster node that replicates WireGuard, firewall and DNS from this primary server and, optionally, publishes App Gateway applications.</p>
<h5>Name and location</h5>
<p>Used to identify the worker. The <strong>hostname</strong> is the address clients use to connect to this worker: it is written as the endpoint of the peer configurations downloaded for this server.</p>
<h5>Token</h5>
<p>Secret used by the worker to authenticate with this primary server. Changing it disconnects the worker until its configuration is updated.</p>
<h5>IP lock</h5>
<p>When enabled, the worker is only accepted from the IP address below. Without it, the address is updated automatically from the last connection.</p>
<h5>Enabled</h5>
<p>Disabled workers are refused and shut down their WireGuard interfaces until enabled again.</p>
2025-08-14 22:43:18 -03:00
''' )
}
2026-11-20 11:56:30 -03:00
2025-08-14 22:43:18 -03:00
context = {
2026-11-20 11:56:30 -03:00
'page_title' : page_title ,
'form' : form ,
'worker' : worker ,
2025-08-14 22:43:18 -03:00
'instance' : worker ,
2026-11-20 11:56:30 -03:00
'form_description' : form_description ,
2025-08-14 22:43:18 -03:00
}
return render ( request , 'generic_form.html' , context )
@login_required
def cluster_settings ( request ):
if not UserAcl . objects . filter ( user = request . user ) . filter ( user_level__gte = 50 ) . exists ():
return render ( request , 'access_denied.html' , { 'page_title' : _ ( 'Access Denied' )})
2026-11-20 11:56:30 -03:00
cluster_settings , cluster_settings_created = ClusterSettings . objects . get_or_create ( name = 'cluster_settings' )
2026-11-20 20:42:42 -03:00
was_distributed = ClusterSettings . is_distributed ()
2026-11-20 11:56:30 -03:00
form = ClusterSettingsForm ( request . POST or None , instance = cluster_settings )
if form . is_valid ():
2026-11-20 20:42:42 -03:00
cluster_settings = form . save ()
2026-11-20 11:56:30 -03:00
messages . success ( request , _ ( 'Cluster settings updated successfully.' ))
2026-11-20 20:42:42 -03:00
if ClusterSettings . is_distributed () != was_distributed :
# Where each instance runs may have changed: workers synchronize the new files right away and the
# primary server moves its interfaces when the configuration is applied
export_wireguard_configuration ()
export_firewall_configuration ()
WireGuardInstance . objects . update ( pending_changes = True )
2026-11-20 11:56:30 -03:00
return redirect ( reverse ( 'cluster_main' ))
2025-08-14 22:43:18 -03:00
form_description = {
'size' : 'col-lg-6' ,
'content' : _ ( '''
2026-11-20 11:56:30 -03:00
<h5>Cluster Enabled</h5>
<p>When disabled, workers are refused and shut down their WireGuard interfaces, and App Gateway applications are only published by this primary server. Requires the WireGuard status cache.</p>
<h5>Primary Enable WireGuard</h5>
<p>Lists this primary server as an option when downloading peer configurations and VPN invites.</p>
2025-08-14 22:43:18 -03:00
<h5>Cluster Mode</h5>
2026-11-20 11:56:30 -03:00
<p><strong>Mirror</strong>: every worker receives the same WireGuard instances, peers, firewall and DNS configuration. Peers can connect to any server.</p>
2026-11-20 20:42:42 -03:00
<p><strong>Distributed</strong>: each WireGuard instance runs only on the server selected in the instance settings (the primary server by default). Peers connect to that server. Firewall rules cannot be bound to a specific instance interface: use wg+ or IP addresses. After changing the mode, apply the WireGuard configuration so the primary server stops the instances that moved to workers.</p>
2026-11-20 11:56:30 -03:00
2025-08-14 22:43:18 -03:00
<h5>Worker Display</h5>
2026-11-20 11:56:30 -03:00
<p>How workers are named in server selections (peer configuration downloads and VPN invites).</p>
2025-08-14 22:43:18 -03:00
''' )
}
2026-11-20 11:56:30 -03:00
2025-08-14 22:43:18 -03:00
context = {
2026-11-20 11:56:30 -03:00
'page_title' : _ ( 'Cluster Settings' ),
2025-08-14 22:43:18 -03:00
'form' : form ,
'cluster_settings' : cluster_settings ,
'instance' : cluster_settings ,
2026-11-20 11:56:30 -03:00
'form_description' : form_description ,
2025-08-14 22:43:18 -03:00
}
return render ( request , 'generic_form.html' , context )