When using a fallback resolver, favor IPv6 for DoH servers if use_ipv6 is set

Fixes #153
This commit is contained in:
Frank Denis 2018-03-21 09:05:30 +01:00
parent 22f69a475a
commit 577ac5c91a
2 changed files with 38 additions and 16 deletions

View File

@ -182,6 +182,8 @@ func ConfigLoad(proxy *Proxy, svcFlag *string) error {
if len(config.FallbackResolver) > 0 {
proxy.xTransport.ignoreSystemDNS = config.IgnoreSystemDNS
}
proxy.xTransport.useIPv4 = config.SourceIPv4
proxy.xTransport.useIPv6 = config.SourceIPv6
proxy.timeout = time.Duration(config.Timeout) * time.Millisecond
proxy.maxClients = config.MaxClients
proxy.mainProto = "udp"

View File

@ -135,30 +135,50 @@ func (xTransport *XTransport) Fetch(method string, url *url.URL, accept string,
dlog.Debugf("IP for [%s] was cached to [%s], but connection failed: [%s]", host, cachedIP, err)
return nil, 0, err
}
if !xTransport.useIPv4 {
return nil, 0, fmt.Errorf("IPv4 connectivity would be required to use [%s]", host)
}
dnsClient := new(dns.Client)
msg := new(dns.Msg)
msg.SetQuestion(dns.Fqdn(host), dns.TypeA)
msg.SetEdns0(4096, true)
if !xTransport.ignoreSystemDNS {
dlog.Noticef("System DNS configuration not usable yet, exceptionally resolving [%s] using fallback resolver [%s]", host, xTransport.fallbackResolver)
} else {
dlog.Debugf("Resolving [%s] using fallback resolver [%s]", host, xTransport.fallbackResolver)
}
in, _, err := dnsClient.Exchange(msg, xTransport.fallbackResolver)
dnsClient := new(dns.Client)
var foundIP *string
err = nil
if xTransport.useIPv4 {
msg := new(dns.Msg)
msg.SetQuestion(dns.Fqdn(host), dns.TypeA)
msg.SetEdns0(4096, true)
var in *dns.Msg
in, _, err = dnsClient.Exchange(msg, xTransport.fallbackResolver)
if err == nil {
for _, answer := range in.Answer {
if answer.Header().Rrtype == dns.TypeA {
foundIPx := answer.(*dns.A).A.String()
foundIP = &foundIPx
break
}
}
}
}
if xTransport.useIPv6 && foundIP == nil {
msg := new(dns.Msg)
msg.SetQuestion(dns.Fqdn(host), dns.TypeAAAA)
msg.SetEdns0(4096, true)
var in *dns.Msg
in, _, err = dnsClient.Exchange(msg, xTransport.fallbackResolver)
if err == nil {
for _, answer := range in.Answer {
if answer.Header().Rrtype == dns.TypeAAAA {
foundIPx := "[" + answer.(*dns.AAAA).AAAA.String() + "]"
foundIP = &foundIPx
break
}
}
}
}
if err != nil {
return nil, 0, err
}
var foundIP *string
for _, answer := range in.Answer {
if answer.Header().Rrtype == dns.TypeA {
foundIPx := answer.(*dns.A).A.String()
foundIP = &foundIPx
break
}
}
if foundIP == nil {
return nil, 0, fmt.Errorf("No IP found for [%s]", host)
}