2020-12-11 12:18:54 +01:00
|
|
|
package resolvers
|
2020-12-12 07:16:13 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-12-15 18:39:10 +01:00
|
|
|
"fmt"
|
2020-12-12 07:16:13 +01:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2020-12-15 18:39:10 +01:00
|
|
|
"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 {
|
2020-12-16 14:08:34 +01:00
|
|
|
client *http.Client
|
|
|
|
server string
|
2020-12-12 07:16:13 +01:00
|
|
|
}
|
|
|
|
|
2020-12-15 18:39:10 +01:00
|
|
|
type DOHResolverOpts struct {
|
|
|
|
Timeout time.Duration
|
|
|
|
}
|
|
|
|
|
2020-12-16 14:08:34 +01:00
|
|
|
// NewDOHResolver accepts a nameserver address and configures a DOH based resolver.
|
|
|
|
func NewDOHResolver(server string, opts DOHResolverOpts) (Resolver, error) {
|
|
|
|
// 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-15 18:39:10 +01:00
|
|
|
}
|
2020-12-16 14:08:34 +01:00
|
|
|
if u.Scheme != "https" {
|
|
|
|
return nil, fmt.Errorf("missing https in %s", server)
|
2020-12-15 18:39:10 +01:00
|
|
|
}
|
2020-12-12 07:16:13 +01:00
|
|
|
httpClient := &http.Client{
|
2020-12-15 18:39:10 +01:00
|
|
|
Timeout: opts.Timeout,
|
2020-12-12 07:16:13 +01:00
|
|
|
}
|
|
|
|
return &DOHResolver{
|
2020-12-16 14:08:34 +01:00
|
|
|
client: httpClient,
|
|
|
|
server: server,
|
2020-12-12 07:16:13 +01:00
|
|
|
}, 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-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.
|
|
|
|
resp, err := d.client.Post(d.server, "application/dns-message", bytes.NewBuffer(b))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, fmt.Errorf("error from nameserver %s", resp.Status)
|
|
|
|
}
|
|
|
|
rtt := time.Since(now)
|
|
|
|
// extract the binary response in DNS Message.
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-12 07:16:13 +01:00
|
|
|
|
2020-12-16 14:08:34 +01:00
|
|
|
err = msg.Unpack(body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rsp := Response{
|
|
|
|
Message: msg,
|
|
|
|
RTT: rtt,
|
|
|
|
Nameserver: d.server,
|
2020-12-12 07:16:13 +01:00
|
|
|
}
|
2020-12-16 14:08:34 +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
|
|
|
}
|