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

HITS = '/root/recon/ns_hits.tsv'
OUT = '/root/recon/ns_adc.tsv'
POS = '/root/recon/ns_verify2.pos'
CONC = 60
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')

VER_RE = re.compile(r'(?:NS ?)?(\d{2}\.\d{1,2})[.: ]?(?:[Bb]uild )?(\d{1,3}\.\d{1,3})(?:\.nc)?')
TITLE_RE = re.compile(r'<title[^>]*>(.{0,90}?)</title>', re.S | re.I)


async def fetch(ip, path, timeout=TIMEOUT):
    r, w = await asyncio.wait_for(asyncio.open_connection(ip, 443, ssl=ctx, server_hostname=ip), timeout)
    req = ('GET %s HTTP/1.1\r\nHost: %s\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)\r\n'
           'Accept: */*\r\nConnection: close\r\n\r\n' % (path, ip)).encode()
    w.write(req)
    await w.drain()
    data = b''
    while len(data) < 60000:
        try:
            chunk = await asyncio.wait_for(r.read(8192), timeout)
        except Exception:
            break
        if not chunk:
            break
        data += chunk
    w.close()
    return data


def split_http(d):
    head, _, body = d.partition(b'\r\n\r\n')
    status = head.split(b'\r\n')[0].decode('latin1', 'replace')
    return status, body


async def work(ip, sem):
    async with sem:
        ver, title, paths = '', '', []
        try:
            d = await fetch(ip, '/nitro/v1/config/nsversion')
            st, body = split_http(d)
            try:
                j = json.loads(body.decode('utf-8', 'replace'))
                if isinstance(j, dict) and isinstance(j.get('nsversion'), dict):
                    ver = j['nsversion'].get('version', '')
            except Exception:
                pass
            if not ver:
                m = re.search(rb'"version"\s*:\s*"([^"]{5,120})"', body)
                if m:
                    ver = m.group(1).decode('latin1', 'replace')
        except Exception:
            pass
        for path in ('/vpn/index.html', '/logon/LogonPoint/tmindex.html', '/'):
            try:
                d = await fetch(ip, path)
            except Exception:
                continue
            st, body = split_http(d)
            paths.append('%s:%s' % (path, st.split(' ')[1] if len(st.split(' ')) > 1 else '?'))
            if not title:
                m = TITLE_RE.search(body.decode('latin1', 'replace'))
                if m:
                    title = ' '.join(m.group(1).split())[:80]
            if not ver:
                t = body.decode('latin1', 'replace')
                m = VER_RE.search(t)
                if m:
                    ver = 'NS%s build %s' % (m.group(1), m.group(2))
        return '%s\t%s\t%s\t%s' % (ip, ver or '-', title or '-', ','.join(paths))


async def main():
    seen, ips = 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):
        try:
            start = int(open(POS).read().strip() or 0)
        except Exception:
            start = 0
    print('unique hits %d resume %d' % (len(ips), start), flush=True)
    sem = asyncio.Semaphore(CONC)
    with open(OUT, 'a', buffering=1) as f:
        for i in range(start, len(ips), 300):
            chunk = ips[i:i + 300]
            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 + 300))
            if (i // 300) % 5 == 0:
                print('done', i + len(chunk), flush=True)
    print('finished', flush=True)

asyncio.run(main())
