feat: Simplify resolvers

pull/2/head
Karan Sharma 2020-12-16 19:40:01 +05:30
parent ec46ed5990
commit 114e5ba68b
5 changed files with 67 additions and 90 deletions

10
TODO.md
View File

@ -6,11 +6,11 @@
- [x] Add a resolve method - [x] Add a resolve method
- [x] Make it separate from Hub - [x] Make it separate from Hub
- [x] Parse output into separate fields - [x] Parse output into separate fields
- [ ] Test IPv6/IPv4 only options - [x] Test IPv6/IPv4 only options
- [x] Add DOH support - [x] Add DOH support
- [ ] Add DOT support - [x] Add DOT support
- [ ] Add DNS protocol on TCP mode support. - [x] Add DNS protocol on TCP mode support.
- [ ] Change lookup method. - [x] Change lookup method.
- [x] Major records supported - [x] Major records supported
- [x] Support multiple resolvers - [x] Support multiple resolvers
- [x] Take multiple transport options and initialise resolvers accordingly. - [x] Take multiple transport options and initialise resolvers accordingly.
@ -37,7 +37,7 @@
- [x] Add client transport options - [x] Add client transport options
- [x] Fix an issue while loading free form args, where the same records are being added twice - [x] Fix an issue while loading free form args, where the same records are being added twice
- [x] Remove urfave/cli in favour of `pflag + koanf` - [x] Remove urfave/cli in favour of `pflag + koanf`
- [ ] Flags - Remove uneeded ones - [x] Flags - Remove uneeded ones
## Refactors ## Refactors
- [ ] Don't abuse Hub as global. Refactor methods to be independent of hub. - [ ] Don't abuse Hub as global. Refactor methods to be independent of hub.

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"fmt"
"os" "os"
"github.com/knadh/koanf" "github.com/knadh/koanf"
@ -39,6 +38,8 @@ func main() {
f.Int("timeout", 5, "Sets the timeout for a query to T seconds. The default timeout is 5 seconds.") f.Int("timeout", 5, "Sets the timeout for a query to T seconds. The default timeout is 5 seconds.")
f.Bool("search", false, "Use the search list provided in resolv.conf. It sets the `ndots` parameter as well unless overriden by `ndots` flag.") f.Bool("search", false, "Use the search list provided in resolv.conf. It sets the `ndots` parameter as well unless overriden by `ndots` flag.")
f.Int("ndots", 1, "Specify the ndots paramter. Default value is taken from resolv.conf and fallbacks to 1 if ndots statement is missing in resolv.conf") f.Int("ndots", 1, "Specify the ndots paramter. Default value is taken from resolv.conf and fallbacks to 1 if ndots statement is missing in resolv.conf")
f.BoolP("ipv4", "4", false, "Use IPv4 only")
f.BoolP("ipv6", "6", false, "Use IPv6 only")
// Output Options // Output Options
f.BoolP("json", "J", false, "Set the output format as JSON") f.BoolP("json", "J", false, "Set the output format as JSON")
@ -82,7 +83,6 @@ func main() {
hub.Logger.Exit(2) hub.Logger.Exit(2)
} }
if ns.Address != "" && ns.Type != "" { if ns.Address != "" && ns.Type != "" {
fmt.Println("appending", ns.Address, ns.Type)
hub.Nameservers = append(hub.Nameservers, ns) hub.Nameservers = append(hub.Nameservers, ns)
} }
} }

View File

@ -19,6 +19,7 @@ const (
DefaultTLSPort = "853" DefaultTLSPort = "853"
// DefaultUDPPort specifies the default port for a DNS server connecting over UDP // DefaultUDPPort specifies the default port for a DNS server connecting over UDP
DefaultUDPPort = "53" DefaultUDPPort = "53"
DefaultTCPPort = "53"
UDPResolver = "udp" UDPResolver = "udp"
DOHResolver = "doh" DOHResolver = "doh"
TCPResolver = "tcp" TCPResolver = "tcp"
@ -40,12 +41,28 @@ func (hub *Hub) initResolver() error {
} }
hub.Resolver = append(hub.Resolver, rslvr) hub.Resolver = append(hub.Resolver, rslvr)
} }
if ns.Type == TCPResolver { if ns.Type == DOTResolver {
hub.Logger.Debug("initiating TCP resolver") hub.Logger.Debug("initiating DOT resolver")
rslvr, err := resolvers.NewTCPResolver(ns.Address, resolvers.TCPResolverOpts{ rslvr, err := resolvers.NewClassicResolver(ns.Address, resolvers.ClassicResolverOpts{
IPv4Only: hub.QueryFlags.UseIPv4, IPv4Only: hub.QueryFlags.UseIPv4,
IPv6Only: hub.QueryFlags.UseIPv6, IPv6Only: hub.QueryFlags.UseIPv6,
Timeout: hub.QueryFlags.Timeout * time.Second, Timeout: hub.QueryFlags.Timeout * time.Second,
UseTLS: true,
UseTCP: true,
})
if err != nil {
return err
}
hub.Resolver = append(hub.Resolver, rslvr)
}
if ns.Type == TCPResolver {
hub.Logger.Debug("initiating TCP resolver")
rslvr, err := resolvers.NewClassicResolver(ns.Address, resolvers.ClassicResolverOpts{
IPv4Only: hub.QueryFlags.UseIPv4,
IPv6Only: hub.QueryFlags.UseIPv6,
Timeout: hub.QueryFlags.Timeout * time.Second,
UseTLS: false,
UseTCP: true,
}) })
if err != nil { if err != nil {
return err return err
@ -54,10 +71,12 @@ func (hub *Hub) initResolver() error {
} }
if ns.Type == UDPResolver { if ns.Type == UDPResolver {
hub.Logger.Debug("initiating UDP resolver") hub.Logger.Debug("initiating UDP resolver")
rslvr, err := resolvers.NewUDPResolver(ns.Address, resolvers.UDPResolverOpts{ rslvr, err := resolvers.NewClassicResolver(ns.Address, resolvers.ClassicResolverOpts{
IPv4Only: hub.QueryFlags.UseIPv4, IPv4Only: hub.QueryFlags.UseIPv4,
IPv6Only: hub.QueryFlags.UseIPv6, IPv6Only: hub.QueryFlags.UseIPv6,
Timeout: hub.QueryFlags.Timeout * time.Second, Timeout: hub.QueryFlags.Timeout * time.Second,
UseTLS: false,
UseTCP: false,
}) })
if err != nil { if err != nil {
return err return err
@ -113,13 +132,21 @@ func initNameserver(n string) (Nameserver, error) {
ns.Type = DOHResolver ns.Type = DOHResolver
ns.Address = u.String() ns.Address = u.String()
} }
if u.Scheme == "tls" {
ns.Type = DOTResolver
if u.Port() == "" {
ns.Address = net.JoinHostPort(u.Hostname(), DefaultTLSPort)
} else {
ns.Address = net.JoinHostPort(u.Hostname(), u.Port())
}
}
if u.Scheme == "tcp" { if u.Scheme == "tcp" {
ns.Type = TCPResolver ns.Type = TCPResolver
if i := net.ParseIP(n); i != nil { if u.Port() == "" {
// if no port specified in nameserver, append defaults. ns.Address = net.JoinHostPort(u.Hostname(), DefaultTCPPort)
n = net.JoinHostPort(n, DefaultTLSPort) } else {
ns.Address = net.JoinHostPort(u.Hostname(), u.Port())
} }
ns.Address = u.String()
} }
if u.Scheme == "udp" { if u.Scheme == "udp" {
ns.Type = UDPResolver ns.Type = UDPResolver

View File

@ -6,33 +6,47 @@ import (
"github.com/miekg/dns" "github.com/miekg/dns"
) )
// UDPResolver represents the config options for setting up a Resolver. // ClassicResolver represents the config options for setting up a Resolver.
type UDPResolver struct { type ClassicResolver struct {
client *dns.Client client *dns.Client
server string server string
} }
// UDPResolverOpts holds options for setting up a Classic resolver. // ClassicResolverOpts holds options for setting up a Classic resolver.
type UDPResolverOpts struct { type ClassicResolverOpts struct {
IPv4Only bool IPv4Only bool
IPv6Only bool IPv6Only bool
Timeout time.Duration Timeout time.Duration
UseTLS bool
UseTCP bool
} }
// NewUDPResolver accepts a list of nameservers and configures a DNS resolver. // NewClassicResolver accepts a list of nameservers and configures a DNS resolver.
func NewUDPResolver(server string, opts UDPResolverOpts) (Resolver, error) { func NewClassicResolver(server string, opts ClassicResolverOpts) (Resolver, error) {
net := "udp"
client := &dns.Client{ client := &dns.Client{
Timeout: opts.Timeout, Timeout: opts.Timeout,
Net: "udp",
}
if opts.UseTCP {
net = "tcp"
} }
client.Net = "udp"
if opts.IPv4Only { if opts.IPv4Only {
client.Net = "udp4" net = net + "4"
} }
if opts.IPv6Only { if opts.IPv6Only {
client.Net = "udp6" net = net + "6"
} }
return &UDPResolver{
if opts.UseTLS {
net = net + "-tls"
}
client.Net = net
return &ClassicResolver{
client: client, client: client,
server: server, server: server,
}, nil }, nil
@ -41,7 +55,7 @@ func NewUDPResolver(server string, opts UDPResolverOpts) (Resolver, error) {
// Lookup prepare a list of DNS messages to be sent to the server. // Lookup prepare a list of DNS messages to be sent to the server.
// It's possible to send multiple question in one message // It's possible to send multiple question in one message
// but some nameservers are not able to // but some nameservers are not able to
func (r *UDPResolver) Lookup(questions []dns.Question) ([]Response, error) { func (r *ClassicResolver) Lookup(questions []dns.Question) ([]Response, error) {
var ( var (
messages = prepareMessages(questions) messages = prepareMessages(questions)
responses []Response responses []Response

View File

@ -1,64 +0,0 @@
package resolvers
import (
"time"
"github.com/miekg/dns"
)
// TCPResolver represents the config options for setting up a Resolver.
type TCPResolver struct {
client *dns.Client
server string
}
// TCPResolverOpts represents the config options for setting up a TCPResolver.
type TCPResolverOpts struct {
IPv4Only bool
IPv6Only bool
Timeout time.Duration
}
// NewTCPResolver accepts a list of nameservers and configures a DNS resolver.
func NewTCPResolver(server string, opts TCPResolverOpts) (Resolver, error) {
client := &dns.Client{
Timeout: opts.Timeout,
}
client.Net = "tcp"
if opts.IPv4Only {
client.Net = "tcp4"
}
if opts.IPv6Only {
client.Net = "tcp6"
}
return &TCPResolver{
client: client,
server: server,
}, nil
}
// 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
func (r *TCPResolver) Lookup(questions []dns.Question) ([]Response, error) {
var (
messages = prepareMessages(questions)
responses []Response
)
for _, msg := range messages {
in, rtt, err := r.client.Exchange(&msg, r.server)
if err != nil {
return nil, err
}
msg.Answer = in.Answer
rsp := Response{
Message: msg,
RTT: rtt,
Nameserver: r.server,
}
responses = append(responses, rsp)
}
return responses, nil
}