DNS over HTTPS with cloudflared

In last weeks post I went over using Unbound to encrypt DNS traffic. The main downside to this was uncached DNS queries had a fairly high latency. In this post I'm going to look at an alternative approach using cloudflared.

Installing cloudflared

The cloudflared daemon is fairly straightforward to install, on CentOS it can be installed with yum:

yum install https://bin.equinox.io/c/VdrWdbjqyF/cloudflared-stable-linux-amd64.rpm

Note: the RPM download link above was taken from CloudFlare's download page.

Once the cloudflared package is installed, you should be able to run cloudflared --version:

$ cloudflared --version
cloudflared version 2018.5.6 (built 2018-05-23-1637 UTC)

Running cloudflared

cloudflared is primarily used to connect to Argo Tunnel Servers, however it can also be used to proxy DNS traffic over HTTPS. This is done with the proxy-dns command:

$ cloudflared proxy-dns
WARN[0000] Cannot determine default configuration path. No file [config.yml config.yaml] in [~/.cloudflared ~/.cloudflare-warp ~/cloudflare-warp /usr/local/etc/cloudflared /etc/cloudflared]
INFO[0000] Adding DNS upstream                           url=""
INFO[0000] Adding DNS upstream                           url=""
INFO[0000] Starting DNS over HTTPS proxy server          addr="dns://localhost:53"
INFO[0000] Starting metrics server                       addr=""

There are also several options that can be used to control the DNS proxy. For example if you wanted to disable auto updates, and use Google as the upstream DNS you could use the following command:

cloudflared --no-autoupdate proxy-dns \
  --upstream https://dns.google.com/experimental?ct

Note: at the time of writing DNS over HTTPS is still a draft standard (draft-ietf-doh-over-https-07); and shouldn't be confused with Google's DNS-over-HTTPS API.

Daemon metrics

Once cloudflared is running, as well as providing a DNS endpoint, it also provides metrics over HTTP:

$ curl |grep cpu_seconds
# HELP process_cpu_seconds_total Total user and system CPU time spent in seconds.
# TYPE process_cpu_seconds_total counter
process_cpu_seconds_total 0.02

Configuring systemd

Unfortunately the cloudflared package only includes the binary; therefore it will not start at boot. This can be fixed by creating a custom unit file with the following contents:

Description=CloudFlare DNS proxy

ExecStart=/usr/local/bin/cloudflared --no-autoupdate  proxy-dns


Save the unit file as /etc/systemd/system/cloudflared-proxy-dns.service, and then run the following commands to enable and start the service:

systemctl daemon-reload
systemctl enable cloudflared-proxy-dns.service
systemctl start cloudflared-proxy-dns.service

Query latency

Like Unbound, cloudflared has to initially establish a TLS connection which introduces noticeable latency:

$ time host example.com
Using domain server:

example.com has address
example.com has IPv6 address 2606:2800:220:1:248:1893:25c8:1946

real    0m0.546s
user    0m0.004s
sys     0m0.002s

However unlike Unbound, the TLS connection is kept open, consequently subsequent queries are significantly faster because the TLS connection is reused:

$ time host example.org
Using domain server:

example.org has address
example.org has IPv6 address 2606:2800:220:1:248:1893:25c8:1946

real    0m0.282s
user    0m0.002s
sys     0m0.005s