chore: Create a resolver package

pull/2/head
Karan Sharma 2020-12-11 16:48:54 +05:30
parent a57814688c
commit b602beda0f
8 changed files with 63 additions and 33 deletions

View File

@ -67,6 +67,12 @@ func main() {
Usage: "Network class of the DNS record to be queried (IN, CH, HS etc)", Usage: "Network class of the DNS record to be queried (IN, CH, HS etc)",
Destination: qFlags.QClasses, Destination: qFlags.QClasses,
}, },
&cli.BoolFlag{
Name: "https",
Usage: "Use the DNS-over-HTTPS protocol",
Destination: &qFlags.IsDOH,
DefaultText: "udp",
},
&cli.BoolFlag{ &cli.BoolFlag{
Name: "verbose", Name: "verbose",
Usage: "Enable verbose logging", Usage: "Enable verbose logging",

View File

@ -2,7 +2,7 @@ package main
import ( import (
"github.com/miekg/dns" "github.com/miekg/dns"
"github.com/mr-karan/doggo/pkg/resolve" "github.com/mr-karan/doggo/pkg/resolvers"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
@ -13,7 +13,7 @@ type Hub struct {
Version string Version string
QueryFlags QueryFlags QueryFlags QueryFlags
Questions []dns.Question Questions []dns.Question
Resolver *resolve.Resolver Resolver resolvers.Resolver
} }
// QueryFlags is used store the value of CLI flags. // QueryFlags is used store the value of CLI flags.
@ -22,6 +22,10 @@ type QueryFlags struct {
QTypes *cli.StringSlice QTypes *cli.StringSlice
QClasses *cli.StringSlice QClasses *cli.StringSlice
Nameservers *cli.StringSlice Nameservers *cli.StringSlice
IsDOH bool
IsDOT bool
IsUDP bool
IsTLS bool
} }
// NewHub initializes an instance of Hub which holds app wide configuration. // NewHub initializes an instance of Hub which holds app wide configuration.

View File

@ -1,11 +1,9 @@
package main package main
import ( import (
"runtime"
"strings" "strings"
"github.com/miekg/dns" "github.com/miekg/dns"
"github.com/mr-karan/doggo/pkg/resolve"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
@ -55,22 +53,3 @@ func (hub *Hub) loadFallbacks(c *cli.Context) {
hub.QueryFlags.QClasses.Set("IN") hub.QueryFlags.QClasses.Set("IN")
} }
} }
// loadResolver checks
func (hub *Hub) loadResolver(c *cli.Context) error {
if len(hub.QueryFlags.Nameservers.Value()) == 0 {
if runtime.GOOS == "windows" {
// TODO: Add a method for reading system default nameserver in windows.
} else {
rslvr, err := resolve.NewResolverFromResolvFile(resolve.DefaultResolvConfPath)
if err != nil {
return err
}
hub.Resolver = rslvr
}
} else {
rslvr := resolve.NewResolver(hub.QueryFlags.Nameservers.Value())
hub.Resolver = rslvr
}
return nil
}

27
cmd/resolver.go 100644
View File

@ -0,0 +1,27 @@
package main
import (
"runtime"
"github.com/mr-karan/doggo/pkg/resolvers"
"github.com/urfave/cli/v2"
)
// loadResolver checks
func (hub *Hub) loadResolver(c *cli.Context) error {
if len(hub.QueryFlags.Nameservers.Value()) == 0 {
if runtime.GOOS == "windows" {
// TODO: Add a method for reading system default nameserver in windows.
} else {
rslvr, err := resolvers.NewResolverFromResolvFile(resolvers.DefaultResolvConfPath)
if err != nil {
return err
}
hub.Resolver = rslvr
}
} else {
rslvr := resolvers.NewResolver(hub.QueryFlags.Nameservers.Value())
hub.Resolver = rslvr
}
return nil
}

View File

@ -1,4 +1,4 @@
package resolve package resolvers
import ( import (
"fmt" "fmt"
@ -7,8 +7,8 @@ import (
"github.com/miekg/dns" "github.com/miekg/dns"
) )
// Resolver holds the configuration for a dns.Client // Manager represents the config options for setting up a Resolver.
type Resolver struct { type Manager struct {
client *dns.Client client *dns.Client
servers []string servers []string
} }
@ -17,7 +17,7 @@ type Resolver struct {
const DefaultResolvConfPath = "/etc/resolv.conf" const DefaultResolvConfPath = "/etc/resolv.conf"
// NewResolver accepts a list of nameservers and configures a DNS resolver. // NewResolver accepts a list of nameservers and configures a DNS resolver.
func NewResolver(servers []string) *Resolver { func NewResolver(servers []string) Resolver {
client := &dns.Client{} client := &dns.Client{}
var nameservers []string var nameservers []string
for _, srv := range servers { for _, srv := range servers {
@ -27,7 +27,7 @@ func NewResolver(servers []string) *Resolver {
nameservers = append(nameservers, dns.Fqdn(srv)+":"+"53") nameservers = append(nameservers, dns.Fqdn(srv)+":"+"53")
} }
} }
return &Resolver{ return &Manager{
client: client, client: client,
servers: nameservers, servers: nameservers,
} }
@ -35,7 +35,7 @@ func NewResolver(servers []string) *Resolver {
// NewResolverFromResolvFile loads the configuration from resolv config file // NewResolverFromResolvFile loads the configuration from resolv config file
// and initialises a DNS resolver. // and initialises a DNS resolver.
func NewResolverFromResolvFile(resolvFilePath string) (*Resolver, error) { func NewResolverFromResolvFile(resolvFilePath string) (Resolver, error) {
if resolvFilePath == "" { if resolvFilePath == "" {
resolvFilePath = DefaultResolvConfPath resolvFilePath = DefaultResolvConfPath
} }
@ -56,7 +56,7 @@ func NewResolverFromResolvFile(resolvFilePath string) (*Resolver, error) {
} }
client := &dns.Client{} client := &dns.Client{}
return &Resolver{ return &Manager{
client: client, client: client,
servers: servers, servers: servers,
}, nil }, nil
@ -65,7 +65,7 @@ func NewResolverFromResolvFile(resolvFilePath string) (*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 *Resolver) Lookup(questions []dns.Question) error { func (m *Manager) Lookup(questions []dns.Question) error {
var messages = make([]dns.Msg, 0, len(questions)) var messages = make([]dns.Msg, 0, len(questions))
for _, q := range questions { for _, q := range questions {
msg := dns.Msg{} msg := dns.Msg{}
@ -76,8 +76,8 @@ func (r *Resolver) Lookup(questions []dns.Question) error {
messages = append(messages, msg) messages = append(messages, msg)
} }
for _, msg := range messages { for _, msg := range messages {
for _, srv := range r.servers { for _, srv := range m.servers {
in, rtt, err := r.client.Exchange(&msg, srv) in, rtt, err := m.client.Exchange(&msg, srv)
if err != nil { if err != nil {
return err return err
} }
@ -91,3 +91,7 @@ func (r *Resolver) Lookup(questions []dns.Question) error {
} }
return nil return nil
} }
func (m *Manager) Name() string {
return "classic"
}

View File

@ -0,0 +1 @@
package resolvers

View File

@ -0,0 +1 @@
package resolvers

View File

@ -0,0 +1,8 @@
package resolvers
import "github.com/miekg/dns"
type Resolver interface {
Name() string
Lookup([]dns.Question) error
}