2020-12-11 12:18:54 +01:00
|
|
|
package resolvers
|
2020-12-10 10:39:05 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
2020-12-12 07:16:13 +01:00
|
|
|
// ClassicResolver represents the config options for setting up a Resolver.
|
|
|
|
type ClassicResolver struct {
|
2020-12-10 10:39:05 +01:00
|
|
|
client *dns.Client
|
|
|
|
servers []string
|
|
|
|
}
|
|
|
|
|
|
|
|
//DefaultResolvConfPath specifies path to default resolv config file on UNIX.
|
|
|
|
const DefaultResolvConfPath = "/etc/resolv.conf"
|
|
|
|
|
2020-12-12 07:16:13 +01:00
|
|
|
// NewClassicResolver accepts a list of nameservers and configures a DNS resolver.
|
|
|
|
func NewClassicResolver(servers []string) (Resolver, error) {
|
2020-12-10 10:39:05 +01:00
|
|
|
client := &dns.Client{}
|
2020-12-11 11:35:16 +01:00
|
|
|
var nameservers []string
|
|
|
|
for _, srv := range servers {
|
|
|
|
if i := net.ParseIP(srv); i != nil {
|
|
|
|
nameservers = append(nameservers, net.JoinHostPort(srv, "53"))
|
|
|
|
} else {
|
2020-12-12 07:16:13 +01:00
|
|
|
host, port, err := net.SplitHostPort(srv)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
nameservers = append(nameservers, fmt.Sprintf("%s:%s", host, port))
|
2020-12-11 11:35:16 +01:00
|
|
|
}
|
|
|
|
}
|
2020-12-12 07:16:13 +01:00
|
|
|
return &ClassicResolver{
|
2020-12-10 10:39:05 +01:00
|
|
|
client: client,
|
2020-12-11 11:35:16 +01:00
|
|
|
servers: nameservers,
|
2020-12-12 07:16:13 +01:00
|
|
|
}, nil
|
2020-12-10 10:39:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewResolverFromResolvFile loads the configuration from resolv config file
|
|
|
|
// and initialises a DNS resolver.
|
2020-12-11 12:18:54 +01:00
|
|
|
func NewResolverFromResolvFile(resolvFilePath string) (Resolver, error) {
|
2020-12-10 10:39:05 +01:00
|
|
|
if resolvFilePath == "" {
|
|
|
|
resolvFilePath = DefaultResolvConfPath
|
|
|
|
}
|
|
|
|
cfg, err := dns.ClientConfigFromFile(resolvFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
servers := make([]string, 0, len(cfg.Servers))
|
|
|
|
for _, s := range cfg.Servers {
|
|
|
|
ip := net.ParseIP(s)
|
|
|
|
// handle IPv6
|
|
|
|
if ip != nil && ip.To4() != nil {
|
|
|
|
servers = append(servers, fmt.Sprintf("%s:%s", s, cfg.Port))
|
|
|
|
} else {
|
|
|
|
servers = append(servers, fmt.Sprintf("[%s]:%s", s, cfg.Port))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &dns.Client{}
|
2020-12-12 07:16:13 +01:00
|
|
|
return &ClassicResolver{
|
2020-12-10 10:39:05 +01:00
|
|
|
client: client,
|
|
|
|
servers: servers,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-12-10 17:14:04 +01:00
|
|
|
// Lookup prepare a list of DNS messages to be sent to the server.
|
|
|
|
// It's possible to send multiple question in one message
|
|
|
|
// but some nameservers are not able to
|
2020-12-12 07:16:13 +01:00
|
|
|
func (c *ClassicResolver) Lookup(questions []dns.Question) error {
|
2020-12-10 17:14:04 +01:00
|
|
|
var messages = make([]dns.Msg, 0, len(questions))
|
|
|
|
for _, q := range questions {
|
2020-12-10 10:39:05 +01:00
|
|
|
msg := dns.Msg{}
|
|
|
|
msg.Id = dns.Id()
|
|
|
|
msg.RecursionDesired = true
|
|
|
|
// It's recommended to only send 1 question for 1 DNS message.
|
2020-12-10 17:14:04 +01:00
|
|
|
msg.Question = []dns.Question{q}
|
2020-12-10 10:39:05 +01:00
|
|
|
messages = append(messages, msg)
|
|
|
|
}
|
|
|
|
for _, msg := range messages {
|
2020-12-12 07:16:13 +01:00
|
|
|
for _, srv := range c.servers {
|
|
|
|
in, rtt, err := c.client.Exchange(&msg, srv)
|
2020-12-10 10:39:05 +01:00
|
|
|
if err != nil {
|
2020-12-10 17:14:04 +01:00
|
|
|
return err
|
2020-12-10 10:39:05 +01:00
|
|
|
}
|
|
|
|
for _, ans := range in.Answer {
|
|
|
|
if t, ok := ans.(*dns.A); ok {
|
|
|
|
fmt.Println(t.String())
|
|
|
|
}
|
|
|
|
}
|
2020-12-10 17:24:53 +01:00
|
|
|
fmt.Println("rtt is", rtt)
|
2020-12-10 10:39:05 +01:00
|
|
|
}
|
|
|
|
}
|
2020-12-10 17:14:04 +01:00
|
|
|
return nil
|
2020-12-10 10:39:05 +01:00
|
|
|
}
|