Repository navigation
Expand file tree
/
Copy pathfilter.go
More file actions
245 lines (206 loc) · 6.02 KB
/
Copy pathfilter.go
File metadata and controls
245 lines (206 loc) · 6.02 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package filter
import (
"context"
"net/netip"
"regexp"
"strings"
"sync"
"time"
"github.com/coredns/coredns/plugin"
clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
var log = clog.NewWithPlugin("filter")
// Filter checks if requested domains are blocked then returns the configured
// response
type Filter struct {
Next plugin.Handler
sync.RWMutex
allowConfig ActionConfig
allowDomains map[string]bool
allowRegex []*regexp.Regexp
allowWildcards map[string]bool
blockConfig ActionConfig
blockDomains map[string]bool
blockRegex []*regexp.Regexp
blockWildcards map[string]bool
response Response
startupOnce sync.Once
updateInterval time.Duration
updateShutdown chan bool
}
func newFilter() *Filter {
return &Filter{
allowConfig: NewActionConfig(ActionTypeAllow),
allowDomains: make(map[string]bool),
allowRegex: make([]*regexp.Regexp, 0),
allowWildcards: make(map[string]bool),
blockConfig: NewActionConfig(ActionTypeBlock),
blockDomains: make(map[string]bool),
blockRegex: make([]*regexp.Regexp, 0),
blockWildcards: make(map[string]bool),
response: RespAddress{
IP4: netip.IPv4Unspecified(),
IP6: netip.IPv6Unspecified(),
},
updateInterval: 24 * time.Hour,
updateShutdown: make(chan bool),
}
}
// Name implements the plugin.Handler interface
// Returns the name of the CoreDNS plugin, in this case "filter"
func (f *Filter) Name() string {
return "filter"
}
// ServeDNS implements the plugin.Handler interface
// Checks whether the requested domain is allowed or blocked.
// Allowed domains are passed to the next plugin in the Corefile. Blocked
// domains return the configured response.
func (f *Filter) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
state := request.Request{W: w, Req: r}
qname := strings.TrimSuffix(state.Name(), ".")
var allowed, blocked bool
f.RLock()
allowed = f.isAllowed(qname)
if !allowed {
blocked = f.isBlocked(qname)
}
f.RUnlock()
if !allowed && blocked {
log.Debugf("blocking %q", qname)
msg := new(dns.Msg)
msg.SetReply(r)
msg.RecursionAvailable = false
response := f.response.Render(state.Name(), state.QType())
msg.Authoritative = response.Authoritative
msg.Answer = response.Answer
w.WriteMsg(msg)
return response.RCode, nil
}
return plugin.NextOrFailure(state.Name(), f.Next, ctx, w, r)
}
func (f *Filter) isAllowed(qname string) bool {
if _, ok := f.allowDomains[qname]; ok {
log.Debugf("request %q matched allowed domain", qname)
return true
}
if wildcard, ok := matchesAnyWildcard(qname, f.allowWildcards); ok {
log.Debugf("request %q matched allow wildcard %q", qname, wildcard)
return true
}
// Evaluate regular expressions last, as they're the most expensive
for _, exp := range f.allowRegex {
if exp.MatchString(qname) {
log.Debugf("request %q matched allow regex", qname)
return true
}
}
return false
}
func (f *Filter) isBlocked(qname string) bool {
if _, ok := f.blockDomains[qname]; ok {
log.Debugf("request %q matched blocked domain", qname)
return true
}
if wildcard, ok := matchesAnyWildcard(qname, f.blockWildcards); ok {
log.Debugf("request %q matched block wildcard %q", qname, wildcard)
return true
}
// Evaluate regular expressions last, as they're the most expensive
for _, exp := range f.blockRegex {
if exp.MatchString(qname) {
log.Debugf("request %q matched block regex", qname)
return true
}
}
return false
}
func matchesAnyWildcard(qname string, wildcards map[string]bool) (string, bool) {
if wildcards[qname] {
return qname, true
}
// Wildcards only match at subdomain boundaries. Test the qname with each
// subdomain removed until we either match or run out of subdomains.
for i, c := range qname {
if c == '.' {
wildcard := qname[i+1:]
if wildcards[wildcard] {
return wildcard, true
}
}
}
return "", false
}
// OnShutdown cleans up the filter and prepares it for removal
func (f *Filter) OnShutdown() error {
if 0 < f.updateInterval {
f.updateShutdown <- true
}
return nil
}
// Build the domain and regular expression lists used to determine how domains
// are handled
func (f *Filter) Build() {
var allowDomains = make(map[string]bool)
f.allowConfig.BuildDomains(allowDomains)
f.allowConfig.BuildHosts(allowDomains)
var blockDomains = make(map[string]bool)
f.blockConfig.BuildDomains(blockDomains)
f.blockConfig.BuildHosts(blockDomains)
var allowRegexBuilder = make(map[string]*regexp.Regexp)
f.allowConfig.BuildRegExps(allowRegexBuilder)
allowRegex := f.consolidateRegex(allowRegexBuilder)
var blockRegexBuilder = make(map[string]*regexp.Regexp)
f.blockConfig.BuildRegExps(blockRegexBuilder)
blockRegex := f.consolidateRegex(blockRegexBuilder)
var allowWildcards = make(map[string]bool)
f.allowConfig.BuildWildcards(allowWildcards)
var blockWildcards = make(map[string]bool)
f.blockConfig.BuildWildcards(blockWildcards)
f.Lock()
f.allowDomains = allowDomains
f.allowRegex = allowRegex
f.allowWildcards = allowWildcards
f.blockDomains = blockDomains
f.blockRegex = blockRegex
f.blockWildcards = blockWildcards
f.Unlock()
log.Infof(
"Successfully updated filter; "+
"%d allowed domains, %d allowed regular expressions, %d allowed wildcards; "+
"%d blocked domains, %d blocked regular expressions, %d blocked wildcards",
len(f.allowDomains),
len(f.allowRegex),
len(f.allowWildcards),
len(f.blockDomains),
len(f.blockRegex),
len(f.blockWildcards),
)
}
func (f *Filter) consolidateRegex(regexes map[string]*regexp.Regexp) []*regexp.Regexp {
out := make([]*regexp.Regexp, 0, len(regexes))
for _, expr := range regexes {
out = append(out, expr)
}
return out
}
// InitUpdate starts the update timer. This should only be run once on startup.
func (f *Filter) InitUpdate() error {
if f.updateInterval == 0 {
return nil
}
tick := time.NewTicker(f.updateInterval)
go func() {
for {
select {
case <-tick.C:
f.Build()
case <-f.updateShutdown:
tick.Stop()
return
}
}
}()
return nil
}