English
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).
| Crate | ours-mod-transport-ws, modules/transports/ws |
| Binary feature | transport-ws |
| Crate features | none |
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 decoderours_transport_api::variant::decodewith this module'sKIND.Provide::Schema { name: "transport.ws" }— the fields ofV2RayWebsocketOptions.
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 sendsGET <path>withHostfrom the header or the server name,Upgrade,Connection,Sec-WebSocket-Version: 13, a random key, and checksSec-WebSocket-Accept.src/handshake.rs— the upgrade request, the101response and the checks of both sides (likegithub.com/sagernet/ws).src/frame.rs— the 2–14-byte header, the mask, the close body; the same rules asws/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 markedstd::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— thetransport.wsblock 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.