Moved A & AAAA record lookups into one function

This commit is contained in:
perp 2024-07-09 19:17:34 +01:00
parent 5e5acbe506
commit e32dc646b1
5 changed files with 39 additions and 87 deletions

View File

@ -162,10 +162,14 @@ var rootCmd = &cobra.Command{
// Print warnings // Print warnings
if opts.UDP { if opts.UDP {
log.Warn().Msg("UDP is enabled") log.Warn().Msg("UDP is enabled")
} else {
log.Warn().Msg("TCP is enabled")
} }
if opts.IPv6 { if opts.IPv6 {
log.Warn().Msg("IPv6 is enabled") log.Warn().Msg("IPv6 is enabled")
} else {
log.Warn().Msg("IPv4 is enabled")
} }
// Wildcard enabled // Wildcard enabled

View File

@ -1,70 +0,0 @@
package dns
import (
"errors"
"fmt"
"math/rand"
"strings"
"github.com/miekg/dns"
)
// Query AAAA record (ipv6)
func (q *Query) AAAA() {
for {
// Create result
result := &Result{
Domain: q.Domain,
Subdomain: q.Subdomain,
}
// Create message
message := new(dns.Msg)
// Set question
message.SetQuestion(dns.Fqdn(q.Subdomain), dns.TypeAAAA)
// Choose random resolver
resolver := q.Resolvers[rand.Intn(len(q.Resolvers))]
// Query response
resp, _, err := q.Client.Exchange(message, resolver)
if err != nil {
// Ratelimited
if strings.ContainsAny(err.Error(), "i/o timeout") {
continue
}
result.Error = err
q.Results <- result
return
}
// Store IPs
var ips []string
// Go through answers
for _, answer := range resp.Answer {
// Map record
record, ok := answer.(*dns.AAAA)
if !ok {
continue
}
// Append IPv6
ips = append(ips, record.AAAA.String())
}
// No IPs found
if len(ips) < 1 {
result.Error = errors.New(fmt.Sprintf("No IPv6s found for %s", q.Domain))
q.Results <- result
return
}
// Send result
result.IPv6 = ips
q.Results <- result
return
}
}

View File

@ -10,6 +10,7 @@ import (
// DNS query // DNS query
type Query struct { type Query struct {
Client *dns.Client // DNS client Client *dns.Client // DNS client
IPv6 bool // IPv6 mode
Resolvers []string // Target resolvers Resolvers []string // Target resolvers
Domain string // Target domain Domain string // Target domain
Subdomain string // Target subdomain Subdomain string // Target subdomain

View File

@ -9,8 +9,8 @@ import (
"github.com/miekg/dns" "github.com/miekg/dns"
) )
// Query A record (ipv4) // Query records
func (q *Query) A() { func (q *Query) Lookup() {
for { for {
// Create result // Create result
result := &Result{ result := &Result{
@ -22,7 +22,12 @@ func (q *Query) A() {
message := new(dns.Msg) message := new(dns.Msg)
// Set question // Set question
message.SetQuestion(dns.Fqdn(q.Subdomain), dns.TypeA) switch q.IPv6 {
case false:
message.SetQuestion(dns.Fqdn(q.Subdomain), dns.TypeA)
case true:
message.SetQuestion(dns.Fqdn(q.Subdomain), dns.TypeAAAA)
}
// Choose random resolver // Choose random resolver
resolver := q.Resolvers[rand.Intn(len(q.Resolvers))] resolver := q.Resolvers[rand.Intn(len(q.Resolvers))]
@ -46,24 +51,40 @@ func (q *Query) A() {
// Go through answers // Go through answers
for _, answer := range resp.Answer { for _, answer := range resp.Answer {
// Map record // Map record
record, ok := answer.(*dns.A) switch q.IPv6 {
if !ok { case false:
continue record, ok := answer.(*dns.A)
} if !ok {
continue
}
// Append IPv4 // Append IPv4
ips = append(ips, record.A.String()) ips = append(ips, record.A.String())
case true:
record, ok := answer.(*dns.AAAA)
if !ok {
continue
}
// Append IPv4
ips = append(ips, record.AAAA.String())
}
} }
// No IPs found // No IPs found
if len(ips) < 1 { if len(ips) < 1 {
result.Error = errors.New(fmt.Sprintf("No IPv4s found for %s", q.Domain)) result.Error = errors.New(fmt.Sprintf("No IPs found for %s", q.Domain))
q.Results <- result q.Results <- result
return return
} }
// Send result // Send result
result.IPv4 = ips switch q.IPv6 {
case false:
result.IPv4 = ips
case true:
result.IPv6 = ips
}
q.Results <- result q.Results <- result
return return
} }

View File

@ -63,6 +63,7 @@ func (r *Runner) Submit() {
// Create query // Create query
query := &dns.Query{ query := &dns.Query{
Client: r.client, Client: r.client,
IPv6: r.options.IPv6,
Resolvers: r.options.Resolvers, Resolvers: r.options.Resolvers,
Domain: domain, Domain: domain,
Subdomain: word + "." + domain, Subdomain: word + "." + domain,
@ -71,12 +72,7 @@ func (r *Runner) Submit() {
// Submit query // Submit query
r.pool.Submit(func() { r.pool.Submit(func() {
// Check IPv6 query.Lookup()
if !r.options.IPv6 {
query.A()
} else {
query.AAAA()
}
}) })
} }
} }