Skip to content

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.

Crateours-mod-adblock, modules/dns/adblock
Binary featureadblock (enables dns)
Crate featuresnone

requires ​

  • dns — extends its dns_filter point (the key skip_adblock) and plugs into its hook ours_dns_types::filter::Hook. The crate does not depend on ours-mod-dns: the link goes through ours-dns-types.
  • dial — lists without via are downloaded through a Dialer with the configuration's DNS.
  • tls — HTTPS downloads: clients from the ours_tls_options::Tls factory 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 publishes Arc<Shared>, Hook(Shared), Vetoes(Shared) and an Admin with the adblock endpoints; commit calls Shared::configure.

Syntax: adblock.

Design ​

  • src/rules.rs — parsing lines: hosts, domains, AdGuard (@@, modifiers, /regex/), .srs (through ours_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 a Rule with a pattern (*, the anchors |/||, the end ^/|) or a regex, clients, types, a rewrite.
  • src/names.rs — Names: the keys as 56-bit hashes (wyhash mixing, fixed constants), one Elias–Fano set per tag; walk hashes 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: Names over 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 which Names is built again, and the text of the other rules (parsing them on read is cheap). The content hash is FNV-1a.
  • src/engine.rs — the Engine of one configuration: lists (Arc<Compiled>), client profiles, verdict (one walk per 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 (Loaded by the key "kind + URL/path": one source is compiled once), statistics, the log, pause and stop. configure compares 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 a Watcher; identical content is not compiled, compilation is in spawn_blocking, then refresh builds a new Engine and 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 ​

PointWhatWhere
dns_filterskip_adblock on upstreams and tomodules/dns/dns/src/syntax.rs
ours_dns_types::filter::Hookchecking a name before the cache, a response after itmodules/dns/dns/src/router.rs (Gate)
ours_api::dispatch::Vetoesrefusing a connection after the route rulesmodules/system/dispatch/src/lib.rs (route)
ours_api::admin::Admin/api/v1/adblock/…app/ui/src/api.rs
adblock in the loadera top-level sectioncrates/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), .srs against the sing-box matcher.
  • tests/reload.rs — an instance with dns: toggling enabled keeps the DNS and the compiled list (Arc::ptr_eq), skip_adblock on 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/draft draft, the module's endpoints through the Admin API.

GPL-3.0-or-later