Skip to content
Projetos

s60sc/ESP32_AdBlocker

Ver no GitHub

Sobre

DNS sinkhole baseado em ESP32 para anúncios e rastreadores

Sistemas e infraestrutura

Como estes números são medidos

Crescimento medido de estrelas

em 24 h
+1
em 7 dias
+6
em 15 dias
+17
em 1 mês
Coletando histórico

Variações líquidas em períodos sobrepostos, não totais diários. Medições ausentes ficam em branco. Estrelas indicam interesse, não qualidade.

README.md

Original

ESP32_AdBlocker

ESP32_AdBlocker acts as a DNS Sinkhole (like Pi-Hole) by returning 0.0.0.0 for any domain names in its blocklist, else uses an external DNS server to resolve IP addresses. This prevents content being retrieved from or sent to blocked domains. A web server is provided to control the service and monitor its operation.

Version 4.1 includes

  • enhancements to DNS query handling and LED status by user @dateno1
  • timezone fix
  • fixes from user @blueheart001
  • compatibility with arduino-esp core v4.0.0
  • Much faster blocklist load, support for 16MB flash: by user @PoloB12
  • ESP32-P4 now supported, Ethernet only boot, performance and resilience improvements, new web inteface: by user @mrcodetastic
  • further web page improvements: by user @mrcodetastic

Version 5.0 is a major update by user @PoloB12 that includes:

  • DNS forwarding and cache rewrite, subdomain blocking and more list formats, security hardening, web pages built into the firmware, network resilience: see Changes in detail

Changes in detail

DNS

  • Full pass-through forwarding. Allowed queries are forwarded unchanged and the upstream answer is relayed as is: all record types (MX, TXT, SRV, HTTPS, NS, PTR...), all addresses and the real TTLs. Previously only one A/AAAA address with a fixed TTL was returned.
  • No lost queries in bursts. Many queries are in flight at once, received through raw lwIP UDP into a PSRAM queue. Over Ethernet a burst of 120 simultaneous queries is fully answered.
  • Response cache in PSRAM (1024 entries, TTLs counted down). A cached answer takes a few milliseconds.
  • Upstream failover: if DNS server 1 does not answer, server 2 is used.
  • DNS rebinding protection: a public name that resolves to a private address (192.168.x.x, 10.x.x.x...) gets an empty answer. Allowed exceptions are configurable (default plex.direct).
  • Local clients only: queries from public addresses are ignored, so the device cannot be abused as an open resolver.
  • The device's own lookups (blocklist download, NTP) use the configured upstream servers, not the router's DNS, which may point back at the AdBlocker itself.

Blocklists

  • Up to three blocklist URLs (main list plus two extra lists under Edit Config > Settings), merged and deduplicated.
  • Subdomains are blocked too: a listed domain also blocks every name below it, as in the adblock lists of Pi-hole and AdGuard Home. Most current lists (Hagezi, OISD, AdGuard) rely on this: they list googlesyndication.com, not every server under it. With exact matching only, these lists blocked a third to two thirds less.
  • More list formats: hosts, adblock (||name^), plain domain lists and *.name wildcard lists. Exceptions in a list (@@||name^, $badfilter) are respected; rules a DNS server cannot apply (paths, patterns, other options) are skipped.
  • Unblock below a listed domain: unblocking pagead2.googlesyndication.com adds an exception while the rest of googlesyndication.com stays blocked. Check shows which entry blocks a name.
  • Lists that do not fit are cut off at the domain limit or when memory is full, and the page says so (Limit reached, list 2 cut off); later extra lists are skipped. Blocklist memory is reserved once at boot, so a list that is too big cannot crash the device.
  • Timed pause: 5 min, 30 min or 2 hours, then blocking resumes by itself.
  • Flash snapshot on by default: after a restart the list is back within about a second, without downloading. An unchanged list is not written to flash again.
  • Reload without restart: DNS keeps answering while a new list downloads.
  • Safer downloads: redirects are followed, error pages are not accepted as a list, and a failed download keeps the previous list and is retried later.
  • Unblocking a domain no longer breaks the lookup of other domains in the list.

Reliability

  • DNS first: at boot the DNS server starts within seconds of the network; the blocklist is restored and downloaded afterwards.
  • Network recovery: a lost ping or a router reboot no longer tears down the network. Ethernet waits for the link and only restarts the device if the router stays unreachable for about 6 minutes. A WiFi station reconnects by itself.
  • Crash handling: a single crash no longer leaves the network without DNS. Safe mode (web page only) needs three unstable boots in a row and retries normally after 10 minutes.
  • Settings survive updates: new settings and renamed labels are merged into an existing configuration instead of resetting it, and the custom block list is kept if the configuration has to be reset.

Security

  • Protection against malicious websites: every request that changes something must carry the header X-AdBlocker: 1, which the device's own pages send and other websites cannot. Scripts need it too, e.g. curl -H "X-AdBlocker: 1" http://<ip>/control?.... Only during first setup over the device's own hotspot (no network yet) is it not needed. Each refused request is logged with the sender's IP address and device type, e.g. Blocked GET from 192.168.1.50 [Linux; Android 14; Pixel 8], missing X-AdBlocker header: /status?....
  • Host name check against DNS rebinding of the web page, no cross-origin access, Content-Security-Policy and X-Frame-Options on the pages, escaped output in the log viewer and settings.
  • Passwords are no longer included in the /status response. WebDAV file access is disabled.
  • Hotspot only when needed: for first-time setup, in Eth+AP mode, or as a fallback when the network fails. The fallback hotspot requires a hotspot password of at least 8 characters; an open hotspot never routes into your LAN.
  • Web pages built into the firmware: nothing is downloaded from GitHub at first start, so the pages always match the firmware and cannot be swapped in transit.
  • Recommended: set a web page password under Edit Config > Network. Without it any device on your LAN can change settings or upload firmware.

Web interface

  • Status line (Blocking / PAUSED / OFF / Updating, number of domains, last update), query counters with the blocked percentage and the uptime, and a Recently blocked list: tap a name to check or unblock it.
  • Check / Block / Unblock buttons, confirmations before destructive actions, live log, long blocklist URLs.
  • Phone layout, dark mode, restyled settings, log, OTA, setup and failure pages.
  • OTA Upload is a tab of the main page: switching tabs never reloads the page and the address stays http://<ip>/. After a firmware upload the page reloads itself once the device runs again, so it matches the new firmware.
  • The Verbose logging switch shows the device's real state in every open browser, also when someone else turned it on.
  • Readable tables in the log: the task, partition and pin tables from System info keep their columns and scroll sideways on their own, instead of wrapping into broken rows on a phone. Normal log lines still wrap.

Boards

  • 16 MB flash boards (e.g. ESP32-S3 N16R8): copy extras/partitions_16MB.csv to partitions.csv in the sketch folder, so LittleFS is 9.5 MB instead of 1.5 MB. Not for boards with less than 16 MB of flash.
  • BOARD_WAVESHARE_S3_ETH build flag (appGlobals.h) for the Waveshare ESP32-S3-ETH: onboard W5500 Ethernet pins, WS2812 LED on GPIO21, Ethernet as default network mode. Build with --build-property "compiler.cpp.extra_flags=-DBOARD_WAVESHARE_S3_ETH".
  • Uptime, clock and NTP state in the System info log output.

Performance

Measured on an ESP32-S3 N16R8 with 8 MB PSRAM, with the StevenBlack list (58,237 domains), upstream DNS 1.1.1.1. The test client was a laptop on WiFi, so every response time includes that WiFi hop: the router's own DNS, measured the same way, answers in 2.7 ms.

DNS Ethernet (W5500) WiFi
Blocked domain, answered locally (median) 2.6 ms 3.2 ms
Cached answer (median) 2.7 ms 3.2 ms
Not cached, forwarded to 1.1.1.1 (median) 14 ms 19 ms
Sustained queries per second ≥ 800 (limit of the test client) ≥ 800
Burst of simultaneous queries, all answered 120 of 120 40 of 40
Blocklist check on the device (the name and each parent domain) 60 – 220 µs 60 – 220 µs
Start-up and blocklist
DNS answering after a restart (Ethernet) about 4 s (link and DHCP take most of it)
Saved list restored from flash 0.5 s after DNS starts (1.1 MB snapshot)
Download and parse StevenBlack hosts (2.4 MB) 6 s, then 1.2 s to sort
Download and parse Hagezi Normal (4.2 MB, 180,098 domains) 10 s, then 3.1 s to sort; 3.5 MB of 6.2 MB
Two lists merged (StevenBlack + Hagezi Light) 133,392 domains, 3.2 MB of 6.2 MB
DNS during a reload or snapshot save keeps answering (168 of 168 queries in a test)
Saving the snapshot skipped when the list is unchanged; otherwise 14 s for 58k domains, written slowly on purpose so DNS stays responsive
Memory and size
Blocklist capacity 6.2 MB PSRAM; limit 200,000 domains by default. With Max domains at 300, memory holds about 288,000 domains (5.9 MB)
DNS cache 1,024 answers, 516 KB PSRAM
Free internal RAM while running about 200 KB
Firmware 1.76 MB, 52% of an OTA slot, including 25 KB of compressed web pages

Requirements

ESP32_AdBlocker is an Arduino sketch. The ESP32 module needs PSRAM:

  • ESP32-S3 with 8MB PSRAM can host a currently sized blocklist (up to about 288,000 domains with Max domains set to 300). See Performance.
  • ESP32 with 4MB PSRAM may truncate a currently sized blocklist. Blocklist checks take well under a millisecond.

The blocklist is kept in PSRAM, not in flash: every DNS query searches it, which needs fast random access. The free space in the firmware partition is reserved for over-the-air updates, and the flash file system only holds a compressed copy of the list for a fast start.

Operation

Desktop Phone
ESP32_AdBlocker main page on a desktop browser ESP32_AdBlocker main page on a phone

After power up the saved blocklist is restored from flash and DNS answers within seconds. On the very first start, or when the saved list is more than a day old, the blocklist is downloaded as soon as the clock is set (needed to check the server certificate). Progress is shown on the web page.

The main blocklist and up to two extra lists are merged. Lists can be in hosts, adblock (||name^), plain domain or *.name format; a listed domain also blocks its subdomains. Lists are downloaded again every day at the configured hour. You can also block or unblock individual domains; these are kept in a custom list and applied after every reload.

Popular lists, with the number of domains this firmware loaded from each (November 2026):

List Domains URL
StevenBlack (default) 58,000 https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts
Hagezi Light 81,000 https://raw.githubusercontent.com/hagezi/dns-blocklists/main/adblock/light.txt
Hagezi Normal (a good choice for most homes) 180,000 https://raw.githubusercontent.com/hagezi/dns-blocklists/main/adblock/multi.txt
Hagezi Pro 231,000 https://raw.githubusercontent.com/hagezi/dns-blocklists/main/adblock/pro.txt (set Max domains to 300)
OISD small / big 64,000 / 219,000 https://small.oisd.nl/, https://big.oisd.nl/ (big: set Max domains to 300)
AdGuard DNS filter 195,000 https://adguardteam.github.io/AdGuardSDNSFilter/Filters/filter.txt

A DNS blocker cannot remove ads that come from the same servers as the content itself, such as YouTube video ads or sponsored posts on Facebook and Instagram. Blocking those servers would break the site. Use an ad-blocking browser extension, such as uBlock Origin in Firefox, for those.

The main page shows:

  • Status line: whether blocking is active, paused or off, the number of domains and when the list was last updated.
  • Statistics: allowed and blocked queries since the last restart, how long ago that restart was (uptime), and the most recently blocked names.
  • Check or change a domain: enter a domain and press Check, Block (not added if already blocked, also through a parent domain, or not resolvable) or Unblock.
  • Blocklist: the current list URL and download progress. Reload blocklist downloads the lists again; to switch to another list, enter its https:// URL in the domain field first. Stop download cancels a running download and keeps the previous list.
  • Custom domains: view or clear your own blocks and unblocks.
  • Enable AdBlocker: switch blocking on or off (kept after a restart).
  • Pause blocking: let everything through for 5 min, 30 min or 2 hours. Devices cache DNS answers, so a pause or resume takes effect within about a minute.

To make ESP32_AdBlocker your preferred DNS server, enter its IPv4 address in place of the current DNS server IPs in your router / devices. ESP32_AdBlocker does not have an IPv6 address but some devices use IPv6 by default, so disable IPv6 DNS on your device / router to force it to use IPv4 DNS.
Eg for a Windows PC network adapter, to use AdBlocker as DNS Server having IP address 192.168.1.168, at the Windows command prompt, enter:
netsh interface ip set dns "Wi-Fi" static 192.168.1.168
To switch back to usual DNS Server, eg Google, enter:
netsh interface ip set dns "Wi-Fi" static 8.8.8.8

Browsers must have Use secure DNS disabled as this overrides adapter and router DNS settings.

Installation

Download the github files into the Arduino IDE sketch folder, removing -main from the application folder name.

Prior to compilation, if user convienience is preferred for less security - in file appGlobals.h set:
#define USE_EMBEDDED false
This allows the web pages to be downloaded from Github or uploaded via OTA or WebDAV instead of using the built-in files.

Compile with the esp32 arduino core (tested with v4.0.0) with PSRAM enabled and the following Partition scheme:

  • ESP32-S3 - 8M with spiffs (...)
  • ESP32 - Minimal SPIFFS (...)

For 16 MB flash boards, copy extras/partitions_16MB.csv to partitions.csv in the sketch folder: same app/OTA slots as 8M with spiffs, with LittleFS extended in place from 1.5 MB to 9.5 MB, leaving room for the flash snapshot as the blocklist grows. Arduino picks it up automatically. It only takes effect with a USB upload, and existing files are kept. Do not use it on boards with less flash.

On first installation the application starts a WiFi hotspot: connect to SSID ESP32_AdBlocker_... and open 192.168.4.1 to choose your router's network and enter its password. The configuration file (except passwords) is created automatically. The web pages are part of the firmware, so nothing else needs to be downloaded or uploaded.

Later updates are made with the OTA Upload tab: upload the firmware .bin, which includes the web pages; the page reloads itself after the restart. Other files, such as a CA.pem certificate, can also be uploaded there; web page files are refused because the built-in pages are always used, unless 'USE_EMBEDDED' changed.

To change the built-in web pages if used, edit data/AdBlocker.htm or data/common.js, run python extras/embed_web.py (which regenerates webPages.h), and compile.

Configuration

More configuration details accessed via Edit Config tab, which displays further buttons:

  • Network: Additional network and webserver settings. In particular set:

    • Router IP Address to obtain static IP
    • Static IP Address, used as AdBlocker DNS Server IP
    • Hotspot password (8+ characters), needed for the fallback hotspot
    • Web page user name and password (recommended)
  • Settings: Blocklist settings: extra blocklist URLs, daily update hour, flash snapshot, memory limits, DNS rebinding protection, timezone and status LED.

  • Ethernet: Select the required Network. To configure Ethernet, define the SPI pin numbers used to connect to the external Ethernet controller.

Most settings take effect at once (the domain limit after a restart); press Save to keep them after a restart. Factory reset /data deletes all settings, the custom list and the saved blocklist.

Logging

The application log messages can be monitored on the web page tab Show Log; new lines appear live while the tab is open.

The Verbose logging switch adds a line for each blocked or allowed query. System info adds memory, storage, uptime and DNS statistics to the log, plus tables of the tasks, flash partitions and assigned pins. On a narrow screen, swipe a table sideways to see all its columns.

Network Selection

Default network interface is WiFi, but Ethernet can be used instead on boards with built-in Ethernet, or by connecting an external Ethernet controller. Feature only tested for W5500 Ethernet controller connected to ESP32S3 board. The selected network interface is available after configuration and reboot.

Options:

  • WiFi: default, also fallback if Ethernet has no link at boot
  • Eth+AP: Ethernet plus ESP Access Point. Do not open web pages on each network concurrently.
  • Ethernet: Ethernet only, no WiFi

If Ethernet has no link at boot the device falls back to WiFi (and a fallback hotspot, if a hotspot password is set). When the cable is connected later, the device restarts on Ethernet.

LED Status

The onboard LED of each device can be used to indicate the AdBlocker status. Value in brackets is WS2812 LED color if available:

  • Starting: Medium blink (Cyan)
  • Downloading hosts: Medium blink (Blue)
  • Active: Steady (Green)
  • Failure: Rapid blink (Red)
  • AP Mode: Fast blink (Yellow)
  • Offline: Slow blink (Magenta)

The LED is configured using the Settings button in the web page tab Edit Config.
Setting the Brightness value to 0 disables the LED.

Explore projetos relacionados

Carregando…