Code Monkey home page Code Monkey logo

dnsp's Introduction

dnsp: A DNS Proxy

Wercker GoDoc Coverage

dnsp is a lightweight but powerful DNS server. Queries are blocked or resolved based on a blacklist or a whitelist. Wildcard host patterns are supported (e.g. *.com) as well as hosted, community-managed hosts files. Ideal for running on mobile devices or embedded systems, given its low memory footprint and simple web interface.

Installation

$ go get -u github.com/gophergala/dnsp/...

Example Usage

  • Forward all queries to Google's public nameservers:
$ sudo dnsp --resolve 8.8.4.4,8.8.8.8
  • Use a community-managed blacklist from hosts-file.net and check it hourly for changes:
$ sudo dnsp --blacklist=http://hosts-file.net/download/hosts.txt --poll 1h
  • Block everything except Wikipedia:
$ cat > /etc/dnsp.whitelist << EOF
*.wikipedia.org
*.wikimedia.org
wikipedia.org
wikimedia.org
EOF

$ sudo dnsp -r 8.8.8.8 --whitelist=/etc/dnsp.whitelist

Advanced Usage

$ dnsp -h
NAME:
   dnsp - DNS proxy with whitelist/blacklist support

USAGE:
   dnsp [global options] command [command options] [arguments...]

VERSION:
   0.9.2

COMMANDS:
   help, h      Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --net, -n "udp"          listen protocol (‘tcp’ or ‘udp’) [$DNSP_NET]
   --listen, -l ":dns"      listen address (host:port, host or :port) [$DNSP_BIND]
   --resolve, -r "8.8.4.4"  comma-separated list of name servers (host:port or host) [$DNSP_SERVER]
   --whitelist, -w          URL or path to file containing whitelisted hosts [$DNSP_WHITELIST]
   --blacklist, -b          URL or path to file containing blacklisted hosts [$DNSP_BLACKLIST]
   --poll, -p "0"           poll the whitelist or blacklist for updates [$DNSP_POLL]
   --http, -t               start a web-based UI on the given address (host:port, host or port) [$DNSP_HTTP]
   --help, -h               show help
   --version, -v            print the version

Notes:

  • --listen defaults to :dns, which is equivalent to 0.0.0.0:53, meaning: listen on all interfaces, on port 53 (default DNS port).
  • --resolve defaults to the list of nameservers found in /etc/resolv.conf. If no nameservers were found, or the file does not exist (e.g. on Windows), the default value will be `8.8.4.4,8.8.8.8" (Google's public DNS service).
    • However, explicitly setting --resolve to false or an empty string disables resolving completely. What that means is all queries will still be checked against the active whitelist or blacklist, but ones that would not be blocked will return a failure response (as opposed to no response).
  • --whitelist and --blacklist are mutually exclusive. Setting both is an error.
  • --whitelist and --blacklist files are parsed according to a simple syntax:
    • Empty lines are ignored, and # begins a single-line comment.
    • Each line can contain a single hostname to be whitelisted or blacklisted.
    • Alternatively, a line can contain a pattern like *.wikipedia.org or *.xxx.
    • Additionally, the /etc/hosts-like syntax is supported.
      • However, only lines starting with 127.0.0.1 or ::1 are taken into parsed, everything else is ignored.
      • This is for compatibility with popular, regularly updated blocklists like the ones on hosts-file.net.
  • --whitelist and --blacklist support both file paths and URLs.
  • --poll instructs dnsp to periodically check the whitelist or blacklist file for changes.
    • The file is only re-parsed if the file size or modification time has changed since the last read.
    • Same is true for URLs: the Content-Length and Last-Modified headers are compared to previous values before re-downloading the file.

Running with a non-root user

Because dnsp binds to port 53 by default, it requires to be run with a privileged user on most systems. To avoid having to run dnsp with sudo, you can set the setuid and setgid access right flags on the compiled executable:

sudo mkdir -p /usr/local/bin
sudo cp $GOPATH/bin/dnsp
sudo chmod ug+s /usr/local/bin/dnsp

While dnsp will still run with root privileges, at least now we can run it with a non-admin user (someone who is not in the sudoers group).

But… Why‽

Why, you ask, is a DNS proxy useful?

  • It is a simple solution for blocking websites (like AdBlock).
  • Does not require an HTTP proxy or a SOCKS proxy. Some apps don't like that.
  • Easy to set up for mobile devices. Run dnsmasq on your router or in any embedded Linux system, and configure your home router to use it as the DNS server in DHCP responses. The blocklist will now apply to everyone on the network.
  • Safer than dnsmasq for community managed hosts files. Because dnsp doesn't do any rewriting (it either blocks or proxies), you don't have to trust everyone having access to online hosts files not to redirect your bank's website to their own servers.

dnsp

dnsp's People

Contributors

attilaolah avatar johngb avatar leavengood avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dnsp's Issues

Blocklist Tree

Store the blocklist in a tree structure.

This should reduce the big memory overhead since we would not store the duplicate segments in the hostnames any more. Implement something along the lines of this:

type Tree struct {
    Head string
    Tail map[string]Tree
}

Then, compile the hostnames in reverse segments, to end up with something like this:

  • "com"
    • "facebook"
      • "apps"
    • "twitter"
  • "net"
    • "example"
      • ""
      • "www"

This tree should be equivalent to:

apps.facebook.com
twitter.com
example.net
www.example.net

Since there are a ton of subdomains in the hosts file, and many common TLDs, this could potentially reduce the memory footprint, and could also make the lookups faster (or slower due to the overhead, we should check).

Resolve blocked entries to 204 No Content

Is it possible to resolve the blocked entries to 204 No Content?

Right now the browser keeps waiting for the blocked sites to resolve and effectively stops rendering of the site.

Can't build the binary

Thanks for a wonderful project!

When I do a go get -u github.com/gophergala/dnsp I can't find the dnsp binary in $GOPATH/bin.

I am using Mac Yosemite 10.10.1 (14B25), go version 1.4

Any hints will be appreciated.

webUI: flat (-t/--http) not recognized

Hi,

Just tried this on a Windows 7 machine: however I can not run it with the flag to launch the web interface. Neither -t nor --http are being recognized as being valid.

I had a quick look through the code but I couldn't figure out where you set the flag and pass it to the webserver.

Web UI refresh resets the counter

When I refresh the web UI, the counter for the private blacklist or whitelist resets to zero, and the hosts disappear. It seems the app isn't calling the /list API.

Web UI

So we can keep track of this here.

Simple Blocklist

Support a simple blocklist based on the current hosts file reader.

  • read the hosts file line by line
  • chop off the comments from the end of the line
  • if there's stuff left before the comment, see if the IP is 127.0.0.1 or ::1.
  • if yes, add the host to the blocklist (s.Block(host)).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.