Skip to content

feat: add WebSocket TCP port forwarding for sandbox containers#523

Open
Timandes wants to merge 2 commits intoalibaba:masterfrom
Timandes:feature/tcp-over-ws
Open

feat: add WebSocket TCP port forwarding for sandbox containers#523
Timandes wants to merge 2 commits intoalibaba:masterfrom
Timandes:feature/tcp-over-ws

Conversation

@Timandes
Copy link
Contributor

@Timandes Timandes commented Feb 28, 2026

Complete GH Issue #520 :

  • Add /sandboxes/{id}/portforward WebSocket endpoint in proxy layer
  • Add /portforward WebSocket endpoint in rocklet for internal TCP proxy
  • Support port range 1024-65535, excluding port 22 (SSH)
  • Implement two-layer architecture: external proxy -> rocklet -> TCP port
  • Add comprehensive logging with target_port in all messages
  • Handle both FastAPI WebSocket and websockets library APIs

实现方式相比Issue #520 做了一点调整:

通过rocklet转发TCP over WebSocket;外层代理层仅负责处理WebSocket转发。


启动沙箱的脚本可以参考:

import asyncio

from rock.actions import CreateBashSessionRequest
from rock.sdk.sandbox.client import Sandbox
from rock.sdk.sandbox.config import SandboxConfig


async def run_sandbox():
    config = SandboxConfig(image="python:3.11", memory="2g", cpus=2.0)
    sandbox = Sandbox(config)

    await sandbox.start()
    await sandbox.create_session(CreateBashSessionRequest(session="bash-1"))
    result = await sandbox.arun(cmd="python3 -m http.server 9999 &", session="bash-1")


if __name__ == "__main__":
    asyncio.run(run_sandbox())

测试程序可以使用:

https://github.com/Timandes/ROCK-TCP-to-WS-Forwarder

- Add /sandboxes/{id}/portforward WebSocket endpoint in proxy layer
- Add /portforward WebSocket endpoint in rocklet for internal TCP proxy
- Support port range 1024-65535, excluding port 22 (SSH)
- Implement two-layer architecture: external proxy -> rocklet -> TCP port
- Add comprehensive logging with target_port in all messages
- Handle both FastAPI WebSocket and websockets library APIs
- Add unit tests for port validation, URL building, and route handling
- Capture close frame (code, reason) when rocklet closes connection
- Forward close frame to client instead of silently dropping
- Properly wait for cancelled tasks to complete
- Improve logging for connection close events
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.

1 participant