This package implements a DNS-over-HTTPS client and server in Go. Currently only JSON responses are implemented.
The client is tested against both Google DNS and CloudFlare 1.1.1.1 DNS-over-HTTPS implementation.
go get -u github.com/jamescun/doh
- doh: human-readable DNS-over-HTTPS command line client
## Libraries
Note about easyjson: by default doh will use encoding/json
, however easyjson is supported by using the easyjson
build tag.
This package directly exposes a client configured to connect to Google's Public DNS:
answer, rtt, err := doh.Do(&doh.Question{
Name: "example.org.",
Type: doh.A,
})
If you with to use another DNS-over-HTTPS provider, such as CloudFlare 1.1.1.1, one can be configured:
client := &doh.Client{
Addr: &url.URL{
Scheme: "https",
Host: "cloudflare-dns.com",
Path: "/dns-query",
},
}
This package includes a net/http
compatible server which can be mounted directly under a http.Server
or with your favourite router.
A simple handler which replies localhost to every question might look like:
func myHandler(q *Question) *Answer {
if q.Type == doh.A {
return &Answer{
Status: doh.NoError,
Question: doh.Questions{q},
Answer: doh.Records{
&doh.Record{Name: "example.org", Type: doh.A, TTL: 300, Data: "127.0.0.1"},
},
}
} else if q.Type == doh.AAAA {
return &Answer{
Status: doh.NoError,
Question: doh.Questions{q},
Answer: doh.Records{
&doh.Record{Name: "example.org", Type: doh.AAAA, TTL: 300, Data: "::1/128"},
},
}
} else {
return &Answer{
Status: doh.NoError,
Question: doh.Questions{q},
}
}
}
Handlers are attached to a Server
object:
dns := &doh.Server{
Handler: myHandler,
}
h := &http.Server{
Addr: "127.0.0.1:443",
Handler: dns,
}
h.ListenAndServerTLS("cert.pem", "key.pem")