Created
October 29, 2018 11:56
-
-
Save oskar456/8de0129e8b4a3602793408c9cb25d6b1 to your computer and use it in GitHub Desktop.
Convert RIPE DB domain dump into Fred-akm data file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import gzip | |
import hashlib | |
import sys | |
from collections import defaultdict | |
def read_db_dump(fname="ripe.db.domain.gz"): | |
obj = None | |
with gzip.open(fname) as inf: | |
for line in inf: | |
comindex = line.find(b"#") | |
if comindex > 0: | |
line = line[:comindex] | |
if line.strip() == b"": | |
if obj: | |
yield obj | |
obj = defaultdict(list) | |
obj["domain"] = "" | |
if b":" not in line: | |
continue | |
k, v = line.split(b":", 1) | |
k = k.decode("ascii") | |
v = v.strip() | |
if k in ["domain", "nserver", "ds-rdata"]: | |
v = v.decode("ascii") | |
if k == "domain": | |
obj[k] = v | |
else: | |
obj[k].append(v) | |
if obj: | |
yield obj | |
def hash_domain_id(domain): | |
md5 = hashlib.md5(domain.encode("ascii")).digest() | |
return str(int.from_bytes(md5, "little") & 0xffffff) | |
def get_fred_akm_config(objects): | |
for o in objects: | |
line = [hash_domain_id(o["domain"]), o["domain"]] | |
if "ds-rdata" in o: | |
line.append("secure-noauto") | |
else: | |
line.append("insecure") | |
for ns in o["nserver"]: | |
yield " ".join([ns, *line]) | |
def main(): | |
for l in get_fred_akm_config(read_db_dump()): | |
print(l) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment