chore: Create a resolver package
parent
a57814688c
commit
b602beda0f
|
@ -67,6 +67,12 @@ func main() {
|
|||
Usage: "Network class of the DNS record to be queried (IN, CH, HS etc)",
|
||||
Destination: qFlags.QClasses,
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "https",
|
||||
Usage: "Use the DNS-over-HTTPS protocol",
|
||||
Destination: &qFlags.IsDOH,
|
||||
DefaultText: "udp",
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "verbose",
|
||||
Usage: "Enable verbose logging",
|
||||
|
|
|
@ -2,7 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"github.com/miekg/dns"
|
||||
"github.com/mr-karan/doggo/pkg/resolve"
|
||||
"github.com/mr-karan/doggo/pkg/resolvers"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
@ -13,7 +13,7 @@ type Hub struct {
|
|||
Version string
|
||||
QueryFlags QueryFlags
|
||||
Questions []dns.Question
|
||||
Resolver *resolve.Resolver
|
||||
Resolver resolvers.Resolver
|
||||
}
|
||||
|
||||
// QueryFlags is used store the value of CLI flags.
|
||||
|
@ -22,6 +22,10 @@ type QueryFlags struct {
|
|||
QTypes *cli.StringSlice
|
||||
QClasses *cli.StringSlice
|
||||
Nameservers *cli.StringSlice
|
||||
IsDOH bool
|
||||
IsDOT bool
|
||||
IsUDP bool
|
||||
IsTLS bool
|
||||
}
|
||||
|
||||
// NewHub initializes an instance of Hub which holds app wide configuration.
|
||||
|
|
21
cmd/parse.go
21
cmd/parse.go
|
@ -1,11 +1,9 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"github.com/mr-karan/doggo/pkg/resolve"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
|
@ -55,22 +53,3 @@ func (hub *Hub) loadFallbacks(c *cli.Context) {
|
|||
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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package resolve
|
||||
package resolvers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
@ -7,8 +7,8 @@ import (
|
|||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
// Resolver holds the configuration for a dns.Client
|
||||
type Resolver struct {
|
||||
// Manager represents the config options for setting up a Resolver.
|
||||
type Manager struct {
|
||||
client *dns.Client
|
||||
servers []string
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ type Resolver struct {
|
|||
const DefaultResolvConfPath = "/etc/resolv.conf"
|
||||
|
||||
// NewResolver accepts a list of nameservers and configures a DNS resolver.
|
||||
func NewResolver(servers []string) *Resolver {
|
||||
func NewResolver(servers []string) Resolver {
|
||||
client := &dns.Client{}
|
||||
var nameservers []string
|
||||
for _, srv := range servers {
|
||||
|
@ -27,7 +27,7 @@ func NewResolver(servers []string) *Resolver {
|
|||
nameservers = append(nameservers, dns.Fqdn(srv)+":"+"53")
|
||||
}
|
||||
}
|
||||
return &Resolver{
|
||||
return &Manager{
|
||||
client: client,
|
||||
servers: nameservers,
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ func NewResolver(servers []string) *Resolver {
|
|||
|
||||
// NewResolverFromResolvFile loads the configuration from resolv config file
|
||||
// and initialises a DNS resolver.
|
||||
func NewResolverFromResolvFile(resolvFilePath string) (*Resolver, error) {
|
||||
func NewResolverFromResolvFile(resolvFilePath string) (Resolver, error) {
|
||||
if resolvFilePath == "" {
|
||||
resolvFilePath = DefaultResolvConfPath
|
||||
}
|
||||
|
@ -56,7 +56,7 @@ func NewResolverFromResolvFile(resolvFilePath string) (*Resolver, error) {
|
|||
}
|
||||
|
||||
client := &dns.Client{}
|
||||
return &Resolver{
|
||||
return &Manager{
|
||||
client: client,
|
||||
servers: servers,
|
||||
}, nil
|
||||
|
@ -65,7 +65,7 @@ func NewResolverFromResolvFile(resolvFilePath string) (*Resolver, error) {
|
|||
// 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 *Resolver) Lookup(questions []dns.Question) error {
|
||||
func (m *Manager) Lookup(questions []dns.Question) error {
|
||||
var messages = make([]dns.Msg, 0, len(questions))
|
||||
for _, q := range questions {
|
||||
msg := dns.Msg{}
|
||||
|
@ -76,8 +76,8 @@ func (r *Resolver) Lookup(questions []dns.Question) error {
|
|||
messages = append(messages, msg)
|
||||
}
|
||||
for _, msg := range messages {
|
||||
for _, srv := range r.servers {
|
||||
in, rtt, err := r.client.Exchange(&msg, srv)
|
||||
for _, srv := range m.servers {
|
||||
in, rtt, err := m.client.Exchange(&msg, srv)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -91,3 +91,7 @@ func (r *Resolver) Lookup(questions []dns.Question) error {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Manager) Name() string {
|
||||
return "classic"
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
package resolvers
|
|
@ -0,0 +1 @@
|
|||
package resolvers
|
|
@ -0,0 +1,8 @@
|
|||
package resolvers
|
||||
|
||||
import "github.com/miekg/dns"
|
||||
|
||||
type Resolver interface {
|
||||
Name() string
|
||||
Lookup([]dns.Question) error
|
||||
}
|
Loading…
Reference in New Issue