From cad1e4e71662836d1dfc79bc2979599b7a29fea1 Mon Sep 17 00:00:00 2001 From: Eugene Burkov Date: Mon, 25 Sep 2023 19:17:53 +0300 Subject: [PATCH] dnsforward: imp code --- internal/dnsforward/http.go | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/internal/dnsforward/http.go b/internal/dnsforward/http.go index 5f431279b4a..70eaaca09e4 100644 --- a/internal/dnsforward/http.go +++ b/internal/dnsforward/http.go @@ -142,18 +142,9 @@ func (s *Server) getDNSConfig() (c *jsonDNSConfig) { upstreamMode = "parallel" } - defLocalPTRUps := []string{} - uc, err := s.prepareLocalUpstreamConfig(s.sysResolvers.Get(), nil, nil) + defPTRUps, err := s.defaultLocalPTRUpstreams() if err != nil { - log.Error("dnsforward: getting system upstream config: %s", err) - } else { - for _, u := range uc.Upstreams { - defLocalPTRUps = append(defLocalPTRUps, u.Address()) - } - - if err = uc.Close(); err != nil { - log.Error("dnsforward: closing system upstream config: %s", err) - } + log.Error("dnsforward: %s", err) } return &jsonDNSConfig{ @@ -180,11 +171,30 @@ func (s *Server) getDNSConfig() (c *jsonDNSConfig) { ResolveClients: &resolveClients, UsePrivateRDNS: &usePrivateRDNS, LocalPTRUpstreams: &localPTRUpstreams, - DefaultLocalPTRUpstreams: defLocalPTRUps, + DefaultLocalPTRUpstreams: defPTRUps, DisabledUntil: protectionDisabledUntil, } } +// defaultLocalPTRUpstreams returns the list of default local PTR resolvers +// filtered of AdGuard Home's own DNS server addresses. It may appear empty. +func (s *Server) defaultLocalPTRUpstreams() (ups []string, err error) { + s.serverLock.RLock() + defer s.serverLock.RUnlock() + + uc, err := s.prepareLocalUpstreamConfig(s.sysResolvers.Get(), nil, nil) + if err != nil { + return nil, fmt.Errorf("getting system upstream config: %w", err) + } + defer func() { err = errors.Join(err, uc.Close()) }() + + for _, u := range uc.Upstreams { + ups = append(ups, u.Address()) + } + + return ups, nil +} + // handleGetConfig handles requests to the GET /control/dns_info endpoint. func (s *Server) handleGetConfig(w http.ResponseWriter, r *http.Request) { resp := s.getDNSConfig()