Skip to content

eugenescodes/adblock2mikrotik

Repository files navigation

adblock2mikrotik

Convert ad-blocking filter lists to MikroTik RouterOS DNS adlist format.

Tip

Ready-to-use URL for RouterOS: https://raw.githubusercontent.com/eugenescodes/adblock2mikrotik/refs/heads/main/hosts.txt

Overview

Transforms popular ad-blocking filter lists (Hagezi) into a compact format compatible with the MikroTik RouterOS 7.15+ DNS adlist feature. Optimized for memory-constrained low-resource devices like the RB951Ui-2nD hAP (which has 16 MB storage).

Sources

List Description
Hagezi Multi PRO mini General ad/tracker blocking
Hagezi TIF mini Threat intelligence feeds
Hagezi Gambling mini Gambling sites

Features

  • Converts ||example.com^ rules to MikroTik DNS adlist format (0.0.0.0 example.com)
  • Deduplicates entries across all sources
  • Validates domains against RFC label rules (rejects double-dots, leading/trailing hyphens)
  • Pre-filters comments and empty lines for efficiency
  • Compatible with RouterOS 7.15+

Usage

Option 1 — uv (recommended)

# Install uv if not already installed (macOS / Linux)
curl -LsSf https://astral.sh/uv/install.sh | sh

# Clone and run
git clone https://github.com/eugenescodes/adblock2mikrotik
cd adblock2mikrotik
uv run convert_to_hosts.py

uv run automatically creates a virtual environment and installs dependencies — no manual setup required.

Option 2 — Docker

docker build -t adblock2mikrotik .

# Linux / macOS
docker run --rm --user $(id -u):$(id -g) -v "$(pwd)":/output adblock2mikrotik

# Windows (PowerShell)
docker run --rm -v "${PWD}:/output" adblock2mikrotik

Note

The -v flag mounts your current directory into the container at /output. The script writes hosts.txt to /output, so the file appears directly in your current directory on the host — no manual copying needed.

On Linux, --user $(id -u):$(id -g) ensures the output file is owned by your current user. Not required on macOS or Windows (Docker Desktop handles this automatically).

After running either option, hosts.txt is created in the current directory.

MikroTik RouterOS Integration

Add adlist via URL

/ip/dns/adlist add url=https://raw.githubusercontent.com/eugenescodes/adblock2mikrotik/refs/heads/main/hosts.txt ssl-verify=no

Optional: enable SSL verification

If you want to use ssl-verify=yes, you can download and import CA certificates using the following commands:

/tool fetch url=https://curl.se/ca/cacert.pem
/certificate import file-name=cacert.pem passphrase=""
/ip/dns/adlist add url=https://raw.githubusercontent.com/eugenescodes/adblock2mikrotik/refs/heads/main/hosts.txt ssl-verify=yes

See also the official MikroTik documentation:

Configuration

By default, the script uses three pre-configured Hagezi filter lists. You can customize which sources are used by creating a config.toml file:

Customize sources

  • Copy the example configuration:
cp config.toml.example config.toml
  • Edit config.toml to add or remove sources:
[sources]
urls = [
    "https://raw.githubusercontent.com/hagezi/dns-blocklists/main/adblock/pro.mini.txt",
    "https://raw.githubusercontent.com/hagezi/dns-blocklists/main/adblock/tif.mini.txt",
    "https://raw.githubusercontent.com/hagezi/dns-blocklists/main/adblock/gambling.mini.txt",
]
  • Run the converter:
uv run convert_to_hosts.py

The script will automatically load sources from config.toml. If the file doesn't exist, it falls back to the default sources above.

Finding additional filter lists

You can use any blocklist in AdBlock format (||domain.com^ syntax)

For more Hagezi lists, visit the Hagezi DNS blocklists repository

Development

This project uses uv for dependency management and Ruff for linting/formatting.

Prerequisites

Install uv (replaces pip and venv) more info about uv:

# macOS / Linux
curl -LsSf https://astral.sh/uv/install.sh | sh

Setup (development environment)

Development tools (ruff, pytest) are in the dev dependency group and need to be installed separately:

uv sync  # Installs all dependencies including dev tools

Lint and format

uv run ruff check . --fix   # lint + autofix
uv run ruff format .        # format

Tests

uv run pytest -v

Note

Development dependencies (ruff, pytest) are not included in the Docker image. Use uv sync locally to run linting, formatting, and tests. The Docker image only includes production dependencies for running the converter.

Contributing

  1. Open a GitHub issue to discuss major changes before starting work.
  2. Fork the repo and create a feature branch: git checkout -b feature/your-feature
  3. Make your changes and run tests: uv run pytest -v
  4. Commit with a clear message and push to your fork.
  5. Open a Pull Request targeting main with a description of what and why.

License

GNU GPL v3.0

Acknowledgments

  • Hagezi for maintaining comprehensive filter lists
  • MikroTik for the DNS adlist feature in RouterOS 7.15+

This tool is not affiliated with MikroTik.

About

Convert AdBlock filter lists to MikroTik RouterOS DNS adlist format

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors