English
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.
| Crate | ours-mod-trojan, modules/protocols/trojan |
| Binary features | trojan-inbound, trojan-outbound (trojan enables both) (enables tls) |
| Crate features | tls (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 themuxfeature).Provide::Inbound/Provide::Outbound { key: "trojan" }, the schemasinbounds.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;ServerStreamimplementsSplice.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'scommon/replay). sing-box does not use it for Trojan, so the inbound enables it only when built throughbuild_inbound_with.src/mux.rs— trojan-go multiplexing (transport/trojan/mux.go): the command0x7f, 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 tosp.mux.sing-box.arpais a sing-mux session (MuxServer::serveof the block'sours_api::mux::Server), an outbound with amuxblock —MuxClient::wrap; the command0x7ftakes the smux server fromExtensions, 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.