Skip to main content

CompactDNS (CDNS) is a minimalistic yet powerful DNS server designed for simplicity and speed.

Project description

CompactDNS (CDNS)

CompactDNS (CDNS) is a minimalistic yet powerful DNS server designed for simplicity and speed, offering local caching and both authoritative and forwarding capabilities.

In 2025, Google Chrome will remove adblockers with the impending Manifest V3 update. This change threaten the existance of software like UBlock Origin. Rather than abandoning Chrome, I explored alternative ways to block ads. This DNS server, CompactDns, was the result: it's lightweight, blazing fast, and can block ads. It also caches all requests on device, leading to a latency as low as 0 ms.

- ninjamar

Installation

Install CDNS

pip install cdns

CDNS can also be installed from source

git clone https://github.com/ninjamar/compactdns
cd compactdns
pip install .

For development,

pip install -e .

Usage

After installation, CDNS can be used by running:

cdns [command] [options]

The server can be run as a daemon, or as a standalone program.

Running the Server

To start the server:

cdns run [options]

Configuration is stored in a toml or json file. See config.toml for an example configuration.

cdns run --config config.toml

The DNS protocol is typically run over port 53 for TCP and 853 for TLS.

When TLS is used, a key and certificate file are required.
Generate a pair:

openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes

Configuration

An example configuration file is available in config.toml.

Running as a Service

MacOS / OSX

CDNS can be set up to run in the background as a service:

sudo cdns install -c /path/to/config

This command will output instructions for starting and stopping the server.

Startup logs are written to /tmp/cdns-startup.log and cdns-startup-err.log .

[!NOTE] MacOS seems to occasionally be killing the server when the device wakes from sleep. A program called sleepwatcher is required to fix this.

Ensure that all paths in the configuration file are relative to the path of the configuration file.

Changing the System DNS Server

CDNS can be configured as the system DNS server. Replace A.B.C.D with the address of the server.

On MacOS:

networksetup -setdnsservers Wi-Fi A.B.C.D

DNS Zones

Zones can be stored in 3 formats:

  • Single zone Json file (e.g., *.json)
  • Multiple zones JSOn file (e.g., *.all.json)
  • Traditional zone file (e.g., *.zone)

See the example-zones/ directory for examples.

From a Host File

To convert a host file to a JSON list:

cdns tools h2j /path/to/host.txt /path/to/output.all.json

example-zones/lowe.all.json was generated from Peter Lowe's adservers list using this script

Preload Files and Cache Dump

A preload file is a plain text list of hosts to be loaded into the cache before server startup.

# These sites will all be preloaded
google.com
github.com
example.com

The cache (and also the zones) can be written to a file upon server shutdown and reloaded on startup.

Architecture

When in recursive mode, the server can achieve latency as low as 0 ms. Initially, cache misses will result in higher latency, but once requests are resolved, responses are cached for the future.

graph TD;
    send["Send the request"]
    done["Response sent back"]
    send --> recv["Server receives the request"]
    recv --> is_cache["Is the request in the cache?"]
    is_cache --> |"Yes"| cache["The request is in the cache"] --> done
    is_cache --> |"No"| recursive["Recursively fulfill the request"]
    recursive --> |"Add response to cache"| done

Other Commands

shell

CDNS includes an interactive debugging shell.

[!WARNING] This feature exists for debbuging. Use at your own risk

[!NOTE] Incoming requests are blocked when in use

Run the shell:

cdns shell [options]

Options:

  • --host, -h: The host address for the shell server in a.b.c.d:port format

The shell opens a Python REPL connected to the main server process.

List the available commands:

>>> help(self.command)

Testing

Some outdated tests exist in tests/ directory

Instead, send test queries using dig.

dig @A.B.C.D -p PORT example.com

License

This project is licensed under the MIT License. See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

compactdns-0.1.0a2.tar.gz (38.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

compactdns-0.1.0a2-py3-none-any.whl (53.6 kB view details)

Uploaded Python 3

File details

Details for the file compactdns-0.1.0a2.tar.gz.

File metadata

  • Download URL: compactdns-0.1.0a2.tar.gz
  • Upload date:
  • Size: 38.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for compactdns-0.1.0a2.tar.gz
Algorithm Hash digest
SHA256 b825248c6f60178c14ab80b9df85b46bbcab9bba71ea91aebca4624fe5189ab9
MD5 39ebec7d8bf0c7465d8a4b51d9823499
BLAKE2b-256 f997cab2b23656b6246ed9effa277bddfd600fb467267d482eca9570862b04d7

See more details on using hashes here.

File details

Details for the file compactdns-0.1.0a2-py3-none-any.whl.

File metadata

  • Download URL: compactdns-0.1.0a2-py3-none-any.whl
  • Upload date:
  • Size: 53.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for compactdns-0.1.0a2-py3-none-any.whl
Algorithm Hash digest
SHA256 54ba96ea6d6e6fc20c7386d6e9b817c45565050c7224be3b07d517b588984ff2
MD5 6e2a3368aef0525b25830ff0d02073a2
BLAKE2b-256 104a361d9edf13e26862d4b8bb1671baf2e5ff8286fade03c607d3fb37ea31d2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page