English
Add a transport
A transport is the layer under a protocol (VLESS, Trojan…): it turns one connection into the protocol's byte stream, hiding WebSocket frames, an HTTP upgrade, an HTTP request or gRPC. The model is modules/transports/ws; the common interface is the contract ours-transport-api (crates/transport-api); the point belongs to the transport module (modules/transports/transport).
How the point works
- The
transportmodule opens the point:Provide::Point("transport")(modules/transports/transport/src/lib.rs). The interface is incrates/transport-api:ClientTransport::connect,ServerTransport::accept,Setup(what is known at build time),Request(what is known about the connection),Caps(what the transport carries; currentlyCaps::STREAM). - A transport module adds a variant of the
transportblock —Provide::Block { block: "transport", key: "<name>", decode }. The kernel (Modules::new) requires the point to be opened by a module fromrequires, hencerequires: &["transport"]. - A protocol does not know transports by name: it declares the block in
Provide::Blocks { …, blocks: &["tls", "transport", …] }and at build time takesours_transport_api::client(blocks, &setup, Caps::STREAM)/server(...). If the transport does not give the neededCaps, the build fails withtransport `<name>` does not carry what this protocol needs.
The protocol opens a stream through connect_client. The transport tries to reuse a session first. For a new session it uses a supplied TCP stream or dials through Setup::connector. The protocol supplies the TLS settings and its dialer, including detour and interface binding. On the server, Incoming::serve dispatches logical streams to their shards. For HTTP/3, the QuicListener of the variant quic of the point (ours_transport_api::Quic, which the transport module adds with its quic feature) accepts QUIC and the transport receives a QuicSession. A protocol takes the variant from Extensions; without it a transport over QUIC is refused.
A transport owns each connection it receives. On failure it closes the connection and its tasks. The protocol or mux handles protocol datagrams.
Steps
The crate
modules/transports/<name>, the packageours-mod-transport-<name>, dependenciesours-api,ours-codec,ours-transport-api(+ours-bufpool,ours-h2and so on as needed) — following module.md.The block options —
#[derive(ours_codec::Options)], likeConfiginmodules/transports/ws/src/options.rs; checks of values that would otherwise surface as an error on the wire — at construction (Options::new).Implement
ClientTransportandServerTransport(kind,alpn,connect/accept). The handshake is bounded bySetup::handshake_timeout(HANDSHAKE_TIMEOUTby default).The kind description and the module:
rustconst KIND: ours_transport_api::Kind<Config> = ours_transport_api::Kind { name: "ws", gives: ours_transport_api::Caps::STREAM, client: |o, s| Ok(std::sync::Arc::new(options::client(o, s)?)), server: |o, s| Ok(std::sync::Arc::new(options::server(o, s)?)), }; pub const MODULE: ours_api::Module = ours_api::Module { name: "transport-ws", requires: &["transport"], provides: &[ ours_api::Provide::Block { block: "transport", key: "ws", decode: |node| ours_transport_api::variant::decode(node, &KIND) }, ours_api::Provide::Schema { name: "transport.ws", schema: |_| ours_config::schema::block(ours_config::schema::fields_of::<V2RayWebsocketOptions>(&[])) }, ], };How
variant::decodeandConfiguredwork — Points and extensions.The binary: the feature
transport-<name> = ["dep:ours-mod-transport-<name>"], a line inLISTunder#[cfg(feature = "transport-<name>")].The kind's JSON Schema — the module's part
Provide::Schema { name: "transport.<name>" }(example above): the loader assembles the schema'stransportblock from such parts of the build's modules.Subscriptions, if a format describes this transport:
modules/config/subscriptions/src/json.rs(Transport) and the format readers.
Tests
modules/transports/<name>/tests/— a client against a server on loopback; the model ismodules/transports/ws/tests/ws.rs, regressions —modules/transports/ws/tests/regressions.rs.- Together with a protocol — the
trojan/vlesstests andtests/interop/vless_singbox.py(transports, TLS, REALITY). - E2E: scenarios like
trojan-ws,httpupgrade-tlsintests/e2e/scenarios/— see E2E.