2021-04-24 17:27:43 +02:00
|
|
|
package resolvers
|
|
|
|
|
|
|
|
import (
|
2021-08-02 23:02:02 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ameshkov/dnscrypt/v2"
|
2021-04-24 17:27:43 +02:00
|
|
|
"github.com/miekg/dns"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DNSCryptResolver represents the config options for setting up a Resolver.
|
|
|
|
type DNSCryptResolver struct {
|
|
|
|
client *dnscrypt.Client
|
|
|
|
server string
|
2021-08-02 23:02:02 +02:00
|
|
|
resolverInfo *dnscrypt.ResolverInfo
|
2021-04-24 17:27:43 +02:00
|
|
|
resolverOptions Options
|
|
|
|
}
|
|
|
|
|
|
|
|
// DNSCryptResolverOpts holds options for setting up a DNSCrypt resolver.
|
|
|
|
type DNSCryptResolverOpts struct {
|
2022-05-18 06:26:07 +02:00
|
|
|
UseTCP bool
|
2021-04-24 17:27:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSCryptResolver accepts a list of nameservers and configures a DNS resolver.
|
|
|
|
func NewDNSCryptResolver(server string, dnscryptOpts DNSCryptResolverOpts, resolverOpts Options) (Resolver, error) {
|
|
|
|
net := "udp"
|
|
|
|
if dnscryptOpts.UseTCP {
|
|
|
|
net = "tcp"
|
|
|
|
}
|
2022-05-18 06:26:07 +02:00
|
|
|
|
2021-08-02 23:02:02 +02:00
|
|
|
client := &dnscrypt.Client{Net: net, Timeout: resolverOpts.Timeout, UDPSize: 4096}
|
|
|
|
resolverInfo, err := client.Dial(server)
|
2021-04-24 17:27:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &DNSCryptResolver{
|
|
|
|
client: client,
|
2021-08-02 23:02:02 +02:00
|
|
|
resolverInfo: resolverInfo,
|
|
|
|
server: resolverInfo.ServerAddress,
|
2021-04-24 17:27:43 +02:00
|
|
|
resolverOptions: resolverOpts,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 *DNSCryptResolver) Lookup(question dns.Question) (Response, error) {
|
|
|
|
var (
|
|
|
|
rsp Response
|
|
|
|
messages = prepareMessages(question, r.resolverOptions.Ndots, r.resolverOptions.SearchList)
|
|
|
|
)
|
|
|
|
for _, msg := range messages {
|
|
|
|
r.resolverOptions.Logger.WithFields(logrus.Fields{
|
|
|
|
"domain": msg.Question[0].Name,
|
|
|
|
"ndots": r.resolverOptions.Ndots,
|
2021-04-24 17:49:06 +02:00
|
|
|
"nameserver": r.server,
|
2021-04-24 17:27:43 +02:00
|
|
|
}).Debug("Attempting to resolve")
|
2021-08-02 23:02:02 +02:00
|
|
|
now := time.Now()
|
|
|
|
in, err := r.client.Exchange(&msg, r.resolverInfo)
|
2021-04-24 17:27:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return rsp, err
|
|
|
|
}
|
2021-08-02 23:02:02 +02:00
|
|
|
rtt := time.Since(now)
|
2021-04-24 17:27:43 +02: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(in, rtt, r.server)
|
|
|
|
rsp.Authorities = output.Authorities
|
|
|
|
rsp.Answers = output.Answers
|
|
|
|
|
|
|
|
if len(output.Answers) > 0 {
|
|
|
|
// stop iterating the searchlist.
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rsp, nil
|
|
|
|
}
|