Skip to content

Layers ​

The repository is a single Cargo workspace. Crates are laid out in directories by role, and the role determines what a crate may depend on.

text
app/ours, app/ui          binary and Admin API/dashboard
        │
        ├── crates/kernel (ours-kernel)       microkernel
        │        │
        ├── modules/<group>/<name> (ours-mod-*)   modules
        │        │
        ├── crates/* auxiliary              ours-relay, ours-tracker, ours-quic, ...
        │        │
        └── crates/api (ours-api)             contract
                 │
              libs/*                          libraries with no knowledge of proxying

Arrows go top-down: an upper layer may depend on lower ones, never the other way round.

Rules at a glance ​

All of them are checked by tests/layers.py.

RuleStatement
P-1libs/* depend only on libs/* (dependencies of every kind).
P-2ours-api depends only on libs/* and a short list of external crates; not on ours-sniff, ours-net-info, ours-config.
P-3ours-config depends on ours-api and libs/*; ours-api does not depend on it.
P-4Every crate under modules/ has a level (M1-M7 below); a module depends on lower levels or over an edge listed with its reason inside a level. A module without a level is an error. SAME_LEVEL is empty: there are no edges inside a level.
P-5A crate outside modules/ and app/ does not depend on a module.
P-6ours-config is read by modules, ours-kernel, ours-native-config, app/* and the shared crates in CONFIG_READERS (ours-dns-types, ours-http, ours-http-config, ours-proto, ours-quic, ours-route-api, ours-tls-options); the list only shrinks.
P-7quinn, quinn-proto only under ours-quic and ours-quinn-ext; rustls, tokio-rustls only under ours-tls13, ours-mod-tls and ours-quic. No exceptions remain.
P-8The sans-IO codecs in ours-wire take no tokio; destinations are read and written only by ours-wire and the contract's bridge to it.
P-9A module depends on another module's crate only over an edge in MODULE_EDGES; a stale entry is an error and the list only shrinks. One permanent entry remains: clients -> wireguard.

Module levels ​

Bottom to top. A module may depend (normal and build dependencies) only on lower levels.

LevelModulesMay depend on
M1 base servicessystem/tls, system/dial, system/cache-
M2 servicessystem/dispatch, system/ntpM1
M3 transport basetransports/transportM1-M2
M4 transportstransports/{ws, grpc, http, httpupgrade, xhttp}M1-M3
M5 multiplexerprotocols/muxM1-M4
M6 protocols, DNS, routingprotocols/* except mux, dns/*, routing/*M1-M5
M7 configurationconfig/{subscriptions, clients}M1-M6

Apart from the one edge in P-9, modules do not depend on another module's crate: what a lower level offers travels as a type through the contract or a shared crate, and the implementation comes at run time from Services or Extensions. Tests build a component from its own Context and take the lower module's implementation as a dev dependency, which the rules do not restrict.

libs/ — libraries ​

Reusable parts with no notion of "connection", "inbound" or "sing-box configuration": YAML/JSON (ours-yaml, ours-json), the option codec (ours-codec, ours-codec-derive), DNS messages (ours-dns-wire), HTTP/2 and an HTTP client (ours-h2, ours-http-client), TLS 1.3 with REALITY and uTLS (ours-tls13, ours-ktls-sys), sniffing (ours-sniff), the .srs format (ours-srs), Linux sockets (ours-net-sys, ours-sys, ours-net-info), buffer pools (ours-bufpool), an on-disk journal (ours-journal), ours-atomic64 and others. In detail — Libraries.

Rule: a library depends only on other libs/ crates (and external crates).

The platform is Linux only. ours-runtime implements the host and socket runtime using ours-net-info and ours-net-sys. The contract describes the host in its own types and still exposes splice capabilities. A non-Linux build stops at a compile_error! in ours-net-sys.

crates/api — the contract (ours-api) ​

Everything the kernel and modules agree on: the module description (Module, Provide), the traits Inbound/Outbound/RuleSet/Dns/Dispatch, connection metadata, streams and packet streams, the decoded configuration (ours_api::config), shared option types (ours_config::options), the service registry. See Contract.

Rule: ours-api depends only on libs/ and, of external crates, only on arc-swap, tokio and tracing. Whatever the contract pulls in ends up in every build, so the list is short.

ours-api is internal and evolves with the in-tree modules. TLS, transport and mux modules own their shared option types, block decoders and schema parts. See Component types.

crates/config and crates/runtime ​

ours-config parses the configuration: node values, the blocks entities share, the names they lower to, the schema helpers modules describe their blocks with, the defaults, and the shared option types (ours_config::options, ours_config::dial). It sits above ours-api and below the modules; the kernel, ours-native-config, the binary, the modules and the shared crates in CONFIG_READERS read it (P-6). ours-runtime is the host and socket runtime: listening, shard placement and the Host implementation. It also holds the QUIC window budget that the kernel updates and QUIC endpoints observe. The kernel and the inbounds use it.

crates/kernel — the microkernel (ours-kernel) ​

Checks the binary's module list, builds from Config the component graph, the reload plan, the instance (Instance) and the shards (Runtime). It knows no protocol itself: it gets everything through the modules' Provide. See Kernel.

The kernel depends on ours-api, ours-config, ours-runtime and libs/.

modules/ — modules (ours-mod-*) ​

Every crate under modules/<group>/<name> is named ours-mod-* and exports one or more ours_api::Module constants: MODULE, or INBOUND and OUTBOUND for a protocol. Directory groups: system (dispatch, dial, tls, cache, ntp), transports, protocols, routing (router, group, rule-process), dns (dns, adblock), config (subscriptions, clients). A description per module — Modules.

Rules:

  • a module does not depend on ours-kernel;
  • a module does not depend on another module's crate; it takes the type from the contracts of the lower modules (see below) and the implementation from Context::services or Extensions. The only permanent exception is clients → wireguard (client configurations are read and written in the endpoint's wg-quick format); it is listed in MODULE_EDGES of tests/layers.py with its reason, and such a dependency must be in requires;
  • a crate under modules/ must be named ours-mod-*, and vice versa.

crates/* — auxiliary crates ​

Not modules (they have no MODULE) and not libraries (they know the contract): ours-relay (data pumping, UDP NAT), ours-tracker (live connections), ours-uot (UDP-over-TCP), ours-quic (QUIC endpoints for hysteria/hysteria2/tuic), ours-transport-api (the transport contract), ours-http (HTTP client for downloads), ours-http-config (HTTP client options), ours-dns-types (DNS types shared by router and dns), ours-native-config (the YAML loader), ours-auto-nft (auto_nft for tproxy), ours-testkit (for tests). See Auxiliary crates.

For them layers.py checks the bans on ours-kernel and on module crates: what they take from a module comes through the contract (ours-http — TLS clients, ours_api::tls; ours-quic — the dialer, ours_api::udp).

The contracts of the lower modules live in ours-api and ours-config: ours_api::dial (the dialer and the dial table), ours_api::tls (TLS clients and servers over a stream, TLS for QUIC), ours_api::mux (the multiplexer), ours_api::cache (the cache file), and the Dispatch methods inject and inject_packet (hand a stream or a datagram to an inbound by tag, without a socket); ours_config::{dial, options} carry the option types and the dialer factory. The shared crates: ours-transport-api, ours-tls-options, ours-proto (what the protocol modules share: h2, http, socks, http1, users, fallback, decoy).

app/ — the binary and UI ​

app/ours puts everything together: the module list LIST under features (app/ours/src/modules.rs), the commands (run, check, explain, format, schema, clean, tools, client, …), the reload loop driven by signals, --watch, subscriptions and the Admin API. app/ui is the Admin API and the dashboard. Only crates under app/ may depend on ours-kernel. See Building the binary.

How the rules are checked ​

CheckWhereWhat it catches
tests/layers.pycargo metadata --no-depsthe dependency rules, listed below
a_module_requires_the_modules_its_crate_depends_onapp/ours/src/modules.rsa module whose crate depends on another module's crate did not list it in requires (an optional dependency counts only if its module is in the build)
const _: () = assert!(requires_are_listed(LIST))app/ours/src/modules.rsa feature enables a module without what it requires: the build does not compile
Modules::newcrates/kernel/src/modules.rsduplicate names and keys, missing requires, extending someone else's point without requires, a cycle, no dispatcher — see Module order

tests/layers.py catches:

  • libs/ → anything outside libs/;
  • ours-api → a module, or an external crate outside its list;
  • ours-kernel → anything other than ours-api, ours-config, ours-runtime and libs/;
  • anyone outside app/ → ours-kernel;
  • an ours-mod-* crate not under modules/, and vice versa;
  • a normal or build dependency between two module crates that is not in MODULE_EDGES, and an entry of MODULE_EDGES without such a dependency;
  • the return of the removed ours-singbox-convert crate;
  • the rule walk or written conditions back in ours-api;
  • tokio under ours-wire;
  • quinn, quinn-proto or h3-quinn outside ours-quic/ours-quinn-ext (any kind, no exceptions);
  • rustls or tokio-rustls as a normal or build dependency outside ours-tls13/ours-mod-tls/ours-quic.

The table "module → modules whose crates its crate depends on" is built by the binary's build.rs (uses.rs) from the manifests' [dependencies]; in detail in Building the binary.

For libs/, layers.py checks dependencies of every kind, including dev and build; the other rules — only normal dependencies (a module's dev dependency on ours-testkit or on the kernel in tests is allowed).

GPL-3.0-or-later