import asyncio
import re

import aiohttp

SRC = "/root/recon/ivanti_real.txt"
OUT = "/root/recon/ivanti_fp.tsv"

YEAR = re.compile(r"(?:Copyright|&copy;|©)[^0-9]{0,20}(20[0-9]{2})", re.I)
DS = re.compile(r"ds_([a-f0-9]{20,64})\.(?:js|css)")
TITLE = re.compile(r"<title>([^<]{0,80})", re.I)
CONC = 60


async def one(sess, ip):
    url = "https://%s/dana-na/auth/url_default/welcome.cgi" % ip
    try:
        async with sess.get(url, ssl=False, allow_redirects=True) as r:
            body = await r.read()
            txt = body.decode("utf-8", "ignore")
            y = YEAR.search(txt)
            d = DS.search(txt)
            t = TITLE.search(txt)
            return "%s\t%d\t%s\t%s\t%s\t%d\n" % (
                ip, r.status,
                y.group(1) if y else "",
                (d.group(1)[:16] if d else ""),
                (t.group(1).strip().replace("\t", " ") if t else ""),
                len(body),
            )
    except Exception as e:
        return "%s\tERR\t%s\t\t\t0\n" % (ip, type(e).__name__)


async def main():
    ips = [l.strip() for l in open(SRC) if l.strip()]
    sem = asyncio.Semaphore(CONC)
    out = open(OUT, "w")

    async def run(sess, ip):
        async with sem:
            line = await one(sess, ip)
            out.write(line)

    conn = aiohttp.TCPConnector(limit=0, ssl=False, force_close=True)
    to = aiohttp.ClientTimeout(total=15, connect=6, sock_read=10)
    async with aiohttp.ClientSession(timeout=to, connector=conn,
                                     headers={"User-Agent": "Mozilla/5.0"}) as sess:
        await asyncio.gather(*[run(sess, ip) for ip in ips])
    out.close()
    print("done", len(ips))


asyncio.run(main())
