English
Add a protocol (inbound and/or outbound)
Models: modules/protocols/block (an outbound in 50 lines), modules/protocols/socks (inbound and outbound; the outbound takes a dial block), modules/protocols/trojan (plus the tls and transport blocks). First — the crate and build steps from module.md.
1. Options
src/options.rs — structures with #[derive(ours_codec::Options)] (see codec-derive). Shared parts come from ours_config::options and are embedded with #[config(flatten)]:
rust
#[derive(Options, Clone, Debug, Default, PartialEq)]
pub struct TrojanInboundOptions {
#[config(flatten)]
pub listen: ListenOptions,
pub users: Vec<TrojanUser>,
// ...
}
#[derive(Options, Clone, Debug, Default, PartialEq)]
pub struct TrojanOutboundOptions {
#[config(flatten)]
pub dialer: DialerOptions,
#[config(flatten)]
pub server: ServerOptions,
// ...
}Field names are as in sing-box: the protocol members are the same in both formats.
2. The block decoder
An entity's protocol block is decoded by the ready functions of ours_config (crates/config/src/blocks.rs):
listener::<T>— for an inbound: the addresslisten: host:portis split intolisten+listen_port, the shared listening members take their native names, and sing-box fields that rule actions replace are an error;listener_with_users::<T>— the same for an inbound withusersthat it can change on the fly (hot_changed):usersare not part of the cold form, and changing them does not rebuild the inbound;dialer::<T>— for an outbound:server: host:port→server+server_port,udp_over_tcpis enabled by presence, dialer members are forbidden in the protocol block (their place is thedialblock).
3. Building the component
- An outbound implements
ours_api::Outbound(crates/api/src/adapter.rs):kind,tag,connect_stream,connect_packet; the rest have defaults (supports— which networks it carries). The dialer to the server is taken from the context:cx.dialer(tag, blocks)(theours_config::dial::DialerContexttrait; it calls the factory thedialmodule publishes) — thendetour,netns,interface,fwmarkmean the same as indirect. Hencerequires: &["dial"]; the crate does not depend onours-mod-dial(MODULE_EDGESintests/layers.py). - An inbound implements
ours_api::Inbound:start(dispatcher, shard, sockets)— open the shard's sockets and pass connections todispatcher.route_stream/route_packet;close. Reload starts the replacement before closing the old one: listening sockets are handed over throughHandover(ours_net_sys::inherited_tcp_listenerand others), and only to a successor that reports the samesocket_identity(ListenOptions::socket_identity).validate_hot/hot_changed— changes without a rebuild (users);attach— if the inbound dials itself;inject— if it can be another inbound'sdetour. - Factories have the type
fn(tag, &Blocks, &Context) -> Result<Arc<dyn …>, String>; the options —blocks.protocol::<T>()?.
4. INBOUND and OUTBOUND
A protocol crate describes each side as its own module, so a build can take only one of them. The side left out of LIST is not referenced, and LTO drops its code. A protocol with only one side (block, tun, tproxy) keeps a single MODULE.
rust
#[cfg(feature = "mux")]
const REQUIRES: &[&str] = &["dial", "tls", "transport", "mux"];
#[cfg(not(feature = "mux"))]
const REQUIRES: &[&str] = &["dial", "tls", "transport"];
// BLOCKS and OUTBOUND_BLOCKS add "mux" the same way.
pub const INBOUND: ours_api::Module = ours_api::Module {
name: "trojan-inbound",
requires: REQUIRES,
provides: &[
ours_api::Provide::Blocks { section: "inbounds", key: "trojan", blocks: BLOCKS },
ours_api::Provide::Schema { name: "inbounds.trojan", schema: |_| ours_config::schema::inbound::<options::TrojanInboundOptions>() },
ours_api::Provide::Inbound { key: "trojan", decode: decode_inbound,
build: |tag, blocks, cx| build_inbound(tag, blocks.protocol()?, blocks, cx.services) },
],
};
pub const OUTBOUND: ours_api::Module = ours_api::Module {
name: "trojan-outbound",
requires: REQUIRES,
provides: &[
ours_api::Provide::Blocks { section: "outbounds", key: "trojan", blocks: OUTBOUND_BLOCKS },
ours_api::Provide::Schema { name: "outbounds.trojan", schema: |_| ours_config::schema::outbound::<options::TrojanOutboundOptions>() },
ours_api::Provide::Outbound { key: "trojan", decode: ours_config::dialer::<options::TrojanOutboundOptions>,
build: |tag, blocks, cx| build_outbound(tag, blocks.protocol()?, blocks, cx) },
],
};name—<protocol>-inbound/<protocol>-outbound, the names of the binary's features.requiresand the blocks depend on the crate's ownmuxfeature: the binary turns it on with anymux-*feature (ours-mod-trojan?/mux-smux), and only then do the sides need themuxmodule.key— the key of the protocol block in YAML (outbounds: { name: { trojan: {...} } }) and the type in sing-box.Provide::Blocks— which other blocks the entity accepts (tls,transport,dial,mux). A type that did not declare them accepts no block decoded by modules. Thetls/transport/muxblocks are taken fromBlocksat build time:ours_transport_api::client(blocks, &setup, Caps::STREAM)(see transport.md), TLS — through the factoryours_tls_options::Tls::of(cx.services)(client,server) andours_api::tls.Provide::Schemanamedinbounds.<key>/outbounds.<key>— a JSON Schema part;ours_config::schema::inbound/outbound::<T>()build it from the field table.
5. Outside the module
The binary (module.md): the features
<protocol>-inboundand<protocol>-outbound(each with"tls"if TLS is needed), an umbrella<protocol> = ["<protocol>-inbound", "<protocol>-outbound"], and two lines inLIST:rust#[cfg(feature = "trojan-inbound")] ours_mod_trojan::INBOUND, #[cfg(feature = "trojan-outbound")] ours_mod_trojan::OUTBOUND,Subscriptions: for a server of this protocol to be imported, the protocol must be in
PROTOCOLS(modules/config/subscriptions/src/lib.rs), and the formats must be able to read it (subscription-format.md).
6. Tests
modules/protocols/<name>/tests/— an end-to-end test on loopback: its own moduleLIST(dispatch,dial,tls,cache,direct, the new one),ours_testkit::config(...),ours_kernel::Instance; the port —ours_testkit::free_port(). The model ismodules/protocols/socks/tests/socks.rs.- Compatibility with sing-box in both directions —
tests/interop/protocols_singbox.py(the pair description intests/interop/pair.py) and the compatibility matrix of the externalbenchlabharness (Benchmarks). - An E2E scenario
tests/e2e/scenarios/<name>/—check.sh,ours.yaml,sing-box.json; it must pass withPROXY=sing-boxandPROXY=ours— see E2E.