doggo/pkg/resolvers/doh.go

102 lines
2.6 KiB
Go
Raw Normal View History

2020-12-11 12:18:54 +01:00
package resolvers
2020-12-12 07:16:13 +01:00
import (
"bytes"
"fmt"
2020-12-12 07:16:13 +01:00
"io/ioutil"
"net/http"
"net/url"
2020-12-12 07:16:13 +01:00
"time"
"github.com/miekg/dns"
2020-12-24 17:25:20 +01:00
"github.com/sirupsen/logrus"
2020-12-12 07:16:13 +01:00
)
// DOHResolver represents the config options for setting up a DOH based resolver.
type DOHResolver struct {
2020-12-24 17:25:20 +01:00
client *http.Client
server string
resolverOptions Options
}
2020-12-16 14:08:34 +01:00
// NewDOHResolver accepts a nameserver address and configures a DOH based resolver.
2020-12-24 17:25:20 +01:00
func NewDOHResolver(server string, resolverOpts Options) (Resolver, error) {
2020-12-16 14:08:34 +01:00
// do basic validation
u, err := url.ParseRequestURI(server)
if err != nil {
return nil, fmt.Errorf("%s is not a valid HTTPS nameserver", server)
}
2020-12-16 14:08:34 +01:00
if u.Scheme != "https" {
return nil, fmt.Errorf("missing https in %s", server)
}
2020-12-12 07:16:13 +01:00
httpClient := &http.Client{
2020-12-24 17:25:20 +01:00
Timeout: resolverOpts.Timeout,
2020-12-12 07:16:13 +01:00
}
return &DOHResolver{
2020-12-24 17:25:20 +01:00
client: httpClient,
server: server,
resolverOptions: resolverOpts,
2020-12-12 07:16:13 +01:00
}, nil
}
2020-12-24 17:25:20 +01:00
// Lookup takes a dns.Question and sends them to DNS Server.
// It parses the Response from the server in a custom output format.
func (r *DOHResolver) Lookup(question dns.Question) (Response, error) {
2020-12-12 11:57:13 +01:00
var (
2020-12-24 17:25:20 +01:00
rsp Response
messages = prepareMessages(question, r.resolverOptions.Ndots, r.resolverOptions.SearchList)
2020-12-12 11:57:13 +01:00
)
2020-12-12 07:46:54 +01:00
2020-12-12 11:57:13 +01:00
for _, msg := range messages {
2020-12-24 17:25:20 +01:00
r.resolverOptions.Logger.WithFields(logrus.Fields{
"domain": msg.Question[0].Name,
"ndots": r.resolverOptions.Ndots,
"nameserver": r.server,
}).Debug("Attempting to resolve")
2020-12-12 11:57:13 +01:00
// get the DNS Message in wire format.
b, err := msg.Pack()
2020-12-12 07:16:13 +01:00
if err != nil {
2020-12-24 17:25:20 +01:00
return rsp, err
2020-12-12 07:16:13 +01:00
}
2020-12-16 14:08:34 +01:00
now := time.Now()
// Make an HTTP POST request to the DNS server with the DNS message as wire format bytes in the body.
2020-12-24 17:25:20 +01:00
resp, err := r.client.Post(r.server, "application/dns-message", bytes.NewBuffer(b))
2020-12-16 14:08:34 +01:00
if err != nil {
2020-12-24 17:25:20 +01:00
return rsp, err
2020-12-16 14:08:34 +01:00
}
if resp.StatusCode != http.StatusOK {
2020-12-24 17:25:20 +01:00
return rsp, fmt.Errorf("error from nameserver %s", resp.Status)
2020-12-16 14:08:34 +01:00
}
rtt := time.Since(now)
// extract the binary response in DNS Message.
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
2020-12-24 17:25:20 +01:00
return rsp, err
2020-12-16 14:08:34 +01:00
}
2020-12-12 07:16:13 +01:00
2020-12-16 14:08:34 +01:00
err = msg.Unpack(body)
if err != nil {
2020-12-24 17:25:20 +01:00
return rsp, err
2020-12-16 14:08:34 +01:00
}
2020-12-24 17:25:20 +01:00
// pack questions in output.
for _, q := range msg.Question {
ques := Question{
Name: q.Name,
Class: dns.ClassToString[q.Qclass],
Type: dns.TypeToString[q.Qtype],
}
rsp.Questions = append(rsp.Questions, ques)
}
// get the authorities and answers.
output := parseMessage(&msg, rtt, r.server)
rsp.Authorities = output.Authorities
rsp.Answers = output.Answers
if len(output.Answers) > 0 {
// stop iterating the searchlist.
break
2020-12-12 07:16:13 +01:00
}
}
2020-12-24 17:25:20 +01:00
return rsp, nil
2020-12-12 07:16:13 +01:00
}