import asyncio, re, ssl
import aiohttp

IPS = []
for line in open("/root/recon/bdui_full.tsv"):
    p = line.rstrip("\n").split("\t")
    if p and p[0]:
        IPS.append(p[0])
print("ips", len(IPS))

UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"}
PATHS = ["/cgi-mod/index.cgi", "/", "/cgi-mod/header_logo.cgi"]


async def probe(sess, ip):
    rec = {"ip": ip}
    for scheme in ("https", "http"):
        for path in PATHS:
            try:
                async with sess.get(f"{scheme}://{ip}{path}", ssl=False,
                                    allow_redirects=True, max_redirects=3) as r:
                    body = (await r.content.read(60000)).decode("utf-8", "replace")
                    srv = r.headers.get("Server", "")
                    rec.update(scheme=scheme, path=path, st=r.status, srv=srv)
                    m = re.search(r"(?is)<title[^>]*>(.*?)</title>", body)
                    rec["title"] = re.sub(r"\s+", " ", m.group(1)).strip()[:60] if m else ""
                    low = body.lower()
                    rec["esg"] = "email security gateway" in low or "ess" in low and "barracuda" in low
                    rec["cgf"] = "cloudgen" in low or "firewall" in low
                    vers = set(re.findall(r"9\.[0-9]\.[0-9]\.[0-9]{3}", body))
                    vers |= set(re.findall(r'(?i)version["\s:=]+([0-9][0-9.]+)', body))
                    rec["vers"] = sorted(vers)[:5]
                    if r.status == 200 and body.strip():
                        return rec
            except Exception:
                continue
    return rec


async def main():
    tout = aiohttp.ClientTimeout(total=20, connect=7, sock_read=12)
    sem = asyncio.Semaphore(8)
    async with aiohttp.ClientSession(timeout=tout, headers=UA) as s:
        async def g(ip):
            async with sem:
                return await probe(s, ip)
        res = await asyncio.gather(*[g(ip) for ip in IPS])
    for r in sorted(res, key=lambda x: x["ip"]):
        print(f"{r['ip']:16s} {r.get('st',''):>4} {r.get('srv','')[:22]:22s} "
              f"esg={str(r.get('esg',''))[:5]:5s} cgf={str(r.get('cgf',''))[:5]:5s} "
              f"title={r.get('title','')[:34]:34s} vers={r.get('vers')}")
    open("/root/recon/bdui_class.tsv", "w").write(
        "\n".join("\t".join([r["ip"], str(r.get("st", "")), str(r.get("title", "")),
                             str(r.get("esg", "")), str(r.get("cgf", "")),
                             ",".join(r.get("vers", []))]) for r in res))

asyncio.run(main())
