-
Notifications
You must be signed in to change notification settings - Fork 63
/
blocklist.go
220 lines (192 loc) · 6.06 KB
/
blocklist.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
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
package rdns
import (
"errors"
"expvar"
"net"
"sync"
"time"
"github.com/miekg/dns"
"github.com/sirupsen/logrus"
)
// Blocklist is a resolver that returns NXDOMAIN or a spoofed IP for every query that
// matches. Everything else is passed through to another resolver.
type Blocklist struct {
id string
BlocklistOptions
resolver Resolver
mu sync.RWMutex
metrics *BlocklistMetrics
}
var _ Resolver = &Blocklist{}
type BlocklistOptions struct {
// Optional, send any blocklist match to this resolver rather
// than return NXDOMAIN.
BlocklistResolver Resolver
BlocklistDB BlocklistDB
// Refresh period for the blocklist. Disabled if 0.
BlocklistRefresh time.Duration
// Optional, send anything that matches the allowlist to an
// alternative resolver rather than the default upstream one.
AllowListResolver Resolver
// Rules that override the blocklist rules, effectively negate them.
AllowlistDB BlocklistDB
// Refresh period for the allowlist. Disabled if 0.
AllowlistRefresh time.Duration
// Optional, allows specifying extended errors to be used in the
// response when blocking.
EDNS0EDETemplate *EDNS0EDETemplate
}
type BlocklistMetrics struct {
// Blocked queries count.
blocked *expvar.Int
// Allowed queries count.
allowed *expvar.Int
}
const (
// Max number of name records to reply with for PTR lookups
maxPTRResponses = 10
)
func NewBlocklistMetrics(id string) *BlocklistMetrics {
return &BlocklistMetrics{
allowed: getVarInt("router", id, "allow"),
blocked: getVarInt("router", id, "deny"),
}
}
// NewBlocklist returns a new instance of a blocklist resolver.
func NewBlocklist(id string, resolver Resolver, opt BlocklistOptions) (*Blocklist, error) {
blocklist := &Blocklist{
id: id,
resolver: resolver,
BlocklistOptions: opt,
metrics: NewBlocklistMetrics(id),
}
// Start the refresh goroutines if we have a list and a refresh period was given
if blocklist.BlocklistDB != nil && blocklist.BlocklistRefresh > 0 {
go blocklist.refreshLoopBlocklist(blocklist.BlocklistRefresh)
}
if blocklist.AllowlistDB != nil && blocklist.AllowlistRefresh > 0 {
go blocklist.refreshLoopAllowlist(blocklist.AllowlistRefresh)
}
return blocklist, nil
}
// Resolve a DNS query by first checking the query against the provided matcher.
// Queries that do not match are passed on to the next resolver.
func (r *Blocklist) Resolve(q *dns.Msg, ci ClientInfo) (*dns.Msg, error) {
if len(q.Question) < 1 {
return nil, errors.New("no question in query")
}
question := q.Question[0]
log := logger(r.id, q, ci)
r.mu.RLock()
blocklistDB := r.BlocklistDB
allowlistDB := r.AllowlistDB
r.mu.RUnlock()
// Forward to upstream or the optional allowlist-resolver immediately if there's a match in the allowlist
if allowlistDB != nil {
if _, _, match, ok := allowlistDB.Match(question); ok {
log = log.WithFields(logrus.Fields{"list": match.List, "rule": match.Rule})
r.metrics.allowed.Add(1)
if r.AllowListResolver != nil {
log.WithField("resolver", r.AllowListResolver.String()).Debug("matched allowlist, forwarding")
return r.AllowListResolver.Resolve(q, ci)
}
log.WithField("resolver", r.resolver.String()).Debug("matched allowlist, forwarding")
return r.resolver.Resolve(q, ci)
}
}
ips, names, match, ok := blocklistDB.Match(question)
if !ok {
// Didn't match anything, pass it on to the next resolver
log.WithField("resolver", r.resolver.String()).Debug("forwarding unmodified query to resolver")
r.metrics.allowed.Add(1)
return r.resolver.Resolve(q, ci)
}
log = log.WithFields(logrus.Fields{"list": match.List, "rule": match.Rule})
r.metrics.blocked.Add(1)
// If we got names for the PTR query, respond to it
if question.Qtype == dns.TypePTR && len(names) > 0 {
log.Debug("responding with ptr blocklist from blocklist")
if len(names) > maxPTRResponses {
names = names[:maxPTRResponses]
}
return ptr(q, names), nil
}
// If an optional blocklist-resolver was given, send the query to that instead of returning NXDOMAIN.
if r.BlocklistResolver != nil {
log.WithField("resolver", r.BlocklistResolver.String()).Debug("matched blocklist, forwarding")
return r.BlocklistResolver.Resolve(q, ci)
}
answer := new(dns.Msg)
answer.SetReply(q)
answer.RecursionAvailable = q.RecursionDesired
// We have an IP address to return, make sure it's of the right type. If not return NXDOMAIN.
var spoof []dns.RR
for _, ip := range ips {
if ip4 := ip.To4(); len(ip4) == net.IPv4len && question.Qtype == dns.TypeA {
spoof = append(spoof, &dns.A{
Hdr: dns.RR_Header{
Name: question.Name,
Rrtype: dns.TypeA,
Class: question.Qclass,
Ttl: 3600,
},
A: ip,
})
} else if len(ip) == net.IPv6len && question.Qtype == dns.TypeAAAA {
spoof = append(spoof, &dns.AAAA{
Hdr: dns.RR_Header{
Name: question.Name,
Rrtype: dns.TypeAAAA,
Class: question.Qclass,
Ttl: 3600,
},
AAAA: ip,
})
}
}
if len(spoof) > 0 {
log.Debug("spoofing response")
answer.Answer = spoof
return answer, nil
}
// Block the request with NXDOMAIN if there was a match but no valid spoofed IP is given
log.Debug("blocking request")
if err := r.EDNS0EDETemplate.Apply(answer, EDNS0EDEInput{q, match}); err != nil {
log.WithError(err).Error("failed to apply edns0ede template")
}
answer.SetRcode(q, dns.RcodeNameError)
return answer, nil
}
func (r *Blocklist) String() string {
return r.id
}
func (r *Blocklist) refreshLoopBlocklist(refresh time.Duration) {
for {
time.Sleep(refresh)
log := Log.WithField("id", r.id)
log.Debug("reloading blocklist")
db, err := r.BlocklistDB.Reload()
if err != nil {
log.WithError(err).Error("failed to load rules")
continue
}
r.mu.Lock()
r.BlocklistDB = db
r.mu.Unlock()
}
}
func (r *Blocklist) refreshLoopAllowlist(refresh time.Duration) {
for {
time.Sleep(refresh)
log := Log.WithField("id", r.id)
log.Debug("reloading allowlist")
db, err := r.AllowlistDB.Reload()
if err != nil {
log.WithError(err).Error("failed to load rules")
continue
}
r.mu.Lock()
r.AllowlistDB = db
r.mu.Unlock()
}
}