doggo/pkg/resolvers/classic.go

124 lines
3.0 KiB
Go
Raw Normal View History

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 08:46:45 +01:00
const (
// DefaultUDPPort specifies the default port for a DNS server connecting over UDP
DefaultUDPPort = "53"
// DefaultTLSPort specifies the default port for a DNS server connecting over TCP over TLS
DefaultTLSPort = "853"
//DefaultResolvConfPath specifies path to default resolv config file on UNIX.
DefaultResolvConfPath = "/etc/resolv.conf"
)
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
}
2020-12-12 07:46:54 +01:00
// ClassicResolverOpts holds options for setting up a Classic resolver.
type ClassicResolverOpts struct {
UseIPv4 bool
UseIPv6 bool
UseTCP bool
2020-12-12 08:46:45 +01:00
UseTLS bool
2020-12-12 07:46:54 +01:00
}
2020-12-12 07:16:13 +01:00
// NewClassicResolver accepts a list of nameservers and configures a DNS resolver.
2020-12-12 07:46:54 +01:00
func NewClassicResolver(servers []string, opts ClassicResolverOpts) (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 {
2020-12-12 08:46:45 +01:00
// if no port specified in nameserver, append defaults.
if opts.UseTLS == true {
nameservers = append(nameservers, net.JoinHostPort(srv, DefaultTLSPort))
} else {
nameservers = append(nameservers, net.JoinHostPort(srv, DefaultUDPPort))
2020-12-12 07:16:13 +01:00
}
2020-12-12 08:46:45 +01:00
} else {
// use the port user specified.
nameservers = append(nameservers, srv)
2020-12-11 11:35:16 +01:00
}
}
2020-12-12 07:46:54 +01:00
client.Net = "udp"
if opts.UseIPv4 {
client.Net = "udp4"
}
if opts.UseIPv6 {
client.Net = "udp6"
}
2020-12-12 08:46:45 +01:00
if opts.UseTCP {
client.Net = "tcp"
}
if opts.UseTLS {
client.Net = "tcp-tls"
}
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 11:57:13 +01:00
func (c *ClassicResolver) Lookup(questions []dns.Question) ([]Response, error) {
var (
messages = prepareMessages(questions)
responses []Response
)
2020-12-12 07:46:54 +01:00
2020-12-10 10:39:05 +01:00
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-12 11:57:13 +01:00
return nil, err
2020-12-10 10:39:05 +01:00
}
2020-12-12 11:57:13 +01:00
msg.Answer = in.Answer
rsp := Response{
Message: msg,
RTT: rtt,
Nameserver: srv,
2020-12-10 10:39:05 +01:00
}
2020-12-12 11:57:13 +01:00
responses = append(responses, rsp)
2020-12-10 10:39:05 +01:00
}
}
2020-12-12 11:57:13 +01:00
return responses, nil
2020-12-10 10:39:05 +01:00
}