Skip to content

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 address listen: host:port is split into listen + 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 with users that it can change on the fly (hot_changed): users are 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_tcp is enabled by presence, dialer members are forbidden in the protocol block (their place is the dial block).

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) (the ours_config::dial::DialerContext trait; it calls the factory the dial module publishes) — then detour, netns, interface, fwmark mean the same as in direct. Hence requires: &["dial"]; the crate does not depend on ours-mod-dial (MODULE_EDGES in tests/layers.py).
  • An inbound implements ours_api::Inbound: start(dispatcher, shard, sockets) — open the shard's sockets and pass connections to dispatcher.route_stream/route_packet; close. Reload starts the replacement before closing the old one: listening sockets are handed over through Handover (ours_net_sys::inherited_tcp_listener and others), and only to a successor that reports the same socket_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's detour.
  • 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.
  • requires and the blocks depend on the crate's own mux feature: the binary turns it on with any mux-* feature (ours-mod-trojan?/mux-smux), and only then do the sides need the mux module.
  • 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. The tls/transport/mux blocks are taken from Blocks at build time: ours_transport_api::client(blocks, &setup, Caps::STREAM) (see transport.md), TLS — through the factory ours_tls_options::Tls::of(cx.services) (client, server) and ours_api::tls.
  • Provide::Schema named inbounds.<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>-inbound and <protocol>-outbound (each with "tls" if TLS is needed), an umbrella <protocol> = ["<protocol>-inbound", "<protocol>-outbound"], and two lines in LIST:

    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 module LIST (dispatch, dial, tls, cache, direct, the new one), ours_testkit::config(...), ours_kernel::Instance; the port — ours_testkit::free_port(). The model is modules/protocols/socks/tests/socks.rs.
  • Compatibility with sing-box in both directions — tests/interop/protocols_singbox.py (the pair description in tests/interop/pair.py) and the compatibility matrix of the external benchlab harness (Benchmarks).
  • An E2E scenario tests/e2e/scenarios/<name>/ — check.sh, ours.yaml, sing-box.json; it must pass with PROXY=sing-box and PROXY=ours — see E2E.

GPL-3.0-or-later