|
| 1 | +--- |
| 2 | +sidebar_position: 6 |
| 3 | +--- |
| 4 | + |
| 5 | +# Sandbox Proxy |
| 6 | + |
| 7 | +ROCK Admin exposes a proxy layer that lets you reach services running **inside** a sandbox from **outside** the cluster, without giving each sandbox its own public address. The proxy supports two transport modes: |
| 8 | + |
| 9 | +| Mode | Endpoint | Use it for | |
| 10 | +|------|----------|------------| |
| 11 | +| HTTP Proxy | `/sandboxes/:sandbox_id/proxy/...` | REST APIs, web UIs, file downloads, any HTTP/1.1 traffic | |
| 12 | +| WebSocket Proxy | `ws(s)://.../sandboxes/:sandbox_id/proxy/...` | Real-time channels, streaming, browser-based WS clients | |
| 13 | + |
| 14 | +Both modes route by `sandbox_id`. The sandbox does **not** need a public IP — Admin terminates the client connection and forwards it to the right runtime inside the cluster. |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +## 1. HTTP Proxy |
| 19 | + |
| 20 | +Forward any HTTP request to a service inside the sandbox. |
| 21 | + |
| 22 | +### Endpoint |
| 23 | + |
| 24 | +```text |
| 25 | +Methods : GET | POST | PUT | DELETE | PATCH | HEAD | OPTIONS |
| 26 | +URL : $ROCK_BASE_URL/sandboxes/:sandbox_id/proxy[/:path] |
| 27 | +``` |
| 28 | + |
| 29 | +- Method, headers, query string, and body are forwarded as-is to the target service. |
| 30 | +- The response (status code, headers, body) is streamed back to the client. |
| 31 | + |
| 32 | +### Choosing the target port |
| 33 | + |
| 34 | +The proxy needs to know which port inside the sandbox to hit. You can specify it in one of three ways — **pick exactly one**; mixing them returns `400 Bad Request`. |
| 35 | + |
| 36 | +| Priority | Mechanism | Example | |
| 37 | +|----------|-----------|---------| |
| 38 | +| 1 | **Path prefix** | `/sandboxes/abc/proxy/port/8080/api/users` | |
| 39 | +| 2 | **Request header** | `X-ROCK-Target-Port: 8080` | |
| 40 | +| 3 | **Query parameter** | `?rock_target_port=8080` | |
| 41 | + |
| 42 | +If no port is specified, the request is delivered to the sandbox's default service port. |
| 43 | + |
| 44 | +### Examples |
| 45 | + |
| 46 | +```bash |
| 47 | +# REST call via path-style port |
| 48 | +curl -X POST \ |
| 49 | + "$ROCK_BASE_URL/sandboxes/sb-123/proxy/port/8080/v1/predict" \ |
| 50 | + -H "Content-Type: application/json" \ |
| 51 | + -d '{"prompt": "hello"}' |
| 52 | + |
| 53 | +# Same call via header-style port |
| 54 | +curl -X POST \ |
| 55 | + "$ROCK_BASE_URL/sandboxes/sb-123/proxy/v1/predict" \ |
| 56 | + -H "X-ROCK-Target-Port: 8080" \ |
| 57 | + -H "Content-Type: application/json" \ |
| 58 | + -d '{"prompt": "hello"}' |
| 59 | + |
| 60 | +# Query-string port + extra query params |
| 61 | +curl "$ROCK_BASE_URL/sandboxes/sb-123/proxy/items?rock_target_port=8080&limit=10" |
| 62 | +``` |
| 63 | + |
| 64 | +--- |
| 65 | + |
| 66 | +## 2. WebSocket Proxy |
| 67 | + |
| 68 | +For services that expect a `ws://` (or `wss://`) connection — typical for streaming, chat, terminal sessions, or any full-duplex channel. |
| 69 | + |
| 70 | +### Endpoint |
| 71 | + |
| 72 | +```text |
| 73 | +ws(s)://$ROCK_BASE_URL/sandboxes/:sandbox_id/proxy/:path |
| 74 | +``` |
| 75 | + |
| 76 | +- The original WebSocket handshake (subprotocols, custom headers) is forwarded. |
| 77 | +- Both text and binary frames pass through transparently in both directions. |
| 78 | +- Closing either side cleanly tears down the upstream connection. |
| 79 | + |
| 80 | +### Choosing the target port |
| 81 | + |
| 82 | +Identical to HTTP Proxy — path, header, or query parameter — and again, only one at a time: |
| 83 | + |
| 84 | +| Priority | Mechanism | Example | |
| 85 | +|----------|-----------|---------| |
| 86 | +| 1 | Path prefix | `/sandboxes/abc/proxy/port/9000/socket` | |
| 87 | +| 2 | Header | `X-ROCK-Target-Port: 9000` | |
| 88 | +| 3 | Query | `?rock_target_port=9000` | |
| 89 | + |
| 90 | +Invalid port values (see [Port restrictions](#3-port-restrictions)) cause the WebSocket to close immediately with code `1008` (Policy Violation). |
| 91 | + |
| 92 | +### Example |
| 93 | + |
| 94 | +```bash |
| 95 | +# Using wscat |
| 96 | +wscat -c "$ROCK_WS_BASE/sandboxes/sb-123/proxy/port/9000/events" |
| 97 | +``` |
| 98 | + |
| 99 | +```javascript |
| 100 | +// Browser client |
| 101 | +const ws = new WebSocket( |
| 102 | + "wss://rock.example.com/sandboxes/sb-123/proxy/events?rock_target_port=9000" |
| 103 | +); |
| 104 | +ws.onmessage = (evt) => console.log(evt.data); |
| 105 | +ws.send("ping"); |
| 106 | +``` |
| 107 | + |
| 108 | +--- |
| 109 | + |
| 110 | +## 3. Port restrictions |
| 111 | + |
| 112 | +The WebSocket proxy enforces the following rules on the **target port** inside the sandbox: |
| 113 | + |
| 114 | +| Rule | Allowed range / value | |
| 115 | +|------|----------------------| |
| 116 | +| Minimum port | `1024` | |
| 117 | +| Maximum port | `65535` | |
| 118 | +| Forbidden | `22` (SSH) | |
| 119 | + |
| 120 | +Requests violating these rules are rejected: |
| 121 | + |
| 122 | +- **HTTP Proxy** → `400 Bad Request` with a `detail` message. |
| 123 | +- **WebSocket Proxy** → connection closed with code `1008` and a reason string. |
| 124 | + |
| 125 | +> Ports below `1024` are blocked because they are reserved for privileged services; port `22` is blocked to prevent inadvertently exposing SSH. |
| 126 | +
|
| 127 | +--- |
| 128 | + |
| 129 | +## 4. Error handling reference |
| 130 | + |
| 131 | +| Symptom | Likely cause | |
| 132 | +|---------|--------------| |
| 133 | +| `400 Bad Request: Cannot specify target port via multiple sources` | You set the port in two of path, header or query — pick one. | |
| 134 | +| `400 Bad Request` / WS close `1008` with port-range message | Target port is `< 1024`, `> 65535`, or equal to `22`. | |
| 135 | +| WS close `1011` (`Proxy error: ...`) | Upstream service inside the sandbox returned an error or could not be reached. Check that the service is actually listening on the target port. | |
| 136 | +| `404 Not Found` from the upstream | The HTTP path inside the sandbox does not exist — verify the service's route, not the proxy URL. | |
| 137 | +| Connection hangs on handshake | The sandbox may still be initializing. Confirm `is_alive` returns `True` before proxying. | |
| 138 | + |
| 139 | +--- |
| 140 | + |
| 141 | +## 5. Quick decision guide |
| 142 | + |
| 143 | +```text |
| 144 | +Need to call a REST API inside the sandbox? → HTTP Proxy |
| 145 | +Need a bidirectional WebSocket to a WS server? → WebSocket Proxy |
| 146 | +``` |
| 147 | + |
| 148 | +## Related documents |
| 149 | + |
| 150 | +- [API Reference](../References/api.md) |
| 151 | +- [Configuration](configuration.md) |
| 152 | +- [Quick Start](../Getting%20Started/quickstart.md) |
0 commit comments