Compare commits
1 Commits
feature/62
...
48bedaf579
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
48bedaf579 |
@@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
|
||||||
"net"
|
"net"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -42,22 +41,6 @@ func rootServerList() []string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const maxRootServers = 3
|
|
||||||
|
|
||||||
// randomRootServers returns a shuffled subset of root servers.
|
|
||||||
func randomRootServers() []string {
|
|
||||||
all := rootServerList()
|
|
||||||
rand.Shuffle(len(all), func(i, j int) {
|
|
||||||
all[i], all[j] = all[j], all[i]
|
|
||||||
})
|
|
||||||
|
|
||||||
if len(all) > maxRootServers {
|
|
||||||
return all[:maxRootServers]
|
|
||||||
}
|
|
||||||
|
|
||||||
return all
|
|
||||||
}
|
|
||||||
|
|
||||||
func checkCtx(ctx context.Context) error {
|
func checkCtx(ctx context.Context) error {
|
||||||
err := ctx.Err()
|
err := ctx.Err()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -244,7 +227,7 @@ func (r *Resolver) followDelegation(
|
|||||||
|
|
||||||
authNS := extractNSSet(resp.Ns)
|
authNS := extractNSSet(resp.Ns)
|
||||||
if len(authNS) == 0 {
|
if len(authNS) == 0 {
|
||||||
return r.resolveNSRecursive(ctx, domain)
|
return r.resolveNSIterative(ctx, domain)
|
||||||
}
|
}
|
||||||
|
|
||||||
glue := extractGlue(resp.Extra)
|
glue := extractGlue(resp.Extra)
|
||||||
@@ -308,60 +291,84 @@ func (r *Resolver) resolveNSIPs(
|
|||||||
return ips
|
return ips
|
||||||
}
|
}
|
||||||
|
|
||||||
// resolveNSRecursive queries for NS records using recursive
|
// resolveNSIterative queries for NS records using iterative
|
||||||
// resolution as a fallback for intercepted environments.
|
// resolution as a fallback when followDelegation finds no
|
||||||
func (r *Resolver) resolveNSRecursive(
|
// authoritative answer in the delegation chain.
|
||||||
|
func (r *Resolver) resolveNSIterative(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
domain string,
|
domain string,
|
||||||
) ([]string, error) {
|
) ([]string, error) {
|
||||||
domain = dns.Fqdn(domain)
|
|
||||||
msg := new(dns.Msg)
|
|
||||||
msg.SetQuestion(domain, dns.TypeNS)
|
|
||||||
msg.RecursionDesired = true
|
|
||||||
|
|
||||||
for _, ip := range randomRootServers() {
|
|
||||||
if checkCtx(ctx) != nil {
|
if checkCtx(ctx) != nil {
|
||||||
return nil, ErrContextCanceled
|
return nil, ErrContextCanceled
|
||||||
}
|
}
|
||||||
|
|
||||||
addr := net.JoinHostPort(ip, "53")
|
domain = dns.Fqdn(domain)
|
||||||
|
servers := rootServerList()
|
||||||
|
|
||||||
resp, _, err := r.client.ExchangeContext(ctx, msg, addr)
|
for range maxDelegation {
|
||||||
|
if checkCtx(ctx) != nil {
|
||||||
|
return nil, ErrContextCanceled
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := r.queryServers(
|
||||||
|
ctx, servers, domain, dns.TypeNS,
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
nsNames := extractNSSet(resp.Answer)
|
nsNames := extractNSSet(resp.Answer)
|
||||||
if len(nsNames) > 0 {
|
if len(nsNames) > 0 {
|
||||||
return nsNames, nil
|
return nsNames, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Follow delegation.
|
||||||
|
authNS := extractNSSet(resp.Ns)
|
||||||
|
if len(authNS) == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
glue := extractGlue(resp.Extra)
|
||||||
|
nextServers := glueIPs(authNS, glue)
|
||||||
|
|
||||||
|
if len(nextServers) == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
servers = nextServers
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, ErrNoNameservers
|
return nil, ErrNoNameservers
|
||||||
}
|
}
|
||||||
|
|
||||||
// resolveARecord resolves a hostname to IPv4 addresses.
|
// resolveARecord resolves a hostname to IPv4 addresses using
|
||||||
|
// iterative resolution through the delegation chain.
|
||||||
func (r *Resolver) resolveARecord(
|
func (r *Resolver) resolveARecord(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
hostname string,
|
hostname string,
|
||||||
) ([]string, error) {
|
) ([]string, error) {
|
||||||
hostname = dns.Fqdn(hostname)
|
|
||||||
msg := new(dns.Msg)
|
|
||||||
msg.SetQuestion(hostname, dns.TypeA)
|
|
||||||
msg.RecursionDesired = true
|
|
||||||
|
|
||||||
for _, ip := range randomRootServers() {
|
|
||||||
if checkCtx(ctx) != nil {
|
if checkCtx(ctx) != nil {
|
||||||
return nil, ErrContextCanceled
|
return nil, ErrContextCanceled
|
||||||
}
|
}
|
||||||
|
|
||||||
addr := net.JoinHostPort(ip, "53")
|
hostname = dns.Fqdn(hostname)
|
||||||
|
servers := rootServerList()
|
||||||
|
|
||||||
resp, _, err := r.client.ExchangeContext(ctx, msg, addr)
|
for range maxDelegation {
|
||||||
if err != nil {
|
if checkCtx(ctx) != nil {
|
||||||
continue
|
return nil, ErrContextCanceled
|
||||||
}
|
}
|
||||||
|
|
||||||
|
resp, err := r.queryServers(
|
||||||
|
ctx, servers, hostname, dns.TypeA,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf(
|
||||||
|
"resolving %s: %w", hostname, err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for A records in the answer section.
|
||||||
var ips []string
|
var ips []string
|
||||||
|
|
||||||
for _, rr := range resp.Answer {
|
for _, rr := range resp.Answer {
|
||||||
@@ -373,6 +380,24 @@ func (r *Resolver) resolveARecord(
|
|||||||
if len(ips) > 0 {
|
if len(ips) > 0 {
|
||||||
return ips, nil
|
return ips, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Follow delegation if present.
|
||||||
|
authNS := extractNSSet(resp.Ns)
|
||||||
|
if len(authNS) == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
glue := extractGlue(resp.Extra)
|
||||||
|
nextServers := glueIPs(authNS, glue)
|
||||||
|
|
||||||
|
if len(nextServers) == 0 {
|
||||||
|
// Resolve NS IPs iteratively — but guard
|
||||||
|
// against infinite recursion by using only
|
||||||
|
// already-resolved servers.
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
servers = nextServers
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, fmt.Errorf(
|
return nil, fmt.Errorf(
|
||||||
@@ -402,7 +427,7 @@ func (r *Resolver) FindAuthoritativeNameservers(
|
|||||||
candidate := strings.Join(labels[i:], ".") + "."
|
candidate := strings.Join(labels[i:], ".") + "."
|
||||||
|
|
||||||
nsNames, err := r.followDelegation(
|
nsNames, err := r.followDelegation(
|
||||||
ctx, candidate, randomRootServers(),
|
ctx, candidate, rootServerList(),
|
||||||
)
|
)
|
||||||
if err == nil && len(nsNames) > 0 {
|
if err == nil && len(nsNames) > 0 {
|
||||||
sort.Strings(nsNames)
|
sort.Strings(nsNames)
|
||||||
|
|||||||
Reference in New Issue
Block a user