# Caddy DNS update


More [light](https://miek.nl/2016/march/14/first-light/)!

My "Port Caddy to be a DNS server"-project is alive and kicking. Code will be published soon-ish,
mostly waiting for actually naming the bloody thing. Code is also littered with TODOs.

I've implemented the following middlewares, the all need tests and actual use, but here we go:

* `log`, for logging (as in Caddy)
* `error`, for error logging (as in Caddy). These both include the `{{placeholder}}` syntax, so you
    can use `{{port}}` and even ``{>}`` for logging header bits.
* `file`, really, really stupid zone file backed zone implementation, more a proof of concept
* `reflect`, reflect (test) middleware
* `proxy`, proxy requests to an upstream nameserver/resolver

I also want to add a `rewrite` middleware that will, for instance, rewrite ANY queries to HINFO
ones.

To give some feeling of what is possible, consider the following Caddyfile:

~~~ txt
.:1053 {
    file db.miek.nl miek.nl
    proxy . 8.8.8.8:53
}

dns.miek.nl:1053 {
    file db.dns.miek.nl
    reflect
}
~~~

This defines two zones, `.` and `dns.miek.nl` that both listen on port 1053 for incoming queries.
The root zone entry (`.`) loads a file from disk (`db.miek.nl`) with the origin set to `miek.nl`.
This means any query hitting Xaddy will be forwarded to this middleware. If the query falls in the
domain `miek.nl.` is will be answered from the file. *If* it doesn't 't fall under `miek.nl` the
query will be proxied to 8.8.8.8. Let's see how this works with `dig`:

Query something under `miek.nl`:

~~~ txt
% dig @localhost -p 1053 a a.miek.nl
;; QUESTION SECTION:
;a.miek.nl.			IN	A

;; ANSWER SECTION:
a.miek.nl.		1800	IN	A	139.162.196.78
~~~

Yep, looks like a correct answer from the contents of the file.

And now for something totally different domain, that should be proxied:

~~~ txt
% dig @localhost -p 1053 mx nlgids.london
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;nlgids.london.			IN	MX

;; ANSWER SECTION:
nlgids.london.		821	IN	MX	10 aspmx3.googlemail.com.
nlgids.london.		821	IN	MX	10 aspmx2.googlemail.com.
nlgids.london.		821	IN	MX	5 alt1.aspmx.l.google.com.
nlgids.london.		821	IN	MX	1 aspmx.l.google.com.
nlgids.london.		821	IN	MX	5 alt2.aspmx.l.google.com.
~~~

Note that this proxy implementation is a complete copy of the one in
[Caddy](https://caddyserver.com/docs/proxy), so the health checking and the matching on specific
names will work in the same way (eventually).

Queries for the `dns.miek.nl` zone will be processed by the second entry in the Caddyfile:

~~~ txt
% dig @localhost -p 1053 TXT go.dns.miek.nl
;; QUESTION SECTION:
;go.dns.miek.nl.			IN	TXT

;; ANSWER SECTION:
go.dns.miek.nl.		1800	IN	TXT	"Hello!"
~~~

The `reflect` middleware will never be used in this case, because all queries for `dns.miek.nl` will
be handled by the file middleware.

