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-10 10:39:05 +01:00
|
|
|
|
2020-12-13 13:19:10 +01:00
|
|
|
func (hub *Hub) loadQueryArgs() error {
|
2020-12-13 08:15:45 +01:00
|
|
|
|
2020-12-13 13:19:10 +01:00
|
|
|
err := hub.loadNamedArgs()
|
|
|
|
|
|
|
|
err = hub.loadFreeArgs()
|
2020-12-10 17:14:04 +01:00
|
|
|
if err != nil {
|
2020-12-13 13:19:10 +01:00
|
|
|
hub.Logger.WithError(err).Error("Error parsing arguments")
|
|
|
|
hub.Logger.Exit(2)
|
2020-12-10 17:14:04 +01:00
|
|
|
}
|
2020-12-13 13:19:10 +01:00
|
|
|
err = hub.initResolver()
|
2020-12-10 17:14:04 +01:00
|
|
|
if err != nil {
|
2020-12-13 13:19:10 +01:00
|
|
|
hub.Logger.WithError(err).Error("Error parsing nameservers")
|
|
|
|
hub.Logger.Exit(2)
|
2020-12-10 17:14:04 +01:00
|
|
|
}
|
2020-12-13 17:16:27 +01:00
|
|
|
|
2020-12-13 13:19:10 +01:00
|
|
|
hub.loadFallbacks()
|
2020-12-10 17:14:04 +01:00
|
|
|
return err
|
|
|
|
}
|
2020-12-10 10:39:05 +01:00
|
|
|
|
2020-12-12 07:16:13 +01:00
|
|
|
// loadFreeArgs tries to parse all the arguments
|
2020-12-10 17:14:04 +01:00
|
|
|
// given to the CLI. These arguments don't have any specific
|
|
|
|
// order so we have to deduce based on the pattern of argument.
|
|
|
|
// For eg, a nameserver must always begin with `@`. In this
|
|
|
|
// pattern we deduce the arguments and map it to internal query
|
|
|
|
// options. In case an argument isn't able to fit in any of the existing
|
|
|
|
// pattern it is considered to be a "query name".
|
2020-12-13 13:19:10 +01:00
|
|
|
func (hub *Hub) loadFreeArgs() error {
|
2020-12-13 17:00:54 +01:00
|
|
|
for _, arg := range hub.FreeArgs {
|
2020-12-13 13:19:10 +01:00
|
|
|
if strings.HasPrefix(arg, "--") || strings.HasPrefix(arg, "-") {
|
|
|
|
continue
|
|
|
|
}
|
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-13 13:19:10 +01:00
|
|
|
// loadNamedArgs checks for all flags and loads their
|
|
|
|
// values inside the Hub.
|
|
|
|
func (hub *Hub) loadNamedArgs() error {
|
|
|
|
// Unmarshall flags to the struct.
|
|
|
|
err := k.Unmarshal("", &hub.QueryFlags)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-10 17:14:04 +01:00
|
|
|
// loadFallbacks sets fallbacks for options
|
|
|
|
// that are not specified by the user.
|
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
|
|
|
}
|
|
|
|
}
|