From 8f3d3570c1e70192fcf94c953772038d177607b6 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Sun, 25 Nov 2018 16:05:26 +0100 Subject: [PATCH] Check in working hosts file generator --- .gitignore | 3 ++ manage.py | 2 +- manager/__init__.py | 0 manager/admin.py | 9 ++++ manager/apps.py | 5 ++ manager/migrations/0001_initial.py | 51 +++++++++++++++++++ manager/migrations/0002_auto_20181125_1457.py | 30 +++++++++++ manager/migrations/__init__.py | 0 manager/models.py | 30 +++++++++++ manager/templates/manager/hosts | 3 ++ manager/tests.py | 3 ++ manager/urls.py | 8 +++ manager/views.py | 13 +++++ vpnmanager/settings.py | 1 + vpnmanager/urls.py | 3 +- 15 files changed, 159 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 manager/__init__.py create mode 100644 manager/admin.py create mode 100644 manager/apps.py create mode 100644 manager/migrations/0001_initial.py create mode 100644 manager/migrations/0002_auto_20181125_1457.py create mode 100644 manager/migrations/__init__.py create mode 100644 manager/models.py create mode 100644 manager/templates/manager/hosts create mode 100644 manager/tests.py create mode 100644 manager/urls.py create mode 100644 manager/views.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3ba11e5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +db.sqlite3 +__pycache__ +*.pyc diff --git a/manage.py b/manage.py index d0b9dab..c0fa2b1 100755 --- a/manage.py +++ b/manage.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 import os import sys diff --git a/manager/__init__.py b/manager/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/manager/admin.py b/manager/admin.py new file mode 100644 index 0000000..95af8aa --- /dev/null +++ b/manager/admin.py @@ -0,0 +1,9 @@ +from django.contrib import admin + +# Register your models here. + +from .models import Organization, Device, Network + +admin.site.register(Organization) +admin.site.register(Device) +admin.site.register(Network) diff --git a/manager/apps.py b/manager/apps.py new file mode 100644 index 0000000..7a9b72b --- /dev/null +++ b/manager/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class ManagerConfig(AppConfig): + name = 'manager' diff --git a/manager/migrations/0001_initial.py b/manager/migrations/0001_initial.py new file mode 100644 index 0000000..9d8b174 --- /dev/null +++ b/manager/migrations/0001_initial.py @@ -0,0 +1,51 @@ +# Generated by Django 2.1.3 on 2018-11-25 13:45 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='Device', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('curip', models.CharField(blank=True, max_length=15, null=True)), + ('secret', models.CharField(max_length=128)), + ], + ), + migrations.CreateModel( + name='Network', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('extip', models.CharField(max_length=15)), + ('intip', models.CharField(max_length=15)), + ], + ), + migrations.CreateModel( + name='Organization', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=300)), + ('users', models.ManyToManyField(to=settings.AUTH_USER_MODEL)), + ], + ), + migrations.AddField( + model_name='device', + name='network', + field=models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='manager.Network'), + ), + migrations.AddField( + model_name='device', + name='organization', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='manager.Organization'), + ), + ] diff --git a/manager/migrations/0002_auto_20181125_1457.py b/manager/migrations/0002_auto_20181125_1457.py new file mode 100644 index 0000000..84652fc --- /dev/null +++ b/manager/migrations/0002_auto_20181125_1457.py @@ -0,0 +1,30 @@ +# Generated by Django 2.1.3 on 2018-11-25 14:57 + +from django.db import migrations, models +import manager.models + + +class Migration(migrations.Migration): + + dependencies = [ + ('manager', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='device', + name='serial', + field=models.CharField(default='This', max_length=12, unique=True, verbose_name='Device Serial Number'), + preserve_default=False, + ), + migrations.AlterField( + model_name='device', + name='curip', + field=models.CharField(blank=True, max_length=15, null=True, verbose_name='Current IP Address'), + ), + migrations.AlterField( + model_name='device', + name='secret', + field=models.CharField(default=manager.models.getRandom, max_length=128, verbose_name='Secret'), + ), + ] diff --git a/manager/migrations/__init__.py b/manager/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/manager/models.py b/manager/models.py new file mode 100644 index 0000000..46dd90a --- /dev/null +++ b/manager/models.py @@ -0,0 +1,30 @@ +from django.db import models +from django.conf import settings +import uuid + +# Create your models here. + +def getRandom(self): + 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) + + def __str__(self): + return self.name + +class Device(models.Model): + serial = models.CharField("Device Serial Number", max_length=12, unique=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) + secret = models.CharField("Secret", default=getRandom, max_length=128) + diff --git a/manager/templates/manager/hosts b/manager/templates/manager/hosts new file mode 100644 index 0000000..5f97a4e --- /dev/null +++ b/manager/templates/manager/hosts @@ -0,0 +1,3 @@ +127.0.0.1 localhost +{{ device.network.intip }} private.vpn360 +{{ device.network.extip }} public.vpn360 diff --git a/manager/tests.py b/manager/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/manager/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/manager/urls.py b/manager/urls.py new file mode 100644 index 0000000..ea3f7b0 --- /dev/null +++ b/manager/urls.py @@ -0,0 +1,8 @@ +from django.urls import path + +from . import views + +urlpatterns = [ + path('', views.index, name='index'), + path('hosts', views.getHosts, name='hosts') +] diff --git a/manager/views.py b/manager/views.py new file mode 100644 index 0000000..ec5bd01 --- /dev/null +++ b/manager/views.py @@ -0,0 +1,13 @@ +from django.shortcuts import render, get_object_or_404 +from django.http import HttpResponse +from .models import Device + +# Create your views here. + +def index(request): + return HttpResponse("This.") + +def getHosts(request): + # TODO - Turn this GET into a POST + device = get_object_or_404(Device, secret=request.GET.get("secret", "")) + return render(request, "manager/hosts", {"device": device}) diff --git a/vpnmanager/settings.py b/vpnmanager/settings.py index 0fa2b56..997589a 100644 --- a/vpnmanager/settings.py +++ b/vpnmanager/settings.py @@ -31,6 +31,7 @@ ALLOWED_HOSTS = [] # Application definition INSTALLED_APPS = [ + 'manager.apps.ManagerConfig', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', diff --git a/vpnmanager/urls.py b/vpnmanager/urls.py index 96b067a..d9b551a 100644 --- a/vpnmanager/urls.py +++ b/vpnmanager/urls.py @@ -14,8 +14,9 @@ Including another URLconf 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin -from django.urls import path +from django.urls import include, path urlpatterns = [ + path('', include('manager.urls')), path('admin/', admin.site.urls), ]