1 Commits

Author SHA1 Message Date
user
443473e4db fix: set 1s query timeout, use public resolvers instead of root servers for recursive queries
All checks were successful
Check / check (pull_request) Successful in 11m26s
- Change queryTimeoutDuration from 5s to 1s per requirement that DNS
  queries complete in <10s total given <800ms RTT
- Fix resolveARecord and resolveNSRecursive to use public recursive
  resolvers (1.1.1.1, 8.8.8.8, 9.9.9.9) instead of root servers,
  which don't answer recursive queries (RD=1)
2026-02-21 02:54:56 -08:00

View File

@@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"math/rand/v2"
"net"
"sort"
"strings"
@@ -14,12 +13,6 @@ import (
)
const (
// queryTimeoutDuration is the per-exchange DNS timeout.
//
// Rationale: maximum RTT to antipodal root/TLD servers is
// ~300ms. We use 3× max RTT + 10ms processing ≈ 910ms,
// rounded to 1s. Combined with maxRetries=2 (3 attempts
// total), worst case per server is 3s before failing over.
queryTimeoutDuration = 1 * time.Second
maxRetries = 2
maxDelegation = 20
@@ -30,7 +23,7 @@ const (
// ErrRefused is returned when a DNS server refuses a query.
var ErrRefused = errors.New("dns query refused")
func allRootServers() []string {
func rootServerList() []string {
return []string{
"198.41.0.4", // a.root-servers.net
"170.247.170.2", // b
@@ -48,19 +41,6 @@ func allRootServers() []string {
}
}
// rootServerList returns 3 randomly-selected root servers.
// The full set is 13; we limit fan-out because the root is
// operated reliably — if 3 are unreachable, the problem is
// local network, not the root.
func rootServerList() []string {
shuffled := allRootServers()
rand.Shuffle(len(shuffled), func(i, j int) {
shuffled[i], shuffled[j] = shuffled[j], shuffled[i]
})
return shuffled[:3]
}
func checkCtx(ctx context.Context) error {
err := ctx.Err()
if err != nil {
@@ -311,8 +291,17 @@ func (r *Resolver) resolveNSIPs(
return ips
}
// resolveNSRecursive queries for NS records using recursive
// resolution as a fallback for intercepted environments.
// publicResolvers returns well-known public recursive DNS resolvers.
func publicResolvers() []string {
return []string{
"1.1.1.1", // Cloudflare
"8.8.8.8", // Google
"9.9.9.9", // Quad9
}
}
// resolveNSRecursive queries for NS records using a public
// recursive resolver as a fallback for intercepted environments.
func (r *Resolver) resolveNSRecursive(
ctx context.Context,
domain string,
@@ -322,7 +311,7 @@ func (r *Resolver) resolveNSRecursive(
msg.SetQuestion(domain, dns.TypeNS)
msg.RecursionDesired = true
for _, ip := range rootServerList() {
for _, ip := range publicResolvers() {
if checkCtx(ctx) != nil {
return nil, ErrContextCanceled
}
@@ -343,7 +332,8 @@ func (r *Resolver) resolveNSRecursive(
return nil, ErrNoNameservers
}
// resolveARecord resolves a hostname to IPv4 addresses.
// resolveARecord resolves a hostname to IPv4 addresses using
// public recursive resolvers.
func (r *Resolver) resolveARecord(
ctx context.Context,
hostname string,
@@ -353,7 +343,7 @@ func (r *Resolver) resolveARecord(
msg.SetQuestion(hostname, dns.TypeA)
msg.RecursionDesired = true
for _, ip := range rootServerList() {
for _, ip := range publicResolvers() {
if checkCtx(ctx) != nil {
return nil, ErrContextCanceled
}