feat: add support for tcp, dot
This commit is contained in:
parent
8bcd940685
commit
0aa3b36b35
6 changed files with 48 additions and 22 deletions
|
@ -7,6 +7,15 @@ import (
|
|||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
// ClassicResolver represents the config options for setting up a Resolver.
|
||||
type ClassicResolver struct {
|
||||
client *dns.Client
|
||||
|
@ -18,24 +27,24 @@ type ClassicResolverOpts struct {
|
|||
UseIPv4 bool
|
||||
UseIPv6 bool
|
||||
UseTCP bool
|
||||
UseTLS bool
|
||||
}
|
||||
|
||||
//DefaultResolvConfPath specifies path to default resolv config file on UNIX.
|
||||
const DefaultResolvConfPath = "/etc/resolv.conf"
|
||||
|
||||
// NewClassicResolver accepts a list of nameservers and configures a DNS resolver.
|
||||
func NewClassicResolver(servers []string, opts ClassicResolverOpts) (Resolver, error) {
|
||||
client := &dns.Client{}
|
||||
var nameservers []string
|
||||
for _, srv := range servers {
|
||||
if i := net.ParseIP(srv); i != nil {
|
||||
nameservers = append(nameservers, net.JoinHostPort(srv, "53"))
|
||||
} else {
|
||||
host, port, err := net.SplitHostPort(srv)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
// 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))
|
||||
}
|
||||
nameservers = append(nameservers, fmt.Sprintf("%s:%s", host, port))
|
||||
} else {
|
||||
// use the port user specified.
|
||||
nameservers = append(nameservers, srv)
|
||||
}
|
||||
}
|
||||
client.Net = "udp"
|
||||
|
@ -45,6 +54,12 @@ func NewClassicResolver(servers []string, opts ClassicResolverOpts) (Resolver, e
|
|||
if opts.UseIPv6 {
|
||||
client.Net = "udp6"
|
||||
}
|
||||
if opts.UseTCP {
|
||||
client.Net = "tcp"
|
||||
}
|
||||
if opts.UseTLS {
|
||||
client.Net = "tcp-tls"
|
||||
}
|
||||
return &ClassicResolver{
|
||||
client: client,
|
||||
servers: nameservers,
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
package resolvers
|
Loading…
Add table
Add a link
Reference in a new issue