113 lines
2.5 KiB
Go
113 lines
2.5 KiB
Go
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package cli
|
|
|
|
import (
|
|
"fmt"
|
|
"math/rand"
|
|
"strings"
|
|
|
|
"git.froth.zone/sam/awl/conf"
|
|
"git.froth.zone/sam/awl/util"
|
|
|
|
"github.com/miekg/dns"
|
|
"golang.org/x/net/idna"
|
|
)
|
|
|
|
// Parse the wildcard arguments, drill style.
|
|
func ParseMiscArgs(args []string, opts *Options) error {
|
|
var err error
|
|
|
|
for _, arg := range args {
|
|
r, ok := dns.StringToType[strings.ToUpper(arg)]
|
|
switch {
|
|
// If it starts with @, it's a DNS server
|
|
case strings.HasPrefix(arg, "@"):
|
|
opts.Request.Server = arg[1:]
|
|
case strings.Contains(arg, "."):
|
|
opts.Request.Name, err = idna.ToASCII(arg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
case ok:
|
|
// If it's a DNS request, it's a DNS request (obviously)
|
|
opts.Request.Type = r
|
|
case strings.HasPrefix(arg, "+"):
|
|
// Dig-style +queries
|
|
err = ParseDig(strings.ToLower(arg[1:]), opts)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
default:
|
|
|
|
opts.Request.Name, err = idna.ToASCII(arg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
// If nothing was set, set a default
|
|
if opts.Request.Name == "" {
|
|
opts.Request.Name = "."
|
|
if opts.Request.Type == 0 {
|
|
opts.Request.Type = dns.StringToType["NS"]
|
|
}
|
|
} else {
|
|
if opts.Request.Type == 0 {
|
|
opts.Request.Type = dns.StringToType["A"]
|
|
}
|
|
}
|
|
//
|
|
if opts.Request.Server == "" {
|
|
switch {
|
|
case opts.TLS:
|
|
opts.Request.Server = "dns.google"
|
|
case opts.HTTPS:
|
|
opts.Request.Server = "https://dns.cloudflare.com/dns-query"
|
|
case opts.QUIC:
|
|
opts.Request.Server = "dns.adguard.com"
|
|
default:
|
|
resolv, err := conf.GetDNSConfig()
|
|
if err != nil {
|
|
opts.Request.Server = "9.9.9.9"
|
|
} else {
|
|
for _, srv := range resolv.Servers {
|
|
if opts.IPv4 {
|
|
if strings.Contains(srv, ".") {
|
|
opts.Request.Server = srv
|
|
break
|
|
}
|
|
} else if opts.IPv6 {
|
|
if strings.Contains(srv, ":") {
|
|
opts.Request.Server = srv
|
|
break
|
|
}
|
|
} else {
|
|
//#nosec -- This isn't used for anything secure
|
|
opts.Request.Server = resolv.Servers[rand.Intn(len(resolv.Servers))]
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Make reverse adresses proper addresses
|
|
if opts.Reverse {
|
|
if dns.TypeToString[opts.Request.Type] == "A" {
|
|
opts.Request.Type = dns.StringToType["PTR"]
|
|
}
|
|
opts.Request.Name, err = util.ReverseDNS(opts.Request.Name, opts.Request.Type)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// if the domain is not canonical, make it canonical
|
|
if !strings.HasSuffix(opts.Request.Name, ".") {
|
|
opts.Request.Name = fmt.Sprintf("%s.", opts.Request.Name)
|
|
}
|
|
return nil
|
|
}
|