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 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 onfig.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 writes a plist file to /Library/LaunchDaemons/com.ninjamar.compactdns.plist.

Start the server:

sudo launchctl bootstrap system /Library/LaunchDaemons/com.ninjamar.compactdns.plist

Stop the server:

sudo launchctl bootout system /Library/LaunchDaemons/com.ninjamar.compactdns.plist

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

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.0a1.tar.gz (37.6 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.0a1-py3-none-any.whl (52.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: compactdns-0.1.0a1.tar.gz
  • Upload date:
  • Size: 37.6 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.0a1.tar.gz
Algorithm Hash digest
SHA256 0a634b1af3d37885fe2b7c5b72fd486894a8ab5a5fdce9851b0ab25aef7b362c
MD5 41dc54c5a9894167fbdd0c41c9954a12
BLAKE2b-256 01600f71c74b676b0bfe02bcd8b182164c113ab0819bd5e5608ff34653772506

See more details on using hashes here.

File details

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

File metadata

  • Download URL: compactdns-0.1.0a1-py3-none-any.whl
  • Upload date:
  • Size: 52.7 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.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 a5b1a649537f4382d86888a6a1a4ffeeafb3eba2b8934a19ec668b97a7e08adc
MD5 9a42725c7343df18b6db67fc8e0df021
BLAKE2b-256 f2544dde76d31ca598e2ca8d7a220c329f2e3a1f85bcebea8f15b5839467bb2f

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