Skip to content

Commit

Permalink
Pull request: 5044 Close upstreams
Browse files Browse the repository at this point in the history
Merge in DNS/adguard-home from 5044-close-upstreams to master

Closes #5044.

Squashed commit of the following:

commit e121380
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Wed Oct 19 15:54:17 2022 +0300

    all: upd proxy again

commit ce7fa53
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Wed Oct 19 14:30:46 2022 +0300

    home: imp docs, names

commit 851c5b8
Merge: b9ee5d6 d2a09e4
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Wed Oct 19 14:21:44 2022 +0300

    Merge branch 'master' into 5044-close-upstreams

commit b9ee5d6
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Wed Oct 19 14:20:15 2022 +0300

    all: close upstreams more

commit eaca476
Merge: f924bc7 8dba4ec
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Oct 18 18:33:53 2022 +0300

    Merge branch 'master' into 5044-close-upstreams

commit f924bc7
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Oct 18 18:23:54 2022 +0300

    all: imp code, docs

commit 011fde1
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Oct 18 17:26:40 2022 +0300

    all: upd dnsproxy
  • Loading branch information
EugeneOne1 committed Oct 19, 2022
1 parent d2a09e4 commit 2de4228
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 29 deletions.
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/AdguardTeam/AdGuardHome
go 1.18

require (
github.com/AdguardTeam/dnsproxy v0.45.3
github.com/AdguardTeam/dnsproxy v0.46.1
github.com/AdguardTeam/golibs v0.10.9
github.com/AdguardTeam/urlfilter v0.16.0
github.com/NYTimes/gziphandler v1.1.1
Expand All @@ -18,7 +18,7 @@ require (
github.com/google/uuid v1.3.0
github.com/insomniacslk/dhcp v0.0.0-20220822114210-de18a9d48e84
github.com/kardianos/service v1.2.1
github.com/lucas-clemente/quic-go v0.29.1
github.com/lucas-clemente/quic-go v0.29.2
github.com/mdlayher/ethernet v0.0.0-20220221185849-529eae5b6118
github.com/mdlayher/netlink v1.6.0
// TODO(a.garipov): This package is deprecated; find a new one or use
Expand Down Expand Up @@ -49,8 +49,8 @@ require (
github.com/golang/mock v1.6.0 // indirect
github.com/josharian/native v1.0.0 // indirect
github.com/marten-seemann/qpack v0.2.1 // indirect
github.com/marten-seemann/qtls-go1-18 v0.1.2 // indirect
github.com/marten-seemann/qtls-go1-19 v0.1.0 // indirect
github.com/marten-seemann/qtls-go1-18 v0.1.3 // indirect
github.com/marten-seemann/qtls-go1-19 v0.1.1 // indirect
github.com/mdlayher/packet v1.0.0 // indirect
github.com/mdlayher/socket v0.2.3 // indirect
github.com/nxadm/tail v1.4.8 // indirect
Expand Down
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/AdguardTeam/dnsproxy v0.45.3 h1:lvJlifDIVjHFVkVcieBhXyQA357Wl+vmLxeDlaQ8DE8=
github.com/AdguardTeam/dnsproxy v0.45.3/go.mod h1:h+0r4GDvHHY2Wu6r7oqva+O37h00KofYysfzy1TEXFE=
github.com/AdguardTeam/dnsproxy v0.46.1 h1:ej9iRorG+vekaXGYB854waAiS+q8OfswYZ1MQRZolHk=
github.com/AdguardTeam/dnsproxy v0.46.1/go.mod h1:PAmRzFqls0E92XTglyY2ESAqMAzZJhHKErG1ZpRnpjA=
github.com/AdguardTeam/golibs v0.4.0/go.mod h1:skKsDKIBB7kkFflLJBpfGX+G8QFTx0WKUzB6TIgtUj4=
github.com/AdguardTeam/golibs v0.10.4/go.mod h1:rSfQRGHIdgfxriDDNgNJ7HmE5zRoURq8R+VdR81Zuzw=
github.com/AdguardTeam/golibs v0.10.9 h1:F9oP2da0dQ9RQDM1lGR7LxUTfUWu8hEFOs4icwAkKM0=
Expand Down Expand Up @@ -90,14 +90,14 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lucas-clemente/quic-go v0.29.1 h1:Z+WMJ++qMLhvpFkRZA+jl3BTxUjm415YBmWanXB8zP0=
github.com/lucas-clemente/quic-go v0.29.1/go.mod h1:CTcNfLYJS2UuRNB+zcNlgvkjBhxX6Hm3WUxxAQx2mgE=
github.com/lucas-clemente/quic-go v0.29.2 h1:O8Mt0O6LpvEW+wfC40vZdcw0DngwYzoxq5xULZNzSI8=
github.com/lucas-clemente/quic-go v0.29.2/go.mod h1:g6/h9YMmLuU54tL1gW25uIi3VlBp3uv+sBihplIuskE=
github.com/marten-seemann/qpack v0.2.1 h1:jvTsT/HpCn2UZJdP+UUB53FfUUgeOyG5K1ns0OJOGVs=
github.com/marten-seemann/qpack v0.2.1/go.mod h1:F7Gl5L1jIgN1D11ucXefiuJS9UMVP2opoCp2jDKb7wc=
github.com/marten-seemann/qtls-go1-18 v0.1.2 h1:JH6jmzbduz0ITVQ7ShevK10Av5+jBEKAHMntXmIV7kM=
github.com/marten-seemann/qtls-go1-18 v0.1.2/go.mod h1:mJttiymBAByA49mhlNZZGrH5u1uXYZJ+RW28Py7f4m4=
github.com/marten-seemann/qtls-go1-19 v0.1.0 h1:rLFKD/9mp/uq1SYGYuVZhm83wkmU95pK5df3GufyYYU=
github.com/marten-seemann/qtls-go1-19 v0.1.0/go.mod h1:5HTDWtVudo/WFsHKRNuOhWlbdjrfs5JHrYb0wIJqGpI=
github.com/marten-seemann/qtls-go1-18 v0.1.3 h1:R4H2Ks8P6pAtUagjFty2p7BVHn3XiwDAl7TTQf5h7TI=
github.com/marten-seemann/qtls-go1-18 v0.1.3/go.mod h1:mJttiymBAByA49mhlNZZGrH5u1uXYZJ+RW28Py7f4m4=
github.com/marten-seemann/qtls-go1-19 v0.1.1 h1:mnbxeq3oEyQxQXwI4ReCgW9DPoPR94sNlqWoDZnjRIE=
github.com/marten-seemann/qtls-go1-19 v0.1.1/go.mod h1:5HTDWtVudo/WFsHKRNuOhWlbdjrfs5JHrYb0wIJqGpI=
github.com/mdlayher/ethernet v0.0.0-20190606142754-0394541c37b7/go.mod h1:U6ZQobyTjI/tJyq2HG+i/dfSoFUt8/aZCM+GKtmFk/Y=
github.com/mdlayher/ethernet v0.0.0-20220221185849-529eae5b6118 h1:2oDp6OOhLxQ9JBoUuysVz9UZ9uI6oLUbvAZu0x8o+vE=
github.com/mdlayher/ethernet v0.0.0-20220221185849-529eae5b6118/go.mod h1:ZFUnHIVchZ9lJoWoEGUg8Q3M4U8aNNWA3CVSUTkW4og=
Expand Down
6 changes: 6 additions & 0 deletions internal/aghtest/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ var _ upstream.Upstream = (*UpstreamMock)(nil)
type UpstreamMock struct {
OnAddress func() (addr string)
OnExchange func(req *dns.Msg) (resp *dns.Msg, err error)
OnClose func() (err error)
}

// Address implements the [upstream.Upstream] interface for *UpstreamMock.
Expand All @@ -173,3 +174,8 @@ func (u *UpstreamMock) Address() (addr string) {
func (u *UpstreamMock) Exchange(req *dns.Msg) (resp *dns.Msg, err error) {
return u.OnExchange(req)
}

// Close implements the [upstream.Upstream] interface for *UpstreamMock.
func (u *UpstreamMock) Close() (err error) {
return u.OnClose()
}
12 changes: 10 additions & 2 deletions internal/aghtest/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"testing"

"github.com/AdguardTeam/dnsproxy/upstream"
"github.com/AdguardTeam/golibs/errors"
"github.com/miekg/dns"
"github.com/stretchr/testify/require"
Expand All @@ -31,6 +32,8 @@ type Upstream struct {
Addr string
}

var _ upstream.Upstream = (*Upstream)(nil)

// RespondTo returns a response with answer if req has class cl, question type
// qt, and target targ.
func RespondTo(t testing.TB, req *dns.Msg, cl, qt uint16, targ, answer string) (resp *dns.Msg) {
Expand Down Expand Up @@ -68,7 +71,7 @@ func RespondTo(t testing.TB, req *dns.Msg, cl, qt uint16, targ, answer string) (
return resp
}

// Exchange implements the upstream.Upstream interface for *Upstream.
// Exchange implements the [upstream.Upstream] interface for *Upstream.
//
// TODO(a.garipov): Split further into handlers.
func (u *Upstream) Exchange(m *dns.Msg) (resp *dns.Msg, err error) {
Expand Down Expand Up @@ -114,11 +117,16 @@ func (u *Upstream) Exchange(m *dns.Msg) (resp *dns.Msg, err error) {
return resp, nil
}

// Address implements upstream.Upstream interface for *Upstream.
// Address implements [upstream.Upstream] interface for *Upstream.
func (u *Upstream) Address() string {
return u.Addr
}

// Close implements [upstream.Upstream] interface for *Upstream.
func (u *Upstream) Close() (err error) {
return nil
}

// NewBlockUpstream returns an [*UpstreamMock] that works like an upstream that
// supports hash-based safe-browsing/adult-blocking feature. If shouldBlock is
// true, hostname's actual hash is returned, blocking it. Otherwise, it returns
Expand Down
33 changes: 27 additions & 6 deletions internal/dnsforward/dnsforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ func validateBlockingMode(mode BlockingMode, blockingIPv4, blockingIPv6 net.IP)
}

// prepareInternalProxy initializes the DNS proxy that is used for internal DNS
// queries, such at client PTR resolving and updater hostname resolving.
// queries, such as public clients PTR resolving and updater hostname resolving.
func (s *Server) prepareInternalProxy() (err error) {
conf := &proxy.Config{
CacheEnabled: true,
Expand Down Expand Up @@ -558,16 +558,37 @@ func (s *Server) Stop() error {
return s.stopLocked()
}

// stopLocked stops the DNS server without locking. For internal use only.
func (s *Server) stopLocked() error {
// stopLocked stops the DNS server without locking. For internal use only.
func (s *Server) stopLocked() (err error) {
var errs []error

if s.dnsProxy != nil {
err := s.dnsProxy.Stop()
err = s.dnsProxy.Stop()
if err != nil {
errs = append(errs, fmt.Errorf("could not stop primary resolvers properly: %w", err))
}
}

if s.internalProxy != nil && s.internalProxy.UpstreamConfig != nil {
err = s.internalProxy.UpstreamConfig.Close()
if err != nil {
errs = append(errs, fmt.Errorf("could not stop internal resolvers properly: %w", err))
}
}

if s.localResolvers != nil && s.localResolvers.UpstreamConfig != nil {
err = s.localResolvers.UpstreamConfig.Close()
if err != nil {
return fmt.Errorf("could not stop the DNS server properly: %w", err)
errs = append(errs, fmt.Errorf("could not stop local resolvers properly: %w", err))
}
}

s.isRunning = false
if len(errs) > 0 {
return errors.List("stopping DNS server", errs...)
} else {
s.isRunning = false
}

return nil
}

Expand Down
1 change: 1 addition & 0 deletions internal/dnsforward/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,7 @@ func checkDNS(
if err != nil {
return fmt.Errorf("failed to choose upstream for %q: %w", upstreamAddr, err)
}
defer func() { err = errors.WithDeferred(err, u.Close()) }()

if err = healthCheck(u); err != nil {
err = fmt.Errorf("upstream %q fails to exchange: %w", upstreamAddr, err)
Expand Down
50 changes: 46 additions & 4 deletions internal/home/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/golibs/netutil"
"github.com/AdguardTeam/golibs/stringutil"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
)

const clientsUpdatePeriod = 10 * time.Minute
Expand Down Expand Up @@ -50,6 +52,18 @@ type Client struct {
UseOwnBlockedServices bool
}

// closeUpstreams closes the client-specific upstream config of c if any.
func (c *Client) closeUpstreams() (err error) {
if c.upstreamConfig != nil {
err = c.upstreamConfig.Close()
if err != nil {
return fmt.Errorf("closing upstreams of client %q: %w", c.Name, err)
}
}

return nil
}

type clientSource uint

// Client sources. The order determines the priority.
Expand Down Expand Up @@ -651,6 +665,10 @@ func (clients *clientsContainer) Del(name string) (ok bool) {
return false
}

if err := c.closeUpstreams(); err != nil {
log.Error("client container: removing client %s: %s", name, err)
}

// update Name index
delete(clients.list, name)

Expand Down Expand Up @@ -709,7 +727,7 @@ func (clients *clientsContainer) Update(name string, c *Client) (err error) {
}
}

// update ID index
// Update ID index.
for _, id := range prev.IDs {
delete(clients.idIndex, id)
}
Expand All @@ -718,14 +736,17 @@ func (clients *clientsContainer) Update(name string, c *Client) (err error) {
}
}

// update Name index
// Update name index.
if prev.Name != c.Name {
delete(clients.list, prev.Name)
clients.list[c.Name] = prev
}

// update upstreams cache
c.upstreamConfig = nil
// Update upstreams cache.
err = c.closeUpstreams()
if err != nil {
return err
}

*prev = *c

Expand Down Expand Up @@ -910,3 +931,24 @@ func (clients *clientsContainer) updateFromDHCP(add bool) {

log.Debug("clients: added %d client aliases from dhcp", n)
}

// Close gracefully closes all the client-specific upstream configurations of
// the persistent clients.
func (clients *clientsContainer) Close() (err error) {
persistent := maps.Values(clients.list)
slices.SortFunc(persistent, func(a, b *Client) (less bool) { return a.Name < b.Name })

var errs []error

for _, cli := range persistent {
if err = cli.closeUpstreams(); err != nil {
errs = append(errs, err)
}
}

if len(errs) > 0 {
return errors.List("closing client specific upstreams", errs...)
}

return nil
}
1 change: 1 addition & 0 deletions internal/home/clientshttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ func (clients *clientsContainer) handleDelClient(w http.ResponseWriter, r *http.

if !clients.Del(cj.Name) {
aghhttp.Error(r, w, http.StatusBadRequest, "Client not found")

return
}

Expand Down
12 changes: 9 additions & 3 deletions internal/home/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,17 +431,23 @@ func reconfigureDNSServer() (err error) {
return nil
}

func stopDNSServer() error {
func stopDNSServer() (err error) {
if !isRunning() {
return nil
}

err := Context.dnsServer.Stop()
err = Context.dnsServer.Stop()
if err != nil {
return fmt.Errorf("couldn't stop forwarding DNS server: %w", err)
return fmt.Errorf("stopping forwarding dns server: %w", err)
}

err = Context.clients.Close()
if err != nil {
return fmt.Errorf("closing clients container: %w", err)
}

closeDNSServer()

return nil
}

Expand Down
1 change: 0 additions & 1 deletion internal/home/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ func Main(clientBuildFS fs.FS) {
case syscall.SIGHUP:
Context.clients.Reload()
Context.tls.reload()

default:
cleanup(context.Background())
cleanupAlways()
Expand Down
8 changes: 7 additions & 1 deletion internal/next/dnssvc/dnssvc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
"github.com/AdguardTeam/AdGuardHome/internal/next/dnssvc"
"github.com/AdguardTeam/dnsproxy/upstream"
"github.com/AdguardTeam/golibs/errors"
"github.com/miekg/dns"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -25,6 +26,8 @@ func TestService(t *testing.T) {
const (
bootstrapAddr = "bootstrap.example"
upstreamAddr = "upstream.example"

closeErr errors.Error = "closing failed"
)

ups := &aghtest.UpstreamMock{
Expand All @@ -36,6 +39,9 @@ func TestService(t *testing.T) {

return resp, nil
},
OnClose: func() (err error) {
return closeErr
},
}

c := &dnssvc.Config{
Expand Down Expand Up @@ -85,5 +91,5 @@ func TestService(t *testing.T) {
defer cancel()

err = svc.Shutdown(ctx)
require.NoError(t, err)
require.ErrorIs(t, err, closeErr)
}

0 comments on commit 2de4228

Please sign in to comment.