English
ours-srs
libs/srs — sing-box's binary rule-set format (.srs, common/srs of 1.14.4) and the matchers its rules are stored in.
Format
SRS, a version byte (VERSION_CURRENT), then a zlib stream: the number of rules and the rules themselves. A default rule is a list of items (a type byte and values), terminated by 0xff and an invert flag; a logical one — the mode, the rules and the invert flag.
API
read(thebinarymodule, thebinaryfeature) — a file →RuleSet { rules: Vec<Rule> };Ruleis aDefaultRuleor aLogicalRule.write(thebinaryfeature) — rules → a.srsfile of the given version, as sing-box'ssrs.Write;routerwrites rule-sets with it.succinct— sing's succinct trie (common/domain/set.go, after openacid/succinct): a LOUDS bitmap, a label per edge, a leaf bit per node. Domains are stored reversed, the search goes from the last label.Matcher(domains),AdGuardMatcher(AdGuard rules):read,matches,dump,build.- Selection (
select) starts from a sample of every 256th one, not from a binary search over all words; the tree is built breadth-first with a queue of one or two levels, the keys are anyAsRef<[u8]>(slices of one buffer, without an allocation per key). IpSet/Prefix— addresses as ranges (from_ranges,contains,ranges).varbin— the format's primitives, like sing'svarbinand Go'sencoding/binary: uvarint, big-endian, length-prefixed slices.
Robustness against corrupted files
- Lengths are not trusted: slices grow as items arrive, so a file that claims more than it contains fails at the end instead of allocating what it claimed.
- A lookup in a set read from a file touches only what the header check confirmed; beyond that — a mismatch, not a panic.
Features
default = ["binary"]; binary pulls in flate2. Without it the matchers remain (inline rule-sets). The binary: rule-set-binary → ours-mod-router/rule-set-binary.
Who uses it
ours-mod-router (an optional dependency) and ours-mod-adblock (reads .srs lists).
Tests and fuzzing
libs/srs/tests/sing_box.rs— files written by sing-box 1.14.4'ssrs.Write(inlibs/srs/tests/data/), and what it reads from them and how it matches.libs/srs/tests/mutations.rs— the same files with random but reproducible mutations: an error or rules that match without a panic.libs/srs/tests/write.rs— rules read from the sing-box files are written back as sing-box wrote them; a version refuses items newer than itself.libs/srs/tests/model.rs— the domain matchers against their rules tried one by one.libs/srs/fuzz— the cargo-fuzz targetsreadandmatcher.