1
0
Fork 0
mirror of https://github.com/NeoCloud/NeoNetwork synced 2024-11-29 14:40:40 +08:00
NeoNetwork/scripts/dns-reverse-generator.py
2020-09-23 14:41:36 +08:00

57 lines
1.7 KiB
Python
Executable file

#!/usr/bin/env python3
import sys
from ipaddress import IPv4Address, ip_network, IPv4Network
from pathlib import Path
import toml
from rfc2317 import gen_reverse_pointers
RESOLVE_FILE = Path("dns", "db.10.127")
ROUTE_FILE = Path("route")
RFC2317_FILE = Path("dns", "rfc2317.toml")
def iter_route(route_type: str):
items = []
for f in ROUTE_FILE.iterdir():
routes = toml.loads(f.read_text())
items.extend(
(entity["name"], ip_network(route).network_address)
for route, entity in routes.items()
if entity["type"] == route_type and isinstance(ip_network(route), IPv4Network)
)
routes = []
remove = []
for item in items[::-1]:
if item[1] in routes:
remove.append(item)
else:
routes.append(item[1])
for i in remove:
items.remove(i)
return sorted(items, key=lambda item: item[1])
def iter_rfc2317_entry():
entries = toml.loads(RFC2317_FILE.read_text())
for (route, attributes) in entries.items():
ns = attributes.get('ns')
yield(route, ns)
def main():
orignal = RESOLVE_FILE.read_text()
orignal = orignal[: orignal.find("\n; AUTOGENERATED")]
records = [orignal, "; AUTOGENERATED", "", "; Loopback Addresses"]
for name, address in iter_route("loopback"):
if isinstance(address, IPv4Address):
pointer = address.reverse_pointer.replace(".127.10.in-addr.arpa", "")
records.append("%s\tIN\tPTR\t%s.neo." % (pointer, name))
records.extend(("", "; rfc2317"))
for route, ns in iter_rfc2317_entry():
records.extend(gen_reverse_pointers(route, ns))
records.append("")
RESOLVE_FILE.write_text("\n".join(records))
if __name__ == "__main__":
main()