import asyncio, sys

import aiohttp

UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36"
SIGS = ("atlassian", "login.action", "login.jsp", "dashboard.jspa", "asessionid", "confluence", "jira", "bitbucket")


async def one(sess, ip, sem, out):
    async with sem:
        try:
            async with sess.get("https://%s/" % ip, ssl=False, allow_redirects=False, headers={"User-Agent": UA}) as r:
                b = await r.read(4000)
                loc = r.headers.get("Location", "")
                srv = r.headers.get("Server", "")
                blob = (loc + " " + b.decode("utf-8", "replace")).lower()
                if any(s in blob for s in SIGS) or "atlassian" in srv.lower():
                    out.write("%s\t%d\t%s\t%s\n" % (ip, r.status, loc[:70], srv[:30]))
                    out.flush()
        except Exception:
            pass


async def main():
    src, shard, nshard, dst = sys.argv[1], int(sys.argv[2]), int(sys.argv[3]), sys.argv[4]
    out = open(dst, "w")
    sem = asyncio.Semaphore(1200)
    conn = aiohttp.TCPConnector(limit=0, ssl=False, force_close=True)
    to = aiohttp.ClientTimeout(total=8, connect=5, sock_read=6)
    done = 0
    async with aiohttp.ClientSession(timeout=to, connector=conn) as sess:
        batch = []
        for i, line in enumerate(open(src)):
            if i % nshard != shard:
                continue
            ip = line.split()[-1].strip()
            if not ip or ip[0] == "#":
                continue
            batch.append(asyncio.create_task(one(sess, ip, sem, out)))
            done += 1
            if len(batch) >= 5000:
                await asyncio.gather(*batch)
                batch = []
                print("shard", shard, "done", done, flush=True)
        if batch:
            await asyncio.gather(*batch)
    out.close()
    print("shard", shard, "finished", done, flush=True)


asyncio.run(main())
