2020-12-11 12:18:54 +01:00
|
|
|
package resolvers
|
2020-12-10 10:39:05 +01:00
|
|
|
|
|
|
|
import (
|
2022-05-18 06:26:07 +02:00
|
|
|
"crypto/tls"
|
2022-05-17 20:04:30 +02:00
|
|
|
"time"
|
|
|
|
|
2020-12-10 10:39:05 +01:00
|
|
|
"github.com/miekg/dns"
|
2020-12-24 17:25:20 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-12-10 10:39:05 +01:00
|
|
|
)
|
|
|
|
|
2020-12-16 15:10:01 +01:00
|
|
|
// ClassicResolver represents the config options for setting up a Resolver.
|
|
|
|
type ClassicResolver struct {
|
2020-12-24 17:25:20 +01:00
|
|
|
client *dns.Client
|
|
|
|
server string
|
|
|
|
resolverOptions Options
|
2020-12-10 10:39:05 +01:00
|
|
|
}
|
|
|
|
|
2020-12-16 15:10:01 +01:00
|
|
|
// ClassicResolverOpts holds options for setting up a Classic resolver.
|
|
|
|
type ClassicResolverOpts struct {
|
2022-05-18 06:26:07 +02:00
|
|
|
UseTLS bool
|
|
|
|
UseTCP bool
|
2020-12-12 07:46:54 +01:00
|
|
|
}
|
|
|
|
|
2020-12-16 15:10:01 +01:00
|
|
|
// NewClassicResolver accepts a list of nameservers and configures a DNS resolver.
|
2020-12-24 17:25:20 +01:00
|
|
|
func NewClassicResolver(server string, classicOpts ClassicResolverOpts, resolverOpts Options) (Resolver, error) {
|
2020-12-16 15:10:01 +01:00
|
|
|
net := "udp"
|
2020-12-15 18:39:10 +01:00
|
|
|
client := &dns.Client{
|
2020-12-24 17:25:20 +01:00
|
|
|
Timeout: resolverOpts.Timeout,
|
2020-12-16 15:10:01 +01:00
|
|
|
Net: "udp",
|
|
|
|
}
|
|
|
|
|
2020-12-24 17:25:20 +01:00
|
|
|
if classicOpts.UseTCP {
|
2020-12-16 15:10:01 +01:00
|
|
|
net = "tcp"
|
2020-12-15 18:39:10 +01:00
|
|
|
}
|
2020-12-13 08:15:45 +01:00
|
|
|
|
2022-05-18 06:26:07 +02:00
|
|
|
if resolverOpts.UseIPv4 {
|
2020-12-16 15:10:01 +01:00
|
|
|
net = net + "4"
|
2020-12-12 07:46:54 +01:00
|
|
|
}
|
2022-05-18 06:26:07 +02:00
|
|
|
if resolverOpts.UseIPv6 {
|
2020-12-16 15:10:01 +01:00
|
|
|
net = net + "6"
|
|
|
|
}
|
|
|
|
|
2020-12-24 17:25:20 +01:00
|
|
|
if classicOpts.UseTLS {
|
2020-12-16 15:10:01 +01:00
|
|
|
net = net + "-tls"
|
2022-05-18 06:26:07 +02:00
|
|
|
// Provide extra TLS config for doing/skipping hostname verification.
|
|
|
|
client.TLSConfig = &tls.Config{
|
|
|
|
ServerName: resolverOpts.TLSHostname,
|
|
|
|
InsecureSkipVerify: resolverOpts.InsecureSkipVerify,
|
|
|
|
}
|
2020-12-12 07:46:54 +01:00
|
|
|
}
|
2020-12-16 15:10:01 +01:00
|
|
|
|
|
|
|
client.Net = net
|
|
|
|
|
|
|
|
return &ClassicResolver{
|
2020-12-24 17:25:20 +01:00
|
|
|
client: client,
|
|
|
|
server: server,
|
|
|
|
resolverOptions: resolverOpts,
|
2020-12-12 07:16:13 +01:00
|
|
|
}, nil
|
2020-12-10 10:39:05 +01:00
|
|
|
}
|
|
|
|
|
2020-12-24 17:25:20 +01:00
|
|
|
// 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 *ClassicResolver) Lookup(question dns.Question) (Response, error) {
|
2020-12-12 11:57:13 +01:00
|
|
|
var (
|
2020-12-24 17:25:20 +01:00
|
|
|
rsp Response
|
|
|
|
messages = prepareMessages(question, r.resolverOptions.Ndots, r.resolverOptions.SearchList)
|
2020-12-12 11:57:13 +01:00
|
|
|
)
|
2020-12-10 10:39:05 +01:00
|
|
|
for _, msg := range messages {
|
2020-12-24 17:25:20 +01:00
|
|
|
r.resolverOptions.Logger.WithFields(logrus.Fields{
|
|
|
|
"domain": msg.Question[0].Name,
|
|
|
|
"ndots": r.resolverOptions.Ndots,
|
|
|
|
"nameserver": r.server,
|
|
|
|
}).Debug("Attempting to resolve")
|
2022-05-17 20:04:30 +02:00
|
|
|
|
|
|
|
// Since the library doesn't include tcp.Dial time,
|
|
|
|
// it's better to not rely on `rtt` provided here and calculate it ourselves.
|
|
|
|
now := time.Now()
|
|
|
|
in, _, err := r.client.Exchange(&msg, r.server)
|
2020-12-16 14:08:34 +01:00
|
|
|
if err != nil {
|
2020-12-24 17:25:20 +01:00
|
|
|
return rsp, err
|
2020-12-16 14:08:34 +01:00
|
|
|
}
|
2022-05-17 20:04:30 +02:00
|
|
|
// Pack questions in output.
|
2020-12-24 17:25:20 +01:00
|
|
|
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)
|
|
|
|
}
|
2022-05-17 20:04:30 +02:00
|
|
|
rtt := time.Since(now)
|
|
|
|
|
|
|
|
// Get the authorities and answers.
|
2020-12-24 17:25:20 +01:00
|
|
|
output := parseMessage(in, rtt, r.server)
|
|
|
|
rsp.Authorities = output.Authorities
|
|
|
|
rsp.Answers = output.Answers
|
|
|
|
|
|
|
|
if len(output.Answers) > 0 {
|
2022-05-17 20:04:30 +02:00
|
|
|
// Stop iterating the searchlist.
|
2020-12-24 17:25:20 +01:00
|
|
|
break
|
2020-12-10 10:39:05 +01:00
|
|
|
}
|
|
|
|
}
|
2020-12-24 17:25:20 +01:00
|
|
|
return rsp, nil
|
2020-12-10 10:39:05 +01:00
|
|
|
}
|