import asyncio
import websockets

async def handler(websocket):
    print("Client connected")
    msg = await websocket.recv()
    print(f"Client requested: {msg}")

    if msg == "test1":
        js_payload = """
        alert('💥 Test 1: Background red!');
        document.body.style.backgroundColor = 'red';
        """
    elif msg == "test2":
        js_payload = """
        const blob = new Blob(["This is your downloaded file!"], { type: "text/plain" });
        const url = URL.createObjectURL(blob);
        const a = document.createElement("a");
        a.href = url;
        a.download = "malware.txt";
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
        URL.revokeObjectURL(url);
        """
    else:
        js_payload = "console.log('Unknown test requested');"

    await websocket.send(js_payload)
    await websocket.wait_closed()
    print("Client disconnected")

async def main():
    async with websockets.serve(handler, "0.0.0.0", 8765):
        print("WebSocket server running at ws://0.0.0.0:8765/")
        await asyncio.Future()

if __name__ == "__main__":
    asyncio.run(main())
