Fast and lightweight DNS proxy as ad-blocker for local network with many features
Go to file
Dimitri Herzog e99ac1856c added prometheus config to readme 2020-02-25 21:54:15 +01:00
.github/workflows Build multi arch images with buildx 2020-02-12 21:38:08 +01:00
config Actually fix it this time 2020-02-25 21:34:40 +01:00
docs added prometheus config to readme 2020-02-25 21:54:15 +01:00
helpertest tests refactoring 2020-01-14 22:32:30 +01:00
lists Blocking response entries (deep CNAME inspection #3 and IP black lists #1) 2020-02-13 18:12:59 +01:00
resolver Actually fix it this time 2020-02-25 21:34:40 +01:00
server Add initial prometheus endpoint support. Fixes #4 2020-02-25 21:34:40 +01:00
stats Added statistics collection and aggregation 2020-02-07 22:13:00 +01:00
testdata initial commit 2020-01-12 18:23:35 +01:00
util Blocking response entries (deep CNAME inspection #3 and IP black lists #1) 2020-02-13 18:12:59 +01:00
.dockerignore initial commit 2020-01-12 18:23:35 +01:00
.gitignore added codecov analysis 2020-01-12 22:58:56 +01:00
.golangci.yml initial commit 2020-01-12 18:23:35 +01:00
Dockerfile Healthcheck in docker container (#10) 2020-02-10 22:39:16 +01:00
LICENSE added license text 2020-01-13 21:57:57 +01:00
Makefile added docker-build target 2020-02-13 17:18:24 +01:00
codecov.yml added codecov config 2020-02-13 22:24:47 +01:00
go.mod Add initial prometheus endpoint support. Fixes #4 2020-02-25 21:34:40 +01:00
go.sum Add initial prometheus endpoint support. Fixes #4 2020-02-25 21:34:40 +01:00
main.go initial commit 2020-01-12 18:23:35 +01:00

docs/README.md

GitHub Workflow Status GitHub Workflow Status GitHub go.mod Go version GitHub latest version Docker latest version GitHub Release Date Codecov Codacy grade

Blocky

Blocky is a DNS proxy for local network written in Go with following features:

  • Blocking of DNS queries with external lists (Ad-block) with whitelisting
    • Definition of black and white lists per client group (Kids, Smart home devices etc) -> for example: you can block some domains for you Kids and allow your network camera only domains from a whitelist
    • periodical reload of external black and white lists
    • blocking of request domain, response CNAME (deep CNAME inspection) and response IP addresses (against IP lists)
  • Caching of DNS answers for queries -> improves DNS resolution speed and reduces amount of external DNS queries
  • Custom DNS resolution for certain domain names
  • Supports UDP, TCP and TCP over TLS DNS resolvers with DNSSEC support
  • Supports DNS over HTTPS (DoH) resolvers
  • Delegates DNS query to 2 external resolver from a list of configured resolvers, uses the answer from the fastest one -> improves you privacy and resolution time
  • Logging of all DNS queries per day / per client in a text file
  • Simple configuration in a single file
  • Prometheus metrics
  • Only one binary in docker container, low memory footprint
  • Runs fine on raspberry pi

Installation and configuration

Create config.yml file with your configuration:

upstream:
    # these external DNS resolvers will be used. Blocky picks 2 random resolvers from the list for each query
    # format for resolver: net:host:[port][/path]. net could be tcp, udp, tcp-tls or https (DoH). If port is empty, default port will be used (53 for udp and tcp, 853 for tcp-tls, 443 for https (Doh))
    externalResolvers:
      - udp:8.8.8.8
      - udp:8.8.4.4
      - udp:1.1.1.1
      - tcp-tls:1.0.0.1:853
      - https://cloudflare-dns.com/dns-query
  
# optional: custom IP address for domain name (with all sub-domains)
# example: query "printer.lan" or "my.printer.lan" will return 192.168.178.3
customDNS:
    mapping:
      printer.lan: 192.168.178.3

# optional: definition, which DNS resolver should be used for queries to the domain (with all sub-domains).
# Example: Query client.fritz.box will ask DNS server 192.168.178.1. This is necessary for local network, to resolve clients by host name
conditional:
    mapping:
      fritz.box: udp:192.168.178.1
  
# optional: use black and white lists to block queries (for example ads, trackers, adult pages etc.)
blocking:
    # definition of blacklist groups. Can be external link (http/https) or local file
    blackLists:
      ads:
        - https://s3.amazonaws.com/lists.disconnect.me/simple_ad.txt
        - https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts
        - https://mirror1.malwaredomains.com/files/justdomains
        - http://sysctl.org/cameleon/hosts
        - https://zeustracker.abuse.ch/blocklist.php?download=domainblocklist
        - https://s3.amazonaws.com/lists.disconnect.me/simple_tracking.txt
      special:
        - https://hosts-file.net/ad_servers.txt
    # definition of whitelist groups. Attention: if the same group has black and whitelists, whitelists will be used to disable particular blacklist entries. If a group has only whitelist entries -> this means only domains from this list are allowed, all other domains will be blocked
    whiteLists:
      ads:
        - whitelist.txt
    # definition: which groups should be applied for which client
    clientGroupsBlock:
      # default will be used, if no special definition for a client name exists
      default:
        - ads
        - special
      # use client name or ip address
      laptop.fritz.box:
        - ads
    # which response will be sent, if query is blocked:
    # zeroIp: 0.0.0.0 will be returned (default)
    # nxDomain: return NXDOMAIN as return code
    blockType: zeroIp
    # optional: automatically list refresh period in minutes. Default: 4h.
    # Negative value -> deactivate automatically refresh.
    # 0 value -> use default
    refreshPeriod: 1

# optional: configuration for caching of DNS responses
caching:
  # amount in minutes, how long a response must be cached (min value). 
  # If <=0, use response's TTL, if >0 use this value, if TTL is smaller
  # Default: 0
  minTime: 40
  # amount in minutes, how long a response must be cached (max value). 
  # If <0, do not cache responses
  # If 0, use TTL
  # If > 0, use this value, if TTL is greater
   # Default: 0
  maxTime: -1
  
# optional: configuration of client name resolution
clientLookup:
    # this DNS resolver will be used to perform reverse DNS lookup (typically local router)
    upstream: udp:192.168.178.1
    # optional: some routers return multiple names for client (host name and user defined name). Define which single name should be used.
    # Example: take second name if present, if not take first name
    singleNameOrder:
      - 2
      - 1
# optional: configuration for prometheus metrics endpoint
prometheus:
  # enabled if true
  enable: true
  # port, optional (default 4000)
  port: 4000
  # url path, optional (default '/metrics')
  path: /metrics
  
# optional: write query information (question, answer, client, duration etc) to daily csv file
queryLog:
    # directory (should be mounted as volume in docker)
    dir: /logs
    # if true, write one file per client. Writes all queries to single file otherwise
    perClient: true
    # if > 0, deletes log files which are older than ... days
    logRetentionDays: 7
  
# Port, should be 53 (UDP and TCP)
port: 53
# Log level (one from debug, info, warn, error)
logLevel: info

Run with docker

Start docker container with following docker-compose.yml file:

version: "2.1"
services:
  blocky:
    image: spx01/blocky
    container_name: blocky
    restart: unless-stopped
    ports:
      - "53:53/tcp"
      - "53:53/udp"
    environment:
      - TZ=Europe/Berlin
    volumes:
      # config file
      - ./config.yml:/app/config.yml
      # write query logs in this directory. You can also use a volume
      - ./logs:/logs

Run standalone

Download binary file for your architecture, put it in one directory with config file. Please be aware, you must run the binary with root privileges if you want to use port 53 or 953.

Additional information

Print current configuration

To print runtime configuration / statistics, you can send SIGUSR1 signal to running process

Statistics

blocky collects statistics and aggregates them hourly. If signal SIGUSR2 is received, this will print statistics for last 24 hours:

  • Top 20 queried domains
  • Top 20 blocked domains
  • Query count per client ...

Hint: To send a signal to a process you can use kill -s USR1 <PID> or docker kill -s SIGUSR1 blocky for docker setup