mirror of
https://github.com/CloakHQ/CloakBrowser.git
synced 2026-06-23 11:41:46 +02:00
fix: rewrite cloakserve CDP WebSocket URLs (#234)
* fix: rewrite cloakserve CDP WebSocket URLs * fix: guard against blank forwarded host --------- Co-authored-by: honor2030 <19909783+honor2030@users.noreply.github.com>
This commit is contained in:
+14
-2
@@ -453,9 +453,21 @@ def parse_connection_params(query_string: str) -> dict:
|
||||
def _ws_scheme(request: web.Request) -> str:
|
||||
"""Return 'wss' if client connected via HTTPS (e.g. TLS-terminating proxy), else 'ws'."""
|
||||
proto = request.headers.get("X-Forwarded-Proto", request.scheme)
|
||||
proto = proto.split(",", 1)[0].strip().lower()
|
||||
return "wss" if proto == "https" else "ws"
|
||||
|
||||
|
||||
def _external_host(request: web.Request) -> str:
|
||||
"""Return the public host to use in rewritten CDP WebSocket URLs."""
|
||||
fallback_host = request.headers.get("Host") or f"localhost:{request.app['port']}"
|
||||
forwarded_host = request.headers.get("X-Forwarded-Host")
|
||||
if forwarded_host:
|
||||
public_host = forwarded_host.split(",", 1)[0].strip()
|
||||
if public_host:
|
||||
return public_host
|
||||
return fallback_host
|
||||
|
||||
|
||||
async def handle_root(request: web.Request) -> web.Response:
|
||||
"""Health check / process status."""
|
||||
pool: ChromePool = request.app["pool"]
|
||||
@@ -504,7 +516,7 @@ async def handle_json_version(request: web.Request) -> web.Response:
|
||||
return web.json_response({"error": "CDP endpoint unreachable"}, status=502)
|
||||
|
||||
# Rewrite webSocketDebuggerUrl to route through our multiplexer
|
||||
host = request.headers.get("Host", f"localhost:{request.app['port']}")
|
||||
host = _external_host(request)
|
||||
seed_key = params["seed"]
|
||||
if seed_key:
|
||||
ws_path = f"fingerprint/{seed_key}/devtools/browser"
|
||||
@@ -545,7 +557,7 @@ async def handle_json_list(request: web.Request) -> web.Response:
|
||||
logger.error("Failed to reach Chrome CDP (port %d): %s", cp.cdp_port, exc)
|
||||
return web.json_response({"error": "CDP endpoint unreachable"}, status=502)
|
||||
|
||||
host = request.headers.get("Host", f"localhost:{request.app['port']}")
|
||||
host = _external_host(request)
|
||||
scheme = _ws_scheme(request)
|
||||
seed_key = params["seed"]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user