Skip to content

transport-ws ​

WebSocket (RFC 6455) over a connection opened by the caller: a port of sing-box's transport/v2raywebsocket onto the transport interface. The same handshake, the same placement of early data, only binary frames, extensions are not negotiated (permessage-deflate never).

Crateours-mod-transport-ws, modules/transports/ws
Binary featuretransport-ws
Crate featuresnone

requires ​

transport — extends its point and implements its traits (the crate depends on the contract ours-transport-api, not on the module).

provides ​

  • Provide::Block { block: "transport", key: "ws" } — the decoder ours_transport_api::variant::decode with this module's KIND.
  • Provide::Schema { name: "transport.ws" } — the fields of V2RayWebsocketOptions.

Syntax: WebSocket.

Dependencies ​

ours-transport-api, ours-proto (http1), ours-api, ours-bufpool, ours-codec, base64, sha1, rand_core + getrandom, tokio.

Design ​

  • src/lib.rs — Client/Server. The client sends GET <path> with Host from the header or the server name, Upgrade, Connection, Sec-WebSocket-Version: 13, a random key, and checks Sec-WebSocket-Accept.
  • src/handshake.rs — the upgrade request, the 101 response and the checks of both sides (like github.com/sagernet/ws).
  • src/frame.rs — the 2–14-byte header, the mask, the close body; the same rules as ws/check.go (reserved opcodes, control frames ≤ 125 bytes, RSV = 0, masking only from the client) — so that a broken peer fails with one clear error. The error branches are marked std::hint::cold_path.
  • src/conn.rs — the connection as a byte stream (WebsocketConn): reading gives out the payload of binary and continuation frames, skips text ones, answers control ones in place; writing — one binary frame per non-empty write, without fragmentation; a peer's close: Ok(0) for a normal code, otherwise an error (wrapWsError).
  • src/options.rs — the transport.ws block and what follows from it (the path, headers, early data).

Tests ​

modules/transports/ws/tests/ws.rs (both sides on TCP, early data at the boundary, refusal by path), tests/regressions.rs; unit tests in all src/ files.

GPL-3.0-or-later