2021-03-25 13:00:27 +00:00
|
|
|
package dnsforward
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
2021-03-31 12:00:47 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
|
2021-05-21 13:15:47 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
2021-03-25 13:00:27 +00:00
|
|
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
2021-03-31 12:00:47 +00:00
|
|
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
2022-03-18 10:37:27 +00:00
|
|
|
"github.com/AdguardTeam/golibs/netutil"
|
2021-03-25 13:00:27 +00:00
|
|
|
"github.com/miekg/dns"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2021-04-08 15:07:29 +00:00
|
|
|
func TestServer_ProcessDetermineLocal(t *testing.T) {
|
|
|
|
s := &Server{
|
2022-03-18 10:37:27 +00:00
|
|
|
privateNets: netutil.SubnetSetFunc(netutil.IsLocallyServed),
|
2021-04-08 15:07:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
2022-03-18 10:37:27 +00:00
|
|
|
want assert.BoolAssertionFunc
|
2021-04-08 15:07:29 +00:00
|
|
|
name string
|
|
|
|
cliIP net.IP
|
|
|
|
}{{
|
2022-03-18 10:37:27 +00:00
|
|
|
want: assert.True,
|
2021-04-08 15:07:29 +00:00
|
|
|
name: "local",
|
|
|
|
cliIP: net.IP{192, 168, 0, 1},
|
|
|
|
}, {
|
2022-03-18 10:37:27 +00:00
|
|
|
want: assert.False,
|
2021-04-08 15:07:29 +00:00
|
|
|
name: "external",
|
|
|
|
cliIP: net.IP{250, 249, 0, 1},
|
2022-03-18 10:37:27 +00:00
|
|
|
}, {
|
|
|
|
want: assert.False,
|
|
|
|
name: "invalid",
|
|
|
|
cliIP: net.IP{1, 2, 3, 4, 5},
|
|
|
|
}, {
|
|
|
|
want: assert.False,
|
|
|
|
name: "nil",
|
|
|
|
cliIP: nil,
|
2021-04-08 15:07:29 +00:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
proxyCtx := &proxy.DNSContext{
|
|
|
|
Addr: &net.TCPAddr{
|
|
|
|
IP: tc.cliIP,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
dctx := &dnsContext{
|
|
|
|
proxyCtx: proxyCtx,
|
|
|
|
}
|
|
|
|
s.processDetermineLocal(dctx)
|
|
|
|
|
2022-03-18 10:37:27 +00:00
|
|
|
tc.want(t, dctx.isLocalClient)
|
2021-04-08 15:07:29 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServer_ProcessInternalHosts_localRestriction(t *testing.T) {
|
2021-03-25 13:00:27 +00:00
|
|
|
knownIP := net.IP{1, 2, 3, 4}
|
2021-04-08 15:07:29 +00:00
|
|
|
|
2021-03-25 13:00:27 +00:00
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
host string
|
|
|
|
wantIP net.IP
|
|
|
|
wantRes resultCode
|
2021-04-08 15:07:29 +00:00
|
|
|
isLocalCli bool
|
2021-03-25 13:00:27 +00:00
|
|
|
}{{
|
2021-04-08 15:07:29 +00:00
|
|
|
name: "local_client_success",
|
2021-03-25 13:00:27 +00:00
|
|
|
host: "example.lan",
|
|
|
|
wantIP: knownIP,
|
|
|
|
wantRes: resultCodeSuccess,
|
2021-04-08 15:07:29 +00:00
|
|
|
isLocalCli: true,
|
2021-03-25 13:00:27 +00:00
|
|
|
}, {
|
2021-04-08 15:07:29 +00:00
|
|
|
name: "local_client_unknown_host",
|
|
|
|
host: "wronghost.lan",
|
2021-03-25 13:00:27 +00:00
|
|
|
wantIP: nil,
|
2021-04-08 15:07:29 +00:00
|
|
|
wantRes: resultCodeFinish,
|
|
|
|
isLocalCli: true,
|
2021-03-25 14:21:00 +00:00
|
|
|
}, {
|
2021-04-08 15:07:29 +00:00
|
|
|
name: "external_client_known_host",
|
2021-03-25 14:21:00 +00:00
|
|
|
host: "example.lan",
|
|
|
|
wantIP: nil,
|
2021-04-08 15:07:29 +00:00
|
|
|
wantRes: resultCodeFinish,
|
|
|
|
isLocalCli: false,
|
|
|
|
}, {
|
|
|
|
name: "external_client_unknown_host",
|
|
|
|
host: "wronghost.lan",
|
|
|
|
wantIP: nil,
|
|
|
|
wantRes: resultCodeFinish,
|
|
|
|
isLocalCli: false,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
s := &Server{
|
2021-04-29 13:00:07 +00:00
|
|
|
dhcpServer: &testDHCP{},
|
2021-04-15 16:00:31 +00:00
|
|
|
localDomainSuffix: defaultLocalDomainSuffix,
|
2021-04-15 14:52:53 +00:00
|
|
|
tableHostToIP: hostToIPTable{
|
2021-04-08 15:07:29 +00:00
|
|
|
"example": knownIP,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &dns.Msg{
|
|
|
|
MsgHdr: dns.MsgHdr{
|
|
|
|
Id: dns.Id(),
|
|
|
|
},
|
|
|
|
Question: []dns.Question{{
|
|
|
|
Name: dns.Fqdn(tc.host),
|
|
|
|
Qtype: dns.TypeA,
|
|
|
|
Qclass: dns.ClassINET,
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
dctx := &dnsContext{
|
|
|
|
proxyCtx: &proxy.DNSContext{
|
|
|
|
Req: req,
|
|
|
|
},
|
|
|
|
isLocalClient: tc.isLocalCli,
|
|
|
|
}
|
|
|
|
|
|
|
|
res := s.processInternalHosts(dctx)
|
|
|
|
require.Equal(t, tc.wantRes, res)
|
|
|
|
pctx := dctx.proxyCtx
|
|
|
|
if tc.wantRes == resultCodeFinish {
|
|
|
|
require.NotNil(t, pctx.Res)
|
|
|
|
|
|
|
|
assert.Equal(t, dns.RcodeNameError, pctx.Res.Rcode)
|
|
|
|
assert.Len(t, pctx.Res.Answer, 0)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if tc.wantIP == nil {
|
|
|
|
assert.Nil(t, pctx.Res)
|
|
|
|
} else {
|
|
|
|
require.NotNil(t, pctx.Res)
|
|
|
|
|
|
|
|
ans := pctx.Res.Answer
|
|
|
|
require.Len(t, ans, 1)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.wantIP, ans[0].(*dns.A).A)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServer_ProcessInternalHosts(t *testing.T) {
|
|
|
|
const (
|
|
|
|
examplecom = "example.com"
|
|
|
|
examplelan = "example.lan"
|
|
|
|
)
|
|
|
|
|
|
|
|
knownIP := net.IP{1, 2, 3, 4}
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
host string
|
|
|
|
suffix string
|
|
|
|
wantIP net.IP
|
|
|
|
wantRes resultCode
|
|
|
|
qtyp uint16
|
|
|
|
}{{
|
|
|
|
name: "success_external",
|
|
|
|
host: examplecom,
|
2021-04-15 16:00:31 +00:00
|
|
|
suffix: defaultLocalDomainSuffix,
|
2021-04-08 15:07:29 +00:00
|
|
|
wantIP: nil,
|
|
|
|
wantRes: resultCodeSuccess,
|
|
|
|
qtyp: dns.TypeA,
|
|
|
|
}, {
|
|
|
|
name: "success_external_non_a",
|
|
|
|
host: examplecom,
|
2021-04-15 16:00:31 +00:00
|
|
|
suffix: defaultLocalDomainSuffix,
|
2021-04-08 15:07:29 +00:00
|
|
|
wantIP: nil,
|
|
|
|
wantRes: resultCodeSuccess,
|
|
|
|
qtyp: dns.TypeCNAME,
|
|
|
|
}, {
|
|
|
|
name: "success_internal",
|
|
|
|
host: examplelan,
|
2021-04-15 16:00:31 +00:00
|
|
|
suffix: defaultLocalDomainSuffix,
|
2021-04-08 15:07:29 +00:00
|
|
|
wantIP: knownIP,
|
|
|
|
wantRes: resultCodeSuccess,
|
|
|
|
qtyp: dns.TypeA,
|
|
|
|
}, {
|
|
|
|
name: "success_internal_unknown",
|
|
|
|
host: "example-new.lan",
|
2021-04-15 16:00:31 +00:00
|
|
|
suffix: defaultLocalDomainSuffix,
|
2021-04-08 15:07:29 +00:00
|
|
|
wantIP: nil,
|
|
|
|
wantRes: resultCodeFinish,
|
|
|
|
qtyp: dns.TypeA,
|
|
|
|
}, {
|
|
|
|
name: "success_internal_aaaa",
|
|
|
|
host: examplelan,
|
2021-04-15 16:00:31 +00:00
|
|
|
suffix: defaultLocalDomainSuffix,
|
2021-04-08 15:07:29 +00:00
|
|
|
wantIP: nil,
|
|
|
|
wantRes: resultCodeSuccess,
|
|
|
|
qtyp: dns.TypeAAAA,
|
|
|
|
}, {
|
|
|
|
name: "success_custom_suffix",
|
|
|
|
host: "example.custom",
|
|
|
|
suffix: ".custom.",
|
|
|
|
wantIP: knownIP,
|
|
|
|
wantRes: resultCodeSuccess,
|
|
|
|
qtyp: dns.TypeA,
|
2021-03-25 13:00:27 +00:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
s := &Server{
|
2021-04-29 13:00:07 +00:00
|
|
|
dhcpServer: &testDHCP{},
|
2021-04-15 16:00:31 +00:00
|
|
|
localDomainSuffix: tc.suffix,
|
2021-04-15 14:52:53 +00:00
|
|
|
tableHostToIP: hostToIPTable{
|
2021-03-25 13:00:27 +00:00
|
|
|
"example": knownIP,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &dns.Msg{
|
|
|
|
MsgHdr: dns.MsgHdr{
|
|
|
|
Id: 1234,
|
|
|
|
},
|
|
|
|
Question: []dns.Question{{
|
|
|
|
Name: dns.Fqdn(tc.host),
|
|
|
|
Qtype: tc.qtyp,
|
|
|
|
Qclass: dns.ClassINET,
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
dctx := &dnsContext{
|
|
|
|
proxyCtx: &proxy.DNSContext{
|
|
|
|
Req: req,
|
|
|
|
},
|
2021-04-08 15:07:29 +00:00
|
|
|
isLocalClient: true,
|
2021-03-25 13:00:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res := s.processInternalHosts(dctx)
|
2021-04-08 15:07:29 +00:00
|
|
|
pctx := dctx.proxyCtx
|
2021-03-25 13:00:27 +00:00
|
|
|
assert.Equal(t, tc.wantRes, res)
|
2021-04-08 15:07:29 +00:00
|
|
|
if tc.wantRes == resultCodeFinish {
|
|
|
|
require.NotNil(t, pctx.Res)
|
|
|
|
assert.Equal(t, dns.RcodeNameError, pctx.Res.Rcode)
|
2021-03-25 13:00:27 +00:00
|
|
|
|
2021-04-08 15:07:29 +00:00
|
|
|
return
|
2021-03-25 13:00:27 +00:00
|
|
|
}
|
|
|
|
|
2021-04-08 15:07:29 +00:00
|
|
|
require.NoError(t, dctx.err)
|
|
|
|
|
2021-03-25 14:21:00 +00:00
|
|
|
if tc.qtyp == dns.TypeAAAA {
|
|
|
|
// TODO(a.garipov): Remove this special handling
|
|
|
|
// when we fully support AAAA.
|
|
|
|
require.NotNil(t, pctx.Res)
|
|
|
|
|
|
|
|
ans := pctx.Res.Answer
|
|
|
|
require.Len(t, ans, 0)
|
|
|
|
} else if tc.wantIP == nil {
|
2021-03-25 13:00:27 +00:00
|
|
|
assert.Nil(t, pctx.Res)
|
|
|
|
} else {
|
|
|
|
require.NotNil(t, pctx.Res)
|
|
|
|
|
|
|
|
ans := pctx.Res.Answer
|
|
|
|
require.Len(t, ans, 1)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.wantIP, ans[0].(*dns.A).A)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-03-31 12:00:47 +00:00
|
|
|
|
2021-05-26 14:55:19 +00:00
|
|
|
func TestServer_ProcessRestrictLocal(t *testing.T) {
|
2022-02-03 18:19:32 +00:00
|
|
|
ups := &aghtest.Upstream{
|
2021-03-31 12:00:47 +00:00
|
|
|
Reverse: map[string][]string{
|
|
|
|
"251.252.253.254.in-addr.arpa.": {"host1.example.net."},
|
|
|
|
"1.1.168.192.in-addr.arpa.": {"some.local-client."},
|
|
|
|
},
|
|
|
|
}
|
2021-05-21 13:15:47 +00:00
|
|
|
s := createTestServer(t, &filtering.Config{}, ServerConfig{
|
2021-04-09 18:01:21 +00:00
|
|
|
UDPListenAddrs: []*net.UDPAddr{{}},
|
|
|
|
TCPListenAddrs: []*net.TCPAddr{{}},
|
|
|
|
}, ups)
|
2021-03-31 12:00:47 +00:00
|
|
|
s.conf.UpstreamConfig.Upstreams = []upstream.Upstream{ups}
|
|
|
|
startDeferStop(t, s)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
want string
|
|
|
|
question net.IP
|
|
|
|
cliIP net.IP
|
|
|
|
wantLen int
|
|
|
|
}{{
|
|
|
|
name: "from_local_to_external",
|
|
|
|
want: "host1.example.net.",
|
|
|
|
question: net.IP{254, 253, 252, 251},
|
|
|
|
cliIP: net.IP{192, 168, 10, 10},
|
|
|
|
wantLen: 1,
|
|
|
|
}, {
|
|
|
|
name: "from_external_for_local",
|
|
|
|
want: "",
|
|
|
|
question: net.IP{192, 168, 1, 1},
|
|
|
|
cliIP: net.IP{254, 253, 252, 251},
|
|
|
|
wantLen: 0,
|
|
|
|
}, {
|
|
|
|
name: "from_local_for_local",
|
|
|
|
want: "some.local-client.",
|
|
|
|
question: net.IP{192, 168, 1, 1},
|
|
|
|
cliIP: net.IP{192, 168, 1, 2},
|
|
|
|
wantLen: 1,
|
|
|
|
}, {
|
|
|
|
name: "from_external_for_external",
|
|
|
|
want: "host1.example.net.",
|
|
|
|
question: net.IP{254, 253, 252, 251},
|
|
|
|
cliIP: net.IP{254, 253, 252, 255},
|
|
|
|
wantLen: 1,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
reqAddr, err := dns.ReverseAddr(tc.question.String())
|
|
|
|
require.NoError(t, err)
|
|
|
|
req := createTestMessageWithType(reqAddr, dns.TypePTR)
|
|
|
|
|
|
|
|
pctx := &proxy.DNSContext{
|
|
|
|
Proto: proxy.ProtoTCP,
|
|
|
|
Req: req,
|
|
|
|
Addr: &net.TCPAddr{
|
|
|
|
IP: tc.cliIP,
|
|
|
|
},
|
|
|
|
}
|
2021-05-26 14:55:19 +00:00
|
|
|
|
2021-03-31 12:00:47 +00:00
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
err = s.handleDNSRequest(nil, pctx)
|
2021-04-29 13:00:07 +00:00
|
|
|
require.NoError(t, err)
|
2021-03-31 12:00:47 +00:00
|
|
|
require.NotNil(t, pctx.Res)
|
|
|
|
require.Len(t, pctx.Res.Answer, tc.wantLen)
|
2021-05-26 14:55:19 +00:00
|
|
|
|
2021-03-31 12:00:47 +00:00
|
|
|
if tc.wantLen > 0 {
|
2021-05-31 17:11:06 +00:00
|
|
|
assert.Equal(t, tc.want, pctx.Res.Answer[0].(*dns.PTR).Ptr)
|
2021-03-31 12:00:47 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-05-26 14:55:19 +00:00
|
|
|
|
|
|
|
func TestServer_ProcessLocalPTR_usingResolvers(t *testing.T) {
|
|
|
|
const locDomain = "some.local."
|
|
|
|
const reqAddr = "1.1.168.192.in-addr.arpa."
|
|
|
|
|
|
|
|
s := createTestServer(t, &filtering.Config{}, ServerConfig{
|
|
|
|
UDPListenAddrs: []*net.UDPAddr{{}},
|
|
|
|
TCPListenAddrs: []*net.TCPAddr{{}},
|
2022-02-03 18:19:32 +00:00
|
|
|
}, &aghtest.Upstream{
|
2021-05-26 14:55:19 +00:00
|
|
|
Reverse: map[string][]string{
|
|
|
|
reqAddr: {locDomain},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
var proxyCtx *proxy.DNSContext
|
|
|
|
var dnsCtx *dnsContext
|
|
|
|
setup := func(use bool) {
|
|
|
|
proxyCtx = &proxy.DNSContext{
|
|
|
|
Addr: &net.TCPAddr{
|
|
|
|
IP: net.IP{127, 0, 0, 1},
|
|
|
|
},
|
|
|
|
Req: createTestMessageWithType(reqAddr, dns.TypePTR),
|
|
|
|
}
|
|
|
|
dnsCtx = &dnsContext{
|
|
|
|
proxyCtx: proxyCtx,
|
|
|
|
unreversedReqIP: net.IP{192, 168, 1, 1},
|
|
|
|
}
|
|
|
|
s.conf.UsePrivateRDNS = use
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("enabled", func(t *testing.T) {
|
|
|
|
setup(true)
|
|
|
|
|
|
|
|
rc := s.processLocalPTR(dnsCtx)
|
|
|
|
require.Equal(t, resultCodeSuccess, rc)
|
|
|
|
require.NotEmpty(t, proxyCtx.Res.Answer)
|
|
|
|
|
2021-05-31 17:11:06 +00:00
|
|
|
assert.Equal(t, locDomain, proxyCtx.Res.Answer[0].(*dns.PTR).Ptr)
|
2021-05-26 14:55:19 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("disabled", func(t *testing.T) {
|
|
|
|
setup(false)
|
|
|
|
|
|
|
|
rc := s.processLocalPTR(dnsCtx)
|
|
|
|
require.Equal(t, resultCodeFinish, rc)
|
|
|
|
require.Empty(t, proxyCtx.Res.Answer)
|
|
|
|
})
|
|
|
|
}
|
2021-05-28 10:02:59 +00:00
|
|
|
|
|
|
|
func TestIPStringFromAddr(t *testing.T) {
|
|
|
|
t.Run("not_nil", func(t *testing.T) {
|
|
|
|
addr := net.UDPAddr{
|
|
|
|
IP: net.ParseIP("1:2:3::4"),
|
|
|
|
Port: 12345,
|
|
|
|
Zone: "eth0",
|
|
|
|
}
|
|
|
|
assert.Equal(t, ipStringFromAddr(&addr), addr.IP.String())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("nil", func(t *testing.T) {
|
|
|
|
assert.Empty(t, ipStringFromAddr(nil))
|
|
|
|
})
|
|
|
|
}
|