Check in working hosts file generator

This commit is contained in:
Kumi 2018-11-25 16:05:26 +01:00
parent b415f9fb69
commit 8f3d3570c1
No known key found for this signature in database
GPG key ID: 8D4E139A5FF9082F
15 changed files with 159 additions and 2 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
db.sqlite3
__pycache__
*.pyc

View file

@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
import os
import sys

0
manager/__init__.py Normal file
View file

9
manager/admin.py Normal file
View file

@ -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)

5
manager/apps.py Normal file
View file

@ -0,0 +1,5 @@
from django.apps import AppConfig
class ManagerConfig(AppConfig):
name = 'manager'

View file

@ -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'),
),
]

View file

@ -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'),
),
]

View file

30
manager/models.py Normal file
View file

@ -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)

View file

@ -0,0 +1,3 @@
127.0.0.1 localhost
{{ device.network.intip }} private.vpn360
{{ device.network.extip }} public.vpn360

3
manager/tests.py Normal file
View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

8
manager/urls.py Normal file
View file

@ -0,0 +1,8 @@
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('hosts', views.getHosts, name='hosts')
]

13
manager/views.py Normal file
View file

@ -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})

View file

@ -31,6 +31,7 @@ ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'manager.apps.ManagerConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',

View file

@ -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),
]