awl/query/general.go
Sam Therapy 1b5d5a3fed
Some checks failed
continuous-integration/drone/push Build is failing
Do "a few things"
Signed-off-by: Sam Therapy <sam@samtherapy.net>
2022-07-20 23:14:15 +02:00

60 lines
1 KiB
Go

// SPDX-License-Identifier: BSD-3-Clause
package query
import (
"fmt"
"git.froth.zone/sam/awl/cli"
"git.froth.zone/sam/awl/internal/structs"
"github.com/miekg/dns"
)
type StandardResolver struct {
server string
opts cli.Options
}
func (r *StandardResolver) LookUp(msg *dns.Msg) (structs.Response, error) {
var (
resp structs.Response
err error
)
dnsClient := new(dns.Client)
if r.opts.TCP || r.opts.TLS {
dnsClient.Net = "tcp"
} else {
dnsClient.Net = "udp"
}
switch {
case r.opts.IPv4:
dnsClient.Net += "4"
case r.opts.IPv6:
dnsClient.Net += "6"
}
if r.opts.TLS {
dnsClient.Net += "-tls"
}
resp.DNS, resp.RTT, err = dnsClient.Exchange(msg, r.server)
if err != nil {
return structs.Response{}, err
}
if resp.DNS.MsgHdr.Truncated && !r.opts.Truncate {
fmt.Printf(";; Truncated, retrying with TCP\n\n")
dnsClient.Net = "tcp"
switch {
case r.opts.IPv4:
dnsClient.Net += "4"
case r.opts.IPv4:
dnsClient.Net += "6"
}
resp.DNS, resp.RTT, err = dnsClient.Exchange(msg, r.server)
}
return resp, err
}