Wire protocol (v2)
A client keeps one WebSocket to wss://connect.<domain>/v2/connect. Every WebSocket binary message carries
exactly one frame; the server multiplexes public requests, WebSockets, TCP connections and inbox deliveries
over it as streams. Implementation: tunnex/protocol/ (frames, multiplexer, version
negotiation) — shared by the client and the server.
Frame
0 1 2 3 4 8
+-------+-------+-------+-------+-----------------------+----------------
| ver | type | flags | rsv | stream id (uint32) | payload ...
+-------+-------+-------+-------+-----------------------+----------------
ver is 0x02. Integers are big-endian. rsv is sent as 0 and ignored. A frame carries at most 64 KiB of
DATA payload; larger frames are a protocol error.
| Type | Name | Stream | Payload | Direction |
|---|---|---|---|---|
0x01 |
HELLO | 0 | JSON: credentials, mode, requested subdomain or inboxes, policy, protocol offer, features | client → server |
0x02 |
WELCOME | 0 | JSON: negotiated protocol and features, URL, ports, account, limits, policy summary, window | server → client |
0x03 |
ERROR | 0 | JSON {code, message} |
both |
0x04 |
POLICY | 0 | JSON policy (client) / {ok, policy} acknowledgement (server) |
both |
0x05 |
GOAWAY | 0 | JSON {reason, message} |
server → client |
0x10 |
OPEN | n | JSON head {kind: http \| ws \| tcp, ...} |
server → client |
0x11 |
RESPONSE | n | JSON head {status, headers}, or {status: 101, subprotocol} for ws |
client → server |
0x12 |
DATA | n | raw bytes | both |
0x13 |
WINDOW | n | uint32 credit increment | both |
0x14 |
RESET | n | JSON {reason}: abort both directions |
both |
DATA flags: 0x01 FIN (last frame in this direction), 0x02 TEXT (WebSocket text message), 0x04 MORE
(the WebSocket message continues in the next frame).
Handshake
HELLO {
"protocol": {"min": 2, "max": 2},
"features": ["listen", "policy_update", "tcp"],
"client_version": "26.10.14.41",
"token": "tnx_…", // or "anon_key" for anonymous tunnels
"mode": "http", // http | tcp | llm | listen
"subdomain": "acme-preview", // tunnels: requested name (optional)
"previous_subdomain": null, // tunnels: keep the same random name after a reconnect
"tcp": false,
"policy": {"basic_auth": [...], "allow_ips": [...], ...},
"listen": {"name": "laptop", "inboxes": [{"inbox": "acme-stripe", "filters": ["json.livemode=false"], "from": "cursor"}]}
}
The server authenticates the token (or the anonymous key), checks the token's scopes (tunnels for tunnels,
listen for listeners), validates the policy, applies subdomain ownership and plan limits, and answers:
WELCOME {protocol, features, server_version, account, …}plus, for tunnels,subdomain, url, kind, tcp_host, tcp_port, limits, policy, initial_window, warnings; for listeners,mode: "listen", initial_window, subscriptions: [{inbox, url, subscription_id, name, cursor, filters}];- or
ERROR {code, message}and closes. Fatal codes stop the client with the message (auth_failed,login_required,insufficient_scope,subdomain_taken,quota_exceeded,invalid_policy,feature_unavailable,unsupported_protocol,inbox_not_found,not_a_member,invalid_filter,invalid_from, …); any other error is retried with exponential backoff and jitter.
The server does not open streams before WELCOME has been sent.
Streams
Only the server opens streams (ids increase from 1).
- http —
OPEN {method, path, headers: [[k, v]...], remote, identity}→ the request body as DATA … FIN → the client answersRESPONSE {status, headers}→ the response body as DATA … FIN. Bodies stream in both directions; nothing is buffered as a whole. If the public caller disconnects, the edge sends RESET and the client aborts the local request. - ws —
OPEN {path, headers, subprotocols}→ the client connects to the local WebSocket and answersRESPONSE {status: 101, subprotocol}before the edge accepts the browser's handshake (so subprotocols and local rejections are preserved). Each message is DATA frames with TEXT/MORE flags. - tcp —
OPEN {remote}→RESPONSE {status: 200}once the local connection is up → bytes as DATA, half-close with FIN. - inbox delivery (feature
listen) — an http stream whose head also hasdelivery: {inbox, event_id, kind: live|catchup|replay, attempt, subscription, resigned}; the request carries thex-tunnex-*headers described in Inboxes. The client forwards it to--forwardand returns the local response; a local connection failure is answered as502withx-tunnex-error: local_unreachable, which makes the server retry the delivery later.
Flow control
Each direction of each stream has a credit window (512 KiB, announced as initial_window). A sender transmits
DATA only while it has credit; the receiver returns credit with WINDOW frames as its consumer reads (after half
the window is consumed). A slow consumer stalls only its own stream, never the connection, and memory per
stream is bounded. Exceeding the window by more than one chunk resets the stream (flow_control_violation).
Session control
POLICY(featurepolicy_update) changes the edge policy of a live tunnel; the server answersPOLICY {ok: true, policy}orERROR invalid_policy.GOAWAY {reason}— the server is about to close the connection. Final reasons stop the client:replaced(the same owner connected the subdomain elsewhere),released,disconnected(from the console),inbox_deleted,token_revoked. Anything else (server_shutdown) means "reconnect".
Compatibility and evolution
The protocol is versioned by negotiation, not by the frame's ver byte:
- HELLO and WELCOME are always sent in v2 framing, so peers of different versions can always talk long enough to negotiate.
- A client offers
protocol: {min, max}. A client that sends no offer (every release up to 26.10.10) means{min: 2, max: 2}. The server answers with the highest version both support inWELCOME.protocol, or withERROR unsupported_protocoland a message telling the user to runpip install -U tunnex. - Additive changes do not bump the version. New JSON fields in heads and control frames are ignored by
peers that do not know them, and unknown control frame types are ignored. New optional behaviour is a
feature listed in
HELLO.featuresandWELCOME.featuresand may be used only when both sides list it. The three current features (listen,policy_update,tcp) predate the list: 26.10.10 clients usepolicy_updateandtcpwithout announcing them, so the server does not require them;listenis only requested by clients that implement it, and a server without it rejects the mode withinvalid_hello. - A version bump is reserved for changes an old peer would misread (a different frame layout or semantics of
an existing frame). The server keeps accepting the previous version for at least one release cycle, and the
CI runs the previous PyPI client against every server build (
tests/compat). - Removing a capability is announced with a dedicated error code and an actionable message, never a silent
failure: UDP tunnels were removed in 26.10.14 and HELLOs asking for them get
ERROR feature_unavailable "UDP tunnels were removed …".
Earlier history: v1 clients (JSON + Base64 on /tunnel/connect) get an error telling them to upgrade, and
their /api/register-domain calls get 410 Gone.