import asyncio, sys
import aiohttp
import bt_poc as B

async def main(ip):
    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)
        print('site', site)
        nk = await B.guest_key(s, base, 63)
        print('key', nk)
        w = await B.open_ws(s, base, site[0], site[1], False)
        print('ws open')
        try:
            line = await w.line()
            print('banner', line[:80])
        except Exception as e:
            print('banner FAIL', type(e).__name__, e)
            return
        await w.write(b'good\n')
        auth = B.fenc({'sessionKey': nk, 'sessionKeyRotateCount': 0, 'platform': 'linux-x86', 'startMethodHint': 'issue_menu'})
        await w.write((auth + '\n').encode())
        try:
            print('auth', (await w.line())[:120])
        except Exception as e:
            print('auth FAIL', type(e).__name__, e)
            return
        await w.write(b'no update token needed\n')
        c = B.MrsClient(w)
        try:
            await c.handshake()
            print('mrs ok', c.properties, c.listener)
        except Exception as e:
            print('mrs FAIL', type(e).__name__, e)

asyncio.run(main(sys.argv[1]))
