import asyncio, ssl, sys

SRC = sys.argv[1]
OUTF = sys.argv[2]
CONC = int(sys.argv[3]) if len(sys.argv) > 3 else 60
SEM = asyncio.Semaphore(CONC)
CTX = ssl.create_default_context()
CTX.check_hostname = False
CTX.verify_mode = ssl.CERT_NONE
ROWS = []


def parse_title(b):
    t = b.decode('utf-8', 'replace')
    i = t.lower().find('<title')
    if i < 0:
        return ''
    j = t.find('>', i)
    k = t.lower().find('</title', j)
    return ' '.join(t[j + 1:k if k > 0 else j + 120].split())[:90]


async def head_of(r, limit=6000):
    buf = b''
    try:
        while len(buf) < limit:
            chunk = await asyncio.wait_for(r.read(2048), 6)
            if not chunk:
                break
            buf += chunk
    except Exception:
        pass
    return buf


async def probe(ip, port, tls):
    async with SEM:
        w = None
        try:
            kw = {'server_hostname': ip} if tls else {}
            r, w = await asyncio.wait_for(asyncio.open_connection(ip, port, ssl=CTX if tls else None, **kw), 5)
            w.write(b'GET / HTTP/1.1\r\nHost: %s\r\nUser-Agent: Mozilla/5.0\r\nConnection: close\r\n\r\n' % ip.encode())
            await w.drain()
            head = await head_of(r)
            lines = head.split(b'\r\n')
            status = lines[0].decode('latin1')[:40] if lines else ''
            hdrs = {}
            for ln in lines[1:]:
                if b':' in ln:
                    k, v = ln.split(b':', 1)
                    hdrs[k.strip().lower()] = v.strip().decode('latin1')[:80]
            body = head.split(b'\r\n\r\n', 1)[1] if b'\r\n\r\n' in head else b''
            ROWS.append((ip, port, status, hdrs.get('server', ''), hdrs.get('location', ''), parse_title(body),
                         hdrs.get('x-powered-by', ''), str(len(head))))
        except Exception as e:
            ROWS.append((ip, port, 'ERR', type(e).__name__, '', '', '', '0'))
        finally:
            if w is not None:
                try:
                    w.close()
                except Exception:
                    pass


async def main():
    tasks = []
    for ln in open(SRC):
        parts = ln.rstrip('\n').split('\t')
        if len(parts) != 2:
            continue
        ip, p = parts[0], int(parts[1])
        if p in (80, 8080):
            tasks.append(asyncio.create_task(probe(ip, p, False)))
        elif p in (443, 8443):
            tasks.append(asyncio.create_task(probe(ip, p, True)))
    await asyncio.gather(*tasks)
    with open(OUTF, 'w') as f:
        for row in sorted(ROWS):
            f.write('\t'.join(str(x) for x in row) + '\n')
    print('done', len(ROWS))

asyncio.run(main())
