2020-12-10 10:39:05 +01:00
|
|
|
package main
|
|
|
|
|
2020-12-10 17:14:04 +01:00
|
|
|
import (
|
|
|
|
"strings"
|
2020-12-10 10:39:05 +01:00
|
|
|
|
2020-12-10 17:14:04 +01:00
|
|
|
"github.com/miekg/dns"
|
2020-12-17 12:27:44 +01:00
|
|
|
flag "github.com/spf13/pflag"
|
2020-12-10 17:14:04 +01:00
|
|
|
)
|
2020-12-10 10:39:05 +01:00
|
|
|
|
2020-12-13 13:19:10 +01:00
|
|
|
func (hub *Hub) loadQueryArgs() error {
|
2020-12-17 12:27:44 +01:00
|
|
|
// Appends a list of unparsed args to
|
|
|
|
// internal query flags.
|
|
|
|
err := hub.loadUnparsedArgs()
|
2020-12-10 17:14:04 +01:00
|
|
|
if err != nil {
|
2020-12-15 18:39:10 +01:00
|
|
|
return err
|
2020-12-10 17:14:04 +01:00
|
|
|
}
|
2020-12-17 12:27:44 +01:00
|
|
|
// check if ndots is set
|
|
|
|
hub.QueryFlags.isNdotsSet = isFlagPassed("ndots", hub.flag)
|
|
|
|
|
|
|
|
// Load all fallbacks in internal query flags.
|
2020-12-13 13:19:10 +01:00
|
|
|
hub.loadFallbacks()
|
2020-12-15 18:39:10 +01:00
|
|
|
return nil
|
2020-12-10 17:14:04 +01:00
|
|
|
}
|
2020-12-10 10:39:05 +01:00
|
|
|
|
2020-12-17 12:27:44 +01:00
|
|
|
// loadUnparsedArgs tries to parse all the arguments
|
|
|
|
// which are unparsed by `flag` library. These arguments don't have any specific
|
2020-12-10 17:14:04 +01:00
|
|
|
// order so we have to deduce based on the pattern of argument.
|
|
|
|
// For eg, a nameserver must always begin with `@`. In this
|
2020-12-17 12:27:44 +01:00
|
|
|
// pattern we deduce the arguments and append it to the
|
|
|
|
// list of internal query flags.
|
|
|
|
// In case an argument isn't able to fit in any of the existing
|
|
|
|
// pattern it is considered to be a "hostname".
|
|
|
|
// Eg of unparsed argument: `dig mrkaran.dev @1.1.1.1 AAAA`
|
|
|
|
// where `@1.1.1.1` and `AAAA` are "unparsed" args.
|
|
|
|
func (hub *Hub) loadUnparsedArgs() error {
|
|
|
|
for _, arg := range hub.UnparsedArgs {
|
2020-12-10 17:14:04 +01:00
|
|
|
if strings.HasPrefix(arg, "@") {
|
2020-12-13 13:19:10 +01:00
|
|
|
hub.QueryFlags.Nameservers = append(hub.QueryFlags.Nameservers, strings.Trim(arg, "@"))
|
2020-12-10 17:14:04 +01:00
|
|
|
} else if _, ok := dns.StringToType[strings.ToUpper(arg)]; ok {
|
2020-12-13 13:19:10 +01:00
|
|
|
hub.QueryFlags.QTypes = append(hub.QueryFlags.QTypes, arg)
|
2020-12-10 17:14:04 +01:00
|
|
|
} else if _, ok := dns.StringToClass[strings.ToUpper(arg)]; ok {
|
2020-12-13 13:19:10 +01:00
|
|
|
hub.QueryFlags.QClasses = append(hub.QueryFlags.QClasses, arg)
|
2020-12-10 17:14:04 +01:00
|
|
|
} else {
|
|
|
|
// if nothing matches, consider it's a query name.
|
2020-12-13 13:19:10 +01:00
|
|
|
hub.QueryFlags.QNames = append(hub.QueryFlags.QNames, arg)
|
2020-12-10 17:14:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2020-12-10 10:39:05 +01:00
|
|
|
}
|
|
|
|
|
2020-12-10 17:14:04 +01:00
|
|
|
// loadFallbacks sets fallbacks for options
|
2020-12-17 12:27:44 +01:00
|
|
|
// that are not specified by the user but necessary
|
|
|
|
// for the resolver.
|
2020-12-13 13:19:10 +01:00
|
|
|
func (hub *Hub) loadFallbacks() {
|
|
|
|
if len(hub.QueryFlags.QTypes) == 0 {
|
|
|
|
hub.QueryFlags.QTypes = append(hub.QueryFlags.QTypes, "A")
|
2020-12-10 17:14:04 +01:00
|
|
|
}
|
2020-12-13 13:19:10 +01:00
|
|
|
if len(hub.QueryFlags.QClasses) == 0 {
|
|
|
|
hub.QueryFlags.QClasses = append(hub.QueryFlags.QClasses, "IN")
|
2020-12-10 17:14:04 +01:00
|
|
|
}
|
|
|
|
}
|
2020-12-17 12:27:44 +01:00
|
|
|
|
|
|
|
// isFlagPassed checks if the flag is supplied by
|
|
|
|
//user or not.
|
|
|
|
func isFlagPassed(name string, f *flag.FlagSet) bool {
|
|
|
|
found := false
|
|
|
|
f.Visit(func(f *flag.Flag) {
|
|
|
|
if f.Name == name {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return found
|
|
|
|
}
|