mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-02-03 22:18:39 +00:00
cmd/ctrld: add default timeout for os resolver
So it can fail fast if internet broken suddenly. While at it, also filtering out ipv6 nameservers if ipv6 not available.
This commit is contained in:
committed by
Cuong Manh Le
parent
096e7ea429
commit
77b62f8734
@@ -342,6 +342,7 @@ func ttlFromMsg(msg *dns.Msg) uint32 {
|
||||
}
|
||||
|
||||
var osUpstreamConfig = &ctrld.UpstreamConfig{
|
||||
Name: "OS resolver",
|
||||
Type: ctrld.ResolverTypeOS,
|
||||
Name: "OS resolver",
|
||||
Type: ctrld.ResolverTypeOS,
|
||||
Timeout: 2000,
|
||||
}
|
||||
|
||||
23
config.go
23
config.go
@@ -165,11 +165,15 @@ func (uc *UpstreamConfig) SetupBootstrapIP() {
|
||||
return ""
|
||||
}
|
||||
|
||||
resolver := &osResolver{nameservers: nameservers()}
|
||||
resolver := &osResolver{nameservers: availableNameservers()}
|
||||
resolver.nameservers = append([]string{net.JoinHostPort(bootstrapDNS, "53")}, resolver.nameservers...)
|
||||
ProxyLog.Debug().Msgf("Resolving %q using bootstrap DNS %q", uc.Domain, resolver.nameservers)
|
||||
timeoutMs := 2000
|
||||
if uc.Timeout > 0 && uc.Timeout < timeoutMs {
|
||||
timeoutMs = uc.Timeout
|
||||
}
|
||||
do := func(dnsType uint16) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(uc.Timeout)*time.Millisecond)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeoutMs)*time.Millisecond)
|
||||
defer cancel()
|
||||
m := new(dns.Msg)
|
||||
m.SetQuestion(uc.Domain+".", dnsType)
|
||||
@@ -349,3 +353,18 @@ func defaultPortFor(typ string) string {
|
||||
}
|
||||
return "53"
|
||||
}
|
||||
|
||||
func availableNameservers() []string {
|
||||
nss := nameservers()
|
||||
n := 0
|
||||
for _, ns := range nss {
|
||||
ip, _, _ := net.SplitHostPort(ns)
|
||||
// skipping invalid entry or ipv6 nameserver if ipv6 not available.
|
||||
if ip == "" || (ctrldnet.IsIPv6(ip) && !ctrldnet.SupportsIPv6()) {
|
||||
continue
|
||||
}
|
||||
nss[n] = ns
|
||||
n++
|
||||
}
|
||||
return nss[:n]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user