import asyncio, os, ssl, sys

SRC = '/root/recon/ips443_all.txt'
HITS = '/root/recon/ns_hits.tsv'
W = int(sys.argv[1]) if len(sys.argv) > 1 else 0
N = int(sys.argv[2]) if len(sys.argv) > 2 else 1
CONC = int(sys.argv[3]) if len(sys.argv) > 3 else 1500
POS = '/root/recon/ns_scan_%d.pos' % W
TIMEOUT = 4
REQ = (b'GET / HTTP/1.1\r\nHost: %s\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)\r\n'
       b'Accept: text/html,application/xhtml+xml,*/*\r\nConnection: close\r\n\r\n')

ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
ctx.set_ciphers('DEFAULT@SECLEVEL=1')

counter = {'n': 0, 'hit': 0, 'err': 0}
out = open(HITS, 'a', buffering=1)


def markers(data):
    low = data.lower()
    cookies = [l.split(b':', 1)[1].strip().lower() for l in data.split(b'\r\n') if l.lower().startswith(b'set-cookie:')]
    ck = ';'.join(c.decode('latin1') for c in cookies)
    hit = []
    if b'nsc_' in ck.encode() or b'nsc_' in low:
        hit.append('NSC')
    if b'/vpn/' in low or b'/nitro/' in low or b'/logon/logonpoint' in low:
        hit.append('VPNPATH')
    if b'citrix' in low or b'netscaler' in low:
        hit.append('BRAND')
    return hit, ck[:120]


async def probe(ip, sem):
    async with sem:
        counter['n'] += 1
        try:
            r, w = await asyncio.wait_for(asyncio.open_connection(ip, 443, ssl=ctx, server_hostname=ip), TIMEOUT)
        except Exception:
            counter['err'] += 1
            return
        try:
            w.write(REQ % ip.encode())
            await w.drain()
            data = await asyncio.wait_for(r.read(4096), TIMEOUT)
        except Exception:
            counter['err'] += 1
            try:
                w.close()
            except Exception:
                pass
            return
        try:
            w.close()
        except Exception:
            pass
        hit, ck = markers(data)
        if hit:
            loc = ''
            for line in data.split(b'\r\n'):
                if line.lower().startswith(b'location:'):
                    loc = line.split(b':', 1)[1].strip().decode('latin1', 'replace')
            status = data.split(b'\r\n')[0].decode('latin1', 'replace')
            out.write('%s\t%s\t%s\t%s\t%s\n' % (ip, status, loc, ck, ','.join(hit)))
            counter['hit'] += 1


async def main():
    start = 0
    if os.path.exists(POS):
        try:
            start = int(open(POS).read().strip())
        except Exception:
            start = 0
    print('worker %d/%d conc %d resume %d' % (W, N, CONC, start), flush=True)
    sem = asyncio.Semaphore(CONC)
    batch = []
    idx = 0
    with open(SRC) as f:
        for line in f:
            idx += 1
            if idx <= start:
                continue
            if (idx - 1) % N != W:
                continue
            p = line.split()
            if not p:
                continue
            ip = p[3] if (p[0] == 'open' and len(p) >= 4) else p[0]
            if ip.count('.') != 3:
                continue
            batch.append(ip)
            if len(batch) >= 20000:
                await asyncio.gather(*[probe(ip, sem) for ip in batch])
                batch = []
                open(POS, 'w').write(str(idx))
                print('done %d hit %d err %d' % (counter['n'], counter['hit'], counter['err']), flush=True)
    if batch:
        await asyncio.gather(*[probe(ip, sem) for ip in batch])
    open(POS, 'w').write(str(idx))
    print('finished', idx, counter, flush=True)

asyncio.run(main())
