forked from AdguardTeam/AdGuardHome
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
all: imp external client restriction
- Loading branch information
1 parent
1208a31
commit 1dbacfc
Showing
13 changed files
with
446 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// This is not the best place for this functionality, but since we need to use | ||
// it in both rDNS (home) and dnsServer (dnsforward) we put it here. | ||
|
||
package aghnet | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/AdguardTeam/AdGuardHome/internal/agherr" | ||
"github.com/AdguardTeam/dnsproxy/upstream" | ||
"github.com/miekg/dns" | ||
) | ||
|
||
// LocalResolvers is used to perform exchanging PTR requests for addresses from | ||
// locally-served networks. | ||
// | ||
// TODO(e.burkov): Maybe expand with method like ExchangeParallel to be able to | ||
// use user's upstream mode settings. | ||
type LocalResolvers interface { | ||
Exchange(req *dns.Msg) (resp *dns.Msg, err error) | ||
} | ||
|
||
// localResolvers is the default implementation of LocalResolvers interface. | ||
type localResolvers struct { | ||
ups []upstream.Upstream | ||
} | ||
|
||
// NewLocalResolvers creates a LocalResolvers instance from passed local | ||
// resolvers addresses. It returns an error if any of addrs failed to become an | ||
// upstream. | ||
func NewLocalResolvers(addrs []string, timeout time.Duration) (lr LocalResolvers, err error) { | ||
defer agherr.Annotate("localResolvers: %w", &err) | ||
|
||
if len(addrs) == 0 { | ||
return &localResolvers{ups: nil}, nil | ||
} | ||
|
||
var ups []upstream.Upstream | ||
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 &localResolvers{ups: ups}, nil | ||
} | ||
|
||
// Exсhange performs a query to each resolver until first response. | ||
func (lr *localResolvers) Exchange(req *dns.Msg) (resp *dns.Msg, err error) { | ||
defer agherr.Annotate("localResolvers", &err) | ||
|
||
var errs []error | ||
for _, u := range lr.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...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 TestNewLocalResolvers(t *testing.T) { | ||
var lr LocalResolvers | ||
var err error | ||
|
||
t.Run("empty", func(t *testing.T) { | ||
lr, err = NewLocalResolvers([]string{}, 0) | ||
require.NoError(t, err) | ||
assert.NotNil(t, lr) | ||
}) | ||
|
||
t.Run("successful", func(t *testing.T) { | ||
lr, err = NewLocalResolvers([]string{"www.example.com"}, 0) | ||
require.NoError(t, err) | ||
assert.NotNil(t, lr) | ||
}) | ||
|
||
t.Run("unsuccessful", func(t *testing.T) { | ||
lr, err = NewLocalResolvers([]string{"tlss://www.example.com"}, 0) | ||
require.Error(t, err) | ||
assert.Nil(t, lr) | ||
}) | ||
} | ||
|
||
func TestLocalResolvers_Exchange(t *testing.T) { | ||
lr := &localResolvers{} | ||
|
||
t.Run("error", func(t *testing.T) { | ||
lr.ups = []upstream.Upstream{&aghtest.TestErrUpstream{}} | ||
|
||
resp, err := lr.Exchange(nil) | ||
require.Error(t, err) | ||
assert.Nil(t, resp) | ||
}) | ||
|
||
t.Run("all_right", func(t *testing.T) { | ||
lr.ups = []upstream.Upstream{&aghtest.TestUpstream{ | ||
Reverse: map[string][]string{ | ||
"abc": {"cba"}, | ||
}, | ||
}} | ||
|
||
resp, err := lr.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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package aghtest | ||
|
||
import ( | ||
"github.com/AdguardTeam/dnsproxy/upstream" | ||
"github.com/miekg/dns" | ||
) | ||
|
||
// LocalResolvers is an implementor aghnet.LocalResolvers interface to | ||
// simplify testing. | ||
type LocalResolvers struct { | ||
Ups upstream.Upstream | ||
} | ||
|
||
// Exchange implements aghnet.LocalResolvers interface for *LocalResolvers. | ||
func (lr *LocalResolvers) Exchange(req *dns.Msg) (resp *dns.Msg, err error) { | ||
if lr.Ups == nil { | ||
lr.Ups = &TestErrUpstream{} | ||
} | ||
|
||
return lr.Ups.Exchange(req) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.