From 4f34b5fb9673fd9286019c6791299fcf93717851 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sat, 20 Apr 2024 23:22:34 +0200 Subject: [PATCH] Split to modules --- .gitignore | 1 + TODO | 2 ++ example/__init__.py | 10 ++++++++++ example/example_org.py | 17 +++++++++++++++++ nsc/__init__.py | 3 +++ nsc.py => nsc/core.py | 38 +++----------------------------------- test.py | 7 +++++++ 7 files changed, 43 insertions(+), 35 deletions(-) create mode 100644 .gitignore create mode 100644 TODO create mode 100644 example/__init__.py create mode 100644 example/example_org.py create mode 100755 nsc/__init__.py rename nsc.py => nsc/core.py (94%) mode change 100755 => 100644 create mode 100755 test.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bee8a64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ diff --git a/TODO b/TODO new file mode 100644 index 0000000..47dcca2 --- /dev/null +++ b/TODO @@ -0,0 +1,2 @@ +- Names with dots +- Classless reverse delegation diff --git a/example/__init__.py b/example/__init__.py new file mode 100644 index 0000000..20e7905 --- /dev/null +++ b/example/__init__.py @@ -0,0 +1,10 @@ +from nsc import Nsc + +nsc = Nsc( + admin_email='admin@example.org', + origin_server='ns.example.org', +) + +for rev in ['10.1.0.0/16', '10.2.0.0/16', 'fd12:3456:789a::/48']: + rz = nsc.add_reverse_zone(rev) + rz[""].NS(['ns1.example.org', 'ns2.example.org']) diff --git a/example/example_org.py b/example/example_org.py new file mode 100644 index 0000000..dfd95dd --- /dev/null +++ b/example/example_org.py @@ -0,0 +1,17 @@ +from example import nsc + +z = nsc.add_zone('example.org') + +(z[""] + .NS(['ns1', 'ns2']) + .MX(0, 'mail') + .MX(10, 'mail.example.net') + .TXT('Litera scripta manet')) + +z.host('ns1', '10.1.0.1', 'fd12:3456:789a:1::1') +z.host('ns2', '10.2.0.1', 'fd12:3456:789a:2::1') + +(z['mail'] + .A('10.1.0.2', 'fd12:3456:789a:1::2') + .MX(0, 'mail') + .MX(10, 'mail.example.net')) diff --git a/nsc/__init__.py b/nsc/__init__.py new file mode 100755 index 0000000..e5376d2 --- /dev/null +++ b/nsc/__init__.py @@ -0,0 +1,3 @@ +from nsc.core import Nsc, NscZone, NscZoneConfig, NscNode + +pass diff --git a/nsc.py b/nsc/core.py old mode 100755 new mode 100644 similarity index 94% rename from nsc.py rename to nsc/core.py index 2a01f23..86f56ae --- a/nsc.py +++ b/nsc/core.py @@ -1,7 +1,4 @@ -#!/usr/bin/env python3 - from collections import defaultdict -from dataclasses import dataclass from datetime import timedelta import dns.name from dns.name import Name @@ -19,7 +16,6 @@ import dns.rdtypes.IN.AAAA from dns.zone import Zone from ipaddress import ip_address, IPv4Address, IPv6Address, ip_network, IPv4Network, IPv6Network import socket -import sys from typing import Optional, Dict, List, Self, Tuple, DefaultDict @@ -295,34 +291,6 @@ class Nsc: for ptr_to in ptr_list: z._add_ipv6_reverse(addr6, ptr_to) - -c = Nsc( - admin_email='admin@ucw.cz', - origin_server='ns.ucw.cz', -) - -z = c.add_zone('ucw.cz') - -z[""].NS(['jabberwock', 'chirigo.gebbeth.cz', 'drak.ucw.cz']) - -z['jabberwock'].A('1.2.3.4', '2a00:da80:fff0:2::2', '195.113.31.123') - -z.host('test', '1.2.3.4', ['5.6.7.8', '8.7.6.5']) - -(z['mnau'] - .A('195.113.31.123') - .MX(0, 'jabberwock') - .ttl(minutes=15) - .TXT('hey?') - .generic('HINFO', 'Something fishy')) - -z.dump() - -r = c.add_reverse_zone('195.113.0.0/16') -r2 = c.add_reverse_zone('2a00:da80:fff0:2::/64') - -c.dump_reverse() -c.fill_reverse() - -r.dump() -r2.dump() + def dump(self) -> None: + for z in self.zones.values(): + z.dump() diff --git a/test.py b/test.py new file mode 100755 index 0000000..d6ea688 --- /dev/null +++ b/test.py @@ -0,0 +1,7 @@ +#!/usr/bin/env python3 + +from example import nsc +import example.example_org + +nsc.fill_reverse() +nsc.dump() -- 2.39.2