mirror of
https://github.com/eduardogsilva/wireguard_webadmin.git
synced 2025-08-26 13:21:14 +00:00
Peer management translation
This commit is contained in:
@@ -1,14 +1,17 @@
|
||||
from django import forms
|
||||
from wireguard.models import Peer, PeerAllowedIP, NETMASK_CHOICES
|
||||
import ipaddress
|
||||
|
||||
from django import forms
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from wireguard.models import NETMASK_CHOICES, Peer, PeerAllowedIP
|
||||
|
||||
|
||||
class PeerForm(forms.ModelForm):
|
||||
name = forms.CharField(label='Name', required=False)
|
||||
public_key = forms.CharField(label='Public Key', required=True)
|
||||
private_key = forms.CharField(label='Private Key', required=False)
|
||||
pre_shared_key = forms.CharField(label='Pre-Shared Key', required=True)
|
||||
persistent_keepalive = forms.IntegerField(label='Persistent Keepalive', required=True)
|
||||
name = forms.CharField(label=_('Name'), required=False)
|
||||
public_key = forms.CharField(label=_('Public Key'), required=True)
|
||||
private_key = forms.CharField(label=_('Private Key'), required=False)
|
||||
pre_shared_key = forms.CharField(label=_('Pre-Shared Key'), required=True)
|
||||
persistent_keepalive = forms.IntegerField(label=_('Persistent Keepalive'), required=True)
|
||||
|
||||
class Meta:
|
||||
model = Peer
|
||||
@@ -23,9 +26,9 @@ class PeerAllowedIPForm(forms.ModelForm):
|
||||
self.current_peer = current_peer
|
||||
self.config_file = config_file
|
||||
|
||||
allowed_ip = forms.GenericIPAddressField(label='Allowed IP or Network', required=True)
|
||||
netmask = forms.ChoiceField(choices=NETMASK_CHOICES, label='Netmask', initial=24, required=True)
|
||||
priority = forms.IntegerField(label='Priority', required=True, initial=1)
|
||||
allowed_ip = forms.GenericIPAddressField(label=_('Allowed IP or Network'), required=True)
|
||||
netmask = forms.ChoiceField(choices=NETMASK_CHOICES, label=_('Netmask'), initial=24, required=True)
|
||||
priority = forms.IntegerField(label=_('Priority'), required=True, initial=1)
|
||||
|
||||
def clean(self):
|
||||
cleaned_data = super().clean()
|
||||
@@ -33,7 +36,7 @@ class PeerAllowedIPForm(forms.ModelForm):
|
||||
allowed_ip = cleaned_data.get('allowed_ip')
|
||||
netmask = cleaned_data.get('netmask')
|
||||
if allowed_ip is None:
|
||||
raise forms.ValidationError("Please provide a valid IP address.")
|
||||
raise forms.ValidationError(_("Please provide a valid IP address."))
|
||||
|
||||
if self.config_file == 'server':
|
||||
wireguard_network = ipaddress.ip_network(f"{self.current_peer.wireguard_instance.address}/{self.current_peer.wireguard_instance.netmask}", strict=False)
|
||||
@@ -42,27 +45,27 @@ class PeerAllowedIPForm(forms.ModelForm):
|
||||
if self.instance:
|
||||
zero_priority_ips_query = zero_priority_ips_query.exclude(uuid=self.instance.uuid)
|
||||
if zero_priority_ips_query.exists():
|
||||
raise forms.ValidationError("A peer can have only one IP with priority zero.")
|
||||
raise forms.ValidationError(_("A peer can have only one IP with priority zero."))
|
||||
|
||||
duplicated_ip = PeerAllowedIP.objects.filter(config_file='server', allowed_ip=allowed_ip)
|
||||
if self.instance:
|
||||
duplicated_ip = duplicated_ip.exclude(uuid=self.instance.uuid)
|
||||
if duplicated_ip.exists():
|
||||
raise forms.ValidationError("This IP is already in use by another peer.")
|
||||
raise forms.ValidationError(_("This IP is already in use by another peer."))
|
||||
if ipaddress.ip_address(allowed_ip) not in wireguard_network:
|
||||
raise forms.ValidationError("The IP address does not belong to the Peer's WireGuard instance network range. Please check the IP address or change the priority.")
|
||||
raise forms.ValidationError(_("The IP address does not belong to the Peer's WireGuard instance network range. Please check the IP address or change the priority."))
|
||||
if str(netmask) != str(32):
|
||||
raise forms.ValidationError("The netmask for priority 0 IP must be 32.")
|
||||
raise forms.ValidationError(_("The netmask for priority 0 IP must be 32."))
|
||||
if self.current_peer.wireguard_instance.address == allowed_ip:
|
||||
raise forms.ValidationError("The IP address is the same as the Peer's WireGuard instance address.")
|
||||
raise forms.ValidationError(_("The IP address is the same as the Peer's WireGuard instance address."))
|
||||
else:
|
||||
if ipaddress.ip_address(allowed_ip) in wireguard_network:
|
||||
raise forms.ValidationError("The IP address belongs to the Peer's WireGuard instance network range. Please check the IP address or change use priority 0 instead.")
|
||||
raise forms.ValidationError(_("The IP address belongs to the Peer's WireGuard instance network range. Please check the IP address or change use priority 0 instead."))
|
||||
elif self.config_file == 'client':
|
||||
if priority < 1:
|
||||
raise forms.ValidationError("Priority must be greater than or equal to 1")
|
||||
raise forms.ValidationError(_("Priority must be greater than or equal to 1"))
|
||||
else:
|
||||
raise forms.ValidationError('Invalid config file')
|
||||
raise forms.ValidationError(_('Invalid config file'))
|
||||
|
||||
class Meta:
|
||||
model = PeerAllowedIP
|
||||
|
@@ -126,7 +126,7 @@ def view_wireguard_peer_manage(request):
|
||||
if not user_has_access_to_instance(user_acl, current_instance):
|
||||
raise Http404
|
||||
current_peer = None
|
||||
page_title = 'Create a new Peer for instance wg' + str(current_instance.instance_id)
|
||||
page_title = _('Create a new Peer for instance wg') + str(current_instance.instance_id)
|
||||
new_peer_data = generate_peer_default(current_instance)
|
||||
|
||||
if new_peer_data['allowed_ip']:
|
||||
@@ -145,12 +145,12 @@ def view_wireguard_peer_manage(request):
|
||||
priority=0,
|
||||
netmask=32,
|
||||
)
|
||||
messages.success(request, 'Peer created|Peer for instance wg' + str(current_instance.instance_id) + ' created successfully with IP ' + new_peer_data['allowed_ip'] + '/32.')
|
||||
messages.success(request, _('Peer created|Peer created successfully.'))
|
||||
new_peer.wireguard_instance.pending_changes = True
|
||||
new_peer.wireguard_instance.save()
|
||||
return redirect('/peer/manage/?peer=' + str(new_peer.uuid))
|
||||
else:
|
||||
messages.warning(request, 'Error creating peer|No available IP address found for peer creation.')
|
||||
messages.warning(request, _('Error creating peer|No available IP address found for peer creation.'))
|
||||
return redirect('/peer/list/')
|
||||
|
||||
elif request.GET.get('peer'):
|
||||
@@ -163,12 +163,12 @@ def view_wireguard_peer_manage(request):
|
||||
current_peer.wireguard_instance.pending_changes = True
|
||||
current_peer.wireguard_instance.save()
|
||||
current_peer.delete()
|
||||
messages.success(request, 'Peer deleted|Peer deleted successfully.')
|
||||
messages.success(request, _('Peer deleted|Peer deleted successfully.'))
|
||||
return redirect('/peer/list/?uuid=' + str(current_instance.uuid))
|
||||
else:
|
||||
messages.warning(request, 'Error deleting peer|Invalid confirmation message. Type "delete" to confirm.')
|
||||
messages.warning(request, _('Error deleting peer|Invalid confirmation message. Type "delete" to confirm.'))
|
||||
return redirect('/peer/manage/?peer=' + str(current_peer.uuid))
|
||||
page_title = 'Update Peer '
|
||||
page_title = _('Update Peer: ')
|
||||
peer_ip_list = current_peer.peerallowedip_set.filter(config_file='server').order_by('priority')
|
||||
peer_client_ip_list = current_peer.peerallowedip_set.filter(config_file='client').order_by('priority')
|
||||
if current_peer.name:
|
||||
@@ -179,7 +179,7 @@ def view_wireguard_peer_manage(request):
|
||||
form = PeerForm(request.POST, instance=current_peer)
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
messages.success(request, 'Peer updated|Peer updated successfully.')
|
||||
messages.success(request, _('Peer updated|Peer updated successfully.'))
|
||||
current_peer.wireguard_instance.pending_changes = True
|
||||
current_peer.wireguard_instance.save()
|
||||
return redirect('/peer/list/?uuid=' + str(current_peer.wireguard_instance.uuid))
|
||||
@@ -217,19 +217,19 @@ def view_manage_ip_address(request):
|
||||
if request.GET.get('action') == 'delete':
|
||||
if request.GET.get('confirmation') == 'delete':
|
||||
current_ip.delete()
|
||||
messages.success(request, 'IP address deleted|IP address deleted successfully.')
|
||||
messages.success(request, _('IP address deleted|IP address deleted successfully.'))
|
||||
current_peer.wireguard_instance.pending_changes = True
|
||||
current_peer.wireguard_instance.save()
|
||||
return redirect('/peer/manage/?peer=' + str(current_peer.uuid))
|
||||
else:
|
||||
messages.warning(request, 'Error deleting IP address|Invalid confirmation message. Type "delete" to confirm.')
|
||||
return redirect('/peer/ip/?ip=' + str(current_ip.uuid))
|
||||
messages.warning(request, _('Error deleting IP address|Invalid confirmation message. Type "delete" to confirm.'))
|
||||
return redirect('/peer/manage_ip_address/?ip=' + str(current_ip.uuid))
|
||||
if config_file not in ['client', 'server']:
|
||||
config_file = 'server'
|
||||
if config_file == 'client':
|
||||
page_title = 'Manage client route'
|
||||
page_title = _('Manage client route')
|
||||
else:
|
||||
page_title = 'Manage IP address or Network'
|
||||
page_title = _('Manage IP address or Network')
|
||||
|
||||
if request.method == 'POST':
|
||||
form = PeerAllowedIPForm(request.POST or None, instance=current_ip, current_peer=current_peer, config_file=config_file)
|
||||
@@ -242,9 +242,9 @@ def view_manage_ip_address(request):
|
||||
current_peer.wireguard_instance.pending_changes = True
|
||||
current_peer.wireguard_instance.save()
|
||||
if current_ip:
|
||||
messages.success(request, 'IP address updated|IP address updated successfully.')
|
||||
messages.success(request, _('IP address updated|IP address updated successfully.'))
|
||||
else:
|
||||
messages.success(request, 'IP address added|IP address added successfully.')
|
||||
messages.success(request, _('IP address added|IP address added successfully.'))
|
||||
return redirect('/peer/manage/?peer=' + str(current_peer.uuid))
|
||||
else:
|
||||
form = PeerAllowedIPForm(instance=current_ip, current_peer=current_peer)
|
||||
|
Reference in New Issue
Block a user