Skip to content

Add a subscription format ​

Subscriptions are read by the subscriptions module (modules/config/subscriptions, the ours-mod-subscriptions crate). A format is a reader function; the module tries the readers in order and translates the understood servers into outbounds of the native schema, which the protocol modules then decode at load time.

How it works ​

  • parse(content, supported) (modules/config/subscriptions/src/lib.rs):
    1. the whole content may be in base64 (decode_base64) — any format, not only a list of links;
    2. empty ("", [], {}) — Error::Empty;
    3. the readers from READERS in order; the first that returns Some(Collected) decides; if none does, Error::Unrecognized;
    4. finish drops servers of protocols not in PROTOCOLS or not built into the binary (supported), then chains with a dropped link (dials through a server that could not be imported); nothing left — Error::NoneImported with the first reason.
  • type Reader = fn(&str) -> Option<Collected>: None — "not my format".
  • READERS — the order from the strictest to the loosest: Xray JSON (recognized by its members), Clash (by proxies), links — last.
  • Collector (src/collector.rs) — what a reader gives its result through: add(tag, name, label, outbound), claim/add_claimed (a tag in advance, for detour), group(name, members, rank), skip(tag, label, reason) (with a tag, those that dial through it drop out too), direct(tag) (freedom/DIRECT: dialing through it is not a detour), discard(tag) (REJECT), label(index, name), finish().
  • json::Outbound/Object/Transport (src/json.rs) — building an outbound the way the native schema writes it. Shared parsing (uTLS, ECH, VLESS flow, ports, bandwidths, group ranks) is in src/common.rs.

Steps ​

  1. The file modules/config/subscriptions/src/<format>.rs with pub fn read(content: &str) -> Option<Collected>: return None quickly if the content does not look like the format (as links::read checks for ://); otherwise Collector::new(FORMAT), per server — add or skip with a reason, finish().

  2. Labels (label) name a server by position and name, never by address or credentials: the report about skipped servers is visible in logs and the dashboard.

  3. A feature in modules/config/subscriptions/Cargo.toml: <format> = [...] (an external parser — dep:), add it to default. In src/lib.rs:

    rust
    #[cfg(feature = "<format>")]
    mod <format>;
    // and in READERS — at its place by strictness:
    #[cfg(feature = "<format>")]
    <format>::read,

    If the format uses common/xhttp, add it to their #[cfg(any(...))].

  4. The binary: sub-<format> = ["subscriptions", "ours-mod-subscriptions/<format>"] in app/ours/Cargo.toml and in default. No line in LIST is needed: there is one module (subscriptions), and the format is its feature.

Tests ​

modules/config/subscriptions/tests/formats.rs with fixtures in modules/config/subscriptions/tests/data/: per format — a sample file and <file>.expected.json (the result of render), plus tests for skips and errors under #[cfg(feature = "<format>")]. Loading subscriptions in a configuration (names, filters, groups) — crates/native-config/tests/subscriptions.rs.

GPL-3.0-or-later