chore: parse options correctly

This commit is contained in:
Karan Sharma 2020-12-12 12:16:54 +05:30
parent 169837d094
commit 8bcd940685
8 changed files with 70 additions and 39 deletions

View file

@ -44,32 +44,52 @@ func main() {
app.Usage = "Command-line DNS Client"
app.Version = buildVersion
var qFlags QueryFlags
// Register command line flags.
app.Flags = []cli.Flag{
&cli.StringSliceFlag{
Name: "query",
Usage: "Domain name to query",
Destination: qFlags.QNames,
Destination: hub.QueryFlags.QNames,
},
&cli.StringSliceFlag{
Name: "type",
Usage: "Type of DNS record to be queried (A, AAAA, MX etc)",
Destination: qFlags.QTypes,
Destination: hub.QueryFlags.QTypes,
},
&cli.StringSliceFlag{
Name: "nameserver",
Usage: "Address of the nameserver to send packets to",
Destination: qFlags.Nameservers,
Destination: hub.QueryFlags.Nameservers,
},
&cli.StringSliceFlag{
Name: "class",
Usage: "Network class of the DNS record to be queried (IN, CH, HS etc)",
Destination: qFlags.QClasses,
Destination: hub.QueryFlags.QClasses,
},
&cli.BoolFlag{
Name: "https",
Usage: "Use the DNS-over-HTTPS protocol",
Name: "udp",
Usage: "Use the DNS protocol over UDP",
},
&cli.BoolFlag{
Name: "tcp",
Usage: "Use the DNS protocol over TCP",
},
&cli.BoolFlag{
Name: "https",
Usage: "Use the DNS-over-HTTPS protocol",
Destination: &hub.QueryFlags.IsDOH,
},
&cli.BoolFlag{
Name: "ipv6",
Aliases: []string{"6"},
Usage: "Use IPv6 only",
Destination: &hub.QueryFlags.UseIPv6,
},
&cli.BoolFlag{
Name: "ipv4",
Aliases: []string{"4"},
Usage: "Use IPv4 only",
Destination: &hub.QueryFlags.UseIPv4,
},
&cli.BoolFlag{
Name: "verbose",

View file

@ -26,6 +26,8 @@ type QueryFlags struct {
IsDOT bool
IsUDP bool
IsTLS bool
UseIPv4 bool
UseIPv6 bool
}
// NewHub initializes an instance of Hub which holds app wide configuration.
@ -39,7 +41,6 @@ func NewHub(logger *logrus.Logger, buildVersion string) *Hub {
QTypes: cli.NewStringSlice(),
QClasses: cli.NewStringSlice(),
Nameservers: cli.NewStringSlice(),
IsDOH: false,
},
}
return hub

View file

@ -1,7 +1,6 @@
package main
import (
"fmt"
"strings"
"github.com/miekg/dns"
@ -12,7 +11,6 @@ func (hub *Hub) Lookup(c *cli.Context) error {
hub.prepareQuestions()
err := hub.Resolver.Lookup(hub.Questions)
if err != nil {
fmt.Println(err)
hub.Logger.Error(err)
}
return nil

View file

@ -8,7 +8,6 @@ import (
)
func (hub *Hub) loadQueryArgs(c *cli.Context) error {
hub.loadTransportArgs(c)
err := hub.loadFreeArgs(c)
if err != nil {
cli.Exit("Error parsing arguments", -1)
@ -54,11 +53,3 @@ func (hub *Hub) loadFallbacks(c *cli.Context) {
hub.QueryFlags.QClasses.Set("IN")
}
}
// loadTransportArgs loads the query flags
// for transport options.
func (hub *Hub) loadTransportArgs(c *cli.Context) {
if c.Bool("https") {
hub.QueryFlags.IsDOH = true
}
}

View file

@ -35,7 +35,10 @@ func (hub *Hub) loadResolver(c *cli.Context) error {
return nil
}
} else {
rslvr, err := resolvers.NewClassicResolver(hub.QueryFlags.Nameservers.Value())
rslvr, err := resolvers.NewClassicResolver(hub.QueryFlags.Nameservers.Value(), resolvers.ClassicResolverOpts{
UseIPv4: hub.QueryFlags.UseIPv4,
UseIPv6: hub.QueryFlags.UseIPv6,
})
if err != nil {
return err
}