From 94cc3ba2a67e5f5a23b25a7c46597f7ae00e23d0 Mon Sep 17 00:00:00 2001 From: altf4arnold Date: Mon, 12 Feb 2024 21:16:22 +0100 Subject: [PATCH 1/4] not yet working --- requirements.in | 2 ++ requirements.txt | 6 ++++- tldtest/settings.py | 1 + tldtester/admin.py | 16 +++++++++++ tldtester/migrations/0001_initial.py | 27 +++++++++++++++++++ tldtester/migrations/0002_zonecontent.py | 24 +++++++++++++++++ ...zonecontent_tldtester_z_name_83c518_idx.py | 17 ++++++++++++ .../0004_zonecontent_lastedition.py | 18 +++++++++++++ .../migrations/0005_alter_zonecontent_ttl.py | 18 +++++++++++++ tldtester/models.py | 15 +++++++++++ tldtester/sorter.py | 20 ++++++++++++-- 11 files changed, 161 insertions(+), 3 deletions(-) create mode 100644 tldtester/migrations/0001_initial.py create mode 100644 tldtester/migrations/0002_zonecontent.py create mode 100644 tldtester/migrations/0003_zonecontent_tldtester_z_name_83c518_idx.py create mode 100644 tldtester/migrations/0004_zonecontent_lastedition.py create mode 100644 tldtester/migrations/0005_alter_zonecontent_ttl.py diff --git a/requirements.in b/requirements.in index 20fc630..6d91126 100644 --- a/requirements.in +++ b/requirements.in @@ -2,6 +2,7 @@ asgiref==3.7.2 build==1.0.3 click==8.1.7 Django==5.0.2 +django-admin-extra-buttons==1.5.7 flake8==7.0.0 mccabe==0.7.0 packaging==23.2 @@ -10,3 +11,4 @@ pycodestyle==2.11.1 pyflakes==3.2.0 pyproject_hooks==1.0.0 sqlparse==0.4.4 +zonefile-parser==0.1.14 diff --git a/requirements.txt b/requirements.txt index 64e9e7d..895a8b8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ # -# This file is autogenerated by pip-compile with Python 3.11 +# This file is autogenerated by pip-compile with Python 3.12 # by the following command: # # pip-compile --output-file=requirements.txt requirements.in @@ -18,6 +18,8 @@ click==8.1.7 # pip-tools django==5.0.2 # via -r requirements.in +django-admin-extra-buttons==1.5.7 + # via -r requirements.in flake8==7.0.0 # via -r requirements.in mccabe==0.7.0 @@ -48,6 +50,8 @@ sqlparse==0.4.4 # django wheel==0.42.0 # via pip-tools +zonefile-parser==0.1.14 + # via -r requirements.in # The following packages are considered to be unsafe in a requirements file: # pip diff --git a/tldtest/settings.py b/tldtest/settings.py index d32ecf8..84ba062 100644 --- a/tldtest/settings.py +++ b/tldtest/settings.py @@ -37,6 +37,7 @@ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'admin_extra_buttons', 'tldtester', ] diff --git a/tldtester/admin.py b/tldtester/admin.py index 40e6f25..fb6c791 100644 --- a/tldtester/admin.py +++ b/tldtester/admin.py @@ -1,9 +1,25 @@ +from admin_extra_buttons.api import ExtraButtonsMixin, button +from admin_extra_buttons.utils import HttpResponseRedirectToReferrer from django.contrib import admin from .models import TLD +from .models import zonecontent +import tldtester.sorter as sorter class tlds(admin.ModelAdmin): list_display = ('tld', 'inet', 'dnssec', 'lastEdition') +class zone(ExtraButtonsMixin, admin.ModelAdmin): + list_display = ('name', 'rtype', 'rclass', 'ttl', 'data', 'lastEdition') + + @button(change_form=True, html_attrs={'style': 'background-color:#88FF88;color:black'}) + def refresh(self, request): + self.message_user(request, 'refresh called') + sorter.main() + # Optional: returns HttpResponse + return HttpResponseRedirectToReferrer(request) + + admin.site.register(TLD, tlds) +admin.site.register(zonecontent,zone) diff --git a/tldtester/migrations/0001_initial.py b/tldtester/migrations/0001_initial.py new file mode 100644 index 0000000..9e10584 --- /dev/null +++ b/tldtester/migrations/0001_initial.py @@ -0,0 +1,27 @@ +# Generated by Django 5.0.2 on 2024-02-12 16:08 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='TLD', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('tld', models.CharField(max_length=30)), + ('dnssec', models.IntegerField(choices=[(0, 'Delete DS'), (1, 'RSA/MD5'), (2, 'Diffie-Hellman'), (3, 'DSA/SHA1'), (5, 'RSA/SHA-1'), (6, 'DSA-NSEC3-SHA1'), (7, 'RSASHA1-NSEC3-SHA1'), (8, 'RSA/SHA-256'), (10, 'RSA/SHA-512'), (12, 'GOST R 34.10-2001'), (13, 'ECDSA Curve P-256 with SHA-256'), (14, 'ECDSA Curve P-384 with SHA-384'), (15, 'Ed25519'), (16, 'Ed448'), (17, 'SM2 signing algorithm with SM3 hashing algorithm'), (23, 'GOST R 34.10-2012'), (252, 'Reserved for Indirect Keys'), (253, 'private algorithm'), (254, 'private algorithm OID'), (300, 'Unknown')], default=300)), + ('inet', models.IntegerField(choices=[(0, 'IPv4'), (1, 'IPv6'), (2, 'IPv4 + IPv6')], default=0)), + ('lastEdition', models.DateTimeField(auto_now=True)), + ], + options={ + 'indexes': [models.Index(fields=['tld'], name='tldtester_t_tld_b4cdc5_idx'), models.Index(fields=['dnssec'], name='tldtester_t_dnssec_694343_idx'), models.Index(fields=['inet'], name='tldtester_t_inet_b6132b_idx')], + }, + ), + ] diff --git a/tldtester/migrations/0002_zonecontent.py b/tldtester/migrations/0002_zonecontent.py new file mode 100644 index 0000000..f2fce85 --- /dev/null +++ b/tldtester/migrations/0002_zonecontent.py @@ -0,0 +1,24 @@ +# Generated by Django 5.0.2 on 2024-02-12 19:45 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('tldtester', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='zonecontent', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('rtype', models.CharField(max_length=10)), + ('name', models.CharField(max_length=100)), + ('rclass', models.CharField(max_length=10)), + ('ttl', models.IntegerField(default=0)), + ('data', models.CharField(max_length=1000)), + ], + ), + ] diff --git a/tldtester/migrations/0003_zonecontent_tldtester_z_name_83c518_idx.py b/tldtester/migrations/0003_zonecontent_tldtester_z_name_83c518_idx.py new file mode 100644 index 0000000..d0eccce --- /dev/null +++ b/tldtester/migrations/0003_zonecontent_tldtester_z_name_83c518_idx.py @@ -0,0 +1,17 @@ +# Generated by Django 5.0.2 on 2024-02-12 19:46 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('tldtester', '0002_zonecontent'), + ] + + operations = [ + migrations.AddIndex( + model_name='zonecontent', + index=models.Index(fields=['name'], name='tldtester_z_name_83c518_idx'), + ), + ] diff --git a/tldtester/migrations/0004_zonecontent_lastedition.py b/tldtester/migrations/0004_zonecontent_lastedition.py new file mode 100644 index 0000000..5148710 --- /dev/null +++ b/tldtester/migrations/0004_zonecontent_lastedition.py @@ -0,0 +1,18 @@ +# Generated by Django 5.0.2 on 2024-02-12 19:48 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('tldtester', '0003_zonecontent_tldtester_z_name_83c518_idx'), + ] + + operations = [ + migrations.AddField( + model_name='zonecontent', + name='lastEdition', + field=models.DateTimeField(auto_now=True), + ), + ] diff --git a/tldtester/migrations/0005_alter_zonecontent_ttl.py b/tldtester/migrations/0005_alter_zonecontent_ttl.py new file mode 100644 index 0000000..7cd4923 --- /dev/null +++ b/tldtester/migrations/0005_alter_zonecontent_ttl.py @@ -0,0 +1,18 @@ +# Generated by Django 5.0.2 on 2024-02-12 20:04 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('tldtester', '0004_zonecontent_lastedition'), + ] + + operations = [ + migrations.AlterField( + model_name='zonecontent', + name='ttl', + field=models.CharField(max_length=5), + ), + ] diff --git a/tldtester/models.py b/tldtester/models.py index 69dcb51..faa7ff1 100644 --- a/tldtester/models.py +++ b/tldtester/models.py @@ -43,3 +43,18 @@ class Meta: models.Index(fields=["dnssec"]), models.Index(fields=["inet"]), ] + +class zonecontent(models.Model): + rtype = models.CharField(max_length=10) + name = models.CharField(max_length=100) + rclass = models.CharField(max_length=10) + ttl = models.CharField(max_length=5) + data = models.CharField(max_length=1000) + lastEdition = models.DateTimeField(auto_now=True) + def __str__(self): + return self.zonecontent + + class Meta: + indexes = [ + models.Index(fields=["name"]), + ] \ No newline at end of file diff --git a/tldtester/sorter.py b/tldtester/sorter.py index 135b630..0d53693 100644 --- a/tldtester/sorter.py +++ b/tldtester/sorter.py @@ -3,6 +3,8 @@ Link to IANA website : https://www.internic.net/domain/root.zone """ import urllib.request +import zonefile_parser +from tldtester.models import zonecontent def downloader(): @@ -23,12 +25,26 @@ def sorter(rawdata): This file removes the tabs and line breaks from rawdata returns as a list with dictionary in it """ - print(str(rawdata)) + encodeddata = zonefile_parser.parse(rawdata) + return encodeddata +def dbwriter(data): + """ + Writes everything in the Zone database + """ + for line in data: + print(line) + DB=zonecontent() + DB.rtype = line["rtype"] + DB.name = line["name"] + DB.rclass = line["rclass"] + DB.data = line["rdata"] + DB.ttl = int(line["ttl"]) + DB.save() def main(): try: - sorter(downloader()) + dbwriter(sorter(downloader())) except Exception as e: print(e) From 943a011e4ad4722dc924f7244b4e4a04f2a7b9c8 Mon Sep 17 00:00:00 2001 From: altf4arnold Date: Thu, 15 Feb 2024 16:47:34 +0100 Subject: [PATCH 2/4] fixes bug on showing the internal database --- tldtester/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tldtester/models.py b/tldtester/models.py index faa7ff1..e7bbea9 100644 --- a/tldtester/models.py +++ b/tldtester/models.py @@ -52,7 +52,7 @@ class zonecontent(models.Model): data = models.CharField(max_length=1000) lastEdition = models.DateTimeField(auto_now=True) def __str__(self): - return self.zonecontent + return self.name class Meta: indexes = [ From 9b1e36dc8128021aa1d6e0d40014bbd95180498e Mon Sep 17 00:00:00 2001 From: altf4arnold Date: Thu, 15 Feb 2024 16:48:49 +0100 Subject: [PATCH 3/4] Needs to do NS records and resolve value problems. Otherwise, mostly works --- tldtester/sorter.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tldtester/sorter.py b/tldtester/sorter.py index 0d53693..b46b11b 100644 --- a/tldtester/sorter.py +++ b/tldtester/sorter.py @@ -4,6 +4,7 @@ """ import urllib.request import zonefile_parser +import json from tldtester.models import zonecontent @@ -24,16 +25,19 @@ def sorter(rawdata): """ This file removes the tabs and line breaks from rawdata returns as a list with dictionary in it + :returns: a list of dictionaries """ encodeddata = zonefile_parser.parse(rawdata) - return encodeddata + properdata = [] + for line in encodeddata: + properdata.append(dict(json.loads(str(line).replace("'",'"')))) + return properdata def dbwriter(data): """ Writes everything in the Zone database """ for line in data: - print(line) DB=zonecontent() DB.rtype = line["rtype"] DB.name = line["name"] From cc621e265392648ea716b9649473ebcd36ac5285 Mon Sep 17 00:00:00 2001 From: altf4arnold Date: Thu, 15 Feb 2024 18:09:45 +0100 Subject: [PATCH 4/4] flake --- tldtester/admin.py | 2 +- tldtester/models.py | 4 +++- tldtester/sorter.py | 6 ++++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/tldtester/admin.py b/tldtester/admin.py index fb6c791..64214a5 100644 --- a/tldtester/admin.py +++ b/tldtester/admin.py @@ -22,4 +22,4 @@ def refresh(self, request): admin.site.register(TLD, tlds) -admin.site.register(zonecontent,zone) +admin.site.register(zonecontent, zone) diff --git a/tldtester/models.py b/tldtester/models.py index e7bbea9..fceaa9c 100644 --- a/tldtester/models.py +++ b/tldtester/models.py @@ -44,6 +44,7 @@ class Meta: models.Index(fields=["inet"]), ] + class zonecontent(models.Model): rtype = models.CharField(max_length=10) name = models.CharField(max_length=100) @@ -51,10 +52,11 @@ class zonecontent(models.Model): ttl = models.CharField(max_length=5) data = models.CharField(max_length=1000) lastEdition = models.DateTimeField(auto_now=True) + def __str__(self): return self.name class Meta: indexes = [ models.Index(fields=["name"]), - ] \ No newline at end of file + ] diff --git a/tldtester/sorter.py b/tldtester/sorter.py index b46b11b..2970d27 100644 --- a/tldtester/sorter.py +++ b/tldtester/sorter.py @@ -30,15 +30,16 @@ def sorter(rawdata): encodeddata = zonefile_parser.parse(rawdata) properdata = [] for line in encodeddata: - properdata.append(dict(json.loads(str(line).replace("'",'"')))) + properdata.append(dict(json.loads(str(line).replace("'", '"')))) return properdata + def dbwriter(data): """ Writes everything in the Zone database """ for line in data: - DB=zonecontent() + DB = zonecontent() DB.rtype = line["rtype"] DB.name = line["name"] DB.rclass = line["rclass"] @@ -46,6 +47,7 @@ def dbwriter(data): DB.ttl = int(line["ttl"]) DB.save() + def main(): try: dbwriter(sorter(downloader()))