unfinished mess
This commit is contained in:
parent
6c3b17ba0d
commit
2e2e3b1ec8
26 changed files with 364 additions and 171 deletions
|
@ -1,55 +0,0 @@
|
|||
package app
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"github.com/mr-karan/doggo/pkg/models"
|
||||
"github.com/mr-karan/doggo/pkg/resolvers"
|
||||
"github.com/mr-karan/logf"
|
||||
)
|
||||
|
||||
// App represents the structure for all app wide configuration.
|
||||
type App struct {
|
||||
Logger *logf.Logger
|
||||
Version string
|
||||
QueryFlags models.QueryFlags
|
||||
Questions []dns.Question
|
||||
Resolvers []resolvers.Resolver
|
||||
ResolverOpts resolvers.Options
|
||||
Nameservers []models.Nameserver
|
||||
}
|
||||
|
||||
// NewApp initializes an instance of App which holds app wide configuration.
|
||||
func New(logger *logf.Logger, buildVersion string) App {
|
||||
app := App{
|
||||
Logger: logger,
|
||||
Version: buildVersion,
|
||||
QueryFlags: models.QueryFlags{
|
||||
QNames: []string{},
|
||||
QTypes: []string{},
|
||||
QClasses: []string{},
|
||||
Nameservers: []string{},
|
||||
},
|
||||
Nameservers: []models.Nameserver{},
|
||||
}
|
||||
return app
|
||||
}
|
||||
|
||||
// Attempts a DNS Lookup with retries for a given Question.
|
||||
func (app *App) LookupWithRetry(attempts int, resolver resolvers.Resolver, ques dns.Question) (resolvers.Response, error) {
|
||||
resp, err := resolver.Lookup(ques)
|
||||
if err != nil {
|
||||
// Retry lookup.
|
||||
attempts--
|
||||
if attempts > 0 {
|
||||
// Add some random delay.
|
||||
time.Sleep(time.Millisecond*300 + (time.Duration(rand.Int63n(int64(time.Millisecond*100))))/2)
|
||||
app.Logger.Debug("retrying lookup")
|
||||
return app.LookupWithRetry(attempts, resolver, ques)
|
||||
}
|
||||
return resolvers.Response{}, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
|
@ -1,54 +0,0 @@
|
|||
package app
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
// LoadFallbacks sets fallbacks for options
|
||||
// that are not specified by the user but necessary
|
||||
// for the resolver.
|
||||
func (app *App) LoadFallbacks() {
|
||||
if len(app.QueryFlags.QTypes) == 0 {
|
||||
app.QueryFlags.QTypes = append(app.QueryFlags.QTypes, "A")
|
||||
}
|
||||
if len(app.QueryFlags.QClasses) == 0 {
|
||||
app.QueryFlags.QClasses = append(app.QueryFlags.QClasses, "IN")
|
||||
}
|
||||
}
|
||||
|
||||
// PrepareQuestions takes a list of query names, query types and query classes
|
||||
// and prepare a question for each combination of the above.
|
||||
func (app *App) PrepareQuestions() {
|
||||
for _, n := range app.QueryFlags.QNames {
|
||||
for _, t := range app.QueryFlags.QTypes {
|
||||
for _, c := range app.QueryFlags.QClasses {
|
||||
app.Questions = append(app.Questions, dns.Question{
|
||||
Name: n,
|
||||
Qtype: dns.StringToType[strings.ToUpper(t)],
|
||||
Qclass: dns.StringToClass[strings.ToUpper(c)],
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ReverseLookup is used to perform a reverse DNS Lookup
|
||||
// using an IPv4 or IPv6 address.
|
||||
// Query Type is set to PTR, Query Class is set to IN.
|
||||
// Query Names must be formatted in in-addr.arpa. or ip6.arpa format.
|
||||
func (app *App) ReverseLookup() {
|
||||
app.QueryFlags.QTypes = []string{"PTR"}
|
||||
app.QueryFlags.QClasses = []string{"IN"}
|
||||
formattedNames := make([]string, 0, len(app.QueryFlags.QNames))
|
||||
|
||||
for _, n := range app.QueryFlags.QNames {
|
||||
addr, err := dns.ReverseAddr(n)
|
||||
if err != nil {
|
||||
app.Logger.WithError(err).Fatal("error formatting address")
|
||||
}
|
||||
formattedNames = append(formattedNames, addr)
|
||||
}
|
||||
app.QueryFlags.QNames = formattedNames
|
||||
}
|
53
internal/client/client.go
Normal file
53
internal/client/client.go
Normal file
|
@ -0,0 +1,53 @@
|
|||
package client
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"github.com/mr-karan/doggo/internal/models"
|
||||
"github.com/mr-karan/doggo/internal/resolvers"
|
||||
"github.com/mr-karan/logf"
|
||||
)
|
||||
|
||||
// Client represents the structure for all app wide configuration.
|
||||
type Client struct {
|
||||
Log *logf.Logger
|
||||
Version string
|
||||
QueryFlags models.QueryFlags
|
||||
Questions []dns.Question
|
||||
Resolvers []resolvers.Resolver
|
||||
Nameservers []models.Nameserver
|
||||
}
|
||||
|
||||
// New initializes an instance of App which holds app wide configuration.
|
||||
func New(logger *logf.Logger, buildVersion string) Client {
|
||||
return Client{
|
||||
Log: logger,
|
||||
Version: buildVersion,
|
||||
QueryFlags: models.QueryFlags{
|
||||
QNames: []string{},
|
||||
QTypes: []string{},
|
||||
QClasses: []string{},
|
||||
Nameservers: []string{},
|
||||
},
|
||||
Nameservers: []models.Nameserver{},
|
||||
}
|
||||
}
|
||||
|
||||
// LookupWithRetry attempts a DNS Lookup with retries for a given Question.
|
||||
func (hub *Client) LookupWithRetry(attempts int, resolver resolvers.Resolver, ques dns.Question) (resolvers.Response, error) {
|
||||
resp, err := resolver.Lookup(ques)
|
||||
if err != nil {
|
||||
// Retry lookup.
|
||||
attempts--
|
||||
if attempts > 0 {
|
||||
// Add some random delay.
|
||||
time.Sleep(time.Millisecond*300 + (time.Duration(rand.Int63n(int64(time.Millisecond*100))))/2)
|
||||
hub.Log.Debug("retrying lookup")
|
||||
return hub.LookupWithRetry(attempts, resolver, ques)
|
||||
}
|
||||
return resolvers.Response{}, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package app
|
||||
package client
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
@ -8,47 +8,47 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/ameshkov/dnsstamps"
|
||||
"github.com/mr-karan/doggo/pkg/config"
|
||||
"github.com/mr-karan/doggo/pkg/models"
|
||||
"github.com/mr-karan/doggo/internal/config"
|
||||
"github.com/mr-karan/doggo/internal/models"
|
||||
)
|
||||
|
||||
// LoadNameservers reads all the user given
|
||||
// nameservers and loads to App.
|
||||
func (app *App) LoadNameservers() error {
|
||||
for _, srv := range app.QueryFlags.Nameservers {
|
||||
// nameservers and loads to Client.
|
||||
func (hub *Client) LoadNameservers() error {
|
||||
for _, srv := range hub.QueryFlags.Nameservers {
|
||||
ns, err := initNameserver(srv)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error parsing nameserver: %s", srv)
|
||||
}
|
||||
// check if properly initialised.
|
||||
if ns.Address != "" && ns.Type != "" {
|
||||
app.Nameservers = append(app.Nameservers, ns)
|
||||
hub.Nameservers = append(hub.Nameservers, ns)
|
||||
}
|
||||
}
|
||||
|
||||
// Set `ndots` to the user specified value.
|
||||
app.ResolverOpts.Ndots = app.QueryFlags.Ndots
|
||||
hub.ResolverOpts.Ndots = hub.QueryFlags.Ndots
|
||||
// fallback to system nameserver
|
||||
// in case no nameserver is specified by user.
|
||||
if len(app.Nameservers) == 0 {
|
||||
ns, ndots, search, err := getDefaultServers(app.QueryFlags.Strategy)
|
||||
if len(hub.Nameservers) == 0 {
|
||||
ns, ndots, search, err := getDefaultServers(hub.QueryFlags.Strategy)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error fetching system default nameserver")
|
||||
}
|
||||
// `-1` indicates the flag is not set.
|
||||
// use from config if user hasn't specified any value.
|
||||
if app.ResolverOpts.Ndots == -1 {
|
||||
app.ResolverOpts.Ndots = ndots
|
||||
if hub.ResolverOpts.Ndots == -1 {
|
||||
hub.ResolverOpts.Ndots = ndots
|
||||
}
|
||||
if len(search) > 0 && app.QueryFlags.UseSearchList {
|
||||
app.ResolverOpts.SearchList = search
|
||||
if len(search) > 0 && hub.QueryFlags.UseSearchList {
|
||||
hub.ResolverOpts.SearchList = search
|
||||
}
|
||||
app.Nameservers = append(app.Nameservers, ns...)
|
||||
hub.Nameservers = append(hub.Nameservers, ns...)
|
||||
}
|
||||
// if the user hasn't given any override of `ndots` AND has
|
||||
// given a custom nameserver. Set `ndots` to 1 as the fallback value
|
||||
if app.ResolverOpts.Ndots == -1 {
|
||||
app.ResolverOpts.Ndots = 0
|
||||
if hub.ResolverOpts.Ndots == -1 {
|
||||
hub.ResolverOpts.Ndots = 0
|
||||
}
|
||||
return nil
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package app
|
||||
package client
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
@ -7,20 +7,20 @@ import (
|
|||
|
||||
"github.com/fatih/color"
|
||||
"github.com/miekg/dns"
|
||||
"github.com/mr-karan/doggo/pkg/resolvers"
|
||||
"github.com/mr-karan/doggo/internal/resolvers"
|
||||
"github.com/olekukonko/tablewriter"
|
||||
)
|
||||
|
||||
func (app *App) outputJSON(rsp []resolvers.Response) {
|
||||
func (hub *Client) outputJSON(rsp []resolvers.Response) {
|
||||
// Pretty print with 4 spaces.
|
||||
res, err := json.MarshalIndent(rsp, "", " ")
|
||||
if err != nil {
|
||||
app.Logger.WithError(err).Fatal("unable to output data in JSON")
|
||||
hub.Log.WithError(err).Fatal("unable to output data in JSON")
|
||||
}
|
||||
fmt.Printf("%s", res)
|
||||
}
|
||||
|
||||
func (app *App) outputShort(rsp []resolvers.Response) {
|
||||
func (hub *Client) outputShort(rsp []resolvers.Response) {
|
||||
for _, r := range rsp {
|
||||
for _, a := range r.Answers {
|
||||
fmt.Printf("%s\n", a.Address)
|
||||
|
@ -28,7 +28,7 @@ func (app *App) outputShort(rsp []resolvers.Response) {
|
|||
}
|
||||
}
|
||||
|
||||
func (app *App) outputTerminal(rsp []resolvers.Response) {
|
||||
func (hub *Client) outputTerminal(rsp []resolvers.Response) {
|
||||
var (
|
||||
green = color.New(color.FgGreen, color.Bold).SprintFunc()
|
||||
blue = color.New(color.FgBlue, color.Bold).SprintFunc()
|
||||
|
@ -39,14 +39,14 @@ func (app *App) outputTerminal(rsp []resolvers.Response) {
|
|||
)
|
||||
|
||||
// Disables colorized output if user specified.
|
||||
if !app.QueryFlags.Color {
|
||||
if !hub.QueryFlags.Color {
|
||||
color.NoColor = true
|
||||
}
|
||||
|
||||
// Conditional Time column.
|
||||
table := tablewriter.NewWriter(os.Stdout)
|
||||
header := []string{"Name", "Type", "Class", "TTL", "Address", "Nameserver"}
|
||||
if app.QueryFlags.DisplayTimeTaken {
|
||||
if hub.QueryFlags.DisplayTimeTaken {
|
||||
header = append(header, "Time Taken")
|
||||
}
|
||||
|
||||
|
@ -106,7 +106,7 @@ func (app *App) outputTerminal(rsp []resolvers.Response) {
|
|||
}
|
||||
output := []string{green(ans.Name), typOut, ans.Class, ans.TTL, ans.Address, ans.Nameserver}
|
||||
// Print how long it took
|
||||
if app.QueryFlags.DisplayTimeTaken {
|
||||
if hub.QueryFlags.DisplayTimeTaken {
|
||||
output = append(output, ans.RTT)
|
||||
}
|
||||
if outputStatus {
|
||||
|
@ -124,7 +124,7 @@ func (app *App) outputTerminal(rsp []resolvers.Response) {
|
|||
}
|
||||
output := []string{green(auth.Name), typOut, auth.Class, auth.TTL, auth.MName, auth.Nameserver}
|
||||
// Print how long it took
|
||||
if app.QueryFlags.DisplayTimeTaken {
|
||||
if hub.QueryFlags.DisplayTimeTaken {
|
||||
output = append(output, auth.RTT)
|
||||
}
|
||||
if outputStatus {
|
||||
|
@ -138,12 +138,12 @@ func (app *App) outputTerminal(rsp []resolvers.Response) {
|
|||
|
||||
// Output takes a list of `dns.Answers` and based
|
||||
// on the output format specified displays the information.
|
||||
func (app *App) Output(responses []resolvers.Response) {
|
||||
if app.QueryFlags.ShowJSON {
|
||||
app.outputJSON(responses)
|
||||
} else if app.QueryFlags.ShortOutput {
|
||||
app.outputShort(responses)
|
||||
func (hub *Client) Output(responses []resolvers.Response) {
|
||||
if hub.QueryFlags.ShowJSON {
|
||||
hub.outputJSON(responses)
|
||||
} else if hub.QueryFlags.ShortOutput {
|
||||
hub.outputShort(responses)
|
||||
} else {
|
||||
app.outputTerminal(responses)
|
||||
hub.outputTerminal(responses)
|
||||
}
|
||||
}
|
54
internal/client/questions.go
Normal file
54
internal/client/questions.go
Normal file
|
@ -0,0 +1,54 @@
|
|||
package client
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
// LoadFallbacks sets fallbacks for options
|
||||
// that are not specified by the user but necessary
|
||||
// for the resolver.
|
||||
func (hub *Client) LoadFallbacks() {
|
||||
if len(hub.QueryFlags.QTypes) == 0 {
|
||||
hub.QueryFlags.QTypes = append(hub.QueryFlags.QTypes, "A")
|
||||
}
|
||||
if len(hub.QueryFlags.QClasses) == 0 {
|
||||
hub.QueryFlags.QClasses = append(hub.QueryFlags.QClasses, "IN")
|
||||
}
|
||||
}
|
||||
|
||||
// PrepareQuestions takes a list of query names, query types and query classes
|
||||
// and prepare a question for each combination of the above.
|
||||
func (hub *Client) PrepareQuestions() {
|
||||
for _, n := range hub.QueryFlags.QNames {
|
||||
for _, t := range hub.QueryFlags.QTypes {
|
||||
for _, c := range hub.QueryFlags.QClasses {
|
||||
hub.Questions = append(hub.Questions, dns.Question{
|
||||
Name: n,
|
||||
Qtype: dns.StringToType[strings.ToUpper(t)],
|
||||
Qclass: dns.StringToClass[strings.ToUpper(c)],
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ReverseLookup is used to perform a reverse DNS Lookup
|
||||
// using an IPv4 or IPv6 address.
|
||||
// Query Type is set to PTR, Query Class is set to IN.
|
||||
// Query Names must be formatted in in-addr.arpa. or ip6.arpa format.
|
||||
func (hub *Client) ReverseLookup() {
|
||||
hub.QueryFlags.QTypes = []string{"PTR"}
|
||||
hub.QueryFlags.QClasses = []string{"IN"}
|
||||
formattedNames := make([]string, 0, len(hub.QueryFlags.QNames))
|
||||
|
||||
for _, n := range hub.QueryFlags.QNames {
|
||||
addr, err := dns.ReverseAddr(n)
|
||||
if err != nil {
|
||||
hub.Log.WithError(err).Fatal("error formatting address")
|
||||
}
|
||||
formattedNames = append(formattedNames, addr)
|
||||
}
|
||||
hub.QueryFlags.QNames = formattedNames
|
||||
}
|
9
internal/config/config.go
Normal file
9
internal/config/config.go
Normal file
|
@ -0,0 +1,9 @@
|
|||
package config
|
||||
|
||||
import "net"
|
||||
|
||||
// the whole `FEC0::/10` prefix is deprecated.
|
||||
// [RFC 3879]: https://tools.ietf.org/html/rfc3879
|
||||
func isUnicastLinkLocal(ip net.IP) bool {
|
||||
return len(ip) == net.IPv6len && ip[0] == 0xfe && ip[1] == 0xc0
|
||||
}
|
30
internal/config/config_unix.go
Normal file
30
internal/config/config_unix.go
Normal file
|
@ -0,0 +1,30 @@
|
|||
// +build !windows
|
||||
|
||||
package config
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
// DefaultResolvConfPath specifies path to default resolv config file on UNIX.
|
||||
const DefaultResolvConfPath = "/etc/resolv.conf"
|
||||
|
||||
// GetDefaultServers get system default nameserver
|
||||
func GetDefaultServers() ([]string, int, []string, error) {
|
||||
// if no nameserver is provided, take it from `resolv.conf`
|
||||
cfg, err := dns.ClientConfigFromFile(DefaultResolvConfPath)
|
||||
if err != nil {
|
||||
return nil, 0, nil, err
|
||||
}
|
||||
servers := make([]string, 0)
|
||||
for _, server := range cfg.Servers {
|
||||
ip := net.ParseIP(server)
|
||||
if isUnicastLinkLocal(ip) {
|
||||
continue
|
||||
}
|
||||
servers = append(servers, server)
|
||||
}
|
||||
return servers, cfg.Ndots, cfg.Search, nil
|
||||
}
|
120
internal/config/config_windows.go
Normal file
120
internal/config/config_windows.go
Normal file
|
@ -0,0 +1,120 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
// GAA_FLAG_INCLUDE_GATEWAYS Return the addresses of default gateways.
|
||||
// This flag is supported on Windows Vista and later.
|
||||
const GAA_FLAG_INCLUDE_GATEWAYS = 0x00000080
|
||||
|
||||
// IpAdapterWinsServerAddress structure in a linked list of Windows Internet Name Service (WINS) server addresses for the adapter.
|
||||
type IpAdapterWinsServerAddress struct {
|
||||
Length uint32
|
||||
_ uint32
|
||||
Next *IpAdapterWinsServerAddress
|
||||
Address windows.SocketAddress
|
||||
}
|
||||
|
||||
// IpAdapterGatewayAddress structure in a linked list of gateways for the adapter.
|
||||
type IpAdapterGatewayAddress struct {
|
||||
Length uint32
|
||||
_ uint32
|
||||
Next *IpAdapterGatewayAddress
|
||||
Address windows.SocketAddress
|
||||
}
|
||||
|
||||
// IpAdapterAddresses structure is the header node for a linked list of addresses for a particular adapter.
|
||||
// This structure can simultaneously be used as part of a linked list of IP_ADAPTER_ADDRESSES structures.
|
||||
type IpAdapterAddresses struct {
|
||||
Length uint32
|
||||
IfIndex uint32
|
||||
Next *IpAdapterAddresses
|
||||
AdapterName *byte
|
||||
FirstUnicastAddress *windows.IpAdapterUnicastAddress
|
||||
FirstAnycastAddress *windows.IpAdapterAnycastAddress
|
||||
FirstMulticastAddress *windows.IpAdapterMulticastAddress
|
||||
FirstDnsServerAddress *windows.IpAdapterDnsServerAdapter
|
||||
DnsSuffix *uint16
|
||||
Description *uint16
|
||||
FriendlyName *uint16
|
||||
PhysicalAddress [syscall.MAX_ADAPTER_ADDRESS_LENGTH]byte
|
||||
PhysicalAddressLength uint32
|
||||
Flags uint32
|
||||
Mtu uint32
|
||||
IfType uint32
|
||||
OperStatus uint32
|
||||
Ipv6IfIndex uint32
|
||||
ZoneIndices [16]uint32
|
||||
FirstPrefix *windows.IpAdapterPrefix
|
||||
/* more fields might be present here. */
|
||||
TransmitLinkSpeed uint64
|
||||
ReceiveLinkSpeed uint64
|
||||
FirstWinsServerAddress *IpAdapterWinsServerAddress
|
||||
FirstGatewayAddress *IpAdapterGatewayAddress
|
||||
}
|
||||
|
||||
func adapterAddresses() ([]*IpAdapterAddresses, error) {
|
||||
var b []byte
|
||||
// https://docs.microsoft.com/en-us/windows/win32/api/iphlpapi/nf-iphlpapi-getadaptersaddresses
|
||||
// #define WORKING_BUFFER_SIZE 15000
|
||||
l := uint32(15000)
|
||||
for {
|
||||
b = make([]byte, l)
|
||||
err := windows.GetAdaptersAddresses(syscall.AF_UNSPEC, GAA_FLAG_INCLUDE_GATEWAYS|windows.GAA_FLAG_INCLUDE_PREFIX, 0, (*windows.IpAdapterAddresses)(unsafe.Pointer(&b[0])), &l)
|
||||
if err == nil {
|
||||
if l == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
break
|
||||
}
|
||||
if err.(syscall.Errno) != syscall.ERROR_BUFFER_OVERFLOW {
|
||||
return nil, os.NewSyscallError("getadaptersaddresses", err)
|
||||
}
|
||||
if l <= uint32(len(b)) {
|
||||
return nil, os.NewSyscallError("getadaptersaddresses", err)
|
||||
}
|
||||
}
|
||||
aas := make([]*IpAdapterAddresses, 0, uintptr(l)/unsafe.Sizeof(IpAdapterAddresses{}))
|
||||
for aa := (*IpAdapterAddresses)(unsafe.Pointer(&b[0])); aa != nil; aa = aa.Next {
|
||||
aas = append(aas, aa)
|
||||
}
|
||||
return aas, nil
|
||||
}
|
||||
|
||||
func getDefaultDNSServers() ([]string, error) {
|
||||
ifs, err := adapterAddresses()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dnsServers := make([]string, 0)
|
||||
for _, ifi := range ifs {
|
||||
if ifi.OperStatus != windows.IfOperStatusUp {
|
||||
continue
|
||||
}
|
||||
|
||||
if ifi.FirstGatewayAddress == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
for dnsServer := ifi.FirstDnsServerAddress; dnsServer != nil; dnsServer = dnsServer.Next {
|
||||
ip := dnsServer.Address.IP()
|
||||
if isUnicastLinkLocal(ip) {
|
||||
continue
|
||||
}
|
||||
dnsServers = append(dnsServers, ip.String())
|
||||
}
|
||||
}
|
||||
return dnsServers, nil
|
||||
}
|
||||
|
||||
// GetDefaultServers get system default nameserver
|
||||
func GetDefaultServers() ([]string, int, []string, error) {
|
||||
// TODO: DNS Suffix
|
||||
servers, err := getDefaultDNSServers()
|
||||
return servers, 0, nil, err
|
||||
}
|
51
internal/models/models.go
Normal file
51
internal/models/models.go
Normal file
|
@ -0,0 +1,51 @@
|
|||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
const (
|
||||
// DefaultTLSPort specifies the default port for a DNS server connecting over TCP over TLS.
|
||||
DefaultTLSPort = "853"
|
||||
// DefaultUDPPort specifies the default port for a DNS server connecting over UDP.
|
||||
DefaultUDPPort = "53"
|
||||
// DefaultTCPPort specifies the default port for a DNS server connecting over TCP.
|
||||
DefaultTCPPort = "53"
|
||||
// DefaultDOQPort specifies the default port for a DNS server connecting over DNS over QUIC.
|
||||
DefaultDOQPort = "853"
|
||||
UDPResolver = "udp"
|
||||
DOHResolver = "doh"
|
||||
TCPResolver = "tcp"
|
||||
DOTResolver = "dot"
|
||||
DNSCryptResolver = "dnscrypt"
|
||||
DOQResolver = "doq"
|
||||
)
|
||||
|
||||
// QueryFlags is used store the query params
|
||||
// supplied by the user.
|
||||
type QueryFlags struct {
|
||||
QNames []string `koanf:"query" json:"query"`
|
||||
QTypes []string `koanf:"type" json:"type"`
|
||||
QClasses []string `koanf:"class" json:"class"`
|
||||
Nameservers []string `koanf:"nameservers" json:"nameservers"`
|
||||
UseIPv4 bool `koanf:"ipv4" json:"ipv4"`
|
||||
UseIPv6 bool `koanf:"ipv6" json:"ipv6"`
|
||||
Ndots int `koanf:"ndots" json:"ndots"`
|
||||
Timeout time.Duration `koanf:"timeout" json:"timeout"`
|
||||
Color bool `koanf:"color" json:"-"`
|
||||
DisplayTimeTaken bool `koanf:"time" json:"-"`
|
||||
ShowJSON bool `koanf:"json" json:"-"`
|
||||
ShortOutput bool `koanf:"short" short:"-"`
|
||||
UseSearchList bool `koanf:"search" json:"-"`
|
||||
ReverseLookup bool `koanf:"reverse" reverse:"-"`
|
||||
Strategy string `koanf:"strategy" strategy:"-"`
|
||||
InsecureSkipVerify bool `koanf:"skip-hostname-verification" skip-hostname-verification:"-"`
|
||||
TLSHostname string `koanf:"tls-hostname" tls-hostname:"-"`
|
||||
RetryCount int `koanf:"retry" retry:"-"`
|
||||
Tweaks []string `koanf:"tweaks" json:"-"`
|
||||
}
|
||||
|
||||
// Nameserver represents the type of Nameserver
|
||||
// along with the server address.
|
||||
type Nameserver struct {
|
||||
Address string
|
||||
Type string
|
||||
}
|
124
internal/resolvers/classic.go
Normal file
124
internal/resolvers/classic.go
Normal file
|
@ -0,0 +1,124 @@
|
|||
package resolvers
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"time"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"github.com/mr-karan/logf"
|
||||
)
|
||||
|
||||
// ClassicResolver represents the config options for setting up a Resolver.
|
||||
type ClassicResolver struct {
|
||||
client *dns.Client
|
||||
server string
|
||||
resolverOptions Options
|
||||
}
|
||||
|
||||
// ClassicResolverOpts holds options for setting up a Classic resolver.
|
||||
type ClassicResolverOpts struct {
|
||||
UseTLS bool
|
||||
UseTCP bool
|
||||
}
|
||||
|
||||
// NewClassicResolver accepts a list of nameservers and configures a DNS resolver.
|
||||
func NewClassicResolver(server string, classicOpts ClassicResolverOpts, resolverOpts Options) (Resolver, error) {
|
||||
net := "udp"
|
||||
client := &dns.Client{
|
||||
Timeout: resolverOpts.Timeout,
|
||||
Net: "udp",
|
||||
}
|
||||
|
||||
if classicOpts.UseTCP {
|
||||
net = "tcp"
|
||||
}
|
||||
|
||||
if resolverOpts.UseIPv4 {
|
||||
net = net + "4"
|
||||
}
|
||||
if resolverOpts.UseIPv6 {
|
||||
net = net + "6"
|
||||
}
|
||||
|
||||
if classicOpts.UseTLS {
|
||||
net = net + "-tls"
|
||||
// Provide extra TLS config for doing/skipping hostname verification.
|
||||
client.TLSConfig = &tls.Config{
|
||||
ServerName: resolverOpts.TLSHostname,
|
||||
InsecureSkipVerify: resolverOpts.InsecureSkipVerify,
|
||||
}
|
||||
}
|
||||
|
||||
client.Net = net
|
||||
|
||||
return &ClassicResolver{
|
||||
client: client,
|
||||
server: server,
|
||||
resolverOptions: resolverOpts,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Lookup takes a dns.Question and sends them to DNS Server.
|
||||
// It parses the Response from the server in a custom output format.
|
||||
func (r *ClassicResolver) Lookup(question dns.Question) (Response, error) {
|
||||
var (
|
||||
rsp Response
|
||||
messages = prepareMessages(question, r.resolverOptions)
|
||||
)
|
||||
for _, msg := range messages {
|
||||
r.resolverOptions.Logger.WithFields(logf.Fields{
|
||||
"domain": msg.Question[0].Name,
|
||||
"ndots": r.resolverOptions.Ndots,
|
||||
"nameserver": r.server,
|
||||
}).Debug("attempting to resolve")
|
||||
|
||||
r.resolverOptions.Logger.Debug("abc")
|
||||
|
||||
// Since the library doesn't include tcp.Dial time,
|
||||
// it's better to not rely on `rtt` provided here and calculate it ourselves.
|
||||
now := time.Now()
|
||||
in, _, err := r.client.Exchange(&msg, r.server)
|
||||
if err != nil {
|
||||
return rsp, err
|
||||
}
|
||||
|
||||
// In case the response size exceeds 512 bytes (can happen with lot of TXT records),
|
||||
// fallback to TCP as with UDP the response is truncated. Fallback mechanism is in-line with `dig`.
|
||||
if in.Truncated {
|
||||
switch r.client.Net {
|
||||
case "udp":
|
||||
r.client.Net = "tcp"
|
||||
case "udp4":
|
||||
r.client.Net = "tcp4"
|
||||
case "udp6":
|
||||
r.client.Net = "tcp6"
|
||||
default:
|
||||
r.client.Net = "tcp"
|
||||
}
|
||||
r.resolverOptions.Logger.WithFields(logf.Fields{"protocol": r.client.Net}).Debug("Response truncated; retrying now")
|
||||
return r.Lookup(question)
|
||||
}
|
||||
|
||||
// Pack questions in output.
|
||||
for _, q := range msg.Question {
|
||||
ques := Question{
|
||||
Name: q.Name,
|
||||
Class: dns.ClassToString[q.Qclass],
|
||||
Type: dns.TypeToString[q.Qtype],
|
||||
}
|
||||
rsp.Questions = append(rsp.Questions, ques)
|
||||
}
|
||||
rtt := time.Since(now)
|
||||
|
||||
// Get the authorities and answers.
|
||||
output := parseMessage(in, rtt, r.server)
|
||||
rsp.Authorities = output.Authorities
|
||||
rsp.Answers = output.Answers
|
||||
|
||||
if len(output.Answers) > 0 {
|
||||
// Stop iterating the searchlist.
|
||||
break
|
||||
}
|
||||
}
|
||||
return rsp, nil
|
||||
}
|
83
internal/resolvers/dnscrypt.go
Normal file
83
internal/resolvers/dnscrypt.go
Normal file
|
@ -0,0 +1,83 @@
|
|||
package resolvers
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/ameshkov/dnscrypt/v2"
|
||||
"github.com/miekg/dns"
|
||||
"github.com/mr-karan/logf"
|
||||
)
|
||||
|
||||
// DNSCryptResolver represents the config options for setting up a Resolver.
|
||||
type DNSCryptResolver struct {
|
||||
client *dnscrypt.Client
|
||||
server string
|
||||
resolverInfo *dnscrypt.ResolverInfo
|
||||
resolverOptions Options
|
||||
}
|
||||
|
||||
// DNSCryptResolverOpts holds options for setting up a DNSCrypt resolver.
|
||||
type DNSCryptResolverOpts struct {
|
||||
UseTCP bool
|
||||
}
|
||||
|
||||
// NewDNSCryptResolver accepts a list of nameservers and configures a DNS resolver.
|
||||
func NewDNSCryptResolver(server string, dnscryptOpts DNSCryptResolverOpts, resolverOpts Options) (Resolver, error) {
|
||||
net := "udp"
|
||||
if dnscryptOpts.UseTCP {
|
||||
net = "tcp"
|
||||
}
|
||||
|
||||
client := &dnscrypt.Client{Net: net, Timeout: resolverOpts.Timeout, UDPSize: 4096}
|
||||
resolverInfo, err := client.Dial(server)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &DNSCryptResolver{
|
||||
client: client,
|
||||
resolverInfo: resolverInfo,
|
||||
server: resolverInfo.ServerAddress,
|
||||
resolverOptions: resolverOpts,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Lookup takes a dns.Question and sends them to DNS Server.
|
||||
// It parses the Response from the server in a custom output format.
|
||||
func (r *DNSCryptResolver) Lookup(question dns.Question) (Response, error) {
|
||||
var (
|
||||
rsp Response
|
||||
messages = prepareMessages(question, r.resolverOptions)
|
||||
)
|
||||
for _, msg := range messages {
|
||||
r.resolverOptions.Logger.WithFields(logf.Fields{
|
||||
"domain": msg.Question[0].Name,
|
||||
"ndots": r.resolverOptions.Ndots,
|
||||
"nameserver": r.server,
|
||||
}).Debug("attempting to resolve")
|
||||
now := time.Now()
|
||||
in, err := r.client.Exchange(&msg, r.resolverInfo)
|
||||
if err != nil {
|
||||
return rsp, err
|
||||
}
|
||||
rtt := time.Since(now)
|
||||
// pack questions in output.
|
||||
for _, q := range msg.Question {
|
||||
ques := Question{
|
||||
Name: q.Name,
|
||||
Class: dns.ClassToString[q.Qclass],
|
||||
Type: dns.TypeToString[q.Qtype],
|
||||
}
|
||||
rsp.Questions = append(rsp.Questions, ques)
|
||||
}
|
||||
// get the authorities and answers.
|
||||
output := parseMessage(in, rtt, r.server)
|
||||
rsp.Authorities = output.Authorities
|
||||
rsp.Answers = output.Answers
|
||||
|
||||
if len(output.Answers) > 0 {
|
||||
// stop iterating the searchlist.
|
||||
break
|
||||
}
|
||||
}
|
||||
return rsp, nil
|
||||
}
|
121
internal/resolvers/doh.go
Normal file
121
internal/resolvers/doh.go
Normal file
|
@ -0,0 +1,121 @@
|
|||
package resolvers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"github.com/mr-karan/logf"
|
||||
)
|
||||
|
||||
// DOHResolver represents the config options for setting up a DOH based resolver.
|
||||
type DOHResolver struct {
|
||||
client *http.Client
|
||||
server string
|
||||
resolverOptions Options
|
||||
}
|
||||
|
||||
// NewDOHResolver accepts a nameserver address and configures a DOH based resolver.
|
||||
func NewDOHResolver(server string, resolverOpts Options) (Resolver, error) {
|
||||
// do basic validation
|
||||
u, err := url.ParseRequestURI(server)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s is not a valid HTTPS nameserver", server)
|
||||
}
|
||||
if u.Scheme != "https" {
|
||||
return nil, fmt.Errorf("missing https in %s", server)
|
||||
}
|
||||
httpClient := &http.Client{
|
||||
Timeout: resolverOpts.Timeout,
|
||||
}
|
||||
return &DOHResolver{
|
||||
client: httpClient,
|
||||
server: server,
|
||||
resolverOptions: resolverOpts,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Lookup takes a dns.Question and sends them to DNS Server.
|
||||
// It parses the Response from the server in a custom output format.
|
||||
func (r *DOHResolver) Lookup(question dns.Question) (Response, error) {
|
||||
var (
|
||||
rsp Response
|
||||
messages = prepareMessages(question, r.resolverOptions)
|
||||
)
|
||||
|
||||
for _, msg := range messages {
|
||||
r.resolverOptions.Logger.WithFields(logf.Fields{
|
||||
"domain": msg.Question[0].Name,
|
||||
"ndots": r.resolverOptions.Ndots,
|
||||
"nameserver": r.server,
|
||||
}).Debug("attempting to resolve")
|
||||
// get the DNS Message in wire format.
|
||||
b, err := msg.Pack()
|
||||
if err != nil {
|
||||
return rsp, err
|
||||
}
|
||||
now := time.Now()
|
||||
// Make an HTTP POST request to the DNS server with the DNS message as wire format bytes in the body.
|
||||
resp, err := r.client.Post(r.server, "application/dns-message", bytes.NewBuffer(b))
|
||||
if err != nil {
|
||||
return rsp, err
|
||||
}
|
||||
if resp.StatusCode == http.StatusMethodNotAllowed {
|
||||
url, err := url.Parse(r.server)
|
||||
if err != nil {
|
||||
return rsp, err
|
||||
}
|
||||
url.RawQuery = fmt.Sprintf("dns=%v", base64.RawURLEncoding.EncodeToString(b))
|
||||
resp, err = r.client.Get(url.String())
|
||||
if err != nil {
|
||||
return rsp, err
|
||||
}
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return rsp, fmt.Errorf("error from nameserver %s", resp.Status)
|
||||
}
|
||||
rtt := time.Since(now)
|
||||
|
||||
// Log the response headers in debug mode.
|
||||
for header, value := range resp.Header {
|
||||
r.resolverOptions.Logger.WithFields(logf.Fields{
|
||||
header: value,
|
||||
}).Debug("DOH response header")
|
||||
}
|
||||
|
||||
// Extract the binary response in DNS Message.
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return rsp, err
|
||||
}
|
||||
|
||||
err = msg.Unpack(body)
|
||||
if err != nil {
|
||||
return rsp, err
|
||||
}
|
||||
// Pack questions in output.
|
||||
for _, q := range msg.Question {
|
||||
ques := Question{
|
||||
Name: q.Name,
|
||||
Class: dns.ClassToString[q.Qclass],
|
||||
Type: dns.TypeToString[q.Qtype],
|
||||
}
|
||||
rsp.Questions = append(rsp.Questions, ques)
|
||||
}
|
||||
// Get the authorities and answers.
|
||||
output := parseMessage(&msg, rtt, r.server)
|
||||
rsp.Authorities = output.Authorities
|
||||
rsp.Answers = output.Answers
|
||||
|
||||
if len(output.Answers) > 0 {
|
||||
// Stop iterating the searchlist.
|
||||
break
|
||||
}
|
||||
}
|
||||
return rsp, nil
|
||||
}
|
130
internal/resolvers/doq.go
Normal file
130
internal/resolvers/doq.go
Normal file
|
@ -0,0 +1,130 @@
|
|||
package resolvers
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/lucas-clemente/quic-go"
|
||||
"github.com/miekg/dns"
|
||||
"github.com/mr-karan/logf"
|
||||
)
|
||||
|
||||
// DOQResolver represents the config options for setting up a DOQ based resolver.
|
||||
type DOQResolver struct {
|
||||
tls *tls.Config
|
||||
server string
|
||||
resolverOptions Options
|
||||
}
|
||||
|
||||
// NewDOQResolver accepts a nameserver address and configures a DOQ based resolver.
|
||||
func NewDOQResolver(server string, resolverOpts Options) (Resolver, error) {
|
||||
return &DOQResolver{
|
||||
tls: &tls.Config{
|
||||
NextProtos: []string{"doq"},
|
||||
},
|
||||
server: server,
|
||||
resolverOptions: resolverOpts,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Lookup takes a dns.Question and sends them to DNS Server.
|
||||
// It parses the Response from the server in a custom output format.
|
||||
func (r *DOQResolver) Lookup(question dns.Question) (Response, error) {
|
||||
var (
|
||||
rsp Response
|
||||
messages = prepareMessages(question, r.resolverOptions)
|
||||
)
|
||||
|
||||
session, err := quic.DialAddr(r.server, r.tls, nil)
|
||||
if err != nil {
|
||||
return rsp, err
|
||||
}
|
||||
defer session.CloseWithError(quic.ApplicationErrorCode(quic.NoError), "")
|
||||
|
||||
for _, msg := range messages {
|
||||
r.resolverOptions.Logger.WithFields(logf.Fields{
|
||||
"domain": msg.Question[0].Name,
|
||||
"ndots": r.resolverOptions.Ndots,
|
||||
"nameserver": r.server,
|
||||
}).Debug("attempting to resolve")
|
||||
|
||||
// ref: https://www.rfc-editor.org/rfc/rfc9250.html#name-dns-message-ids
|
||||
msg.Id = 0
|
||||
|
||||
// get the DNS Message in wire format.
|
||||
var b []byte
|
||||
b, err = msg.Pack()
|
||||
if err != nil {
|
||||
return rsp, err
|
||||
}
|
||||
now := time.Now()
|
||||
|
||||
var stream quic.Stream
|
||||
stream, err = session.OpenStream()
|
||||
if err != nil {
|
||||
return rsp, err
|
||||
}
|
||||
|
||||
var msgLen = uint16(len(b))
|
||||
var msgLenBytes = []byte{byte(msgLen >> 8), byte(msgLen & 0xFF)}
|
||||
_, err = stream.Write(msgLenBytes)
|
||||
if err != nil {
|
||||
return rsp, err
|
||||
}
|
||||
// Make a QUIC request to the DNS server with the DNS message as wire format bytes in the body.
|
||||
_, err = stream.Write(b)
|
||||
if err != nil {
|
||||
return rsp, err
|
||||
}
|
||||
|
||||
err = stream.SetDeadline(time.Now().Add(r.resolverOptions.Timeout))
|
||||
if err != nil {
|
||||
return rsp, err
|
||||
}
|
||||
|
||||
var buf []byte
|
||||
buf, err = io.ReadAll(stream)
|
||||
if err != nil {
|
||||
if errors.Is(err, os.ErrDeadlineExceeded) {
|
||||
return rsp, fmt.Errorf("timeout")
|
||||
}
|
||||
return rsp, err
|
||||
}
|
||||
rtt := time.Since(now)
|
||||
|
||||
_ = stream.Close()
|
||||
|
||||
packetLen := binary.BigEndian.Uint16(buf[:2])
|
||||
if packetLen != uint16(len(buf[2:])) {
|
||||
return rsp, fmt.Errorf("packet length mismatch")
|
||||
}
|
||||
err = msg.Unpack(buf[2:])
|
||||
if err != nil {
|
||||
return rsp, err
|
||||
}
|
||||
// pack questions in output.
|
||||
for _, q := range msg.Question {
|
||||
ques := Question{
|
||||
Name: q.Name,
|
||||
Class: dns.ClassToString[q.Qclass],
|
||||
Type: dns.TypeToString[q.Qtype],
|
||||
}
|
||||
rsp.Questions = append(rsp.Questions, ques)
|
||||
}
|
||||
// get the authorities and answers.
|
||||
output := parseMessage(&msg, rtt, r.server)
|
||||
rsp.Authorities = output.Authorities
|
||||
rsp.Answers = output.Answers
|
||||
|
||||
if len(output.Answers) > 0 {
|
||||
// stop iterating the searchlist.
|
||||
break
|
||||
}
|
||||
}
|
||||
return rsp, nil
|
||||
}
|
150
internal/resolvers/resolver.go
Normal file
150
internal/resolvers/resolver.go
Normal file
|
@ -0,0 +1,150 @@
|
|||
package resolvers
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"github.com/mr-karan/doggo/pkg/models"
|
||||
"github.com/mr-karan/logf"
|
||||
)
|
||||
|
||||
// Options represent a set of common options
|
||||
// to configure a Resolver.
|
||||
type Options struct {
|
||||
Logger *logf.Logger
|
||||
|
||||
Nameservers []models.Nameserver
|
||||
UseIPv4 bool
|
||||
UseIPv6 bool
|
||||
SearchList []string
|
||||
Ndots int
|
||||
Timeout time.Duration
|
||||
Strategy string
|
||||
InsecureSkipVerify bool
|
||||
TLSHostname string
|
||||
|
||||
// DNS Protocol Flags.
|
||||
Authoritative bool
|
||||
AuthenticatedData bool
|
||||
CheckingDisabled bool
|
||||
RecursionDesired bool
|
||||
}
|
||||
|
||||
// Resolver implements the configuration for a DNS
|
||||
// Client. Different types of providers can load
|
||||
// a DNS Resolver satisfying this interface.
|
||||
type Resolver interface {
|
||||
Lookup(dns.Question) (Response, error)
|
||||
}
|
||||
|
||||
// Response represents a custom output format
|
||||
// for DNS queries. It wraps metadata about the DNS query
|
||||
// and the DNS Answer as well.
|
||||
type Response struct {
|
||||
Answers []Answer `json:"answers"`
|
||||
Authorities []Authority `json:"authorities"`
|
||||
Questions []Question `json:"questions"`
|
||||
}
|
||||
|
||||
type Question struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Class string `json:"class"`
|
||||
}
|
||||
|
||||
type Answer struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Class string `json:"class"`
|
||||
TTL string `json:"ttl"`
|
||||
Address string `json:"address"`
|
||||
Status string `json:"status"`
|
||||
RTT string `json:"rtt"`
|
||||
Nameserver string `json:"nameserver"`
|
||||
}
|
||||
|
||||
type Authority struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Class string `json:"class"`
|
||||
TTL string `json:"ttl"`
|
||||
MName string `json:"mname"`
|
||||
Status string `json:"status"`
|
||||
RTT string `json:"rtt"`
|
||||
Nameserver string `json:"nameserver"`
|
||||
}
|
||||
|
||||
// LoadResolvers loads differently configured
|
||||
// resolvers based on a list of nameserver.
|
||||
func LoadResolvers(opts Options) ([]Resolver, error) {
|
||||
// For each nameserver, initialise the correct resolver.
|
||||
rslvrs := make([]Resolver, 0, len(opts.Nameservers))
|
||||
|
||||
for _, ns := range opts.Nameservers {
|
||||
if ns.Type == models.DOHResolver {
|
||||
opts.Logger.Debug("initiating DOH resolver")
|
||||
rslvr, err := NewDOHResolver(ns.Address, opts)
|
||||
if err != nil {
|
||||
return rslvrs, err
|
||||
}
|
||||
rslvrs = append(rslvrs, rslvr)
|
||||
}
|
||||
if ns.Type == models.DOTResolver {
|
||||
opts.Logger.Debug("initiating DOT resolver")
|
||||
rslvr, err := NewClassicResolver(ns.Address,
|
||||
ClassicResolverOpts{
|
||||
UseTLS: true,
|
||||
UseTCP: true,
|
||||
}, opts)
|
||||
|
||||
if err != nil {
|
||||
return rslvrs, err
|
||||
}
|
||||
rslvrs = append(rslvrs, rslvr)
|
||||
}
|
||||
if ns.Type == models.TCPResolver {
|
||||
opts.Logger.Debug("initiating TCP resolver")
|
||||
rslvr, err := NewClassicResolver(ns.Address,
|
||||
ClassicResolverOpts{
|
||||
UseTLS: false,
|
||||
UseTCP: true,
|
||||
}, opts)
|
||||
if err != nil {
|
||||
return rslvrs, err
|
||||
}
|
||||
rslvrs = append(rslvrs, rslvr)
|
||||
}
|
||||
if ns.Type == models.UDPResolver {
|
||||
opts.Logger.Debug("initiating UDP resolver")
|
||||
rslvr, err := NewClassicResolver(ns.Address,
|
||||
ClassicResolverOpts{
|
||||
UseTLS: false,
|
||||
UseTCP: false,
|
||||
}, opts)
|
||||
if err != nil {
|
||||
return rslvrs, err
|
||||
}
|
||||
rslvrs = append(rslvrs, rslvr)
|
||||
}
|
||||
if ns.Type == models.DNSCryptResolver {
|
||||
opts.Logger.Debug("initiating DNSCrypt resolver")
|
||||
rslvr, err := NewDNSCryptResolver(ns.Address,
|
||||
DNSCryptResolverOpts{
|
||||
UseTCP: false,
|
||||
}, opts)
|
||||
if err != nil {
|
||||
return rslvrs, err
|
||||
}
|
||||
rslvrs = append(rslvrs, rslvr)
|
||||
}
|
||||
if ns.Type == models.DOQResolver {
|
||||
opts.Logger.Debug("initiating DOQ resolver")
|
||||
rslvr, err := NewDOQResolver(ns.Address, opts)
|
||||
if err != nil {
|
||||
return rslvrs, err
|
||||
}
|
||||
rslvrs = append(rslvrs, rslvr)
|
||||
}
|
||||
}
|
||||
return rslvrs, nil
|
||||
}
|
134
internal/resolvers/utils.go
Normal file
134
internal/resolvers/utils.go
Normal file
|
@ -0,0 +1,134 @@
|
|||
package resolvers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
// prepareMessages takes a DNS Question and returns the
|
||||
// corresponding DNS messages for the same.
|
||||
func prepareMessages(q dns.Question, opts Options) []dns.Msg {
|
||||
var (
|
||||
possibleQNames = constructPossibleQuestions(q.Name, opts.Ndots, opts.SearchList)
|
||||
messages = make([]dns.Msg, 0, len(possibleQNames))
|
||||
)
|
||||
|
||||
for _, qName := range possibleQNames {
|
||||
msg := dns.Msg{
|
||||
MsgHdr: dns.MsgHdr{
|
||||
Id: dns.Id(),
|
||||
Authoritative: opts.Authoritative,
|
||||
AuthenticatedData: opts.AuthenticatedData,
|
||||
CheckingDisabled: opts.CheckingDisabled,
|
||||
RecursionDesired: true,
|
||||
},
|
||||
}
|
||||
// It's recommended to only send 1 question for 1 DNS message.
|
||||
msg.Question = []dns.Question{{
|
||||
Name: qName,
|
||||
Qtype: q.Qtype,
|
||||
Qclass: q.Qclass,
|
||||
}}
|
||||
messages = append(messages, msg)
|
||||
}
|
||||
|
||||
return messages
|
||||
}
|
||||
|
||||
// NameList returns all of the names that should be queried based on the
|
||||
// config. It is based off of go's net/dns name building, but it does not
|
||||
// check the length of the resulting names.
|
||||
// NOTE: It is taken from `miekg/dns/clientconfig.go: func (c *ClientConfig) NameList`
|
||||
// and slightly modified.
|
||||
func constructPossibleQuestions(name string, ndots int, searchList []string) []string {
|
||||
// if this domain is already fully qualified, no append needed.
|
||||
if dns.IsFqdn(name) {
|
||||
return []string{name}
|
||||
}
|
||||
|
||||
// Check to see if the name has more labels than Ndots. Do this before making
|
||||
// the domain fully qualified.
|
||||
hasNdots := dns.CountLabel(name) > ndots
|
||||
// Make the domain fully qualified.
|
||||
name = dns.Fqdn(name)
|
||||
|
||||
// Make a list of names based off search.
|
||||
names := []string{}
|
||||
|
||||
// If name has enough dots, try that first.
|
||||
if hasNdots {
|
||||
names = append(names, name)
|
||||
}
|
||||
for _, s := range searchList {
|
||||
names = append(names, dns.Fqdn(name+s))
|
||||
}
|
||||
// If we didn't have enough dots, try after suffixes.
|
||||
if !hasNdots {
|
||||
names = append(names, name)
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
// parseMessage takes a `dns.Message` and returns a custom
|
||||
// Response data struct.
|
||||
func parseMessage(msg *dns.Msg, rtt time.Duration, server string) Response {
|
||||
var resp Response
|
||||
timeTaken := fmt.Sprintf("%dms", rtt.Milliseconds())
|
||||
|
||||
// Parse Authorities section.
|
||||
for _, ns := range msg.Ns {
|
||||
// check for SOA record
|
||||
soa, ok := ns.(*dns.SOA)
|
||||
if !ok {
|
||||
// Currently we only check for SOA in Authority.
|
||||
// If it's not SOA, skip this message.
|
||||
continue
|
||||
}
|
||||
mname := soa.Ns + " " + soa.Mbox +
|
||||
" " + strconv.FormatInt(int64(soa.Serial), 10) +
|
||||
" " + strconv.FormatInt(int64(soa.Refresh), 10) +
|
||||
" " + strconv.FormatInt(int64(soa.Retry), 10) +
|
||||
" " + strconv.FormatInt(int64(soa.Expire), 10) +
|
||||
" " + strconv.FormatInt(int64(soa.Minttl), 10)
|
||||
h := ns.Header()
|
||||
name := h.Name
|
||||
qclass := dns.Class(h.Class).String()
|
||||
ttl := strconv.FormatInt(int64(h.Ttl), 10) + "s"
|
||||
qtype := dns.Type(h.Rrtype).String()
|
||||
auth := Authority{
|
||||
Name: name,
|
||||
Type: qtype,
|
||||
TTL: ttl,
|
||||
Class: qclass,
|
||||
MName: mname,
|
||||
Nameserver: server,
|
||||
RTT: timeTaken,
|
||||
Status: dns.RcodeToString[msg.Rcode],
|
||||
}
|
||||
resp.Authorities = append(resp.Authorities, auth)
|
||||
}
|
||||
// Parse Answers section.
|
||||
for _, a := range msg.Answer {
|
||||
var (
|
||||
h = a.Header()
|
||||
// Source https://github.com/jvns/dns-lookup/blob/main/dns.go#L121.
|
||||
parts = strings.Split(a.String(), "\t")
|
||||
ans = Answer{
|
||||
Name: h.Name,
|
||||
Type: dns.Type(h.Rrtype).String(),
|
||||
TTL: strconv.FormatInt(int64(h.Ttl), 10) + "s",
|
||||
Class: dns.Class(h.Class).String(),
|
||||
Address: parts[len(parts)-1],
|
||||
RTT: timeTaken,
|
||||
Nameserver: server,
|
||||
}
|
||||
)
|
||||
|
||||
resp.Answers = append(resp.Answers, ans)
|
||||
}
|
||||
return resp
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue