import asyncio, os, re, ssl

SRC = '/root/recon/gw_all.txt'
OUT = '/root/recon/ns_dates.tsv'
POS = '/root/recon/ns_dates.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*(.+)$')


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:
        epa, ui = '-', '-'
        try:
            h = await gethead(ip, '/vpn/pluginlist.xml')
            m = LM.search(h)
            if m:
                epa = m.group(1).strip()
        except Exception:
            pass
        try:
            h = await gethead(ip, '/logon/LogonPoint/tmindex.html')
            m = LM.search(h)
            if m:
                ui = m.group(1).strip()
        except Exception:
            pass
        return '%s\t%s\t%s' % (ip, epa, ui)


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('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())
