feat: Multiple resolvers continued
This commit is contained in:
parent
b46b64c1ae
commit
53e7f2e59c
11 changed files with 253 additions and 214 deletions
|
@ -2,7 +2,6 @@ package resolvers
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
@ -14,34 +13,30 @@ import (
|
|||
|
||||
// DOHResolver represents the config options for setting up a DOH based resolver.
|
||||
type DOHResolver struct {
|
||||
client *http.Client
|
||||
servers []string
|
||||
client *http.Client
|
||||
server string
|
||||
}
|
||||
|
||||
type DOHResolverOpts struct {
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
// 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`)
|
||||
// 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)
|
||||
}
|
||||
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)
|
||||
}
|
||||
if u.Scheme != "https" {
|
||||
return nil, fmt.Errorf("missing https in %s", server)
|
||||
}
|
||||
httpClient := &http.Client{
|
||||
Timeout: opts.Timeout,
|
||||
}
|
||||
return &DOHResolver{
|
||||
client: httpClient,
|
||||
servers: servers,
|
||||
client: httpClient,
|
||||
server: server,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
@ -57,34 +52,32 @@ func (d *DOHResolver) Lookup(questions []dns.Question) ([]Response, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, srv := range d.servers {
|
||||
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(srv, "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
|
||||
}
|
||||
|
||||
err = msg.Unpack(body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rsp := Response{
|
||||
Message: msg,
|
||||
RTT: rtt,
|
||||
Nameserver: srv,
|
||||
}
|
||||
responses = append(responses, rsp)
|
||||
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
|
||||
}
|
||||
|
||||
err = msg.Unpack(body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rsp := Response{
|
||||
Message: msg,
|
||||
RTT: rtt,
|
||||
Nameserver: d.server,
|
||||
}
|
||||
responses = append(responses, rsp)
|
||||
}
|
||||
return responses, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue