import asyncio

import aiohttp

SRC = "/root/recon/ivanti_real.txt"
OUT = "/root/recon/dscgi_bogus.tsv"
URL = "https://%s/dana-na/auth/url_default/welcome.cgi"
HDR = {"X-DSCGI-PATH": "/home/webserver/htdocs/dana/zz_nonexistent_9x.cgi"}
CONC = 40


async def one(sess, ip, sem):
    async with sem:
        try:
            async with sess.get(URL % ip, ssl=False, allow_redirects=True) as r:
                s0 = r.status
                l0 = len(await r.read())
            async with sess.get(URL % ip, ssl=False, allow_redirects=True, headers=HDR) as r:
                s1 = r.status
                l1 = len(await r.read())
            d = l1 - l0
            return "%s\t%d\t%d\t%d\t%d\t%s\n" % (ip, s0, l0, s1, l1, "DIFF" if abs(d) > 2 else "same")
        except Exception as e:
            return "%s\tERR\t0\t0\t0\t%s\n" % (ip, type(e).__name__)


async def main():
    ips = [l.strip() for l in open(SRC) if l.strip()]
    sem = asyncio.Semaphore(CONC)
    conn = aiohttp.TCPConnector(limit=0, ssl=False, force_close=True)
    to = aiohttp.ClientTimeout(total=20, connect=8, sock_read=12)
    async with aiohttp.ClientSession(timeout=to, connector=conn,
                                     headers={"User-Agent": "Mozilla/5.0"}) as sess:
        res = await asyncio.gather(*[one(sess, ip, sem) for ip in ips])
    open(OUT, "w").writelines(res)
    print("done", len(res))


asyncio.run(main())
