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)