diff --git a/manager/apps.py b/manager/apps.py index bbf7ad3..9f18bff 100644 --- a/manager/apps.py +++ b/manager/apps.py @@ -3,3 +3,6 @@ from django.apps import AppConfig class ManagerConfig(AppConfig): name = 'manager' verbose_name = 'VPN360 Management' + + def ready(self): + from .logger import logger diff --git a/manager/models.py b/manager/models.py index b28d22a..40ecd24 100644 --- a/manager/models.py +++ b/manager/models.py @@ -65,3 +65,32 @@ class Device(models.Model): def __str__(self): return "%s%s" % (self.serial, " (%s)" % self.curip if self.curip else "") +class DeviceLog(models.Model): + ADD = 0 + REMOVE = 1 + NAME = 2 + NET = 3 + REBOOT = 4 + UPDATE = 5 + STATUS = 6 + INTERNALIP = 7 + WIFI = 8 + + ACTION_CHOICES = ( + (ADD, "ADD"), + (REMOVE, "REMOVE"), + (NAME, "NAME"), + (NET, "NET"), + (REBOOT, "REBOOT"), + (UPDATE, "UPDATE"), + (STATUS, "STATUS"), + (INTERNALIP, "INTERNALIP"), + (WIFI, "WIFI") + ) + + objid = models.CharField("Device ID", max_length=64) + user = models.CharField("User Name", max_length=128, null=True) + date = models.DateTimeField("Action Date", auto_now_add=True) + action = models.IntegerField("Action", choices=ACTION_CHOICES) + oldvalue = models.CharField("Old Value", max_length=256, blank=True, null=True) + newvalue = models.CharField("New Value", max_length=256, blank=True, null=True) diff --git a/manager/views.py b/manager/views.py index 639bdaf..8e37990 100644 --- a/manager/views.py +++ b/manager/views.py @@ -10,6 +10,7 @@ from django.contrib.auth.models import User from django.contrib.auth.decorators import login_required, user_passes_test from .models import Device, Organization, Network, Model, Wifi +from .signals import * from distutils.dir_util import copy_tree @@ -40,6 +41,7 @@ def heartbeat(request): ip = request.POST.get("ip", "") if device.update: + sigRebootDevice(device.serial, None, False) device.reboot = False code = """ . /etc/vpnsecret @@ -110,6 +112,7 @@ def wireless(request): @csrf_exempt def hosts(request): device = get_object_or_404(Device, secret=request.POST.get("secret", "")) + sigRebootDevice(device.serial, None, False) device.reboot = False device.save() return render(request, "manager/hosts", {"device": device}) @@ -200,6 +203,7 @@ def update(request): if not mkfirmware(device, FWDIR): return HttpResponse(status=503) + sigUpdateDevice(device.serial, None, False) device.update = False device.save() @@ -299,11 +303,31 @@ def editdevice(request, device_id): subnet = Network.objects.filter(intip=request.POST.get("subnet", device[0].network.intip if device[0].network else "No VPN")) if subnet[0] in subnets: - device[0].name = request.POST.get("name", "") - device[0].network = subnet[0] - device[0].reboot = True if request.POST.get("reboot", "0") == "True" else False - device[0].update = True if request.POST.get("update", "0") == "True" else False - device[0].wifi.set(request.POST.getlist("wifi", [])) + newname = request.POST.get("name", "") + if newname != device[0].name: + sigRenameDevice(device[0].serial, request.user.username, device[0].name, newname) + device[0].name = newname + + if subnet[0] != device[0].network: + sigNetDevice(device[0].serial, request.user.username, str(device[0].network), str(subnet[0])) + device[0].network = subnet[0] + + newreboot = True if request.POST.get("reboot", "0") == "True" else False + if newreboot != device[0].reboot: + sigRebootDevice(device[0].serial, request.user.username, newreboot) + device[0].reboot = newreboot + + newupdate = True if request.POST.get("update", "0") == "True" else False + if newupdate != device[0].update: + sigUpdateDevice(device[0].serial, request.user.username, newupdate) + device[0].update = newupdate + + newwifis = set(request.POST.getlist("wifi", [])) + oldwifis = set(device[0].wifi.all()) + if newwifis != oldwifis: + sigWifiDevice(device[0].serial, request.user.username, oldwifis, newwifis) + device[0].wifi.set(newwifis) + device[0].changed = timezone.now() device[0].save() @@ -390,6 +414,7 @@ def getconfig(request, device_id): if not mkfirmware(device, FWDIR): return HttpResponse("Something went wrong generating the firmware image. The server may be busy, please try again later.") + sigUpdateDevice(device.serial, None, False) device.update = False device.save() @@ -404,7 +429,8 @@ def rebootdevice(request, device_id): for organization in Organization.objects.filter(users=request.user): device = device or Device.objects.filter(id=device_id, organization=organization) - if device: + if device and not device[0].reboot: + sigRebootDevice(device[0].serial, request.user.username, True) device[0].reboot = True device[0].save() @@ -417,7 +443,8 @@ def updatedevice(request, device_id): for organization in Organization.objects.filter(users=request.user): device = device or Device.objects.filter(id=device_id, organization=organization) - if device: + if device and not device[0].update: + sigUpdateDevice(device[0].serial, request.user.username, True) device[0].update = True device[0].save()