English
adblock
Filtering client DNS queries by AdGuard, hosts, domain and .srs lists: blocking, exceptions, $dnsrewrite, per-client settings, statistics, a query log, the Admin API. The whole adblock section is applied in place.
| Crate | ours-mod-adblock, modules/dns/adblock |
| Binary feature | adblock (enables dns) |
| Crate features | none |
requires
dns— extends itsdns_filterpoint (the keyskip_adblock) and plugs into its hookours_dns_types::filter::Hook. The crate does not depend onours-mod-dns: the link goes throughours-dns-types.dial— lists withoutviaare downloaded through aDialerwith the configuration's DNS.tls— HTTPS downloads: clients from theours_tls_options::Tlsfactory the module publishes.
provides
Provide::Section { key: "adblock" }—syntax::section→Options,Decoded::hot(), without parts: the section does not get into the reload graph and rebuilds nothing.Provide::Schema { name: "adblock" }.Provide::Extension { point: "dns_filter", key: "skip_adblock" }.Provide::Unit(runtime::Unit)— one per instance; with the section it publishesArc<Shared>,Hook(Shared),Vetoes(Shared)and anAdminwith theadblockendpoints;commitcallsShared::configure.
Syntax: adblock.
Design
src/rules.rs— parsing lines: hosts, domains, AdGuard (@@, modifiers,/regex/),.srs(throughours_srs::read, domains and AdGuard rules from the dump). The rules||d^,|d^,d, hosts with a zero address (with or without$important) are name keys: the reversed name and a tag (bits: subdomains, exception, important), all in one buffer. The rest is aRulewith a pattern (*, the anchors|/||, the end^/|) or aregex, clients, types, a rewrite.src/names.rs—Names: the keys as 56-bit hashes (wyhash mixing, fixed constants), one Elias–Fano set per tag;walkhashes each suffix of the reversed name up to a.and checks every tag's set. About 5 bytes a key; a name not listed matches with probability n / 2^56 per suffix (the odds for users:docs/usage/config/adblock.md, "False matches").src/list.rs—Compiled:Namesover the keys, the other rules with an index on three bytes of the longest literal part (candidates by the name's trigrams; regular expressions are always checked), texts for$badfilter, warnings with the line. The on-disk format (OAB2): the hashes with their tags, 8 bytes each, from whichNamesis built again, and the text of the other rules (parsing them on read is cheap). The content hash is FNV-1a.src/engine.rs— theEngineof one configuration: lists (Arc<Compiled>), client profiles,verdict(onewalkper list; AdGuard Home's priority), blocking responses and$dnsrewrite, response targets (CNAME, A/AAAA).src/runtime.rs—Shared(lives for the instance's whole lifetime):ArcSwapOption<Engine>, sources (Loadedby the key "kind + URL/path": one source is compiled once), statistics, the log, pause and stop.configurecompares the sources: the same ones stay as they are, changed ones (update,via) get a new task but keep what was compiled; own rules are compiled synchronously when they change. A source's task: first the compiled file from disk (stats.path/lists/<key hash>.bin, the time is the mtime), then a download on schedule or rereading the file through aWatcher; identical content is not compiled, compilation is inspawn_blocking, thenrefreshbuilds a newEngineand swaps it atomically.impl Filter— checking a name and a response, recording statistics and the log;impl Veto—enforce_on_route.src/stats.rs— counters per hour, per day — tops (up to 1000 keys a day, rare ones are evicted), lists, upstreams (queries, time, unchecked); JSON to disk every 10 minutes (Shared::flush) and on close.src/querylog.rs— a ring of entries, a JSON lines file in portions (up to 2000 lines or 10 minutes), rotation at 8 MiB, address anonymization.src/api.rs—ours_api::admin::Endpoints:status,stats,log,pause,stop,update,check,clear_stats.
Where the module meets the others
| Point | What | Where |
|---|---|---|
dns_filter | skip_adblock on upstreams and to | modules/dns/dns/src/syntax.rs |
ours_dns_types::filter::Hook | checking a name before the cache, a response after it | modules/dns/dns/src/router.rs (Gate) |
ours_api::dispatch::Vetoes | refusing a connection after the route rules | modules/system/dispatch/src/lib.rs (route) |
ours_api::admin::Admin | /api/v1/adblock/… | app/ui/src/api.rs |
adblock in the loader | a top-level section | crates/native-config/src/entity.rs |
Memory and speed
examples/match_cost.rs: compilation, memory and decision time for names from the list and random ones. Real lists (AdGuard DNS filter, ~190 thousand rules; OISD big, ~229 thousand) take ~3.4 MiB each, a decision over two lists — about 2 µs per name on a workstation.
Tests
src/*— parsing modifiers, patterns,$dnsrewrite, the on-disk format, statistics (sums, retention, restart, eviction).tests/rules.rs— formats with per-line warnings, AdGuard Home semantics (exact and suffix rules, priority, allowlists,$badfilter,$client,$dnstype,$dnsrewrite, the blocking response, response targets),.srsagainst the sing-box matcher.tests/reload.rs— an instance withdns: togglingenabledkeeps the DNS and the compiled list (Arc::ptr_eq),skip_adblockon a rule and an upstream, the Admin API (statistics, the log, check, pause, stop), clients, a restart with the compiled list and statistics from disk,enforce_on_route.modules/dns/dns/tests/filter.rs— the hook: blocking before the cache and the upstream, the cache on top, skipping by upstream.tests/dashboard/test_admin.py— the/api/v1/draftdraft, the module's endpoints through the Admin API.