Refine DNS strategies

This commit is contained in:
JimhHan 2021-04-09 23:36:36 +08:00
parent f4a048aa0c
commit 726a722019
No known key found for this signature in database
GPG key ID: 48D5D7CF95157AC5
21 changed files with 255 additions and 208 deletions

View file

@ -59,24 +59,15 @@ func (h *Handler) policy() policy.Session {
}
func (h *Handler) resolveIP(ctx context.Context, domain string, localAddr net.Address) net.Address {
if c, ok := h.dns.(dns.ClientWithIPOption); ok {
c.SetFakeDNSOption(false) // Skip FakeDNS
} else {
newError("DNS client doesn't implement ClientWithIPOption")
}
var lookupFunc func(string) ([]net.IP, error) = h.dns.LookupIP
var opt = dns.LookupIP
if h.config.DomainStrategy == Config_USE_IP4 || (localAddr != nil && localAddr.Family().IsIPv4()) {
if lookupIPv4, ok := h.dns.(dns.IPv4Lookup); ok {
lookupFunc = lookupIPv4.LookupIPv4
}
opt = dns.LookupIPv4
} else if h.config.DomainStrategy == Config_USE_IP6 || (localAddr != nil && localAddr.Family().IsIPv6()) {
if lookupIPv6, ok := h.dns.(dns.IPv6Lookup); ok {
lookupFunc = lookupIPv6.LookupIPv6
}
opt = dns.LookupIPv6
}
opt.FakeEnable = true
ips, err := lookupFunc(domain)
ips, err := h.dns.LookupOptions(domain, opt)
if err != nil {
newError("failed to get IP address for domain ", domain).Base(err).WriteToLog(session.ExportIDToError(ctx))
}