Skip to content

Services ​

crates/api/src/services.rs.

Services is what modules publish for each other while a configuration is being built. The key is the type: at most one value per TypeId.

rust
impl Services {
    fn new() -> Services;
    fn of<T>(value: Arc<T>) -> Services;
    fn insert<T>(&mut self, value: Arc<T>);          // replaces the previous value of the type
    fn get<T>(&self) -> Option<Arc<T>>;
    fn require<T>(&self) -> Result<Arc<T>, String>;  // "T is not available in this build"
}

Why by type and not by name: the consumer depends on the provider's crate for the type anyway, and a crate dependency obliges listing the module in requires (requires) — so the provider is in the build and its unit has run earlier. A require error is possible for a service that the provider does not always publish (the cache file — only when it is enabled); such a consumer uses get and does without it.

Where services live ​

Who publishesWhenWhere it goes
the kernelat the beginning of every build()Arc<DialTable>, Arc<Extensions>, OwnMark, ours_clock::Clock, QuicWindowBudget
module unitsUnit::build, in module orderthe services of this configuration
the dispatcher factoryonce in Instance::newthe instance's services

The configuration's services are seen by: Build::services of the following units, Context::services of the inbound/outbound/rule-set factories, DnsContext::services, attach_services of DNS, outbounds and rule-sets. From outside — Instance::service::<T>(): first the services of the running configuration, then those of the instance. The table of what modules publish — Units and services.

Frequently used ​

TypeProviderWho takes it
ours_api::DialTablethe kernelthe ntp, subscriptions units
ours_config::dial::Dialers (publish/dialers)dialoutbounds with a dial block (cx.dialer), DNS transports, subscriptions, adblock
ours_api::Extensionsthe kernelrouter rule-sets (modules/routing/router/src/rule_set.rs), vless (the transport over QUIC), shadowsocks (v2ray-plugin: the ws transport)
ours_tls_options::TlstlsHTTP downloads: router remote rule-sets, group probes, adblock, subscriptions, the binary (fetch, check --fetch); TLS over streams: http, mixed, anytls, trojan, vless, shadowtls, shadowsocks (v2ray-plugin); dns (DoT, DoH), the hysteria2 masquerade
ours_quic::tls::Factorytls (feature quic)the QUIC protocols (hysteria, hysteria2, tuic), the QUIC transport (the variant quic of the point transport, for vless), dns (DoQ, DoH3)
ours_api::cache::Cache (publish/of)cachegroups, DNS, remote rule-sets (the cache may be off)
ours_dns_types::rules::Compilerrouterdns — compiling DNS rules with route conditions
ours_api::dns::FakeRangesdnsthe binary's auto_nft
ours_dns_types::filter::Hookadblockdns — the client query filter (attach_services)
ours_api::dispatch::Vetoesadblockthe kernel puts it into Routing::veto; dispatch asks it after the rules
ours_api::admin::Adminadblockthe Admin API (app/ui/src/api.rs) — /api/v1/<module>/… requests
ours_mod_ntp::Ntpntptests; from outside through Instance::service
ours_tracker::Trackerdispatchthe Admin API (app/ui/src/api.rs)

GPL-3.0-or-later