doggo/pkg/resolvers/doh.go

72 lines
1.5 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"
"io/ioutil"
"net/http"
"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
}
// NewDOHResolver accepts a list of nameservers and configures a DOH based resolver.
func NewDOHResolver(servers []string) (Resolver, error) {
httpClient := &http.Client{
Timeout: 10 * time.Second,
}
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 {
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
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
}