handshake_probe2.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. """Handshake probes H3/H5 on the game gateway (fixed, self-contained).
  2. H3: send CS 2001 -> read 6B -> echo it back -> read more
  3. H5: direct connect to :8021 (bypass proxy) -> send CS 2001 -> read
  4. """
  5. import asyncio
  6. import base64
  7. import json
  8. import struct
  9. import sys
  10. from pathlib import Path
  11. sys.path.insert(0, "src")
  12. from cq2_sdk.utils.encoding import encode6bit
  13. raw = Path(r"D:\ProgramFiles\996box\GameBoxDownload\1\env.json").read_bytes()
  14. text = raw.decode("utf-8", errors="replace")
  15. idx = text.find("eyJ")
  16. b64 = text[idx:].rstrip()
  17. padded = b64 + "=" * ((-len(b64)) % 4)
  18. env = json.loads(base64.urlsafe_b64decode(padded).decode("utf-8", errors="replace"))
  19. uid = str(env["uid"])
  20. pwd_enc = encode6bit(env["token"].encode()).decode("ascii")
  21. ch_enc = encode6bit(b"110585").decode("ascii")
  22. BODY = f"{uid}/{pwd_enc}/{ch_enc}/0/0"
  23. HOST = "gfgame.dhsf.xqhuyu.com"
  24. def frame() -> bytes:
  25. return (
  26. struct.pack("<6i", 2001, 900000, 0x0F3DB000, 14414736, 8021, len(BODY.encode()))
  27. + BODY.encode()
  28. )
  29. async def h3_echo() -> None:
  30. print("=== H3: send 2001 first, wait 6B, echo back, read more ===")
  31. r, w = await asyncio.wait_for(asyncio.open_connection(HOST, 12000), timeout=6)
  32. try:
  33. w.write(frame())
  34. await w.drain()
  35. try:
  36. first = await asyncio.wait_for(r.readexactly(6), timeout=4)
  37. print(f" got 6B: {first.hex()}")
  38. w.write(first)
  39. await w.drain()
  40. print(" echoed back")
  41. try:
  42. data = await asyncio.wait_for(r.readexactly(6), timeout=4)
  43. print(f" second reply: {data.hex()}")
  44. except (TimeoutError, asyncio.TimeoutError):
  45. print(" silence after echo")
  46. except asyncio.IncompleteReadError:
  47. print(" closed after echo")
  48. except (TimeoutError, asyncio.TimeoutError):
  49. print(" no 6B within 4s")
  50. except asyncio.IncompleteReadError as e:
  51. print(f" closed, partial {len(e.partial)}B: {e.partial.hex()}")
  52. finally:
  53. w.close()
  54. try:
  55. await w.wait_closed()
  56. except Exception:
  57. pass
  58. async def h5_direct() -> None:
  59. print("=== H5: direct :8021 bypass proxy, send 2001 ===")
  60. try:
  61. r, w = await asyncio.wait_for(asyncio.open_connection(HOST, 8021), timeout=8)
  62. except Exception as e:
  63. print(f" connect :8021 failed: {type(e).__name__}")
  64. return
  65. try:
  66. w.write(frame())
  67. await w.drain()
  68. print(" sent 102B to :8021")
  69. try:
  70. data = await asyncio.wait_for(r.readexactly(6), timeout=6)
  71. print(f" reply 6B: {data.hex()}")
  72. try:
  73. rest = await asyncio.wait_for(r.read(4096), timeout=3)
  74. if rest:
  75. print(f" more: {len(rest)}B {rest.hex()[:100]}")
  76. except Exception:
  77. pass
  78. except asyncio.IncompleteReadError as e:
  79. print(f" closed, partial {len(e.partial)}B: {e.partial.hex()}")
  80. except (TimeoutError, asyncio.TimeoutError):
  81. print(" no reply (6s)")
  82. finally:
  83. w.close()
  84. try:
  85. await w.wait_closed()
  86. except Exception:
  87. pass
  88. async def main() -> None:
  89. await h3_echo()
  90. print()
  91. await h5_direct()
  92. asyncio.run(main())