Skip to content

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 transport module opens the point: Provide::Point("transport") (modules/transports/transport/src/lib.rs). The interface is in crates/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; currently Caps::STREAM).
  • A transport module adds a variant of the transport block — Provide::Block { block: "transport", key: "<name>", decode }. The kernel (Modules::new) requires the point to be opened by a module from requires, hence requires: &["transport"].
  • A protocol does not know transports by name: it declares the block in Provide::Blocks { …, blocks: &["tls", "transport", …] } and at build time takes ours_transport_api::client(blocks, &setup, Caps::STREAM) / server(...). If the transport does not give the needed Caps, the build fails with transport `<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 ​

  1. The crate modules/transports/<name>, the package ours-mod-transport-<name>, dependencies ours-api, ours-codec, ours-transport-api (+ ours-bufpool, ours-h2 and so on as needed) — following module.md.

  2. The block options — #[derive(ours_codec::Options)], like Config in modules/transports/ws/src/options.rs; checks of values that would otherwise surface as an error on the wire — at construction (Options::new).

  3. Implement ClientTransport and ServerTransport (kind, alpn, connect/accept). The handshake is bounded by Setup::handshake_timeout (HANDSHAKE_TIMEOUT by default).

  4. The kind description and the module:

    rust
    const 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::decode and Configured work — Points and extensions.

  5. The binary: the feature transport-<name> = ["dep:ours-mod-transport-<name>"], a line in LIST under #[cfg(feature = "transport-<name>")].

  6. The kind's JSON Schema — the module's part Provide::Schema { name: "transport.<name>" } (example above): the loader assembles the schema's transport block from such parts of the build's modules.

  7. 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 is modules/transports/ws/tests/ws.rs, regressions — modules/transports/ws/tests/regressions.rs.
  • Together with a protocol — the trojan/vless tests and tests/interop/vless_singbox.py (transports, TLS, REALITY).
  • E2E: scenarios like trojan-ws, httpupgrade-tls in tests/e2e/scenarios/ — see E2E.

GPL-3.0-or-later