Skip to content

mux ​

sing-mux multiplexing: many streams over one connection opened by an outbound. Three protocols: smux and yamux (the framing of their Go libraries) and h2mux (HTTP/2, every stream is a CONNECT). On top, all of them share the same sing-mux protocol: a session request, then a request and a response per stream. Plus Xray's mux.cool client (session frames over a connection to v1.mux.cool).

Crateours-mod-mux, modules/protocols/mux
Binary featuremux-smux, mux-yamux, mux-h2, mux-xray, mux-brutal, mux-padding (each = ours-mod-mux with its feature, which only the binary switches on, + the feature of the same name of vless, trojan, shadowsocks, which gates their own code); mux enables all six
Crate featuressmux, yamux, h2mux (the only one with ours-h2), cool (the mux.cool client), brutal, padding; all in default; consumers link the crate without default features

requires ​

Nothing: the module opens the block and the point. It is required by vless, trojan and shadowsocks (with any mux-* feature), which take its multiplexers through the contract (ours_api::mux) without naming the crate.

provides ​

  • Provide::Point("mux") — the entities' mux block.
  • Provide::Block { block: "mux", key: "h2mux" | "smux" | "yamux" } — an outbound's block names the protocol with a key (OutboundConfig).
  • Provide::Block { block: "mux", key: "xray" } — Xray's mux.cool on an outbound (xray::XrayConfig), VLESS only.
  • Provide::Block { block: "mux", key: "inbound" } — an inbound's block (InboundConfig): the server accepts whichever protocol the client speaks.
  • Provide::Extension { point: "mux", key: "smux-server", value: &Sessions } — trojan-go's smux server (ours_api::mux::Sessions), in every build; without the smux feature it has none and trojan answers «unknown command».

The blocks' value is the contract's handle, the canonical form is the decoded options: an outbound's ours_api::mux::Client (MuxClient: cool, wrap; limits, padding and brutal are checked when the outbound is built), an inbound's ours_api::mux::Server (a MuxServer, or the error the inbound answers when it is built, such as «multiplex: padding is not built in»).

Syntax: the mux block.

Dependencies ​

ours-h2 (h2mux), ours-api, ours-bufpool, ours-net-sys (TCP Brutal), ours-codec, ours-json, bytes, rand_core + getrandom, tokio.

Design ​

  • src/config.rs — MODULE, OutboundConfig, InboundConfig, Brutal, the handles of the block.
  • src/client.rs — the client (sing-mux's client.go): a session is opened when a stream first needs it; streams are kept with a margin up to the limit; idle sessions are closed. The dialer is the whole outbound, it is asked for an entire multiplexed connection, not a stream. With Brutal the client keeps one session and runs the speed exchange right after opening it.
  • src/brutal.rs — TCP Brutal (sing-mux's brutal.go): a stream to _BrutalBwExchange, the receive speed of each side, TCP_CONGESTION brutal and TCP_BRUTAL_PARAMS on the socket (needs the tcp-brutal kernel module). The client's socket is a duplicate of the first TCP socket opened by the session's dial (ours_api::stream::capture_socket); the server's socket is provided by the inbound.
  • src/outbound.rs — the wrapper of the protocol's outbound: the sessions' connections are its connect_stream to sp.mux.sing-box.arpa:444.
  • src/server.rs — the server: accepts the connection's streams, reads their request and hands them to the router (ours_api::mux::Route) as ordinary connections; a stream to _BrutalBwExchange is the Brutal exchange; MuxServer for ServerConfig and trojan-go's smux server (Sessions).
  • src/packet.rs — UDP over a sing-mux stream: the length and the packet, with packet_addr also the address.
  • src/cool.rs — mux.cool frames (Destination over ours_wire::cool); the single session of v2ray-plugin is the plugin's own (shadowsocks, src/plugin/cool.rs).
  • src/xray.rs — Xray's mux.cool client (common/mux/client.go): a connection takes sessions while fewer than concurrency are open and fewer than 128 have been in total; an empty one is closed after 16 s; a UDP packet larger than 8 KiB is dropped (Xray tears the whole connection down on it).
  • src/session.rs — a session of any protocol and the adapters that add a stream's request/response.
  • src/protocol.rs — a stream's request with a Destination, and reading and writing the requests and responses of protocol.go, client_conn.go, server_conn.go; the bytes are ours_wire::sing_mux.
  • src/smux.rs — smux v1: an 8-byte header (ours_wire::smux), no window update; receiving stops while more than max_receive_buffer is unread.
  • src/yamux.rs — yamux: a 12-byte header (ours_wire::yamux), a 256 KiB window per direction, WINDOW_UPDATE.
  • src/h2mux.rs — h2mux on top of ours-h2.
  • src/padding.rs — sing-mux padding: the first 16 writes and reads with a length header and 256–767 random bytes.

Tests ​

modules/protocols/mux/tests/interop.rs — the crate's client and server on one connection, all protocols, keepalive, many streams, the client's pool; unit tests in almost all src/ files. With the references — tests/interop/protocols_singbox.py (trojan, shadowsocks, Brutal) and tests/interop/vless_xray_mux.py (mux.cool with Xray).

Fuzzing — fuzz/, a separate cargo-fuzz project, like libs/h2/fuzz: cool (Mux.Cool frames and the address), request (sing-mux's stream request read off a stream), smux and yamux (a server session on hostile input, all streams read to the end). Run with cargo +nightly fuzz run <target> from modules/protocols/mux. The frame and request codecs alone are fuzzed in libs/wire/fuzz.

GPL-3.0-or-later