This commit is contained in:
Kumi 2018-11-25 22:02:16 +01:00
parent 8f3d3570c1
commit 4c3b316430
No known key found for this signature in database
GPG key ID: 8D4E139A5FF9082F
6 changed files with 96 additions and 38 deletions

View file

@ -1,7 +1,4 @@
from django.contrib import admin
# Register your models here.
from .models import Organization, Device, Network
admin.site.register(Organization)

View file

@ -2,18 +2,9 @@ from django.db import models
from django.conf import settings
import uuid
# Create your models here.
def getRandom(self):
def getRandom():
return str(uuid.uuid4().hex)
class Network(models.Model):
extip = models.CharField(max_length=15)
intip = models.CharField(max_length=15)
def __str__(self):
return "%s (%s)" % (self.intip, self.extip)
class Organization(models.Model):
name = models.CharField(max_length=300)
users = models.ManyToManyField(settings.AUTH_USER_MODEL)
@ -21,10 +12,22 @@ class Organization(models.Model):
def __str__(self):
return self.name
class Network(models.Model):
extip = models.CharField(max_length=15)
intip = models.CharField(max_length=15)
organizations = models.ManyToManyField(Organization)
def __str__(self):
return "%s (%s)" % (self.intip, self.extip)
class Device(models.Model):
serial = models.CharField("Device Serial Number", max_length=12, unique=True)
name = models.CharField("Common Name", max_length=100, blank=True, null=True)
organization = models.ForeignKey(Organization, on_delete=models.CASCADE)
network = models.OneToOneField(Network, on_delete=models.SET_NULL, blank=True, null=True)
curip = models.CharField("Current IP Address", max_length=15, blank=True, null=True)
lasttime = models.DateTimeField("Last Received Heartbeat", blank=True, null=True)
secret = models.CharField("Secret", default=getRandom, max_length=128)
def __str__(self):
return "%s%s" % (self.serial, " (%s)" % self.curip if self.curip else "")

View file

@ -1,8 +1,11 @@
from django.urls import path
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('hosts', views.getHosts, name='hosts')
path('devices', views.devices, name='devices'),
path('hosts', views.hosts, name='hosts'),
path('devices/<int:device_id>', views.editdevice, name='editdevice'),
path('heartbeat', views.heartbeat, name='heartbeat')
]

View file

@ -1,13 +1,74 @@
from django.shortcuts import render, get_object_or_404
from django.shortcuts import render, get_object_or_404, redirect
from django.http import HttpResponse
from .models import Device
# Create your views here.
from django.contrib.auth.forms import AuthenticationForm
from django.db.models import Q
from django.views.decorators.csrf import csrf_exempt
from django.utils import timezone
from .models import Device, Organization, Network
def index(request):
return HttpResponse("This.")
if request.user.is_authenticated:
return redirect("/devices")
else:
return redirect("/accounts/login")
def getHosts(request):
# TODO - Turn this GET into a POST
device = get_object_or_404(Device, secret=request.GET.get("secret", ""))
@csrf_exempt
def heartbeat(request):
device = get_object_or_404(Device, secret=request.POST.get("secret", ""))
device.curip = request.POST.get("ip", "")
device.lasttime = timezone.now()
device.save()
return HttpResponse("")
def hosts(request):
device = get_object_or_404(Device, secret=request.POST.get("secret", ""))
return render(request, "manager/hosts", {"device": device})
def devices(request):
if request.user.is_authenticated:
user = request.user
organization = Organization.objects.filter(users__username=request.user.username)[0]
devices = Device.objects.filter(organization=organization.id)
return render(request, "manager/index.html",
{
"title": "Device Administration",
"user": user,
"organization": organization,
"devices": devices
}
)
else:
return redirect("/")
def editdevice(request, device_id):
if request.user.is_authenticated:
device = None
subnets = set()
for organization in Organization.objects.filter(users__id=request.user.id):
device = device or Device.objects.filter(id=device_id, organization=organization.id)
for subnet in Network.objects.filter(organizations=organization.id):
subnets.add(subnet)
if not device:
return redirect("/")
if request.POST.get("subnet", ""):
subnet = Network.objects.filter(intip=request.POST.get("subnet", device[0].network.intip))
if subnet[0] in subnets:
device[0].name = request.POST.get("name", "")
device[0].network = subnet[0]
device[0].save()
return redirect("/")
return render(request, "manager/edit.html",
{
"title": "Edit Device",
"device": device[0],
"subnets": subnets
}
)
else:
return redirect("/")

View file

@ -32,6 +32,7 @@ ALLOWED_HOSTS = []
INSTALLED_APPS = [
'manager.apps.ManagerConfig',
'bootstrap3',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
@ -119,3 +120,6 @@ USE_TZ = True
# https://docs.djangoproject.com/en/2.1/howto/static-files/
STATIC_URL = '/static/'
LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/'

View file

@ -1,22 +1,12 @@
"""vpnmanager URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import include, path
admin.site.site_header = 'VPN360 Network Administration'
admin.site.site_title = admin.site.site_header
admin.site.index_title = admin.site.index_title
urlpatterns = [
path('', include('manager.urls')),
path('admin/', admin.site.urls),
path('accounts/', include('django.contrib.auth.urls'))
]