2021-03-25 16:00:27 +03:00
|
|
|
package dnsforward
|
|
|
|
|
|
|
|
import (
|
2024-04-04 15:52:39 +03:00
|
|
|
"cmp"
|
2021-03-25 16:00:27 +03:00
|
|
|
"net"
|
2022-10-26 21:12:51 +03:00
|
|
|
"net/netip"
|
2021-03-25 16:00:27 +03:00
|
|
|
"testing"
|
|
|
|
|
2021-03-31 15:00:47 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
|
2021-05-21 16:15:47 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
2021-03-25 16:00:27 +03:00
|
|
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
2024-04-11 14:03:37 +03:00
|
|
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
2024-07-10 15:18:46 +03:00
|
|
|
"github.com/AdguardTeam/golibs/logutil/slogutil"
|
2022-03-18 13:37:27 +03:00
|
|
|
"github.com/AdguardTeam/golibs/netutil"
|
2022-10-26 21:12:51 +03:00
|
|
|
"github.com/AdguardTeam/golibs/testutil"
|
2023-07-18 17:02:07 +03:00
|
|
|
"github.com/AdguardTeam/urlfilter/rules"
|
2021-03-25 16:00:27 +03:00
|
|
|
"github.com/miekg/dns"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-05-30 16:04:28 +03:00
|
|
|
const (
|
|
|
|
ddrTestDomainName = "dns.example.net"
|
|
|
|
ddrTestFQDN = ddrTestDomainName + "."
|
|
|
|
)
|
2022-04-27 11:39:48 +03:00
|
|
|
|
2023-07-18 17:02:07 +03:00
|
|
|
func TestServer_ProcessInitial(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
target string
|
|
|
|
wantRCode rules.RCode
|
|
|
|
qType rules.RRType
|
|
|
|
aaaaDisabled bool
|
|
|
|
wantRC resultCode
|
|
|
|
}{{
|
|
|
|
name: "success",
|
|
|
|
target: testQuestionTarget,
|
|
|
|
wantRCode: -1,
|
|
|
|
qType: dns.TypeA,
|
|
|
|
aaaaDisabled: false,
|
|
|
|
wantRC: resultCodeSuccess,
|
|
|
|
}, {
|
|
|
|
name: "aaaa_disabled",
|
|
|
|
target: testQuestionTarget,
|
|
|
|
wantRCode: dns.RcodeSuccess,
|
|
|
|
qType: dns.TypeAAAA,
|
|
|
|
aaaaDisabled: true,
|
|
|
|
wantRC: resultCodeFinish,
|
|
|
|
}, {
|
|
|
|
name: "aaaa_disabled_a",
|
|
|
|
target: testQuestionTarget,
|
|
|
|
wantRCode: -1,
|
|
|
|
qType: dns.TypeA,
|
|
|
|
aaaaDisabled: true,
|
|
|
|
wantRC: resultCodeSuccess,
|
|
|
|
}, {
|
|
|
|
name: "mozilla_canary",
|
|
|
|
target: mozillaFQDN,
|
|
|
|
wantRCode: dns.RcodeNameError,
|
|
|
|
qType: dns.TypeA,
|
|
|
|
aaaaDisabled: false,
|
|
|
|
wantRC: resultCodeFinish,
|
|
|
|
}, {
|
|
|
|
name: "adguardhome_healthcheck",
|
|
|
|
target: healthcheckFQDN,
|
|
|
|
wantRCode: dns.RcodeSuccess,
|
|
|
|
qType: dns.TypeA,
|
|
|
|
aaaaDisabled: false,
|
|
|
|
wantRC: resultCodeFinish,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
c := ServerConfig{
|
2023-08-30 18:26:02 +03:00
|
|
|
Config: Config{
|
2023-07-18 17:02:07 +03:00
|
|
|
AAAADisabled: tc.aaaaDisabled,
|
2023-12-25 14:16:48 +03:00
|
|
|
UpstreamMode: UpstreamModeLoadBalance,
|
2023-07-18 17:02:07 +03:00
|
|
|
EDNSClientSubnet: &EDNSClientSubnet{Enabled: false},
|
|
|
|
},
|
2023-11-22 13:49:02 +03:00
|
|
|
ServePlainDNS: true,
|
2023-07-18 17:02:07 +03:00
|
|
|
}
|
|
|
|
|
2023-09-04 17:18:43 +03:00
|
|
|
s := createTestServer(t, &filtering.Config{
|
|
|
|
BlockingMode: filtering.BlockingModeDefault,
|
2024-03-11 18:17:04 +03:00
|
|
|
}, c)
|
2023-07-18 17:02:07 +03:00
|
|
|
|
|
|
|
var gotAddr netip.Addr
|
|
|
|
s.addrProc = &aghtest.AddressProcessor{
|
|
|
|
OnProcess: func(ip netip.Addr) { gotAddr = ip },
|
|
|
|
OnClose: func() (err error) { panic("not implemented") },
|
|
|
|
}
|
|
|
|
|
|
|
|
dctx := &dnsContext{
|
|
|
|
proxyCtx: &proxy.DNSContext{
|
|
|
|
Req: createTestMessageWithType(tc.target, tc.qType),
|
2023-12-05 17:43:50 +03:00
|
|
|
Addr: testClientAddrPort,
|
2023-07-18 17:02:07 +03:00
|
|
|
RequestID: 1234,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
gotRC := s.processInitial(dctx)
|
|
|
|
assert.Equal(t, tc.wantRC, gotRC)
|
2023-12-05 17:43:50 +03:00
|
|
|
assert.Equal(t, testClientAddrPort.Addr(), gotAddr)
|
2023-07-18 17:02:07 +03:00
|
|
|
|
|
|
|
if tc.wantRCode > 0 {
|
|
|
|
gotResp := dctx.proxyCtx.Res
|
|
|
|
require.NotNil(t, gotResp)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.wantRCode, gotResp.Rcode)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-22 17:18:35 +03:00
|
|
|
func TestServer_ProcessFilteringAfterResponse(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
var (
|
|
|
|
testIPv4 net.IP = netip.MustParseAddr("1.1.1.1").AsSlice()
|
|
|
|
testIPv6 net.IP = netip.MustParseAddr("1234::cdef").AsSlice()
|
|
|
|
)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
req *dns.Msg
|
|
|
|
aaaaDisabled bool
|
|
|
|
respAns []dns.RR
|
|
|
|
wantRC resultCode
|
|
|
|
wantRespAns []dns.RR
|
|
|
|
}{{
|
|
|
|
name: "pass",
|
|
|
|
req: createTestMessageWithType(aghtest.ReqFQDN, dns.TypeHTTPS),
|
|
|
|
aaaaDisabled: false,
|
|
|
|
respAns: newSVCBHintsAnswer(
|
|
|
|
aghtest.ReqFQDN,
|
|
|
|
[]dns.SVCBKeyValue{
|
|
|
|
&dns.SVCBIPv4Hint{Hint: []net.IP{testIPv4}},
|
|
|
|
&dns.SVCBIPv6Hint{Hint: []net.IP{testIPv6}},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
wantRespAns: newSVCBHintsAnswer(
|
|
|
|
aghtest.ReqFQDN,
|
|
|
|
[]dns.SVCBKeyValue{
|
|
|
|
&dns.SVCBIPv4Hint{Hint: []net.IP{testIPv4}},
|
|
|
|
&dns.SVCBIPv6Hint{Hint: []net.IP{testIPv6}},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
wantRC: resultCodeSuccess,
|
|
|
|
}, {
|
|
|
|
name: "filter",
|
|
|
|
req: createTestMessageWithType(aghtest.ReqFQDN, dns.TypeHTTPS),
|
|
|
|
aaaaDisabled: true,
|
|
|
|
respAns: newSVCBHintsAnswer(
|
|
|
|
aghtest.ReqFQDN,
|
|
|
|
[]dns.SVCBKeyValue{
|
|
|
|
&dns.SVCBIPv4Hint{Hint: []net.IP{testIPv4}},
|
|
|
|
&dns.SVCBIPv6Hint{Hint: []net.IP{testIPv6}},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
wantRespAns: newSVCBHintsAnswer(
|
|
|
|
aghtest.ReqFQDN,
|
|
|
|
[]dns.SVCBKeyValue{
|
|
|
|
&dns.SVCBIPv4Hint{Hint: []net.IP{testIPv4}},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
wantRC: resultCodeSuccess,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
c := ServerConfig{
|
2023-08-30 18:26:02 +03:00
|
|
|
Config: Config{
|
2023-08-22 17:18:35 +03:00
|
|
|
AAAADisabled: tc.aaaaDisabled,
|
2023-12-25 14:16:48 +03:00
|
|
|
UpstreamMode: UpstreamModeLoadBalance,
|
2023-08-22 17:18:35 +03:00
|
|
|
EDNSClientSubnet: &EDNSClientSubnet{Enabled: false},
|
|
|
|
},
|
2023-11-22 13:49:02 +03:00
|
|
|
ServePlainDNS: true,
|
2023-08-22 17:18:35 +03:00
|
|
|
}
|
|
|
|
|
2023-09-04 17:18:43 +03:00
|
|
|
s := createTestServer(t, &filtering.Config{
|
|
|
|
BlockingMode: filtering.BlockingModeDefault,
|
2024-03-11 18:17:04 +03:00
|
|
|
}, c)
|
2023-08-22 17:18:35 +03:00
|
|
|
|
|
|
|
resp := newResp(dns.RcodeSuccess, tc.req, tc.respAns)
|
|
|
|
dctx := &dnsContext{
|
|
|
|
setts: &filtering.Settings{
|
|
|
|
FilteringEnabled: true,
|
|
|
|
ProtectionEnabled: true,
|
|
|
|
},
|
|
|
|
protectionEnabled: true,
|
|
|
|
responseFromUpstream: true,
|
|
|
|
result: &filtering.Result{},
|
|
|
|
proxyCtx: &proxy.DNSContext{
|
|
|
|
Proto: proxy.ProtoUDP,
|
|
|
|
Req: tc.req,
|
|
|
|
Res: resp,
|
2023-12-05 17:43:50 +03:00
|
|
|
Addr: testClientAddrPort,
|
2023-08-22 17:18:35 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
gotRC := s.processFilteringAfterResponse(dctx)
|
|
|
|
assert.Equal(t, tc.wantRC, gotRC)
|
|
|
|
assert.Equal(t, newResp(dns.RcodeSuccess, tc.req, tc.wantRespAns), dctx.proxyCtx.Res)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-27 11:39:48 +03:00
|
|
|
func TestServer_ProcessDDRQuery(t *testing.T) {
|
|
|
|
dohSVCB := &dns.SVCB{
|
|
|
|
Priority: 1,
|
2022-05-30 16:04:28 +03:00
|
|
|
Target: ddrTestFQDN,
|
2022-04-27 11:39:48 +03:00
|
|
|
Value: []dns.SVCBKeyValue{
|
|
|
|
&dns.SVCBAlpn{Alpn: []string{"h2"}},
|
|
|
|
&dns.SVCBPort{Port: 8044},
|
2022-09-20 15:26:10 +03:00
|
|
|
&dns.SVCBDoHPath{Template: "/dns-query{?dns}"},
|
2022-04-27 11:39:48 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
dotSVCB := &dns.SVCB{
|
2022-06-20 17:48:56 +03:00
|
|
|
Priority: 1,
|
2022-05-30 16:04:28 +03:00
|
|
|
Target: ddrTestFQDN,
|
2022-04-27 11:39:48 +03:00
|
|
|
Value: []dns.SVCBKeyValue{
|
|
|
|
&dns.SVCBAlpn{Alpn: []string{"dot"}},
|
|
|
|
&dns.SVCBPort{Port: 8043},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-05-26 17:53:11 +03:00
|
|
|
doqSVCB := &dns.SVCB{
|
2022-06-20 17:48:56 +03:00
|
|
|
Priority: 1,
|
2022-05-30 16:04:28 +03:00
|
|
|
Target: ddrTestFQDN,
|
2022-05-26 17:53:11 +03:00
|
|
|
Value: []dns.SVCBKeyValue{
|
|
|
|
&dns.SVCBAlpn{Alpn: []string{"doq"}},
|
|
|
|
&dns.SVCBPort{Port: 8042},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-04-27 11:39:48 +03:00
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
host string
|
|
|
|
want []*dns.SVCB
|
|
|
|
wantRes resultCode
|
2024-03-11 18:17:04 +03:00
|
|
|
addrsDoH []*net.TCPAddr
|
|
|
|
addrsDoT []*net.TCPAddr
|
|
|
|
addrsDoQ []*net.UDPAddr
|
2022-04-27 11:39:48 +03:00
|
|
|
qtype uint16
|
|
|
|
ddrEnabled bool
|
|
|
|
}{{
|
|
|
|
name: "pass_host",
|
|
|
|
wantRes: resultCodeSuccess,
|
2023-07-18 17:02:07 +03:00
|
|
|
host: testQuestionTarget,
|
2022-04-27 11:39:48 +03:00
|
|
|
qtype: dns.TypeSVCB,
|
|
|
|
ddrEnabled: true,
|
2024-03-11 18:17:04 +03:00
|
|
|
addrsDoH: []*net.TCPAddr{{Port: 8043}},
|
2022-04-27 11:39:48 +03:00
|
|
|
}, {
|
|
|
|
name: "pass_qtype",
|
|
|
|
wantRes: resultCodeFinish,
|
|
|
|
host: ddrHostFQDN,
|
|
|
|
qtype: dns.TypeA,
|
|
|
|
ddrEnabled: true,
|
2024-03-11 18:17:04 +03:00
|
|
|
addrsDoH: []*net.TCPAddr{{Port: 8043}},
|
2022-04-27 11:39:48 +03:00
|
|
|
}, {
|
|
|
|
name: "pass_disabled_tls",
|
|
|
|
wantRes: resultCodeFinish,
|
|
|
|
host: ddrHostFQDN,
|
|
|
|
qtype: dns.TypeSVCB,
|
|
|
|
ddrEnabled: true,
|
|
|
|
}, {
|
|
|
|
name: "pass_disabled_ddr",
|
|
|
|
wantRes: resultCodeSuccess,
|
|
|
|
host: ddrHostFQDN,
|
|
|
|
qtype: dns.TypeSVCB,
|
|
|
|
ddrEnabled: false,
|
2024-03-11 18:17:04 +03:00
|
|
|
addrsDoH: []*net.TCPAddr{{Port: 8043}},
|
2022-04-27 11:39:48 +03:00
|
|
|
}, {
|
|
|
|
name: "dot",
|
|
|
|
wantRes: resultCodeFinish,
|
|
|
|
want: []*dns.SVCB{dotSVCB},
|
|
|
|
host: ddrHostFQDN,
|
|
|
|
qtype: dns.TypeSVCB,
|
|
|
|
ddrEnabled: true,
|
2024-03-11 18:17:04 +03:00
|
|
|
addrsDoT: []*net.TCPAddr{{Port: 8043}},
|
2022-04-27 11:39:48 +03:00
|
|
|
}, {
|
|
|
|
name: "doh",
|
|
|
|
wantRes: resultCodeFinish,
|
|
|
|
want: []*dns.SVCB{dohSVCB},
|
|
|
|
host: ddrHostFQDN,
|
|
|
|
qtype: dns.TypeSVCB,
|
|
|
|
ddrEnabled: true,
|
2024-03-11 18:17:04 +03:00
|
|
|
addrsDoH: []*net.TCPAddr{{Port: 8044}},
|
2022-05-26 17:53:11 +03:00
|
|
|
}, {
|
|
|
|
name: "doq",
|
|
|
|
wantRes: resultCodeFinish,
|
|
|
|
want: []*dns.SVCB{doqSVCB},
|
|
|
|
host: ddrHostFQDN,
|
|
|
|
qtype: dns.TypeSVCB,
|
|
|
|
ddrEnabled: true,
|
2024-03-11 18:17:04 +03:00
|
|
|
addrsDoQ: []*net.UDPAddr{{Port: 8042}},
|
2022-04-27 11:39:48 +03:00
|
|
|
}, {
|
|
|
|
name: "dot_doh",
|
|
|
|
wantRes: resultCodeFinish,
|
|
|
|
want: []*dns.SVCB{dotSVCB, dohSVCB},
|
|
|
|
host: ddrHostFQDN,
|
|
|
|
qtype: dns.TypeSVCB,
|
|
|
|
ddrEnabled: true,
|
2024-03-11 18:17:04 +03:00
|
|
|
addrsDoT: []*net.TCPAddr{{Port: 8043}},
|
|
|
|
addrsDoH: []*net.TCPAddr{{Port: 8044}},
|
2022-04-27 11:39:48 +03:00
|
|
|
}}
|
|
|
|
|
2024-03-11 18:17:04 +03:00
|
|
|
_, certPem, keyPem := createServerTLSConfig(t)
|
|
|
|
|
2022-04-27 11:39:48 +03:00
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2024-03-11 18:17:04 +03:00
|
|
|
s := createTestServer(t, &filtering.Config{
|
|
|
|
BlockingMode: filtering.BlockingModeDefault,
|
|
|
|
}, ServerConfig{
|
|
|
|
Config: Config{
|
|
|
|
HandleDDR: tc.ddrEnabled,
|
|
|
|
UpstreamMode: UpstreamModeLoadBalance,
|
|
|
|
EDNSClientSubnet: &EDNSClientSubnet{Enabled: false},
|
|
|
|
},
|
|
|
|
TLSConfig: TLSConfig{
|
|
|
|
ServerName: ddrTestDomainName,
|
|
|
|
CertificateChainData: certPem,
|
|
|
|
PrivateKeyData: keyPem,
|
|
|
|
TLSListenAddrs: tc.addrsDoT,
|
|
|
|
HTTPSListenAddrs: tc.addrsDoH,
|
|
|
|
QUICListenAddrs: tc.addrsDoQ,
|
|
|
|
},
|
|
|
|
ServePlainDNS: true,
|
|
|
|
})
|
|
|
|
// TODO(e.burkov): Generate a certificate actually containing the
|
|
|
|
// IP addresses.
|
|
|
|
s.conf.hasIPAddrs = true
|
2022-04-27 11:39:48 +03:00
|
|
|
|
|
|
|
req := createTestMessageWithType(tc.host, tc.qtype)
|
|
|
|
|
|
|
|
dctx := &dnsContext{
|
|
|
|
proxyCtx: &proxy.DNSContext{
|
|
|
|
Req: req,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
res := s.processDDRQuery(dctx)
|
|
|
|
require.Equal(t, tc.wantRes, res)
|
|
|
|
|
|
|
|
if tc.wantRes != resultCodeFinish {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := dctx.proxyCtx.Res
|
|
|
|
require.NotNil(t, msg)
|
|
|
|
|
|
|
|
for _, v := range tc.want {
|
|
|
|
v.Hdr = s.hdr(req, dns.TypeSVCB)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.ElementsMatch(t, tc.want, msg.Answer)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-04 17:18:43 +03:00
|
|
|
// createTestDNSFilter returns the minimum valid DNSFilter.
|
|
|
|
func createTestDNSFilter(t *testing.T) (f *filtering.DNSFilter) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
f, err := filtering.New(&filtering.Config{
|
|
|
|
BlockingMode: filtering.BlockingModeDefault,
|
|
|
|
}, []filtering.Filter{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2022-06-28 19:09:26 +03:00
|
|
|
func TestServer_ProcessDHCPHosts_localRestriction(t *testing.T) {
|
2023-08-30 14:02:12 +03:00
|
|
|
const (
|
|
|
|
localDomainSuffix = "lan"
|
|
|
|
dhcpClient = "example"
|
|
|
|
|
|
|
|
knownHost = dhcpClient + "." + localDomainSuffix
|
|
|
|
unknownHost = "wronghost." + localDomainSuffix
|
|
|
|
)
|
|
|
|
|
2022-10-26 21:12:51 +03:00
|
|
|
knownIP := netip.MustParseAddr("1.2.3.4")
|
2023-08-30 14:02:12 +03:00
|
|
|
dhcp := &testDHCP{
|
|
|
|
OnEnabled: func() (_ bool) { return true },
|
|
|
|
OnIPByHost: func(host string) (ip netip.Addr) {
|
|
|
|
if host == dhcpClient {
|
|
|
|
ip = knownIP
|
|
|
|
}
|
|
|
|
|
|
|
|
return ip
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-03-25 16:00:27 +03:00
|
|
|
testCases := []struct {
|
2023-07-18 17:02:07 +03:00
|
|
|
wantIP netip.Addr
|
2021-03-25 16:00:27 +03:00
|
|
|
name string
|
|
|
|
host string
|
2021-04-08 18:07:29 +03:00
|
|
|
isLocalCli bool
|
2021-03-25 16:00:27 +03:00
|
|
|
}{{
|
2023-07-18 17:02:07 +03:00
|
|
|
wantIP: knownIP,
|
2021-04-08 18:07:29 +03:00
|
|
|
name: "local_client_success",
|
2023-08-30 14:02:12 +03:00
|
|
|
host: knownHost,
|
2021-04-08 18:07:29 +03:00
|
|
|
isLocalCli: true,
|
2021-03-25 16:00:27 +03:00
|
|
|
}, {
|
2023-07-18 17:02:07 +03:00
|
|
|
wantIP: netip.Addr{},
|
2021-04-08 18:07:29 +03:00
|
|
|
name: "local_client_unknown_host",
|
2023-08-30 14:02:12 +03:00
|
|
|
host: unknownHost,
|
2021-04-08 18:07:29 +03:00
|
|
|
isLocalCli: true,
|
2021-03-25 17:21:00 +03:00
|
|
|
}, {
|
2023-07-18 17:02:07 +03:00
|
|
|
wantIP: netip.Addr{},
|
2021-04-08 18:07:29 +03:00
|
|
|
name: "external_client_known_host",
|
2023-08-30 14:02:12 +03:00
|
|
|
host: knownHost,
|
2021-04-08 18:07:29 +03:00
|
|
|
isLocalCli: false,
|
|
|
|
}, {
|
2023-07-18 17:02:07 +03:00
|
|
|
wantIP: netip.Addr{},
|
2021-04-08 18:07:29 +03:00
|
|
|
name: "external_client_unknown_host",
|
2023-08-30 14:02:12 +03:00
|
|
|
host: unknownHost,
|
2021-04-08 18:07:29 +03:00
|
|
|
isLocalCli: false,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
s := &Server{
|
2023-09-04 17:18:43 +03:00
|
|
|
dnsFilter: createTestDNSFilter(t),
|
2023-08-30 14:02:12 +03:00
|
|
|
dhcpServer: dhcp,
|
|
|
|
localDomainSuffix: localDomainSuffix,
|
2024-08-29 16:20:05 +03:00
|
|
|
baseLogger: slogutil.NewDiscardLogger(),
|
2021-04-08 18:07:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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{
|
2024-04-11 14:03:37 +03:00
|
|
|
Req: req,
|
|
|
|
IsPrivateClient: tc.isLocalCli,
|
2021-04-08 18:07:29 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-06-28 19:09:26 +03:00
|
|
|
res := s.processDHCPHosts(dctx)
|
2023-08-30 14:02:12 +03:00
|
|
|
|
2021-04-08 18:07:29 +03:00
|
|
|
pctx := dctx.proxyCtx
|
2023-08-30 14:02:12 +03:00
|
|
|
if !tc.isLocalCli {
|
|
|
|
require.Equal(t, resultCodeFinish, res)
|
2021-04-08 18:07:29 +03:00
|
|
|
require.NotNil(t, pctx.Res)
|
|
|
|
|
|
|
|
assert.Equal(t, dns.RcodeNameError, pctx.Res.Rcode)
|
2023-08-30 14:02:12 +03:00
|
|
|
assert.Empty(t, pctx.Res.Answer)
|
2021-04-08 18:07:29 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-30 14:02:12 +03:00
|
|
|
require.Equal(t, resultCodeSuccess, res)
|
|
|
|
|
2022-10-26 21:12:51 +03:00
|
|
|
if tc.wantIP == (netip.Addr{}) {
|
2021-04-08 18:07:29 +03:00
|
|
|
assert.Nil(t, pctx.Res)
|
|
|
|
|
2023-08-30 14:02:12 +03:00
|
|
|
return
|
|
|
|
}
|
2021-04-08 18:07:29 +03:00
|
|
|
|
2023-08-30 14:02:12 +03:00
|
|
|
require.NotNil(t, pctx.Res)
|
2022-10-26 21:12:51 +03:00
|
|
|
|
2023-08-30 14:02:12 +03:00
|
|
|
ans := pctx.Res.Answer
|
|
|
|
require.Len(t, ans, 1)
|
2022-10-26 21:12:51 +03:00
|
|
|
|
2023-08-30 14:02:12 +03:00
|
|
|
a := testutil.RequireTypeAssert[*dns.A](t, ans[0])
|
|
|
|
|
|
|
|
ip, err := netutil.IPToAddr(a.A, netutil.AddrFamilyIPv4)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.wantIP, ip)
|
2021-04-08 18:07:29 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-28 19:09:26 +03:00
|
|
|
func TestServer_ProcessDHCPHosts(t *testing.T) {
|
2021-04-08 18:07:29 +03:00
|
|
|
const (
|
2023-08-30 14:02:12 +03:00
|
|
|
localTLD = "lan"
|
|
|
|
|
|
|
|
knownClient = "example"
|
|
|
|
externalHost = knownClient + ".com"
|
|
|
|
clientHost = knownClient + "." + localTLD
|
2021-04-08 18:07:29 +03:00
|
|
|
)
|
|
|
|
|
2022-10-26 21:12:51 +03:00
|
|
|
knownIP := netip.MustParseAddr("1.2.3.4")
|
2023-08-30 14:02:12 +03:00
|
|
|
|
2021-04-08 18:07:29 +03:00
|
|
|
testCases := []struct {
|
2023-07-18 17:02:07 +03:00
|
|
|
wantIP netip.Addr
|
2021-04-08 18:07:29 +03:00
|
|
|
name string
|
|
|
|
host string
|
|
|
|
suffix string
|
|
|
|
wantRes resultCode
|
|
|
|
qtyp uint16
|
|
|
|
}{{
|
2023-07-18 17:02:07 +03:00
|
|
|
wantIP: netip.Addr{},
|
2023-08-30 14:02:12 +03:00
|
|
|
name: "external",
|
|
|
|
host: externalHost,
|
|
|
|
suffix: localTLD,
|
2021-04-08 18:07:29 +03:00
|
|
|
wantRes: resultCodeSuccess,
|
|
|
|
qtyp: dns.TypeA,
|
|
|
|
}, {
|
2023-07-18 17:02:07 +03:00
|
|
|
wantIP: netip.Addr{},
|
2023-08-30 14:02:12 +03:00
|
|
|
name: "external_non_a",
|
|
|
|
host: externalHost,
|
|
|
|
suffix: localTLD,
|
2021-04-08 18:07:29 +03:00
|
|
|
wantRes: resultCodeSuccess,
|
|
|
|
qtyp: dns.TypeCNAME,
|
|
|
|
}, {
|
2023-07-18 17:02:07 +03:00
|
|
|
wantIP: knownIP,
|
2023-08-30 14:02:12 +03:00
|
|
|
name: "internal",
|
|
|
|
host: clientHost,
|
|
|
|
suffix: localTLD,
|
2021-04-08 18:07:29 +03:00
|
|
|
wantRes: resultCodeSuccess,
|
|
|
|
qtyp: dns.TypeA,
|
|
|
|
}, {
|
2023-07-18 17:02:07 +03:00
|
|
|
wantIP: netip.Addr{},
|
2023-08-30 14:02:12 +03:00
|
|
|
name: "internal_unknown",
|
2021-04-08 18:07:29 +03:00
|
|
|
host: "example-new.lan",
|
2023-08-30 14:02:12 +03:00
|
|
|
suffix: localTLD,
|
2022-09-02 16:56:59 +03:00
|
|
|
wantRes: resultCodeSuccess,
|
2021-04-08 18:07:29 +03:00
|
|
|
qtyp: dns.TypeA,
|
|
|
|
}, {
|
2023-07-18 17:02:07 +03:00
|
|
|
wantIP: netip.Addr{},
|
2023-08-30 14:02:12 +03:00
|
|
|
name: "internal_aaaa",
|
|
|
|
host: clientHost,
|
|
|
|
suffix: localTLD,
|
2021-04-08 18:07:29 +03:00
|
|
|
wantRes: resultCodeSuccess,
|
|
|
|
qtyp: dns.TypeAAAA,
|
|
|
|
}, {
|
2023-07-18 17:02:07 +03:00
|
|
|
wantIP: knownIP,
|
2023-08-30 14:02:12 +03:00
|
|
|
name: "custom_suffix",
|
|
|
|
host: knownClient + ".custom",
|
2022-06-28 19:09:26 +03:00
|
|
|
suffix: "custom",
|
2021-04-08 18:07:29 +03:00
|
|
|
wantRes: resultCodeSuccess,
|
|
|
|
qtyp: dns.TypeA,
|
2021-03-25 16:00:27 +03:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
2023-08-30 14:02:12 +03:00
|
|
|
testDHCP := &testDHCP{
|
|
|
|
OnEnabled: func() (_ bool) { return true },
|
|
|
|
OnIPByHost: func(host string) (ip netip.Addr) {
|
|
|
|
if host == knownClient {
|
|
|
|
ip = knownIP
|
|
|
|
}
|
|
|
|
|
|
|
|
return ip
|
|
|
|
},
|
|
|
|
OnHostByIP: func(ip netip.Addr) (host string) { panic("not implemented") },
|
|
|
|
}
|
|
|
|
|
2022-06-28 19:09:26 +03:00
|
|
|
s := &Server{
|
2023-09-04 17:18:43 +03:00
|
|
|
dnsFilter: createTestDNSFilter(t),
|
2022-09-13 23:45:35 +03:00
|
|
|
dhcpServer: testDHCP,
|
2022-06-28 19:09:26 +03:00
|
|
|
localDomainSuffix: tc.suffix,
|
2024-08-29 16:20:05 +03:00
|
|
|
baseLogger: slogutil.NewDiscardLogger(),
|
2022-06-28 19:09:26 +03:00
|
|
|
}
|
2021-03-25 16:00:27 +03:00
|
|
|
|
2022-06-28 19:09:26 +03:00
|
|
|
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{
|
2024-04-11 14:03:37 +03:00
|
|
|
Req: req,
|
|
|
|
IsPrivateClient: true,
|
2022-06-28 19:09:26 +03:00
|
|
|
},
|
|
|
|
}
|
2021-03-25 16:00:27 +03:00
|
|
|
|
2022-06-28 19:09:26 +03:00
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
res := s.processDHCPHosts(dctx)
|
2021-04-08 18:07:29 +03:00
|
|
|
pctx := dctx.proxyCtx
|
2021-03-25 16:00:27 +03:00
|
|
|
assert.Equal(t, tc.wantRes, res)
|
2021-04-08 18:07:29 +03:00
|
|
|
require.NoError(t, dctx.err)
|
|
|
|
|
2021-03-25 17:21:00 +03: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)
|
2022-10-26 21:12:51 +03:00
|
|
|
} else if tc.wantIP == (netip.Addr{}) {
|
2021-03-25 16:00:27 +03:00
|
|
|
assert.Nil(t, pctx.Res)
|
|
|
|
} else {
|
|
|
|
require.NotNil(t, pctx.Res)
|
|
|
|
|
|
|
|
ans := pctx.Res.Answer
|
|
|
|
require.Len(t, ans, 1)
|
|
|
|
|
2022-10-26 21:12:51 +03:00
|
|
|
a := testutil.RequireTypeAssert[*dns.A](t, ans[0])
|
|
|
|
|
|
|
|
ip, err := netutil.IPToAddr(a.A, netutil.AddrFamilyIPv4)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.wantIP, ip)
|
2021-03-25 16:00:27 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-03-31 15:00:47 +03:00
|
|
|
|
2024-04-11 14:03:37 +03:00
|
|
|
// TODO(e.burkov): Rewrite this test to use the whole server instead of just
|
|
|
|
// testing the [handleDNSRequest] method. See comment on
|
|
|
|
// "from_external_for_local" test case.
|
|
|
|
func TestServer_HandleDNSRequest_restrictLocal(t *testing.T) {
|
|
|
|
intAddr := netip.MustParseAddr("192.168.1.1")
|
|
|
|
intPTRQuestion, err := netutil.IPToReversedAddr(intAddr.AsSlice())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
extAddr := netip.MustParseAddr("254.253.252.1")
|
|
|
|
extPTRQuestion, err := netutil.IPToReversedAddr(extAddr.AsSlice())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-10-27 16:11:36 +03:00
|
|
|
const (
|
2024-04-11 14:03:37 +03:00
|
|
|
extPTRAnswer = "host1.example.net."
|
|
|
|
intPTRAnswer = "some.local-client."
|
2022-10-27 16:11:36 +03:00
|
|
|
)
|
|
|
|
|
2024-03-11 18:17:04 +03:00
|
|
|
localUpsHdlr := dns.HandlerFunc(func(w dns.ResponseWriter, req *dns.Msg) {
|
2024-04-04 15:52:39 +03:00
|
|
|
resp := cmp.Or(
|
2022-10-28 19:35:59 +03:00
|
|
|
aghtest.MatchedResponse(req, dns.TypePTR, extPTRQuestion, extPTRAnswer),
|
|
|
|
aghtest.MatchedResponse(req, dns.TypePTR, intPTRQuestion, intPTRAnswer),
|
2024-04-11 14:03:37 +03:00
|
|
|
(&dns.Msg{}).SetRcode(req, dns.RcodeNameError),
|
2024-03-11 18:17:04 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
require.NoError(testutil.PanicT{}, w.WriteMsg(resp))
|
2022-10-28 19:35:59 +03:00
|
|
|
})
|
2024-03-11 18:17:04 +03:00
|
|
|
localUpsAddr := aghtest.StartLocalhostUpstream(t, localUpsHdlr).String()
|
2022-10-27 16:11:36 +03:00
|
|
|
|
2023-09-04 17:18:43 +03:00
|
|
|
s := createTestServer(t, &filtering.Config{
|
|
|
|
BlockingMode: filtering.BlockingModeDefault,
|
|
|
|
}, ServerConfig{
|
2021-04-09 21:01:21 +03:00
|
|
|
UDPListenAddrs: []*net.UDPAddr{{}},
|
|
|
|
TCPListenAddrs: []*net.TCPAddr{{}},
|
2023-03-01 16:16:16 +03:00
|
|
|
// TODO(s.chzhen): Add tests where EDNSClientSubnet.Enabled is true.
|
2023-08-30 18:26:02 +03:00
|
|
|
// Improve Config declaration for tests.
|
|
|
|
Config: Config{
|
2024-03-11 18:17:04 +03:00
|
|
|
UpstreamDNS: []string{localUpsAddr},
|
2023-12-25 14:16:48 +03:00
|
|
|
UpstreamMode: UpstreamModeLoadBalance,
|
2023-03-01 16:16:16 +03:00
|
|
|
EDNSClientSubnet: &EDNSClientSubnet{Enabled: false},
|
|
|
|
},
|
2024-03-11 18:17:04 +03:00
|
|
|
UsePrivateRDNS: true,
|
|
|
|
LocalPTRResolvers: []string{localUpsAddr},
|
|
|
|
ServePlainDNS: true,
|
|
|
|
})
|
2021-03-31 15:00:47 +03:00
|
|
|
startDeferStop(t, s)
|
|
|
|
|
|
|
|
testCases := []struct {
|
2024-04-11 14:03:37 +03:00
|
|
|
name string
|
|
|
|
question string
|
|
|
|
wantErr error
|
|
|
|
wantAns []dns.RR
|
|
|
|
isPrivate bool
|
2021-03-31 15:00:47 +03:00
|
|
|
}{{
|
2024-04-11 14:03:37 +03:00
|
|
|
name: "from_local_for_external",
|
|
|
|
question: extPTRQuestion,
|
|
|
|
wantErr: nil,
|
|
|
|
wantAns: []dns.RR{&dns.PTR{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: dns.Fqdn(extPTRQuestion),
|
|
|
|
Rrtype: dns.TypePTR,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
Ttl: 60,
|
|
|
|
Rdlength: uint16(len(extPTRAnswer) + 1),
|
|
|
|
},
|
|
|
|
Ptr: dns.Fqdn(extPTRAnswer),
|
|
|
|
}},
|
|
|
|
isPrivate: true,
|
|
|
|
}, {
|
|
|
|
// In theory this case is not reproducible because [proxy.Proxy] should
|
|
|
|
// respond to such queries with NXDOMAIN before they reach
|
|
|
|
// [Server.handleDNSRequest].
|
|
|
|
name: "from_external_for_local",
|
|
|
|
question: intPTRQuestion,
|
|
|
|
wantErr: upstream.ErrNoUpstreams,
|
|
|
|
wantAns: nil,
|
|
|
|
isPrivate: false,
|
2021-03-31 15:00:47 +03:00
|
|
|
}, {
|
|
|
|
name: "from_local_for_local",
|
2024-04-11 14:03:37 +03:00
|
|
|
question: intPTRQuestion,
|
|
|
|
wantErr: nil,
|
|
|
|
wantAns: []dns.RR{&dns.PTR{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: dns.Fqdn(intPTRQuestion),
|
|
|
|
Rrtype: dns.TypePTR,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
Ttl: 60,
|
|
|
|
Rdlength: uint16(len(intPTRAnswer) + 1),
|
|
|
|
},
|
|
|
|
Ptr: dns.Fqdn(intPTRAnswer),
|
|
|
|
}},
|
|
|
|
isPrivate: true,
|
2021-03-31 15:00:47 +03:00
|
|
|
}, {
|
|
|
|
name: "from_external_for_external",
|
2024-04-11 14:03:37 +03:00
|
|
|
question: extPTRQuestion,
|
|
|
|
wantErr: nil,
|
|
|
|
wantAns: []dns.RR{&dns.PTR{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: dns.Fqdn(extPTRQuestion),
|
|
|
|
Rrtype: dns.TypePTR,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
Ttl: 60,
|
|
|
|
Rdlength: uint16(len(extPTRAnswer) + 1),
|
|
|
|
},
|
|
|
|
Ptr: dns.Fqdn(extPTRAnswer),
|
|
|
|
}},
|
|
|
|
isPrivate: false,
|
2021-03-31 15:00:47 +03:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
2024-04-11 14:03:37 +03:00
|
|
|
pref, extErr := netutil.ExtractReversedAddr(tc.question)
|
|
|
|
require.NoError(t, extErr)
|
2021-03-31 15:00:47 +03:00
|
|
|
|
2024-04-11 14:03:37 +03:00
|
|
|
req := createTestMessageWithType(dns.Fqdn(tc.question), dns.TypePTR)
|
2021-03-31 15:00:47 +03:00
|
|
|
pctx := &proxy.DNSContext{
|
2024-04-11 14:03:37 +03:00
|
|
|
Req: req,
|
|
|
|
IsPrivateClient: tc.isPrivate,
|
|
|
|
}
|
|
|
|
// TODO(e.burkov): Configure the subnet set properly.
|
|
|
|
if netutil.IsLocallyServed(pref.Addr()) {
|
|
|
|
pctx.RequestedPrivateRDNS = pref
|
2021-03-31 15:00:47 +03:00
|
|
|
}
|
2021-05-26 17:55:19 +03:00
|
|
|
|
2021-03-31 15:00:47 +03:00
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2024-04-11 14:03:37 +03:00
|
|
|
err = s.handleDNSRequest(s.dnsProxy, pctx)
|
|
|
|
require.ErrorIs(t, err, tc.wantErr)
|
2021-05-26 17:55:19 +03:00
|
|
|
|
2024-04-11 14:03:37 +03:00
|
|
|
require.NotNil(t, pctx.Res)
|
|
|
|
assert.Equal(t, tc.wantAns, pctx.Res.Answer)
|
2021-03-31 15:00:47 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-05-26 17:55:19 +03:00
|
|
|
|
2024-04-11 14:03:37 +03:00
|
|
|
func TestServer_ProcessUpstream_localPTR(t *testing.T) {
|
2021-05-26 17:55:19 +03:00
|
|
|
const locDomain = "some.local."
|
|
|
|
const reqAddr = "1.1.168.192.in-addr.arpa."
|
|
|
|
|
2024-03-11 18:17:04 +03:00
|
|
|
localUpsHdlr := dns.HandlerFunc(func(w dns.ResponseWriter, req *dns.Msg) {
|
2024-04-04 15:52:39 +03:00
|
|
|
resp := cmp.Or(
|
2024-03-11 18:17:04 +03:00
|
|
|
aghtest.MatchedResponse(req, dns.TypePTR, reqAddr, locDomain),
|
2024-04-11 14:03:37 +03:00
|
|
|
(&dns.Msg{}).SetRcode(req, dns.RcodeNameError),
|
2024-03-11 18:17:04 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
require.NoError(testutil.PanicT{}, w.WriteMsg(resp))
|
|
|
|
})
|
|
|
|
localUpsAddr := aghtest.StartLocalhostUpstream(t, localUpsHdlr).String()
|
|
|
|
|
2024-04-11 14:03:37 +03:00
|
|
|
newPrxCtx := func() (prxCtx *proxy.DNSContext) {
|
|
|
|
return &proxy.DNSContext{
|
|
|
|
Addr: testClientAddrPort,
|
|
|
|
Req: createTestMessageWithType(reqAddr, dns.TypePTR),
|
|
|
|
IsPrivateClient: true,
|
|
|
|
RequestedPrivateRDNS: netip.MustParsePrefix("192.168.1.1/32"),
|
2021-05-26 17:55:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("enabled", func(t *testing.T) {
|
2024-04-11 14:03:37 +03:00
|
|
|
s := createTestServer(
|
|
|
|
t,
|
|
|
|
&filtering.Config{
|
|
|
|
BlockingMode: filtering.BlockingModeDefault,
|
|
|
|
},
|
|
|
|
ServerConfig{
|
|
|
|
UDPListenAddrs: []*net.UDPAddr{{}},
|
|
|
|
TCPListenAddrs: []*net.TCPAddr{{}},
|
|
|
|
Config: Config{
|
|
|
|
UpstreamMode: UpstreamModeLoadBalance,
|
|
|
|
EDNSClientSubnet: &EDNSClientSubnet{Enabled: false},
|
|
|
|
},
|
|
|
|
UsePrivateRDNS: true,
|
|
|
|
LocalPTRResolvers: []string{localUpsAddr},
|
|
|
|
ServePlainDNS: true,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
pctx := newPrxCtx()
|
2021-05-26 17:55:19 +03:00
|
|
|
|
2024-04-11 14:03:37 +03:00
|
|
|
rc := s.processUpstream(&dnsContext{proxyCtx: pctx})
|
2021-05-26 17:55:19 +03:00
|
|
|
require.Equal(t, resultCodeSuccess, rc)
|
2024-04-11 14:03:37 +03:00
|
|
|
require.NotEmpty(t, pctx.Res.Answer)
|
|
|
|
ptr := testutil.RequireTypeAssert[*dns.PTR](t, pctx.Res.Answer[0])
|
2021-05-26 17:55:19 +03:00
|
|
|
|
2024-04-11 14:03:37 +03:00
|
|
|
assert.Equal(t, locDomain, ptr.Ptr)
|
2021-05-26 17:55:19 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("disabled", func(t *testing.T) {
|
2024-04-11 14:03:37 +03:00
|
|
|
s := createTestServer(
|
|
|
|
t,
|
|
|
|
&filtering.Config{
|
|
|
|
BlockingMode: filtering.BlockingModeDefault,
|
|
|
|
},
|
|
|
|
ServerConfig{
|
|
|
|
UDPListenAddrs: []*net.UDPAddr{{}},
|
|
|
|
TCPListenAddrs: []*net.TCPAddr{{}},
|
|
|
|
Config: Config{
|
|
|
|
UpstreamMode: UpstreamModeLoadBalance,
|
|
|
|
EDNSClientSubnet: &EDNSClientSubnet{Enabled: false},
|
|
|
|
},
|
|
|
|
UsePrivateRDNS: false,
|
|
|
|
LocalPTRResolvers: []string{localUpsAddr},
|
|
|
|
ServePlainDNS: true,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
pctx := newPrxCtx()
|
2021-05-26 17:55:19 +03:00
|
|
|
|
2024-04-11 14:03:37 +03:00
|
|
|
rc := s.processUpstream(&dnsContext{proxyCtx: pctx})
|
|
|
|
require.Equal(t, resultCodeError, rc)
|
|
|
|
require.Empty(t, pctx.Res.Answer)
|
2021-05-26 17:55:19 +03:00
|
|
|
})
|
|
|
|
}
|
2021-05-28 13:02:59 +03: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))
|
|
|
|
})
|
|
|
|
}
|