/
result.go
110 lines (87 loc) · 3 KB
/
result.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package internal
import (
"github.com/AdguardTeam/AdGuardDNS/internal/agd"
"github.com/AdguardTeam/AdGuardDNS/internal/dnsmsg"
"github.com/miekg/dns"
)
// Filtering Results
// Result is a sum type of all possible filtering actions. See the following
// types as implementations:
//
// - [*ResultAllowed]
// - [*ResultBlocked]
// - [*ResultModified]
type Result interface {
// MatchedRule returns data about the matched rule and its rule list.
MatchedRule() (id agd.FilterListID, text agd.FilterRuleText)
// isResult is a marker method.
isResult()
}
// ResultAllowed means that this request or response was allowed by an allowlist
// rule within the given filter list.
type ResultAllowed struct {
List agd.FilterListID
Rule agd.FilterRuleText
}
// type check
var _ Result = (*ResultAllowed)(nil)
// MatchedRule implements the Result interface for *ResultAllowed.
func (a *ResultAllowed) MatchedRule() (id agd.FilterListID, text agd.FilterRuleText) {
return a.List, a.Rule
}
// isResult implements the Result interface for *ResultAllowed.
func (*ResultAllowed) isResult() {}
// ResultBlocked means that this request or response was blocked by a blocklist
// rule within the given filter list.
type ResultBlocked struct {
List agd.FilterListID
Rule agd.FilterRuleText
}
// type check
var _ Result = (*ResultBlocked)(nil)
// MatchedRule implements the Result interface for *ResultBlocked.
func (b *ResultBlocked) MatchedRule() (id agd.FilterListID, text agd.FilterRuleText) {
return b.List, b.Rule
}
// isResult implements the Result interface for *ResultBlocked.
func (*ResultBlocked) isResult() {}
// ResultModified means that this request or response was rewritten or modified
// by a rewrite rule within the given filter list.
type ResultModified struct {
// Msg is the new, rewritten or modified request or response.
Msg *dns.Msg
// List is the ID of the filter list.
List agd.FilterListID
// Rule is the filtering rule that triggered the rewrite.
Rule agd.FilterRuleText
}
// type check
var _ Result = (*ResultModified)(nil)
// MatchedRule implements the Result interface for *ResultModified.
func (m *ResultModified) MatchedRule() (id agd.FilterListID, text agd.FilterRuleText) {
return m.List, m.Rule
}
// isResult implements the Result interface for *ResultModified.
func (*ResultModified) isResult() {}
// Clone returns a deep clone of m.
func (m *ResultModified) Clone(c *dnsmsg.Cloner) (clone *ResultModified) {
msg := c.Clone(m.Msg)
return &ResultModified{
Msg: msg,
List: m.List,
Rule: m.Rule,
}
}
// CloneForReq returns a deep clone of m with Msg set as a reply to req, if any.
func (m *ResultModified) CloneForReq(c *dnsmsg.Cloner, req *dns.Msg) (clone *ResultModified) {
msg := c.Clone(m.Msg)
// TODO(a.garipov): This will become invalid if Msg ever contains a
// non-success response, which is not the case currently. If that happens,
// find a better way to cache as much of the response as possible.
msg.SetReply(req)
return &ResultModified{
Msg: msg,
List: m.List,
Rule: m.Rule,
}
}