2021-01-27 15:32:13 +00:00
|
|
|
package dnsforward
|
|
|
|
|
|
|
|
import (
|
2023-12-05 14:43:50 +00:00
|
|
|
"net/netip"
|
2021-01-27 15:32:13 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-12-06 14:26:43 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
2021-05-21 14:15:47 +01:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
2021-01-27 15:32:13 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/querylog"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/stats"
|
|
|
|
"github.com/AdguardTeam/dnsproxy/proxy"
|
|
|
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-03-11 14:32:58 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-01-27 15:32:13 +00:00
|
|
|
)
|
|
|
|
|
2023-02-07 14:50:39 +00:00
|
|
|
// testQueryLog is a simple [querylog.QueryLog] implementation for tests.
|
2021-01-27 15:32:13 +00:00
|
|
|
type testQueryLog struct {
|
|
|
|
// QueryLog is embedded here simply to make testQueryLog
|
2023-02-07 14:50:39 +00:00
|
|
|
// a [querylog.QueryLog] without actually implementing all methods.
|
2021-01-27 15:32:13 +00:00
|
|
|
querylog.QueryLog
|
|
|
|
|
2021-12-13 15:06:01 +00:00
|
|
|
lastParams *querylog.AddParams
|
2021-01-27 15:32:13 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:50:39 +00:00
|
|
|
// Add implements the [querylog.QueryLog] interface for *testQueryLog.
|
2021-12-13 15:06:01 +00:00
|
|
|
func (l *testQueryLog) Add(p *querylog.AddParams) {
|
2021-01-27 15:32:13 +00:00
|
|
|
l.lastParams = p
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:50:39 +00:00
|
|
|
// ShouldLog implements the [querylog.QueryLog] interface for *testQueryLog.
|
2023-04-07 11:17:40 +01:00
|
|
|
func (l *testQueryLog) ShouldLog(string, uint16, uint16, []string) bool {
|
2023-02-07 14:50:39 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-02-13 15:15:33 +00:00
|
|
|
// testStats is a simple [stats.Interface] implementation for tests.
|
2021-01-27 15:32:13 +00:00
|
|
|
type testStats struct {
|
2023-02-13 15:15:33 +00:00
|
|
|
// Stats is embedded here simply to make testStats a [stats.Interface]
|
|
|
|
// without actually implementing all methods.
|
2022-08-04 17:05:28 +01:00
|
|
|
stats.Interface
|
2021-01-27 15:32:13 +00:00
|
|
|
|
2023-08-09 12:33:52 +01:00
|
|
|
lastEntry *stats.Entry
|
2021-01-27 15:32:13 +00:00
|
|
|
}
|
|
|
|
|
2023-02-13 15:15:33 +00:00
|
|
|
// Update implements the [stats.Interface] interface for *testStats.
|
2023-08-09 12:33:52 +01:00
|
|
|
func (l *testStats) Update(e *stats.Entry) {
|
2023-06-20 08:36:20 +01:00
|
|
|
if e.Domain == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-27 15:32:13 +00:00
|
|
|
l.lastEntry = e
|
|
|
|
}
|
|
|
|
|
2023-02-13 15:15:33 +00:00
|
|
|
// ShouldCount implements the [stats.Interface] interface for *testStats.
|
2023-04-07 11:17:40 +01:00
|
|
|
func (l *testStats) ShouldCount(string, uint16, uint16, []string) bool {
|
2023-02-13 15:15:33 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-06-20 08:36:20 +01:00
|
|
|
func TestServer_ProcessQueryLogsAndStats(t *testing.T) {
|
|
|
|
const domain = "example.com."
|
|
|
|
|
2021-01-27 15:32:13 +00:00
|
|
|
testCases := []struct {
|
|
|
|
name string
|
2023-06-20 08:36:20 +01:00
|
|
|
domain string
|
2021-06-29 13:53:28 +01:00
|
|
|
proto proxy.Proto
|
2023-12-05 14:43:50 +00:00
|
|
|
addr netip.AddrPort
|
2021-01-27 15:32:13 +00:00
|
|
|
clientID string
|
|
|
|
wantLogProto querylog.ClientProto
|
|
|
|
wantStatClient string
|
|
|
|
wantCode resultCode
|
2021-05-21 14:15:47 +01:00
|
|
|
reason filtering.Reason
|
2021-01-27 15:32:13 +00:00
|
|
|
wantStatResult stats.Result
|
|
|
|
}{{
|
|
|
|
name: "success_udp",
|
2023-06-20 08:36:20 +01:00
|
|
|
domain: domain,
|
2021-01-27 15:32:13 +00:00
|
|
|
proto: proxy.ProtoUDP,
|
2023-12-05 14:43:50 +00:00
|
|
|
addr: testClientAddrPort,
|
2021-01-27 15:32:13 +00:00
|
|
|
clientID: "",
|
|
|
|
wantLogProto: "",
|
|
|
|
wantStatClient: "1.2.3.4",
|
|
|
|
wantCode: resultCodeSuccess,
|
2021-05-21 14:15:47 +01:00
|
|
|
reason: filtering.NotFilteredNotFound,
|
2021-01-27 15:32:13 +00:00
|
|
|
wantStatResult: stats.RNotFiltered,
|
|
|
|
}, {
|
2022-02-10 12:42:59 +00:00
|
|
|
name: "success_tls_clientid",
|
2023-06-20 08:36:20 +01:00
|
|
|
domain: domain,
|
2021-01-27 15:32:13 +00:00
|
|
|
proto: proxy.ProtoTLS,
|
2023-12-05 14:43:50 +00:00
|
|
|
addr: testClientAddrPort,
|
2021-01-27 15:32:13 +00:00
|
|
|
clientID: "cli42",
|
2021-06-18 16:13:36 +01:00
|
|
|
wantLogProto: querylog.ClientProtoDoT,
|
2021-01-27 15:32:13 +00:00
|
|
|
wantStatClient: "cli42",
|
|
|
|
wantCode: resultCodeSuccess,
|
2021-05-21 14:15:47 +01:00
|
|
|
reason: filtering.NotFilteredNotFound,
|
2021-01-27 15:32:13 +00:00
|
|
|
wantStatResult: stats.RNotFiltered,
|
|
|
|
}, {
|
|
|
|
name: "success_tls",
|
2023-06-20 08:36:20 +01:00
|
|
|
domain: domain,
|
2021-01-27 15:32:13 +00:00
|
|
|
proto: proxy.ProtoTLS,
|
2023-12-05 14:43:50 +00:00
|
|
|
addr: testClientAddrPort,
|
2021-01-27 15:32:13 +00:00
|
|
|
clientID: "",
|
2021-06-18 16:13:36 +01:00
|
|
|
wantLogProto: querylog.ClientProtoDoT,
|
2021-01-27 15:32:13 +00:00
|
|
|
wantStatClient: "1.2.3.4",
|
|
|
|
wantCode: resultCodeSuccess,
|
2021-05-21 14:15:47 +01:00
|
|
|
reason: filtering.NotFilteredNotFound,
|
2021-01-27 15:32:13 +00:00
|
|
|
wantStatResult: stats.RNotFiltered,
|
|
|
|
}, {
|
|
|
|
name: "success_quic",
|
2023-06-20 08:36:20 +01:00
|
|
|
domain: domain,
|
2021-01-27 15:32:13 +00:00
|
|
|
proto: proxy.ProtoQUIC,
|
2023-12-05 14:43:50 +00:00
|
|
|
addr: testClientAddrPort,
|
2021-01-27 15:32:13 +00:00
|
|
|
clientID: "",
|
2021-06-18 16:13:36 +01:00
|
|
|
wantLogProto: querylog.ClientProtoDoQ,
|
2021-01-27 15:32:13 +00:00
|
|
|
wantStatClient: "1.2.3.4",
|
|
|
|
wantCode: resultCodeSuccess,
|
2021-05-21 14:15:47 +01:00
|
|
|
reason: filtering.NotFilteredNotFound,
|
2021-01-27 15:32:13 +00:00
|
|
|
wantStatResult: stats.RNotFiltered,
|
|
|
|
}, {
|
|
|
|
name: "success_https",
|
2023-06-20 08:36:20 +01:00
|
|
|
domain: domain,
|
2021-01-27 15:32:13 +00:00
|
|
|
proto: proxy.ProtoHTTPS,
|
2023-12-05 14:43:50 +00:00
|
|
|
addr: testClientAddrPort,
|
2021-01-27 15:32:13 +00:00
|
|
|
clientID: "",
|
2021-06-18 16:13:36 +01:00
|
|
|
wantLogProto: querylog.ClientProtoDoH,
|
2021-01-27 15:32:13 +00:00
|
|
|
wantStatClient: "1.2.3.4",
|
|
|
|
wantCode: resultCodeSuccess,
|
2021-05-21 14:15:47 +01:00
|
|
|
reason: filtering.NotFilteredNotFound,
|
2021-01-27 15:32:13 +00:00
|
|
|
wantStatResult: stats.RNotFiltered,
|
|
|
|
}, {
|
|
|
|
name: "success_dnscrypt",
|
2023-06-20 08:36:20 +01:00
|
|
|
domain: domain,
|
2021-01-27 15:32:13 +00:00
|
|
|
proto: proxy.ProtoDNSCrypt,
|
2023-12-05 14:43:50 +00:00
|
|
|
addr: testClientAddrPort,
|
2021-01-27 15:32:13 +00:00
|
|
|
clientID: "",
|
|
|
|
wantLogProto: querylog.ClientProtoDNSCrypt,
|
|
|
|
wantStatClient: "1.2.3.4",
|
|
|
|
wantCode: resultCodeSuccess,
|
2021-05-21 14:15:47 +01:00
|
|
|
reason: filtering.NotFilteredNotFound,
|
2021-01-27 15:32:13 +00:00
|
|
|
wantStatResult: stats.RNotFiltered,
|
|
|
|
}, {
|
|
|
|
name: "success_udp_filtered",
|
2023-06-20 08:36:20 +01:00
|
|
|
domain: domain,
|
2021-01-27 15:32:13 +00:00
|
|
|
proto: proxy.ProtoUDP,
|
2023-12-05 14:43:50 +00:00
|
|
|
addr: testClientAddrPort,
|
2021-01-27 15:32:13 +00:00
|
|
|
clientID: "",
|
|
|
|
wantLogProto: "",
|
|
|
|
wantStatClient: "1.2.3.4",
|
|
|
|
wantCode: resultCodeSuccess,
|
2021-05-21 14:15:47 +01:00
|
|
|
reason: filtering.FilteredBlockList,
|
2021-01-27 15:32:13 +00:00
|
|
|
wantStatResult: stats.RFiltered,
|
|
|
|
}, {
|
|
|
|
name: "success_udp_sb",
|
2023-06-20 08:36:20 +01:00
|
|
|
domain: domain,
|
2021-01-27 15:32:13 +00:00
|
|
|
proto: proxy.ProtoUDP,
|
2023-12-05 14:43:50 +00:00
|
|
|
addr: testClientAddrPort,
|
2021-01-27 15:32:13 +00:00
|
|
|
clientID: "",
|
|
|
|
wantLogProto: "",
|
|
|
|
wantStatClient: "1.2.3.4",
|
|
|
|
wantCode: resultCodeSuccess,
|
2021-05-21 14:15:47 +01:00
|
|
|
reason: filtering.FilteredSafeBrowsing,
|
2021-01-27 15:32:13 +00:00
|
|
|
wantStatResult: stats.RSafeBrowsing,
|
|
|
|
}, {
|
|
|
|
name: "success_udp_ss",
|
2023-06-20 08:36:20 +01:00
|
|
|
domain: domain,
|
2021-01-27 15:32:13 +00:00
|
|
|
proto: proxy.ProtoUDP,
|
2023-12-05 14:43:50 +00:00
|
|
|
addr: testClientAddrPort,
|
2021-01-27 15:32:13 +00:00
|
|
|
clientID: "",
|
|
|
|
wantLogProto: "",
|
|
|
|
wantStatClient: "1.2.3.4",
|
|
|
|
wantCode: resultCodeSuccess,
|
2021-05-21 14:15:47 +01:00
|
|
|
reason: filtering.FilteredSafeSearch,
|
2021-01-27 15:32:13 +00:00
|
|
|
wantStatResult: stats.RSafeSearch,
|
|
|
|
}, {
|
|
|
|
name: "success_udp_pc",
|
2023-06-20 08:36:20 +01:00
|
|
|
domain: domain,
|
2021-01-27 15:32:13 +00:00
|
|
|
proto: proxy.ProtoUDP,
|
2023-12-05 14:43:50 +00:00
|
|
|
addr: testClientAddrPort,
|
2021-01-27 15:32:13 +00:00
|
|
|
clientID: "",
|
|
|
|
wantLogProto: "",
|
|
|
|
wantStatClient: "1.2.3.4",
|
|
|
|
wantCode: resultCodeSuccess,
|
2021-05-21 14:15:47 +01:00
|
|
|
reason: filtering.FilteredParental,
|
2021-01-27 15:32:13 +00:00
|
|
|
wantStatResult: stats.RParental,
|
2023-06-20 08:36:20 +01:00
|
|
|
}, {
|
|
|
|
name: "success_udp_pc_empty_fqdn",
|
|
|
|
domain: ".",
|
|
|
|
proto: proxy.ProtoUDP,
|
2023-12-05 14:43:50 +00:00
|
|
|
addr: netip.MustParseAddrPort("4.3.2.1:1234"),
|
2023-06-20 08:36:20 +01:00
|
|
|
clientID: "",
|
|
|
|
wantLogProto: "",
|
2023-12-05 14:43:50 +00:00
|
|
|
wantStatClient: "4.3.2.1",
|
2023-06-20 08:36:20 +01:00
|
|
|
wantCode: resultCodeSuccess,
|
|
|
|
reason: filtering.FilteredParental,
|
|
|
|
wantStatResult: stats.RParental,
|
2021-01-27 15:32:13 +00:00
|
|
|
}}
|
|
|
|
|
2021-06-29 13:53:28 +01:00
|
|
|
ups, err := upstream.AddressToUpstream("1.1.1.1", nil)
|
2021-10-22 09:58:18 +01:00
|
|
|
require.NoError(t, err)
|
2021-01-27 15:32:13 +00:00
|
|
|
|
|
|
|
for _, tc := range testCases {
|
2021-11-15 14:42:10 +00:00
|
|
|
ql := &testQueryLog{}
|
|
|
|
st := &testStats{}
|
|
|
|
srv := &Server{
|
2021-12-06 14:26:43 +00:00
|
|
|
queryLog: ql,
|
|
|
|
stats: st,
|
|
|
|
anonymizer: aghnet.NewIPMut(nil),
|
2021-11-15 14:42:10 +00:00
|
|
|
}
|
2021-01-27 15:32:13 +00:00
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
req := &dns.Msg{
|
|
|
|
Question: []dns.Question{{
|
2023-06-20 08:36:20 +01:00
|
|
|
Name: tc.domain,
|
2021-01-27 15:32:13 +00:00
|
|
|
}},
|
|
|
|
}
|
|
|
|
pctx := &proxy.DNSContext{
|
|
|
|
Proto: tc.proto,
|
|
|
|
Req: req,
|
|
|
|
Res: &dns.Msg{},
|
|
|
|
Addr: tc.addr,
|
|
|
|
Upstream: ups,
|
|
|
|
}
|
|
|
|
dctx := &dnsContext{
|
|
|
|
proxyCtx: pctx,
|
|
|
|
startTime: time.Now(),
|
2021-05-21 14:15:47 +01:00
|
|
|
result: &filtering.Result{
|
2021-01-27 15:32:13 +00:00
|
|
|
Reason: tc.reason,
|
|
|
|
},
|
|
|
|
clientID: tc.clientID,
|
|
|
|
}
|
|
|
|
|
2021-11-15 14:42:10 +00:00
|
|
|
code := srv.processQueryLogsAndStats(dctx)
|
2021-01-27 15:32:13 +00:00
|
|
|
assert.Equal(t, tc.wantCode, code)
|
|
|
|
assert.Equal(t, tc.wantLogProto, ql.lastParams.ClientProto)
|
|
|
|
assert.Equal(t, tc.wantStatClient, st.lastEntry.Client)
|
|
|
|
assert.Equal(t, tc.wantStatResult, st.lastEntry.Result)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|