Skip to content

Commit

Permalink
Pull request: 2704 local addresses vol.2
Browse files Browse the repository at this point in the history
Merge in DNS/adguard-home from 2704-local-addresses-vol.2 to master

Updates AdguardTeam#2704.
Updates AdguardTeam#2829.

Squashed commit of the following:

commit 507d038
Author: Eugene Burkov <[email protected]>
Date:   Wed Mar 31 14:33:05 2021 +0300

    aghtest: fix file name

commit 8e19f99
Author: Eugene Burkov <[email protected]>
Date:   Wed Mar 31 14:06:43 2021 +0300

    aghnet: rm redundant mutexes

commit 361fa41
Author: Eugene Burkov <[email protected]>
Date:   Wed Mar 31 13:45:30 2021 +0300

    all: fix names, docs

commit 14034f4
Merge: 35e265c a72ce1c
Author: Eugene Burkov <[email protected]>
Date:   Wed Mar 31 13:38:15 2021 +0300

    Merge branch 'master' into 2704-local-addresses-vol.2

commit 35e265c
Author: Eugene Burkov <[email protected]>
Date:   Wed Mar 31 13:33:35 2021 +0300

    aghnet: imp naming

commit 7a7edac
Author: Eugene Burkov <[email protected]>
Date:   Tue Mar 30 20:59:54 2021 +0300

    changelog: oops, nope yet

commit d26a5d2
Author: Eugene Burkov <[email protected]>
Date:   Tue Mar 30 20:55:53 2021 +0300

    all: some renaming for the glory of semantics

commit 9937fa6
Author: Eugene Burkov <[email protected]>
Date:   Mon Mar 29 15:34:42 2021 +0300

    all: log changes

commit d8d9e6d
Author: Eugene Burkov <[email protected]>
Date:   Fri Mar 26 18:32:23 2021 +0300

    all: imp localresolver, imp cutting off own addresses

commit 344140d
Author: Eugene Burkov <[email protected]>
Date:   Fri Mar 26 14:53:33 2021 +0300

    all: imp code quality

commit 1c5c0ba
Author: Eugene Burkov <[email protected]>
Date:   Thu Mar 25 20:44:08 2021 +0300

    all: fix go.mod

commit 0b9fb3c
Author: Eugene Burkov <[email protected]>
Date:   Thu Mar 25 20:38:51 2021 +0300

    all: add error handling

commit a7a2e51
Merge: c13be63 27f4f05
Author: Eugene Burkov <[email protected]>
Date:   Thu Mar 25 19:48:36 2021 +0300

    Merge branch 'master' into 2704-local-addresses-vol.2

commit c13be63
Author: Eugene Burkov <[email protected]>
Date:   Thu Mar 25 18:52:28 2021 +0300

    all: cover rdns with tests, imp aghnet functionality

commit 48bed90
Author: Eugene Burkov <[email protected]>
Date:   Wed Mar 24 20:18:07 2021 +0300

    home: make rdns great again

commit 1dbacfc
Author: Eugene Burkov <[email protected]>
Date:   Wed Mar 24 16:07:52 2021 +0300

    all: imp external client restriction

commit 1208a31
Author: Eugene Burkov <[email protected]>
Date:   Mon Mar 22 15:26:45 2021 +0300

    all: finish local ptr processor

commit c8827fc
Author: Eugene Burkov <[email protected]>
Date:   Tue Mar 2 13:41:22 2021 +0300

    all: imp ipdetector, add local ptr processor
  • Loading branch information
EugeneOne1 authored and heyxkhoa committed Mar 17, 2023
1 parent b5c949a commit 2a28f10
Show file tree
Hide file tree
Showing 30 changed files with 1,416 additions and 358 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ and this project adheres to

### Changed

- The reverse lookup for local addresses is now performed via local resolvers
([#2704]).
- Stricter validation of the IP addresses of static leases in the DHCP server
with regards to the netmask ([#2838]).
- Stricter validation of `$dnsrewrite` filter modifier parameters ([#2498]).
Expand Down Expand Up @@ -50,6 +52,7 @@ and this project adheres to
[#2498]: https://github.com/AdguardTeam/AdGuardHome/issues/2498
[#2533]: https://github.com/AdguardTeam/AdGuardHome/issues/2533
[#2541]: https://github.com/AdguardTeam/AdGuardHome/issues/2541
[#2704]: https://github.com/AdguardTeam/AdGuardHome/issues/2704
[#2828]: https://github.com/AdguardTeam/AdGuardHome/issues/2828
[#2835]: https://github.com/AdguardTeam/AdGuardHome/issues/2835
[#2838]: https://github.com/AdguardTeam/AdGuardHome/issues/2838
Expand Down
4 changes: 2 additions & 2 deletions HACKING.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ on GitHub and most other Markdown renderers. -->
### <a id="formatting" href="#formatting">Formatting</a>
* Add an empty line before `break`, `continue`, `fallthrough`, and `return`,
unless it's the only statement in that block.
* Decorate `break`, `continue`, `fallthrough`, `return`, and other function
exit points with empty lines unless it's the only statement in that block.
* Use `gofumpt --extra -s`.
Expand Down
79 changes: 79 additions & 0 deletions internal/aghnet/exchanger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package aghnet

import (
"time"

"github.com/AdguardTeam/AdGuardHome/internal/agherr"
"github.com/AdguardTeam/dnsproxy/upstream"
"github.com/miekg/dns"
)

// This package is not the best place for this functionality, but we put it here
// since we need to use it in both rDNS (home) and dnsServer (dnsforward).

// NoUpstreamsErr should be returned when there are no upstreams inside
// Exchanger implementation.
const NoUpstreamsErr agherr.Error = "no upstreams specified"

// Exchanger represents an object able to resolve DNS messages.
//
// TODO(e.burkov): Maybe expand with method like ExchangeParallel to be able to
// use user's upstream mode settings. Also, think about Update method to
// refresh the internal state.
type Exchanger interface {
Exchange(req *dns.Msg) (resp *dns.Msg, err error)
}

// multiAddrExchanger is the default implementation of Exchanger interface.
type multiAddrExchanger struct {
ups []upstream.Upstream
}

// NewMultiAddrExchanger creates an Exchanger instance from passed addresses.
// It returns an error if any of addrs failed to become an upstream.
func NewMultiAddrExchanger(addrs []string, timeout time.Duration) (e Exchanger, err error) {
defer agherr.Annotate("exchanger: %w", &err)

if len(addrs) == 0 {
return &multiAddrExchanger{}, nil
}

var ups []upstream.Upstream = make([]upstream.Upstream, 0, len(addrs))
for _, addr := range addrs {
var u upstream.Upstream
u, err = upstream.AddressToUpstream(addr, upstream.Options{Timeout: timeout})
if err != nil {
return nil, err
}

ups = append(ups, u)
}

return &multiAddrExchanger{ups: ups}, nil
}

// Exсhange performs a query to each resolver until first response.
func (e *multiAddrExchanger) Exchange(req *dns.Msg) (resp *dns.Msg, err error) {
defer agherr.Annotate("exchanger: %w", &err)

// TODO(e.burkov): Maybe prohibit the initialization without upstreams.
if len(e.ups) == 0 {
return nil, NoUpstreamsErr
}

var errs []error
for _, u := range e.ups {
resp, err = u.Exchange(req)
if err != nil {
errs = append(errs, err)

continue
}

if resp != nil {
return resp, nil
}
}

return nil, agherr.Many("can't exchange", errs...)
}
64 changes: 64 additions & 0 deletions internal/aghnet/exchanger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package aghnet

import (
"testing"

"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
"github.com/AdguardTeam/dnsproxy/upstream"
"github.com/miekg/dns"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestNewMultiAddrExchanger(t *testing.T) {
var e Exchanger
var err error

t.Run("empty", func(t *testing.T) {
e, err = NewMultiAddrExchanger([]string{}, 0)
require.NoError(t, err)
assert.NotNil(t, e)
})

t.Run("successful", func(t *testing.T) {
e, err = NewMultiAddrExchanger([]string{"www.example.com"}, 0)
require.NoError(t, err)
assert.NotNil(t, e)
})

t.Run("unsuccessful", func(t *testing.T) {
e, err = NewMultiAddrExchanger([]string{"invalid-proto://www.example.com"}, 0)
require.Error(t, err)
assert.Nil(t, e)
})
}

func TestMultiAddrExchanger_Exchange(t *testing.T) {
e := &multiAddrExchanger{}

t.Run("error", func(t *testing.T) {
e.ups = []upstream.Upstream{&aghtest.TestErrUpstream{}}

resp, err := e.Exchange(nil)
require.Error(t, err)
assert.Nil(t, resp)
})

t.Run("success", func(t *testing.T) {
e.ups = []upstream.Upstream{&aghtest.TestUpstream{
Reverse: map[string][]string{
"abc": {"cba"},
},
}}

resp, err := e.Exchange(&dns.Msg{
Question: []dns.Question{{
Name: "abc",
Qtype: dns.TypePTR,
}},
})
require.NoError(t, err)
require.Len(t, resp.Answer, 1)
assert.Equal(t, "cba", resp.Answer[0].Header().Name)
})
}
73 changes: 0 additions & 73 deletions internal/aghnet/ipdetector.go

This file was deleted.

Loading

0 comments on commit 2a28f10

Please sign in to comment.