doggo/pkg/resolvers/doh.go

91 lines
2.0 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"
"errors"
"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"
)
// DOHResolver represents the config options for setting up a DOH based resolver.
type DOHResolver struct {
client *http.Client
servers []string
}
type DOHResolverOpts struct {
Timeout time.Duration
}
2020-12-12 07:16:13 +01:00
// NewDOHResolver accepts a list of nameservers and configures a DOH based resolver.
func NewDOHResolver(servers []string, opts DOHResolverOpts) (Resolver, error) {
if len(servers) == 0 {
return nil, errors.New(`no DOH server specified`)
}
for _, s := range servers {
u, err := url.ParseRequestURI(s)
if err != nil {
return nil, fmt.Errorf("%s is not a valid HTTPS nameserver", s)
}
if u.Scheme != "https" {
return nil, fmt.Errorf("missing https in %s", s)
}
}
2020-12-12 07:16:13 +01:00
httpClient := &http.Client{
Timeout: opts.Timeout,
2020-12-12 07:16:13 +01:00
}
return &DOHResolver{
client: httpClient,
servers: servers,
}, nil
}
2020-12-13 08:15:45 +01:00
func (d *DOHResolver) Lookup(questions []dns.Question) ([]Response, error) {
2020-12-12 11:57:13 +01:00
var (
messages = prepareMessages(questions)
responses []Response
)
2020-12-12 07:46:54 +01:00
2020-12-12 11:57:13 +01:00
for _, msg := range messages {
// get the DNS Message in wire format.
b, err := msg.Pack()
2020-12-12 07:16:13 +01:00
if err != nil {
2020-12-12 11:57:13 +01:00
return nil, err
2020-12-12 07:16:13 +01:00
}
2020-12-13 08:15:45 +01:00
for _, srv := range d.servers {
2020-12-12 11:57:13 +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-13 08:15:45 +01:00
resp, err := d.client.Post(srv, "application/dns-message", bytes.NewBuffer(b))
2020-12-12 07:16:13 +01:00
if err != nil {
2020-12-12 11:57:13 +01:00
return nil, err
2020-12-12 07:16:13 +01:00
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("error from nameserver %s", resp.Status)
2020-12-12 07:16:13 +01:00
}
2020-12-12 11:57:13 +01:00
rtt := time.Since(now)
// extract the binary response in DNS Message.
2020-12-12 07:16:13 +01:00
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
2020-12-12 11:57:13 +01:00
return nil, err
2020-12-12 07:16:13 +01:00
}
2020-12-12 11:57:13 +01:00
err = msg.Unpack(body)
2020-12-12 07:16:13 +01:00
if err != nil {
2020-12-12 11:57:13 +01:00
return nil, err
2020-12-12 07:16:13 +01:00
}
2020-12-12 11:57:13 +01:00
rsp := Response{
Message: msg,
RTT: rtt,
Nameserver: srv,
2020-12-12 07:16:13 +01:00
}
2020-12-12 11:57:13 +01:00
responses = append(responses, rsp)
2020-12-12 07:16:13 +01:00
}
}
2020-12-12 11:57:13 +01:00
return responses, nil
2020-12-12 07:16:13 +01:00
}