wg instance dns settings and peer list refresh interval configuration

This commit is contained in:
Eduardo Silva 2024-02-17 15:03:29 -03:00
parent cfcabed244
commit 3563da423b
10 changed files with 88 additions and 16 deletions

View File

@ -192,7 +192,7 @@
<footer class="main-footer">
wireguard-webadmin
<div class="float-right d-none d-sm-inline-block">
<b>Version</b> 0.8.3 beta
<b>Version</b> 0.8.4 beta
</div>
</footer>

View File

@ -7,10 +7,8 @@
<p>If you encounter any issues or have suggestions, please open an issue on GitHub so I can review it.</p>
<h2>TODO list</h2>
<ul>
<li>The DNS server provided to the peer is still hardcoded.</li>
<li>AllowedIPs on client configuration side.</li>
<li>Make Peer's last handshake permanent</li>
<li>Setting for refresh interval in Peer list</li>
<li>wireguard_webadmin Update notification</li>
</ul>

View File

@ -24,18 +24,21 @@
{% csrf_token %}
<div class="card-body row">
<div class="col-lg-6">
<!-- Line 1: Name and Instance ID -->
<!-- Line 1: Name and peer_list_refresh_interval -->
<div class="form-row">
<div class="form-group col-md-6">
<label for="{{ form.name.id_for_label }}">{{ form.name.label }}</label>
<input type="text" class="form-control" id="{{ form.name.id_for_label }}" name="{{ form.name.html_name }}" placeholder="Enter Name" value="{{ form.name.value|default_if_none:'' }}">
</div>
<div class="form-group col-md-6">
<label for="{{ form.instance_id.id_for_label }}">{{ form.instance_id.label }}</label>
<input type="number" class="form-control" id="{{ form.instance_id.id_for_label }}" name="{{ form.instance_id.html_name }}" placeholder="Instance ID" value="{{ form.instance_id.value|default_if_none:'' }}" required>
<label for="{{ form.peer_list_refresh_interval.id_for_label }}">{{ form.peer_list_refresh_interval.label }}</label>
<input type="number" class="form-control" id="{{ form.peer_list_refresh_interval.id_for_label }}" name="{{ form.peer_list_refresh_interval.html_name }}" placeholder="Persistent Keepalive" value="{{ form.peer_list_refresh_interval.value|default_if_none:'' }}" required>
</div>
</div>
<!-- Line 2: Hostname, Listen Port and keepalive -->
<!-- Line 2: Hostname, Listen Port and instance id -->
<div class="form-row">
<div class="form-group col-md-6">
<label for="{{ form.hostname.id_for_label }}">{{ form.hostname.label }}</label>
@ -45,10 +48,12 @@
<label for="{{ form.listen_port.id_for_label }}">{{ form.listen_port.label }}</label>
<input type="number" class="form-control" id="{{ form.listen_port.id_for_label }}" name="{{ form.listen_port.html_name }}" placeholder="Listen Port" value="{{ form.listen_port.value|default_if_none:'' }}" required>
</div>
<div class="form-group col-md-3">
<label for="{{ form.persistent_keepalive.id_for_label }}">{{ form.persistent_keepalive.label }}</label>
<input type="number" class="form-control" id="{{ form.persistent_keepalive.id_for_label }}" name="{{ form.persistent_keepalive.html_name }}" placeholder="Persistent Keepalive" value="{{ form.persistent_keepalive.value|default_if_none:'' }}" required>
<label for="{{ form.instance_id.id_for_label }}">{{ form.instance_id.label }}</label>
<input type="number" class="form-control" id="{{ form.instance_id.id_for_label }}" name="{{ form.instance_id.html_name }}" placeholder="Instance ID" value="{{ form.instance_id.value|default_if_none:'' }}" required>
</div>
</div>
<!-- Line 3: Private Key and Persistent Keepalive -->
<div class="form-row">
@ -77,6 +82,22 @@
</select>
</div>
</div>
<!-- Line 5: Primary and secondary DNS -->
<div class="form-row">
<div class="form-group col-md-6">
<label for="{{ form.dns_primary.id_for_label }}">{{ form.dns_primary.label }}</label>
<input type="text" class="form-control" id="{{ form.dns_primary.id_for_label }}" name="{{ form.dns_primary.html_name }}" placeholder="1.1.1.1" value="{{ form.dns_primary.value|default_if_none:'' }}" required>
</div>
<div class="form-group col-md-6">
<label for="{{ form.dns_secondary.id_for_label }}">{{ form.dns_secondary.label }}</label>
<input type="text" class="form-control" id="{{ form.dns_secondary.id_for_label }}" name="{{ form.dns_secondary.html_name }}" placeholder="1.0.0.1" value="{{ form.dns_secondary.value|default_if_none:'' }}">
</div>
</div>
</div>
<div class="col-lg-6">
<!-- Line 1: Post Up -->

View File

@ -136,7 +136,7 @@
};
fetchWireguardStatus();
setInterval(fetchWireguardStatus, 30000);
setInterval(fetchWireguardStatus, {{ current_instance.peer_list_refresh_interval }} * 1000);
});
const updateUI = (data) => {

View File

@ -3,8 +3,8 @@ from .models import WireGuardInstance, Peer, PeerAllowedIP
class WireGuardInstanceAdmin(admin.ModelAdmin):
list_display = ('name', 'instance_id', 'private_key', 'hostname', 'listen_port', 'address', 'netmask', 'post_up', 'post_down', 'persistent_keepalive', 'created', 'updated', 'uuid')
search_fields = ('name', 'instance_id', 'private_key', 'hostname', 'listen_port', 'address', 'netmask', 'post_up', 'post_down', 'persistent_keepalive', 'created', 'updated', 'uuid')
list_display = ('name', 'instance_id', 'private_key', 'hostname', 'listen_port', 'address', 'netmask', 'post_up', 'post_down', 'created', 'updated', 'uuid')
search_fields = ('name', 'instance_id', 'private_key', 'hostname', 'listen_port', 'address', 'netmask', 'post_up', 'post_down', 'created', 'updated', 'uuid')
admin.site.register(WireGuardInstance, WireGuardInstanceAdmin)

View File

@ -14,12 +14,15 @@ class WireGuardInstanceForm(forms.ModelForm):
netmask = forms.ChoiceField(choices=NETMASK_CHOICES, label='Netmask')
post_up = forms.CharField(label='Post Up', required=False)
post_down = forms.CharField(label='Post Down', required=False)
persistent_keepalive = forms.IntegerField(label='Keepalive')
peer_list_refresh_interval = forms.IntegerField(label='Web Refresh Interval', initial=20)
dns_primary = forms.GenericIPAddressField(label='Primary DNS', initial='1.1.1.1')
dns_secondary = forms.GenericIPAddressField(label='Secondary DNS', initial='1.0.0.1', required=False)
class Meta:
model = WireGuardInstance
fields = [
'name', 'instance_id', 'private_key', 'public_key','hostname', 'listen_port', 'address', 'netmask', 'post_up', 'post_down', 'persistent_keepalive'
'name', 'instance_id', 'private_key', 'public_key','hostname', 'listen_port', 'address',
'netmask', 'post_up', 'post_down', 'peer_list_refresh_interval', 'dns_primary', 'dns_secondary'
]
def clean(self):
@ -27,6 +30,9 @@ class WireGuardInstanceForm(forms.ModelForm):
hostname = cleaned_data.get('hostname')
address = cleaned_data.get('address')
netmask = cleaned_data.get('netmask')
peer_list_refresh_interval = cleaned_data.get('peer_list_refresh_interval')
if peer_list_refresh_interval < 10:
raise forms.ValidationError('Peer List Refresh Interval must be at least 10 seconds')
if not is_valid_ip_or_hostname(hostname):
raise forms.ValidationError('Invalid hostname or IP Address')

View File

@ -0,0 +1,17 @@
# Generated by Django 5.0.1 on 2024-02-17 17:21
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('wireguard', '0006_peerstatus'),
]
operations = [
migrations.RemoveField(
model_name='wireguardinstance',
name='persistent_keepalive',
),
]

View File

@ -0,0 +1,28 @@
# Generated by Django 5.0.1 on 2024-02-17 17:25
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('wireguard', '0007_remove_wireguardinstance_persistent_keepalive'),
]
operations = [
migrations.AddField(
model_name='wireguardinstance',
name='dns_primary',
field=models.GenericIPAddressField(default='1.1.1.1', protocol='IPv4', unique=True),
),
migrations.AddField(
model_name='wireguardinstance',
name='dns_secondary',
field=models.GenericIPAddressField(blank=True, default='1.0.0.1', null=True, protocol='IPv4', unique=True),
),
migrations.AddField(
model_name='wireguardinstance',
name='peer_list_refresh_interval',
field=models.IntegerField(default=20),
),
]

View File

@ -40,7 +40,9 @@ class WireGuardInstance(models.Model):
netmask = models.IntegerField(default=24, choices=NETMASK_CHOICES)
post_up = models.TextField(blank=True, null=True)
post_down = models.TextField(blank=True, null=True)
persistent_keepalive = models.IntegerField(default=25)
peer_list_refresh_interval = models.IntegerField(default=20)
dns_primary = models.GenericIPAddressField(unique=True, protocol='IPv4', default='1.1.1.1')
dns_secondary = models.GenericIPAddressField(unique=True, protocol='IPv4', default='1.0.0.1', blank=True, null=True)
pending_changes = models.BooleanField(default=True)
created = models.DateTimeField(auto_now_add=True)

View File

@ -36,7 +36,7 @@ def generate_peer_config(peer_uuid):
"[Interface]",
f"PrivateKey = {peer.private_key}" if peer.private_key else "",
f"Address = {client_address}",
f"DNS = 8.8.8.8",
f"DNS = {wg_instance.dns_primary}" + (f", {wg_instance.dns_secondary}" if wg_instance.dns_secondary else ""),
"\n[Peer]",
f"PublicKey = {wg_instance.public_key}",
f"Endpoint = {wg_instance.hostname}:{wg_instance.listen_port}",