#!/usr/bin/env python3
"""Extract (domain, ip) pairs from masscan TLS cert banners -> /root/recon/dom_ip.tsv"""
import re

SRC = "/root/recon/scan_p443.list"
DST = "/root/recon/dom_ip.tsv"
name_re = re.compile(r"^(?:\*\.|[a-z0-9-]+\.)+[a-z]{2,63}$")

n = 0
with open(SRC, errors="replace") as src, open(DST, "w") as out:
    for line in src:
        if " ssl TLS/" not in line:
            continue
        parts = line.split()
        ip = parts[3]
        i = line.find("cipher:")
        if i < 0:
            continue
        j = line.find(", ", i)
        if j < 0:
            continue
        for tok in line[j + 2:].strip().split(", "):
            tok = tok.strip().lower().rstrip(".")
            if name_re.match(tok):
                out.write(f"{tok}\t{ip}\n")
                n += 1
print(f"dom_ip pairs: {n}")
