import asyncio, json, os, re, ssl, sys

HITS = '/root/recon/ns_hits.tsv'
OUT = '/root/recon/ns_versions.tsv'
POS = '/root/recon/ns_verify.pos'
CONC = 24
TIMEOUT = 8

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


def get(url_path, ip):
    return (b'GET ' + url_path.encode() + b' HTTP/1.1\r\nHost: ' + ip.encode() +
            b'\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)\r\nAccept: */*\r\nConnection: close\r\n\r\n')


async def fetch(ip, path):
    r, w = await asyncio.wait_for(asyncio.open_connection(ip, 443, ssl=ctx, server_hostname=ip), TIMEOUT)
    w.write(get(path, ip))
    await w.drain()
    data = b''
    while True:
        try:
            chunk = await asyncio.wait_for(r.read(8192), TIMEOUT)
        except Exception:
            break
        if not chunk:
            break
        data += chunk
        if len(data) > 200000:
            break
    w.close()
    return data


def ver_from_json(text):
    try:
        j = json.loads(text)
    except Exception:
        return ''
    if isinstance(j, dict) and 'nsversion' in j:
        v = j['nsversion']
        if isinstance(v, dict):
            return v.get('version', '')
    return ''


async def work(ip, sem):
    async with sem:
        ver = ''
        try:
            d = await fetch(ip, '/nitro/v1/config/nsversion')
            body = d.split(b'\r\n\r\n', 1)[-1].decode('utf-8', 'replace')
            ver = ver_from_json(body)
        except Exception:
            pass
        gw = ''
        if not ver:
            try:
                d = await fetch(ip, '/vpn/index.html')
                t = d.decode('latin1', 'replace')
                m = re.search(r'NS(\d+(?:\.\d+){1,2})[:\s]+[Bb]uild\s+([0-9.]+)', t)
                if m:
                    ver = 'NS%s build %s' % (m.group(1), m.group(2))
                if 'Citrix' in t or 'NetScaler' in t:
                    gw = 'gateway-page'
            except Exception:
                pass
        return '%s\t%s\t%s' % (ip, ver or '-', gw or '-')


async def main():
    ips = []
    seen = set()
    for line in open(HITS):
        ip = line.split('\t')[0]
        if ip not in seen:
            seen.add(ip)
            ips.append(ip)
    start = 0
    if os.path.exists(POS):
        start = int(open(POS).read().strip() or 0)
    print('hits', len(ips), 'resume', start, flush=True)
    sem = asyncio.Semaphore(CONC)
    with open(OUT, 'a', buffering=1) as f:
        for i in range(start, len(ips), 200):
            chunk = ips[i:i + 200]
            res = await asyncio.gather(*[work(ip, sem) for ip in chunk])
            for r in res:
                f.write(r + '\n')
            open(POS, 'w').write(str(i + 200))
            print('verified', i + len(chunk), flush=True)
    print('finished', flush=True)

asyncio.run(main())
