Skip to content

DNS ad blocking with dnsmasq lists, no new process (#30) - #79

Merged
Cd1s merged 1 commit into
mainfrom
feat/dns-adblock
Sep 26, 2026
Merged

Cd1s merged 1 commit into
mainfrom
feat/dns-adblock

Conversation

@Cd1s

@Cd1s Cd1s commented Sep 26, 2026

Copy link
Copy Markdown
Owner

What

dns.adblock, off by default. No new process: blocklists become dnsmasq local=/name/ lines that both dnsmasq instances (main and proxy) load.

  • mr dns adblock update
    • Downloads the lists: https only, redirects only to https, at most 64 MiB each.
    • Formats: plain domains, hosts files, adblock ||name^, dnsmasq lines. Element-hiding rules, exceptions and server=/x/# lines are not treated as names.
    • Keeps only valid names, folds names that sit under another listed name, and applies allow.
    • Never blocks the lists' own hosts, the NTP servers or the local domain.
    • An allowed name under a blocked one is forwarded as usual (server=/name/#).
  • An update never makes things worse
    • The previous file is kept when a list fails, is too big, or is not a domain list (under half of its lines usable, e.g. an HTML error page).
    • It is also kept when the result is above max_domains (default 300000).
    • After a new file, dnsmasq restarts. If it doesn't answer within 30 s, the previous file is put back.
    • Unchanged lists don't restart dnsmasq.
  • Schedule: crond runs it hourly at a minute fixed per router.
    • --cron only downloads when the file is 20 h old or was built from other settings. So switching it on, or a failed update, is retried within the hour.
    • It never runs while a change waits for confirmation.
  • Interfaces: CLI mr dns adblock status | update; API dns.adblock (token scope operate); web UI 网络 › DNS › 去广告, with presets HaGeZi Multi NORMAL / LIGHT and anti-AD.

Why

Ad blocking is a common ask. AdGuard Home is about 34 MiB and gets OOM-killed with large lists; dnsmasq ≥ 2.86 handles large local= sets compactly.

Verification

  • Go tests:
    • every list format, folding and allow, validation, render (the empty file is written only when none exists), the cron line;
    • updates against a TLS test server: unchanged lists, cron skip and rebuild, failed / HTML / oversized lists, dnsmasq not coming back.
  • tools/ci.d/dns.sh:
    • the real update pulls 150k names from a local HTTPS server;
    • the real dnsmasq answers listed names and their subdomains with NXDOMAIN;
    • allowed and unlisted names are forwarded.
  • Measured with dnsmasq 2.92 and 150k names: answering 232 ms after start, RSS 18.6 MB (x86_64).
  • sh tools/ci.sh: ALL CHECKS PASSED.

Closes #30

https://claude.ai/code/session_01HWeUjo1dmS7gBkCYHrVjCr

Off by default. `mr dns adblock update` downloads the lists (https only; plain domains, hosts,
adblock ||name^ or dnsmasq lines), keeps valid names, folds names under another listed name, removes
allowed ones (never the lists' hosts, the NTP servers, the local domain) and writes one `local=/name/`
per domain; both dnsmasq instances load it with conf-file=.

- Never worse: a failed, oversized or non-domain list (an HTML error page) keeps the previous file,
  so does a result above max_domains; a dnsmasq that does not come back gets the previous file.
- crond hourly; --cron downloads when the file is 20 h old or the settings changed, never while a
  change waits for confirmation.
- CLI `mr dns adblock status | update`, API dns.adblock, web UI 网络 › DNS › 去广告.

Tests and CI with a local HTTPS server and the real dnsmasq (150k names: answering 232 ms after
start, RSS 18.6 MB on x86_64). Docs: docs/modules/dns.md.

Closes #30

Claude-Session: https://claude.ai/code/session_01HWeUjo1dmS7gBkCYHrVjCr
@Cd1s
Cd1s merged commit 08b6af9 into main Sep 26, 2026
2 checks passed
@Cd1s
Cd1s deleted the feat/dns-adblock branch September 26, 2026 02:28
@Cd1s Cd1s mentioned this pull request Sep 26, 2026
52 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

提议:可选的 DNS 去广告(dnsmasq 列表,不新增进程)

1 participant