cdp_session_check.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. """Check 996box session state via CDP: localStorage tokens, then try
  2. navigating straight to the main page (skipping login if session alive).
  3. """
  4. import asyncio
  5. import json
  6. import sys
  7. import httpx
  8. import websockets
  9. async def get_targets():
  10. async with httpx.AsyncClient(timeout=5) as c:
  11. r = await c.get("http://127.0.0.1:9222/json/list")
  12. return r.json()
  13. async def cdp_eval(ws, expr: str, msg_id: int) -> dict:
  14. payload = json.dumps({"id": msg_id, "method": "Runtime.evaluate",
  15. "params": {"expression": expr, "returnByValue": True}})
  16. await ws.send(payload)
  17. while True:
  18. msg = json.loads(await ws.recv())
  19. if msg.get("id") == msg_id:
  20. return msg.get("result", {})
  21. async def cdp_navigate(ws, url: str, msg_id: int) -> dict:
  22. payload = json.dumps({"id": msg_id, "method": "Page.navigate",
  23. "params": {"url": url}})
  24. await ws.send(payload)
  25. while True:
  26. msg = json.loads(await ws.recv())
  27. if msg.get("id") == msg_id:
  28. return msg.get("result", {})
  29. async def main() -> int:
  30. targets = await get_targets()
  31. print("=== current CDP targets ===")
  32. for t in targets:
  33. print(f" [{t['type']}] {t.get('title','')[:30]} {t['url'][:75]}")
  34. login = next((t for t in targets if "login" in t.get("url", "")), None)
  35. if not login:
  36. print("\nno login page target — checking any hzwlan page")
  37. login = next((t for t in targets if "hzwlan" in t.get("url", "") or "996box" in t.get("url", "")), None)
  38. if not login:
  39. print("no usable target")
  40. return 1
  41. ws_url = login["webSocketDebuggerUrl"]
  42. print(f"\nusing: {login['url'][:60]}")
  43. async with websockets.connect(ws_url, max_size=10 * 1024 * 1024) as ws:
  44. # 1. localStorage recon
  45. print("\n=== localStorage keys ===")
  46. res = await cdp_eval(ws, """
  47. (function() {
  48. const out = {};
  49. for (let i = 0; i < localStorage.length; i++) {
  50. const k = localStorage.key(i);
  51. const v = localStorage.getItem(k) || '';
  52. out[k] = v.length > 60 ? v.slice(0, 60) + '...' : v;
  53. }
  54. return JSON.stringify(out, null, 1);
  55. })()
  56. """, 1)
  57. val = res.get("result", {}).get("value", "{}")
  58. try:
  59. ls = json.loads(val)
  60. for k, v in ls.items():
  61. print(f" {k} = {v}")
  62. except Exception:
  63. print(val[:500])
  64. # 2. Check for token presence
  65. res2 = await cdp_eval(ws, """
  66. (function() {
  67. const t = localStorage.getItem('token') || localStorage.getItem('TOKEN') || '';
  68. return JSON.stringify({hasToken: t.length > 0, len: t.length});
  69. })()
  70. """, 2)
  71. print(f"\ntoken check: {res2.get('result', {}).get('value')}")
  72. # 3. Navigate to the main/game page — session cookie may carry us through
  73. print("\n=== navigating to main page (game list) ===")
  74. await cdp_navigate(ws, "https://h5qfdt.996box.com/", 3)
  75. await asyncio.sleep(6)
  76. res3 = await cdp_eval(ws, "location.href + ' | title=' + document.title", 4)
  77. print(f"after nav: {res3.get('result', {}).get('value')}")
  78. return 0
  79. sys.exit(asyncio.run(main()))