Skip to content

trojan ​

Trojan (sing-box's protocol/trojan and the transport/trojan service): TLS always on both sides, then the SHA-224 of the password in hex, CRLF, the command, the destination, CRLF; then a stream or packets (destination, length, CRLF, data) inside the same TCP connection.

Crateours-mod-trojan, modules/protocols/trojan
Binary featurestrojan-inbound, trojan-outbound (trojan enables both) (enables tls)
Crate featurestls (one cfg in src/), unstable-ktls — the TLS of the tests (ours-mod-tls, a dev-dependency); mux — the mux block; mux-smux, mux-yamux, mux-h2, mux-xray, mux-brutal, mux-padding — enable mux and gate only the crate's own code (the binary's mux-* switch on the multiplexers of ours-mod-mux); trojan-go multiplexing needs mux-smux; the binary links the crate without default features

requires ​

dial, tls, transport (and mux with the mux feature) — connections to the server; TLS; the multiplexers: the handles of the mux block and the variant smux-server of the point mux (ours_api::mux). The crate calls ours-transport-api.

provides ​

  • Provide::Blocks: inbound — tls, transport, mux; outbound — tls, transport, mux, dial (mux — with the mux feature).
  • Provide::Inbound/Provide::Outbound { key: "trojan" }, the schemas inbounds.trojan, outbounds.trojan.

Syntax: trojan.

Dependencies ​

ours-tls-options, ours-tls13 (the TLS 1.3 stream type), ours-transport-api, ours-net-sys, ours-bufpool, ours-api, ours-codec, sha2, arc-swap.

Design ​

  • src/protocol.rs — the password key, the request header, address framing for UDP.
  • src/conn.rs — what Trojan runs over, and the wrapper the inbound hands the relay: the request is read first, and whatever was read beyond it goes to the relay first; ServerStream implements Splice.
  • src/udp.rs — UDP inside TCP; writes accumulate until the next flush, so that a batch of packets fills TLS records instead of taking one each.
  • src/replay.rs — an optional replay filter by the TLS client random (sing's common/replay). sing-box does not use it for Trojan, so the inbound enables it only when built through build_inbound_with.
  • src/mux.rs — trojan-go multiplexing (transport/trojan/mux.go): the command 0x7f, an smux session without keepalive, a stream begins with a command and a destination.
  • src/inbound.rs, src/outbound.rs, src/options.rs. A request to sp.mux.sing-box.arpa is a sing-mux session (MuxServer::serve of the block's ours_api::mux::Server), an outbound with a mux block — MuxClient::wrap; the command 0x7f takes the smux server from Extensions, without it — «unknown command».

Reload ​

Users reload in place, as in http. validate_hot checks the new list: an invalid one rejects the reload.

Limitations ​

fallback and fallback_for_alpn (present in sing-box 1.14), the inbound's detour and netns are rejected.

Tests ​

modules/protocols/trojan/tests/trojan.rs — TCP and UDP through TLS, a wrong user, a repeated handshake. Unit tests in protocol.rs, udp.rs, replay.rs.

GPL-3.0-or-later