diff --git a/app/dns/nameserver.go b/app/dns/nameserver.go index 8738885c8..7ba3a798f 100644 --- a/app/dns/nameserver.go +++ b/app/dns/nameserver.go @@ -68,8 +68,10 @@ func NewServer(ctx context.Context, dest net.Destination, dispatcher routing.Dis return NewTCPNameServer(u, dispatcher, disableCache, serveStale, serveExpiredTTL, clientIP) case strings.EqualFold(u.Scheme, "tcp+local"): // DNS-over-TCP Local mode return NewTCPLocalNameServer(u, disableCache, serveStale, serveExpiredTTL, clientIP) + case strings.EqualFold(u.Scheme, "udp"): // UDP DNS Remote mode + return NewUDPNameServer(u, dispatcher, false, disableCache, serveStale, serveExpiredTTL, clientIP) case strings.EqualFold(u.Scheme, "udp+local"): // UDP DNS Local mode - return NewUDPLocalNameServer(u, dispatcher, disableCache, serveStale, serveExpiredTTL, clientIP) + return NewUDPNameServer(u, dispatcher, true, disableCache, serveStale, serveExpiredTTL, clientIP) case strings.EqualFold(u.String(), "fakedns"): var fd dns.FakeDNSEngine err = core.RequireFeatures(ctx, func(fdns dns.FakeDNSEngine) { diff --git a/app/dns/nameserver_udp.go b/app/dns/nameserver_udp.go index c6b3b5094..4d303a735 100644 --- a/app/dns/nameserver_udp.go +++ b/app/dns/nameserver_udp.go @@ -64,10 +64,10 @@ func NewClassicNameServer(address net.Destination, dispatcher routing.Dispatcher return s } -// NewUDPLocalNameServer creates a UDP DNS client for local resolving. -func NewUDPLocalNameServer(url *url.URL, dispatcher routing.Dispatcher, disableCache bool, serveStale bool, serveExpiredTTL uint32, clientIP net.IP) (*ClassicNameServer, error) { +// NewUDPNameServer creates a UDP DNS client from a URL. +func NewUDPNameServer(url *url.URL, dispatcher routing.Dispatcher, local bool, disableCache bool, serveStale bool, serveExpiredTTL uint32, clientIP net.IP) (*ClassicNameServer, error) { if url.Hostname() == "" { - return nil, errors.New("missing UDP local nameserver host") + return nil, errors.New("missing UDP nameserver host") } port := net.Port(53) if url.Port() != "" { @@ -76,8 +76,13 @@ func NewUDPLocalNameServer(url *url.URL, dispatcher routing.Dispatcher, disableC return nil, err } } - s := NewClassicNameServer(net.UDPDestination(net.ParseAddress(url.Hostname()), port), &localUDPDispatcher{dispatcher}, disableCache, serveStale, serveExpiredTTL, clientIP) - s.cacheController.name = "UDPL//" + s.address.NetAddr() + if local { + dispatcher = &localUDPDispatcher{dispatcher} + } + s := NewClassicNameServer(net.UDPDestination(net.ParseAddress(url.Hostname()), port), dispatcher, disableCache, serveStale, serveExpiredTTL, clientIP) + if local { + s.cacheController.name = "UDPL//" + s.address.NetAddr() + } return s, nil }