mirror of
https://github.com/eduardogsilva/wireguard_webadmin.git
synced 2026-01-10 18:06:18 +00:00
add API endpoint to submit WireGuard stats
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import glob
|
||||
import json
|
||||
import os
|
||||
|
||||
from django.conf import settings
|
||||
from django.http import JsonResponse, FileResponse
|
||||
from django.utils import timezone
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
|
||||
from .models import ClusterSettings, Worker, WorkerStatus
|
||||
|
||||
@@ -131,7 +133,7 @@ def api_get_worker_dnsmasq_config(request):
|
||||
response["Content-Length"] = str(os.path.getsize(dnsmasq_file))
|
||||
return response
|
||||
|
||||
|
||||
@csrf_exempt
|
||||
def api_submit_worker_wireguard_stats(request):
|
||||
worker, success = get_worker(request)
|
||||
if worker:
|
||||
@@ -141,9 +143,21 @@ def api_submit_worker_wireguard_stats(request):
|
||||
else:
|
||||
data = {'status': 'error', 'message': 'Worker not found'}
|
||||
return JsonResponse(data, status=403)
|
||||
worker_status = worker.workerstatus
|
||||
data = {'status': 'success', 'message': 'Stats received'}
|
||||
return JsonResponse(data, status=200)
|
||||
|
||||
try:
|
||||
if request.method == 'POST':
|
||||
payload = json.loads(request.body)
|
||||
worker_status = worker.workerstatus
|
||||
worker_status.wireguard_status = payload
|
||||
worker_status.wireguard_status_updated = timezone.now()
|
||||
worker_status.save()
|
||||
data = {'status': 'success', 'message': 'Stats received'}
|
||||
return JsonResponse(data, status=200)
|
||||
except Exception as e:
|
||||
pass
|
||||
|
||||
data = {'status': 'error', 'message': 'Stats not received'}
|
||||
return JsonResponse(data, status=400)
|
||||
|
||||
|
||||
def api_get_worker_config_files(request):
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 5.2.9 on 2026-01-08 16:37
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('cluster', '0015_alter_workerstatus_wireguard_status'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='workerstatus',
|
||||
name='wireguard_status_updated',
|
||||
field=models.DateTimeField(blank=True, null=True),
|
||||
),
|
||||
]
|
||||
@@ -65,6 +65,7 @@ class WorkerStatus(models.Model):
|
||||
worker_version = models.PositiveIntegerField(default=0)
|
||||
active_peers = models.PositiveIntegerField(default=0)
|
||||
wireguard_status = models.JSONField()
|
||||
wireguard_status_updated = models.DateTimeField(blank=True, null=True)
|
||||
|
||||
updated = models.DateTimeField(auto_now=True)
|
||||
created = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
Reference in New Issue
Block a user