Skip to content

feat(proxy): websocket proxy support user-specified port; http proxy support all methods#666

Merged
StephenRi merged 34 commits intoalibaba:masterfrom
xdlkc:feat/proxy-enhancements
Mar 27, 2026
Merged

feat(proxy): websocket proxy support user-specified port; http proxy support all methods#666
StephenRi merged 34 commits intoalibaba:masterfrom
xdlkc:feat/proxy-enhancements

Conversation

@xdlkc
Copy link
Contributor

@xdlkc xdlkc commented Mar 25, 2026

Summary

  • WebSocket proxy: add optional port query param to /sandboxes/{id}/proxy/ws[/{path}]; connects to ws://{host_ip}:{port} when specified, falls back to mapped SERVER port (8080) when omitted. Invalid ports rejected with WS close code 1008.
  • HTTP proxy: change /sandboxes/{sandbox_id}/proxy[/{path}] from POST-only to api_route supporting GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS; method is transparently forwarded to the sandbox service.
  • HTTP proxy: add optional port query param; connects to http://{host_ip}:{port} when specified, falls back to mapped SERVER port (8080) when omitted.
  • Path-based port routing: add /sandboxes/{id}/proxy/port/{port}[/{path}] (HTTP) and /sandboxes/{id}/proxy/port/{port}/ws[/{path}] (WS) routes; embeds port in path so browser relative-path requests (JS/CSS/images) inherit the port automatically — enables VNC/Jupyter browser access.
  • All changes are fully backward compatible.

Changes

  • rock/admin/entrypoints/sandbox_proxy_api.py: add port query param to WS and HTTP proxy endpoints; replace @post with @api_route for HTTP proxy; add path-based port routes registered before {path:path} to avoid FastAPI matching ambiguity
  • rock/sandbox/service/sandbox_proxy_service.py: add port param to websocket_proxy / get_sandbox_websocket_url / http_proxy; rename post_proxyhttp_proxy with method and port params
  • tests/unit/sandbox/test_proxy_enhancements.py: new unit tests (28 cases)
  • tests/unit/sandbox/test_sandbox_proxy_router.py: update to reflect renamed service method
  • docs/_specs/proxy-enhancements/: requirement / interface / implementation spec docs

Test Plan

  • All 28 new unit tests pass
  • Existing proxy router tests pass
  • Full unit test suite — no regressions

closes #665

xdlkc added 16 commits March 25, 2026 14:51
receive_text() and receive_bytes() share the same ASGI message queue.
When receive_text() consumed a binary frame and raised KeyError, the
frame was permanently lost — the fallback receive_bytes() read the NEXT
message instead. This silently dropped ~50% of binary frames, breaking
VNC/noVNC and other binary WebSocket protocols.

Switch to raw receive() which reads exactly once per message, then
dispatches by content type. Also removes per-frame logger.info calls
that printed full binary content (severe perf hit for VNC traffic) and
fixes websockets v15+ compat (ConnectionClosed import path).

Made-with: Cursor
@xdlkc xdlkc force-pushed the feat/proxy-enhancements branch from 349476d to cc5ff2b Compare March 26, 2026 11:28
xdlkc added 2 commits March 27, 2026 10:56
- Remove HTTP routes: /sandboxes/{id}/proxy/port/{port}/{path}
- Remove WS routes: /sandboxes/{id}/proxy/port/{port}/ws/{path}
- Add generic WS route: /sandboxes/{id}/proxy/{path}
- Port now only specified via rock_target_port query parameter
- Update tests to verify new behavior
@xdlkc xdlkc force-pushed the feat/proxy-enhancements branch from f406cce to e386710 Compare March 27, 2026 04:19
- Add HTTP routes: /sandboxes/{sandbox_id}/proxy/vnc/{path}
- Add WebSocket route: /sandboxes/{sandbox_id}/proxy/vnc/{path}
- Fixed port forwarding to 8006 for all VNC requests
- Ignore rock_target_port query parameter for VNC routes
- Add comprehensive unit tests for HTTP and WebSocket VNC proxy
- Add spec, plan, and task documentation to docs/_specs/vnc-proxy/
@xdlkc xdlkc force-pushed the feat/proxy-enhancements branch from e386710 to 4c9e739 Compare March 27, 2026 04:22
xdlkc added 6 commits March 27, 2026 12:35
- Add X-ROCK-Target-Port header support for HTTP proxy
- Add X-ROCK-Target-Port header support for WebSocket proxy
- Return 400 error when both header and query param are specified
- Add resolve_target_port helper function
- Add comprehensive tests for header port support
- Document X-ROCK-Target-Port header for WebSocket proxy
- Document X-ROCK-Target-Port header for HTTP proxy
- Add usage examples with header
- Document conflict error when both header and query param are specified
- Restore /sandboxes/{sandbox_id}/proxy/port/{port}/{path} routes (backward compatible)
- Restore /sandboxes/{id}/proxy/port/{port}/ws/{path} WebSocket routes
- Fix VNC proxy: add proxy_prefix and query_string parameters
- This fixes Location header rewriting for noVNC callbacks
- Add resolve_target_port_from_ws for WebSocket port resolution
- Both HTTP and WebSocket proxy extract port from path (port/8006/...)
- Return 400 error when port specified via multiple sources
- Update tests to reflect new behavior
FastAPI matches routes by registration order. VNC WebSocket route must be
registered before generic /proxy/{path:path} to match correctly.
Shorter URL path for VNC access:
- HTTP: /sandboxes/{id}/vnc/{path}
- WebSocket: /sandboxes/{id}/vnc/{path}
Copy link
Collaborator

@StephenRi StephenRi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@StephenRi StephenRi merged commit ec34f00 into alibaba:master Mar 27, 2026
4 of 5 checks passed
Issac-Newton pushed a commit that referenced this pull request Mar 27, 2026
Shortened VNC route path:

HTTP: /sandboxes/{id}/vnc/{path}
WebSocket: /sandboxes/{id}/vnc/{path}
Moved from /proxy/vnc to /vnc for cleaner, more concise URLs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: websocket proxy support user-specified port; http proxy support all methods

2 participants