import asyncio, os, re, ssl

SRC = '/root/recon/ns_nodate.txt'
OUT = '/root/recon/ns_dates3.tsv'
POS = '/root/recon/ns_dates3.pos'
CONC = 100
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')

LM = re.compile(r'(?im)^last-modified:\s*(.+)$')
PATHS = ('/logon/LogonPoint/index.html', '/logon/LogonPoint/tmindex.html',
         '/epa/scripts/win/nsepa_setup.exe', '/vpn/pluginlist.xml', '/')


async def gethead(ip, path):
    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\r\nAccept: */*\r\nConnection: close\r\n\r\n'
           % (path, ip)).encode()
    w.write(req)
    await w.drain()
    data = b''
    while b'\r\n\r\n' not in data and len(data) < 8192:
        try:
            c = await asyncio.wait_for(r.read(1024), TIMEOUT)
        except Exception:
            break
        if not c:
            break
        data += c
    w.close()
    return data.decode('latin1', 'replace')


async def work(ip, sem):
    async with sem:
        found = '-'
        for p in PATHS:
            try:
                m = LM.search(await gethead(ip, p))
                if m:
                    found = m.group(1).strip()
                    break
            except Exception:
                continue
        return '%s\t%s' % (ip, found)


async def main():
    ips = [l.split()[0] for l in open(SRC) if l.strip()]
    start = 0
    if os.path.exists(POS):
        start = int(open(POS).read().strip() or 0)
    print('nodate ips %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))
            print('done', i + len(chunk), flush=True)
    print('finished', flush=True)

asyncio.run(main())
