doggo/cmd/lookup.go

83 lines
2.0 KiB
Go
Raw Normal View History

2020-12-10 17:14:04 +01:00
package main
import (
"strings"
"github.com/miekg/dns"
2020-12-13 08:15:45 +01:00
"github.com/mr-karan/doggo/pkg/resolvers"
"github.com/sirupsen/logrus"
2020-12-10 17:14:04 +01:00
"github.com/urfave/cli/v2"
)
2020-12-12 11:57:13 +01:00
// Lookup sends the DNS queries to the server.
2020-12-10 17:14:04 +01:00
func (hub *Hub) Lookup(c *cli.Context) error {
2020-12-13 08:15:45 +01:00
err := hub.prepareQuestions()
if err != nil {
return err
}
2020-12-12 11:57:13 +01:00
responses, err := hub.Resolver.Lookup(hub.Questions)
2020-12-10 17:14:04 +01:00
if err != nil {
2020-12-12 15:10:28 +01:00
return err
2020-12-10 17:14:04 +01:00
}
2020-12-12 15:10:28 +01:00
hub.Output(responses)
2020-12-10 17:14:04 +01:00
return nil
}
// prepareQuestions iterates on list of domain names
// and prepare a list of questions
// sent to the server with all possible combinations.
2020-12-13 08:15:45 +01:00
func (hub *Hub) prepareQuestions() error {
var (
question dns.Question
)
2020-12-10 17:14:04 +01:00
for _, name := range hub.QueryFlags.QNames.Value() {
2020-12-13 08:15:45 +01:00
var (
domains []string
ndots int
)
// If `search` flag is specified then fetch the search list
// from `resolv.conf` and set the
if hub.QueryFlags.UseSearchList {
list, n, err := fetchDomainList(name, hub.cliContext.IsSet("ndots"), hub.QueryFlags.Ndots)
if err != nil {
return err
}
domains = list
ndots = n
} else {
domains = []string{dns.Fqdn(name)}
}
for _, d := range domains {
hub.Logger.WithFields(logrus.Fields{
"domain": d,
"ndots": ndots,
}).Debug("Attmepting to resolve")
question.Name = d
// iterate on a list of query types.
for _, q := range hub.QueryFlags.QTypes.Value() {
question.Qtype = dns.StringToType[strings.ToUpper(q)]
// iterate on a list of query classes.
for _, c := range hub.QueryFlags.QClasses.Value() {
question.Qclass = dns.StringToClass[strings.ToUpper(c)]
// append a new question for each possible pair.
hub.Questions = append(hub.Questions, question)
}
2020-12-10 17:14:04 +01:00
}
}
}
2020-12-13 08:15:45 +01:00
return nil
}
func fetchDomainList(d string, isNdotsSet bool, ndots int) ([]string, int, error) {
cfg, err := dns.ClientConfigFromFile(resolvers.DefaultResolvConfPath)
if err != nil {
return nil, 0, err
}
// if user specified a custom ndots parameter, override it
if isNdotsSet {
cfg.Ndots = ndots
}
return cfg.NameList(d), cfg.Ndots, nil
2020-12-10 17:14:04 +01:00
}