import asyncio, re, ssl, sys

UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36"
REQ = "GET / HTTP/1.1\r\nHost: %s\r\nUser-Agent: %s\r\nAccept: */*\r\nConnection: close\r\n\r\n"
SIGS = ("atlassian", "login.action", "login.jsp", "dashboard.jspa")


def ctx():
    c = ssl.create_default_context()
    c.check_hostname = False
    c.verify_mode = ssl.CERT_NONE
    c.set_ciphers("DEFAULT@SECLEVEL=1")
    return c


CTX = ctx()


async def one(ip, sem):
    async with sem:
        w = None
        try:
            r, w = await asyncio.wait_for(asyncio.open_connection(ip, 443, ssl=CTX, server_hostname=ip), 5)
            w.write((REQ % (ip, UA)).encode())
            await w.drain()
            data = await asyncio.wait_for(r.read(2500), 6)
            return ip, data
        except Exception:
            return ip, b""
        finally:
            if w is not None:
                try:
                    w.close()
                except Exception:
                    pass


async def flush(batch, out):
    resp = empty = hits = 0
    for ip, data in await asyncio.gather(*batch):
        if not data:
            empty += 1
            continue
        resp += 1
        head = data.split(b"\r\n\r\n", 1)
        line = head[0].decode("latin-1")[:800]
        body = (head[1] if len(head) > 1 else b"")[:1500].decode("latin-1")
        m = re.match(r"HTTP/1\.[01] (\d{3})", line)
        st = m.group(1) if m else "?"
        loc = ""
        for h in line.split("\r\n")[1:]:
            if h.lower().startswith("location:"):
                loc = h[9:].strip()
        blob = (line + " " + body).lower()
        if any(s in blob for s in SIGS):
            out.write("%s\t%s\t%s\n" % (ip, st, loc[:70]))
            out.flush()
            hits += 1
    return resp, empty, hits


async def main():
    src, dst = sys.argv[1], sys.argv[2]
    shard = int(sys.argv[3]) if len(sys.argv) > 3 else 0
    nshard = int(sys.argv[4]) if len(sys.argv) > 4 else 1
    out = open(dst, "w")
    sem = asyncio.Semaphore(1000)
    resp = hits = 0
    batch = []
    idx = 0
    for line in open(src):
        if idx % nshard == shard:
            parts = line.split()
            ip = parts[3] if len(parts) > 3 else (parts[-1] if parts else "")
            ip = ip.strip()
            if ip and not ip.startswith("#"):
                batch.append(asyncio.create_task(one(ip, sem)))
        idx += 1
        if len(batch) >= 2000:
            r, _, h = await flush(batch, out)
            resp += r
            hits += h
            batch = []
            print("shard", shard, "lines", idx, "resp", resp, "hits", hits, flush=True)
    if batch:
        r, _, h = await flush(batch, out)
        resp += r
        hits += h
    out.close()
    print("shard", shard, "finished resp", resp, "hits", hits, flush=True)


asyncio.run(main())
