import asyncio, sys
import websockets

ORIGIN = "https://computernewb.com"
UA = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120 Safari/537.36"
PROXY = "socks4://36.88.242.103:1080"

def enc(*e):
    return ",".join(f"{sum(1 if ord(c)<=0xFFFF else 2 for c in x)}.{x}" for x in e) + ";"

def dec(msg):
    cps=list(msg); u16=[1 if ord(c)<=0xFFFF else 2 for c in cps]
    out=[]; i=0; N=len(cps)
    while i<N:
        inst=[]
        while True:
            j=i; num=''
            while cps[j]!='.': num+=cps[j]; j+=1
            L=int(num); j+=1; k=j; c=0
            while c<L: c+=u16[k]; k+=1
            inst.append(''.join(cps[j:k])); i=k
            sep=cps[i]; i+=1
            if sep==';': break
        out.append(inst)
    return out

async def main():
    node="vm0"; uri=f"wss://computernewb.com/collab-vm/{node}"
    async with websockets.connect(uri, subprotocols=["guacamole"], origin=ORIGIN,
            additional_headers={"User-Agent":UA}, proxy=PROXY, max_size=None, open_timeout=25) as ws:
        print("CONNECTED", file=sys.stderr)
        await ws.send(enc("rename"))
        await ws.send(enc("connect", node))
        end = 45
        while True:
            try:
                msg = await asyncio.wait_for(ws.recv(), timeout=end)
            except asyncio.TimeoutError:
                print("(idle timeout)", file=sys.stderr); break
            if isinstance(msg, bytes):
                print("BIN", len(msg)); continue
            for inst in dec(msg):
                op=inst[0]
                if op=="nop":
                    await ws.send(enc("nop")); continue
                if op=="png": continue
                if op=="sync": continue
                shown=[(e[:60]+"…" if len(e)>60 else e) for e in inst]
                print("|".join(shown))

asyncio.run(main())
