Compare commits
1 Commits
fix/dns-ti
...
66f8ad4048
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
66f8ad4048 |
@@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand/v2"
|
|
||||||
"net"
|
"net"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -14,13 +13,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// queryTimeoutDuration is the per-exchange DNS timeout.
|
queryTimeoutDuration = 700 * time.Millisecond
|
||||||
//
|
|
||||||
// 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
|
maxRetries = 2
|
||||||
maxDelegation = 20
|
maxDelegation = 20
|
||||||
timeoutMultiplier = 2
|
timeoutMultiplier = 2
|
||||||
@@ -30,7 +23,7 @@ const (
|
|||||||
// ErrRefused is returned when a DNS server refuses a query.
|
// ErrRefused is returned when a DNS server refuses a query.
|
||||||
var ErrRefused = errors.New("dns query refused")
|
var ErrRefused = errors.New("dns query refused")
|
||||||
|
|
||||||
func allRootServers() []string {
|
func rootServerList() []string {
|
||||||
return []string{
|
return []string{
|
||||||
"198.41.0.4", // a.root-servers.net
|
"198.41.0.4", // a.root-servers.net
|
||||||
"170.247.170.2", // b
|
"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 {
|
func checkCtx(ctx context.Context) error {
|
||||||
err := ctx.Err()
|
err := ctx.Err()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -247,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)
|
||||||
@@ -311,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)
|
if checkCtx(ctx) != nil {
|
||||||
msg := new(dns.Msg)
|
return nil, ErrContextCanceled
|
||||||
msg.SetQuestion(domain, dns.TypeNS)
|
}
|
||||||
msg.RecursionDesired = true
|
|
||||||
|
|
||||||
for _, ip := range rootServerList() {
|
domain = dns.Fqdn(domain)
|
||||||
|
servers := rootServerList()
|
||||||
|
|
||||||
|
for range maxDelegation {
|
||||||
if checkCtx(ctx) != nil {
|
if checkCtx(ctx) != nil {
|
||||||
return nil, ErrContextCanceled
|
return nil, ErrContextCanceled
|
||||||
}
|
}
|
||||||
|
|
||||||
addr := net.JoinHostPort(ip, "53")
|
resp, err := r.queryServers(
|
||||||
|
ctx, servers, domain, dns.TypeNS,
|
||||||
resp, _, err := r.client.ExchangeContext(ctx, msg, addr)
|
)
|
||||||
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)
|
if checkCtx(ctx) != nil {
|
||||||
msg := new(dns.Msg)
|
return nil, ErrContextCanceled
|
||||||
msg.SetQuestion(hostname, dns.TypeA)
|
}
|
||||||
msg.RecursionDesired = true
|
|
||||||
|
|
||||||
for _, ip := range rootServerList() {
|
hostname = dns.Fqdn(hostname)
|
||||||
|
servers := rootServerList()
|
||||||
|
|
||||||
|
for range maxDelegation {
|
||||||
if checkCtx(ctx) != nil {
|
if checkCtx(ctx) != nil {
|
||||||
return nil, ErrContextCanceled
|
return nil, ErrContextCanceled
|
||||||
}
|
}
|
||||||
|
|
||||||
addr := net.JoinHostPort(ip, "53")
|
resp, err := r.queryServers(
|
||||||
|
ctx, servers, hostname, dns.TypeA,
|
||||||
resp, _, err := r.client.ExchangeContext(ctx, msg, addr)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
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 {
|
||||||
@@ -376,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(
|
||||||
|
|||||||
Reference in New Issue
Block a user