1
0
Fork 0
mirror of https://github.com/NeoCloud/NeoNetwork synced 2024-11-14 11:22:23 +08:00
NeoNetwork/scripts/rfc2317.py

45 lines
1.2 KiB
Python
Raw Normal View History

2020-09-23 14:41:36 +08:00
#!/usr/bin/env python3
import ipaddress
2021-06-08 08:26:26 +08:00
ZONE = ".127.10.in-addr.arpa"
2020-09-23 14:41:36 +08:00
def truncate(rev: str) -> str:
assert rev.endswith(ZONE)
2021-06-08 08:26:26 +08:00
rev = rev[: -len(ZONE)]
2020-09-23 14:41:36 +08:00
return rev
2021-06-08 08:26:26 +08:00
2021-04-14 20:15:56 +08:00
def gen_reverse_pointers(network: str, ns: list, ds: list = [], ttl: int = -1) -> list:
ttl = f"{ttl} " if 900 <= ttl <= 86400 else ""
2020-09-23 14:41:36 +08:00
buf = list()
net = ipaddress.IPv4Network(network, strict=True)
assert net.prefixlen > 24
netrev = truncate(net.reverse_pointer)
for _ns in ns:
2021-04-14 20:15:56 +08:00
buf.append(f"{netrev} {ttl}IN NS {_ns}")
for _ds in ds:
buf.append(f"{netrev} {ttl}IN DS {_ds}")
2020-09-23 14:41:36 +08:00
for addr in net:
cnamefr = truncate(addr.reverse_pointer)
cnameto = f"{int.from_bytes(addr.packed, byteorder='big', signed=False) & 0xff}.{netrev}"
2021-04-14 20:15:56 +08:00
buf.append(f"{cnamefr} {ttl}IN CNAME {cnameto}")
2020-09-23 14:41:36 +08:00
return buf
2021-06-08 08:26:26 +08:00
2020-09-23 14:41:36 +08:00
if __name__ == "__main__":
2021-06-08 08:26:26 +08:00
print(
"\n".join(
gen_reverse_pointers(
"10.127.8.64/26",
["ns1.jerry.neo."],
[
"18792 13 2 2F335456EEE70FC4833886E5EEDC28E7195E90E2A337860B3E805D5EB9F3A804"
],
ttl=1500,
)
)
)