diff --git a/kjg/IPv6/ula/ula/settings.py b/kjg/IPv6/ula/ula/settings.py index fd9826f..6a2ed8c 100644 --- a/kjg/IPv6/ula/ula/settings.py +++ b/kjg/IPv6/ula/ula/settings.py @@ -25,7 +25,7 @@ SECRET_KEY = 'fj+%jfo4xd5rb(7w!7feda03q)y4md)*)e_rjw8i_gyab!6#-h' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True -ALLOWED_HOSTS = ["192.168.0.12",] +ALLOWED_HOSTS = ["192.168.0.12", "testjg.django.lab.ungleich.ch", ] # Application definition diff --git a/kjg/IPv6/ula/ularegistry/forms.py b/kjg/IPv6/ula/ularegistry/forms.py index e166b86..6a01ebb 100644 --- a/kjg/IPv6/ula/ularegistry/forms.py +++ b/kjg/IPv6/ula/ularegistry/forms.py @@ -4,3 +4,13 @@ from netfields import CidrAddressField, NetManager class ip(forms.Form): inputIP = forms.CharField() #inputIP = CidrAddressField() + +class ip_random(forms.Form): + inputNetwork = forms.CharField() + NetworkName = forms.CharField() + NetworkDescription = forms.CharField() + +class new_ip_random(forms.Form): + NetworkName = forms.CharField() + NetworkDescription = forms.CharField() + diff --git a/kjg/IPv6/ula/ularegistry/migrations/0002_ips2.py b/kjg/IPv6/ula/ularegistry/migrations/0002_ips2.py new file mode 100644 index 0000000..a653443 --- /dev/null +++ b/kjg/IPv6/ula/ularegistry/migrations/0002_ips2.py @@ -0,0 +1,23 @@ +# Generated by Django 3.0.8 on 2020-08-03 14:30 + +from django.db import migrations, models +import netfields.fields + + +class Migration(migrations.Migration): + + dependencies = [ + ('ularegistry', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='ips2', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('inet2', netfields.fields.CidrAddressField(max_length=43)), + ('inet2_name', models.CharField(max_length=200)), + ('inet2_descrip', models.CharField(max_length=200)), + ], + ), + ] diff --git a/kjg/IPv6/ula/ularegistry/models.py b/kjg/IPv6/ula/ularegistry/models.py index 9e47b7c..058a0de 100644 --- a/kjg/IPv6/ula/ularegistry/models.py +++ b/kjg/IPv6/ula/ularegistry/models.py @@ -10,4 +10,8 @@ class ips(models.Model): st = str(self.inet) return st - +class ips2(models.Model): + inet2 = CidrAddressField() + inet2_name = models.CharField(max_length=200) + inet2_descrip = models.CharField(max_length=200) + diff --git a/kjg/IPv6/ula/ularegistry/randomcreate.py b/kjg/IPv6/ula/ularegistry/randomcreate.py new file mode 100644 index 0000000..7b39869 --- /dev/null +++ b/kjg/IPv6/ula/ularegistry/randomcreate.py @@ -0,0 +1,29 @@ +from sys import argv +import argparse +import ipaddress +import random + +def createIP(ip): + ipnet = ip + size = 80 + + ip = ipnet.split('/') + ip2 = ipaddress.ip_network(ip[0]).supernet(new_prefix=int(ip[1])) + + # minimum network + minNet = ipaddress.ip_network(ip[0]).supernet(new_prefix=128-size) + + # seperate network + ip2first = ipaddress.IPv6Network(ip2)[0] + ip2last = ipaddress.IPv6Network(ip2)[-1] + minNetlast = ipaddress.IPv6Network(minNet)[-1] + + # calculation network + maxRan = int(ip2last) - int(minNetlast) >> size + result = ipaddress.ip_address((random.randrange(0,maxRan) << size) + int(ip2first) ) + resultNetwork = ipaddress.ip_network(result).supernet(new_prefix=128-size) + + print(resultNetwork) + return resultNetwork + + diff --git a/kjg/IPv6/ula/ularegistry/templates/base.html b/kjg/IPv6/ula/ularegistry/templates/base.html new file mode 100644 index 0000000..937752c --- /dev/null +++ b/kjg/IPv6/ula/ularegistry/templates/base.html @@ -0,0 +1,24 @@ + + + + + + + Document + + + + + {% if user.is_authenticated %} +

{{ user.username }} welcome

+ logout + {% else %} + signup + login + {% endif %} + + + {% block container %} + {% endblock %} + + diff --git a/kjg/IPv6/ula/ularegistry/templates/login.html b/kjg/IPv6/ula/ularegistry/templates/login.html new file mode 100644 index 0000000..3482225 --- /dev/null +++ b/kjg/IPv6/ula/ularegistry/templates/login.html @@ -0,0 +1,11 @@ +{% extends 'base.html' %} +{% block container %} +

login

+ +
+ {% csrf_token %} + + {{ login_form }} + +
+{% endblock %} diff --git a/kjg/IPv6/ula/ularegistry/templates/newrandomIP.html b/kjg/IPv6/ula/ularegistry/templates/newrandomIP.html new file mode 100644 index 0000000..4afc020 --- /dev/null +++ b/kjg/IPv6/ula/ularegistry/templates/newrandomIP.html @@ -0,0 +1,19 @@ +{% extends 'base.html' %} +{% block container %} +
+ {% csrf_token %} + + {{ form2 }} +
+ +
+{% if ips_listr %} + +{% else %} +

No registered IP.

+{% endif %} +{% endblock %} diff --git a/kjg/IPv6/ula/ularegistry/templates/randomIP.html b/kjg/IPv6/ula/ularegistry/templates/randomIP.html new file mode 100644 index 0000000..58079b3 --- /dev/null +++ b/kjg/IPv6/ula/ularegistry/templates/randomIP.html @@ -0,0 +1,16 @@ +
+ {% csrf_token %} + + {{ form }} +
+ +
+{% if ips_listr %} + +{% else %} +

No registered IP.

+{% endif %} diff --git a/kjg/IPv6/ula/ularegistry/templates/signup.html b/kjg/IPv6/ula/ularegistry/templates/signup.html new file mode 100644 index 0000000..77c7932 --- /dev/null +++ b/kjg/IPv6/ula/ularegistry/templates/signup.html @@ -0,0 +1,11 @@ +{% extends 'base.html' %} +{% block container %} +

sign-up

+ +
+ {% csrf_token %} + {{ signup_form }} + +
+ +{% endblock %} diff --git a/kjg/IPv6/ula/ularegistry/urls.py b/kjg/IPv6/ula/ularegistry/urls.py index 1abf425..d6d270a 100644 --- a/kjg/IPv6/ula/ularegistry/urls.py +++ b/kjg/IPv6/ula/ularegistry/urls.py @@ -4,5 +4,9 @@ from . import views urlpatterns = [ path('', views.index, name='index'), - path('/results/', views.results, name='results'), + path('randomIP', views.randomIP, name='randomIP'), + path('newrandomIP', views.newrandomIP, name='newrandomIP'), + path('login/', views.login, name='login'), + path('logout/', views.logout, name='logout'), + path('signup/', views.signup, name='signup'), ] diff --git a/kjg/IPv6/ula/ularegistry/views.py b/kjg/IPv6/ula/ularegistry/views.py index c801d2f..47b45f8 100644 --- a/kjg/IPv6/ula/ularegistry/views.py +++ b/kjg/IPv6/ula/ularegistry/views.py @@ -1,9 +1,14 @@ from django.shortcuts import render from django.http import HttpResponse -from .forms import ip -from ularegistry.models import ips +from .forms import ip_random, new_ip_random, ip +from ularegistry.models import ips, ips2 from django.shortcuts import redirect from ularegistry.checkip import * +from ularegistry.randomcreate import * +from django.shortcuts import render, redirect +from django.contrib.auth.forms import UserCreationForm, AuthenticationForm +from django.contrib.auth import login as auth_login +from django.contrib.auth import logout as auth_logout # Create your views here. def index(request): @@ -42,7 +47,126 @@ def index(request): return render(request, 'index.html', ctx) #return HttpResponse("Hello, world.") +def randomIP(request): + ips_listr = ips2.objects.values() + if request.method == "POST": + form = ip_random(request.POST) + print(form) + if form.is_valid(): + while True: + newIP = createIP(form.cleaned_data['inputNetwork']) + ips_listr2 = list(ips_listr) + if not ips_listr2: + print("test") + q = ips2(inet2=newIP, inet2_name=form.cleaned_data['NetworkName'], inet2_descrip=form.cleaned_data['NetworkDescription']) + q.save() + break + + overlapchk = 0 + + for value in ips_listr2: + if overlapip(newIP,value['inet2']): + overlapchk = 1 + print(overlapchk) + q = ips2(inet2=newIP, inet2_name=form.cleaned_data['NetworkName'], inet2_descrip=form.cleaned_data['NetworkDescription']) + q.save() + break + else: + overlapchk = 0 + + if overlapchk: + break + + return redirect('randomIP') -def results(request, ips_id): - return HttpResponse(ips_id) + else: + form = ip_random() + ctx = { 'form' : form, + 'ips_listr' : ips_listr, + } + return render(request, 'randomIP.html', ctx) + + +def newrandomIP(request): + ips_listr = ips2.objects.values() + if request.method == "POST": + form2 = new_ip_random(request.POST) + print(form2) + if form2.is_valid(): + while True: + newIP = createIP("fd00::/8") + print("test3") + ips_listr2 = list(ips_listr) + if not ips_listr2: + print("test2") + q = ips2(inet2=newIP, inet2_name=form2.cleaned_data['NetworkName'], inet2_descrip=form2.cleaned_data['NetworkDescription']) + q.save() + break + + overlapchk = 0 + + for value in ips_listr2: + if overlapip(newIP,value['inet2']): + overlapchk = 1 + print(overlapchk) + q = ips2(inet2=newIP, inet2_name=form2.cleaned_data['NetworkName'], inet2_descrip=form2.cleaned_data['NetworkDescription']) + q.save() + break + else: + overlapchk = 0 + + if overlapchk: + break + + return redirect('newrandomIP') + + else: + form2 = new_ip_random() + + ctx2 = { 'form2' : form2, + 'ips_listr' : ips_listr, + } + return render(request, 'newrandomIP.html', ctx2) + + +def signup(request): + if request.user.is_authenticated: + return redirect('newrandomIP') + + if request.method == 'POST': + signup_form = UserCreationForm(request.POST) + if signup_form.is_valid(): + user = signup_form.save() + auth_login(request, user) + return redirect('login') + else: + print("fail singup") + + else: + signup_form = UserCreationForm() + + return render(request, 'signup.html', {'signup_form':signup_form}) + + +def login(request): + if request.user.is_authenticated: + return redirect('newrandomIP') + + if request.method == 'POST': + login_form = AuthenticationForm(request, request.POST) + if login_form.is_valid(): + auth_login(request, login_form.get_user()) + return redirect('newrandomIP') + else: + print("fail login") + + else: + login_form = AuthenticationForm() + + return render(request, 'login.html', {'login_form' : login_form}) + + +def logout(request): + auth_logout(request) + return redirect('login')