1
0
Fork 0
mirror of https://github.com/NeoCloud/NeoNetwork synced 2024-11-15 07:22:23 +08:00
NeoNetwork/scripts/dns-reverse-generator.py

58 lines
1.7 KiB
Python
Raw Normal View History

2020-05-23 19:00:02 +08:00
#!/usr/bin/env python3
import sys
from ipaddress import IPv4Address, ip_network, IPv4Network
2020-05-23 19:00:02 +08:00
from pathlib import Path
import toml
2020-09-23 14:41:36 +08:00
from rfc2317 import gen_reverse_pointers
2020-05-23 19:00:02 +08:00
RESOLVE_FILE = Path("dns", "db.10.127")
ROUTE_FILE = Path("route")
2020-09-23 14:41:36 +08:00
RFC2317_FILE = Path("dns", "rfc2317.toml")
2020-05-23 19:00:02 +08:00
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)
2020-05-23 19:00:02 +08:00
)
2020-09-23 14:41:36 +08:00
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)
2020-05-23 19:00:02 +08:00
return sorted(items, key=lambda item: item[1])
2020-09-23 14:41:36 +08:00
def iter_rfc2317_entry():
entries = toml.loads(RFC2317_FILE.read_text())
for (route, attributes) in entries.items():
ns = attributes.get('ns')
yield(route, ns)
2020-05-23 19:00:02 +08:00
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))
2020-09-23 14:41:36 +08:00
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))
2020-05-23 19:00:02 +08:00
if __name__ == "__main__":
main()