Compare commits
1 Commits
2e1a4b2dbd
...
fix/issue-
| Author | SHA1 | Date | |
|---|---|---|---|
| f2970143d2 |
@@ -24,4 +24,8 @@ var (
|
|||||||
// ErrContextCanceled wraps context cancellation for the
|
// ErrContextCanceled wraps context cancellation for the
|
||||||
// resolver's iterative queries.
|
// resolver's iterative queries.
|
||||||
ErrContextCanceled = errors.New("context canceled")
|
ErrContextCanceled = errors.New("context canceled")
|
||||||
|
|
||||||
|
// ErrSERVFAIL is returned when a DNS server responds with
|
||||||
|
// SERVFAIL after all retries are exhausted.
|
||||||
|
ErrSERVFAIL = errors.New("SERVFAIL from server")
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -13,7 +14,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
queryTimeoutDuration = 700 * time.Millisecond
|
queryTimeoutDuration = 2 * time.Second
|
||||||
maxRetries = 2
|
maxRetries = 2
|
||||||
maxDelegation = 20
|
maxDelegation = 20
|
||||||
timeoutMultiplier = 2
|
timeoutMultiplier = 2
|
||||||
@@ -41,6 +42,22 @@ 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 {
|
||||||
@@ -227,7 +244,7 @@ func (r *Resolver) followDelegation(
|
|||||||
|
|
||||||
authNS := extractNSSet(resp.Ns)
|
authNS := extractNSSet(resp.Ns)
|
||||||
if len(authNS) == 0 {
|
if len(authNS) == 0 {
|
||||||
return r.resolveNSIterative(ctx, domain)
|
return r.resolveNSRecursive(ctx, domain)
|
||||||
}
|
}
|
||||||
|
|
||||||
glue := extractGlue(resp.Extra)
|
glue := extractGlue(resp.Extra)
|
||||||
@@ -291,84 +308,60 @@ func (r *Resolver) resolveNSIPs(
|
|||||||
return ips
|
return ips
|
||||||
}
|
}
|
||||||
|
|
||||||
// resolveNSIterative queries for NS records using iterative
|
// resolveNSRecursive queries for NS records using recursive
|
||||||
// resolution as a fallback when followDelegation finds no
|
// resolution as a fallback for intercepted environments.
|
||||||
// authoritative answer in the delegation chain.
|
func (r *Resolver) resolveNSRecursive(
|
||||||
func (r *Resolver) resolveNSIterative(
|
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
domain string,
|
domain string,
|
||||||
) ([]string, error) {
|
) ([]string, error) {
|
||||||
if checkCtx(ctx) != nil {
|
|
||||||
return nil, ErrContextCanceled
|
|
||||||
}
|
|
||||||
|
|
||||||
domain = dns.Fqdn(domain)
|
domain = dns.Fqdn(domain)
|
||||||
servers := rootServerList()
|
msg := new(dns.Msg)
|
||||||
|
msg.SetQuestion(domain, dns.TypeNS)
|
||||||
|
msg.RecursionDesired = true
|
||||||
|
|
||||||
for range maxDelegation {
|
for _, ip := range randomRootServers() {
|
||||||
if checkCtx(ctx) != nil {
|
if checkCtx(ctx) != nil {
|
||||||
return nil, ErrContextCanceled
|
return nil, ErrContextCanceled
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := r.queryServers(
|
addr := net.JoinHostPort(ip, "53")
|
||||||
ctx, servers, domain, dns.TypeNS,
|
|
||||||
)
|
resp, _, err := r.client.ExchangeContext(ctx, msg, addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
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 using
|
// resolveARecord resolves a hostname to IPv4 addresses.
|
||||||
// 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) {
|
||||||
if checkCtx(ctx) != nil {
|
|
||||||
return nil, ErrContextCanceled
|
|
||||||
}
|
|
||||||
|
|
||||||
hostname = dns.Fqdn(hostname)
|
hostname = dns.Fqdn(hostname)
|
||||||
servers := rootServerList()
|
msg := new(dns.Msg)
|
||||||
|
msg.SetQuestion(hostname, dns.TypeA)
|
||||||
|
msg.RecursionDesired = true
|
||||||
|
|
||||||
for range maxDelegation {
|
for _, ip := range randomRootServers() {
|
||||||
if checkCtx(ctx) != nil {
|
if checkCtx(ctx) != nil {
|
||||||
return nil, ErrContextCanceled
|
return nil, ErrContextCanceled
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := r.queryServers(
|
addr := net.JoinHostPort(ip, "53")
|
||||||
ctx, servers, hostname, dns.TypeA,
|
|
||||||
)
|
resp, _, err := r.client.ExchangeContext(ctx, msg, addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf(
|
continue
|
||||||
"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 {
|
||||||
@@ -380,24 +373,6 @@ 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(
|
||||||
@@ -427,7 +402,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, rootServerList(),
|
ctx, candidate, randomRootServers(),
|
||||||
)
|
)
|
||||||
if err == nil && len(nsNames) > 0 {
|
if err == nil && len(nsNames) > 0 {
|
||||||
sort.Strings(nsNames)
|
sort.Strings(nsNames)
|
||||||
@@ -484,9 +459,15 @@ func (r *Resolver) queryAllTypes(
|
|||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
singleTypeMaxRetries = 3
|
||||||
|
singleTypeInitialBackoff = 100 * time.Millisecond
|
||||||
|
)
|
||||||
|
|
||||||
type queryState struct {
|
type queryState struct {
|
||||||
gotNXDomain bool
|
gotNXDomain bool
|
||||||
gotSERVFAIL bool
|
gotSERVFAIL bool
|
||||||
|
gotTimeout bool
|
||||||
hasRecords bool
|
hasRecords bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -514,6 +495,21 @@ func (r *Resolver) queryEachType(
|
|||||||
return state
|
return state
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// isTimeout checks whether an error represents a DNS timeout.
|
||||||
|
func isTimeout(err error) bool {
|
||||||
|
if err == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
var netErr net.Error
|
||||||
|
if errors.As(err, &netErr) && netErr.Timeout() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Also catch i/o timeout strings from the dns library.
|
||||||
|
return strings.Contains(err.Error(), "i/o timeout")
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Resolver) querySingleType(
|
func (r *Resolver) querySingleType(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
nsIP string,
|
nsIP string,
|
||||||
@@ -522,23 +518,99 @@ func (r *Resolver) querySingleType(
|
|||||||
resp *NameserverResponse,
|
resp *NameserverResponse,
|
||||||
state *queryState,
|
state *queryState,
|
||||||
) {
|
) {
|
||||||
msg, err := r.queryDNS(ctx, nsIP, hostname, qtype)
|
msg, lastErr := r.querySingleTypeWithRetry(
|
||||||
if err != nil {
|
ctx, nsIP, hostname, qtype,
|
||||||
|
)
|
||||||
|
if msg == nil {
|
||||||
|
r.recordRetryFailure(lastErr, state)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
r.handleDNSResponse(msg, resp, state)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Resolver) querySingleTypeWithRetry(
|
||||||
|
ctx context.Context,
|
||||||
|
nsIP string,
|
||||||
|
hostname string,
|
||||||
|
qtype uint16,
|
||||||
|
) (*dns.Msg, error) {
|
||||||
|
var lastErr error
|
||||||
|
|
||||||
|
backoff := singleTypeInitialBackoff
|
||||||
|
|
||||||
|
for attempt := range singleTypeMaxRetries {
|
||||||
|
if checkCtx(ctx) != nil {
|
||||||
|
return nil, ErrContextCanceled
|
||||||
|
}
|
||||||
|
|
||||||
|
if attempt > 0 {
|
||||||
|
if !waitBackoff(ctx, backoff) {
|
||||||
|
return nil, ErrContextCanceled
|
||||||
|
}
|
||||||
|
|
||||||
|
backoff *= timeoutMultiplier
|
||||||
|
}
|
||||||
|
|
||||||
|
msg, err := r.queryDNS(ctx, nsIP, hostname, qtype)
|
||||||
|
if err != nil {
|
||||||
|
lastErr = err
|
||||||
|
|
||||||
|
if !isTimeout(err) {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if msg.Rcode == dns.RcodeServerFailure {
|
||||||
|
lastErr = ErrSERVFAIL
|
||||||
|
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
return msg, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, lastErr
|
||||||
|
}
|
||||||
|
|
||||||
|
func waitBackoff(ctx context.Context, d time.Duration) bool {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return false
|
||||||
|
case <-time.After(d):
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Resolver) recordRetryFailure(
|
||||||
|
lastErr error,
|
||||||
|
state *queryState,
|
||||||
|
) {
|
||||||
|
if lastErr == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if isTimeout(lastErr) {
|
||||||
|
state.gotTimeout = true
|
||||||
|
} else if errors.Is(lastErr, ErrSERVFAIL) {
|
||||||
|
state.gotSERVFAIL = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Resolver) handleDNSResponse(
|
||||||
|
msg *dns.Msg,
|
||||||
|
resp *NameserverResponse,
|
||||||
|
state *queryState,
|
||||||
|
) {
|
||||||
if msg.Rcode == dns.RcodeNameError {
|
if msg.Rcode == dns.RcodeNameError {
|
||||||
state.gotNXDomain = true
|
state.gotNXDomain = true
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if msg.Rcode == dns.RcodeServerFailure {
|
|
||||||
state.gotSERVFAIL = true
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
collectAnswerRecords(msg, resp, state)
|
collectAnswerRecords(msg, resp, state)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -565,8 +637,12 @@ func classifyResponse(resp *NameserverResponse, state queryState) {
|
|||||||
switch {
|
switch {
|
||||||
case state.gotNXDomain && !state.hasRecords:
|
case state.gotNXDomain && !state.hasRecords:
|
||||||
resp.Status = StatusNXDomain
|
resp.Status = StatusNXDomain
|
||||||
|
case state.gotTimeout && !state.hasRecords:
|
||||||
|
resp.Status = StatusTimeout
|
||||||
|
resp.Error = "all queries timed out after retries"
|
||||||
case state.gotSERVFAIL && !state.hasRecords:
|
case state.gotSERVFAIL && !state.hasRecords:
|
||||||
resp.Status = StatusError
|
resp.Status = StatusError
|
||||||
|
resp.Error = "server failure (SERVFAIL) after retries"
|
||||||
case !state.hasRecords && !state.gotNXDomain:
|
case !state.hasRecords && !state.gotNXDomain:
|
||||||
resp.Status = StatusNoData
|
resp.Status = StatusNoData
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ const (
|
|||||||
StatusError = "error"
|
StatusError = "error"
|
||||||
StatusNXDomain = "nxdomain"
|
StatusNXDomain = "nxdomain"
|
||||||
StatusNoData = "nodata"
|
StatusNoData = "nodata"
|
||||||
|
StatusTimeout = "timeout"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MaxCNAMEDepth is the maximum CNAME chain depth to follow.
|
// MaxCNAMEDepth is the maximum CNAME chain depth to follow.
|
||||||
|
|||||||
Reference in New Issue
Block a user