-
Notifications
You must be signed in to change notification settings - Fork 2
/
filter.go
147 lines (121 loc) · 3.12 KB
/
filter.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
package filter
import (
"context"
"strings"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/metrics"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
const defaultResponseTTL = 3600 // Default TTL used for generated responses.
// Filter represents a plugin instance that can filter and block requests based
// on predefined lists and regex rules.
type Filter struct {
Next plugin.Handler
allowlist *PatternMatcher
denylist *PatternMatcher
// sources to load data into filters.
sources []listSource
// uncloak enables response inspection for CNAME cloaking.
uncloak bool
// ttl sets a custom TTL.
ttl uint32
}
func New() *Filter {
return &Filter{
allowlist: NewPatternMatcher(),
denylist: NewPatternMatcher(),
ttl: defaultResponseTTL,
}
}
// ServeDNS implements the plugin.Handler interface.
func (f *Filter) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
state := request.Request{W: w, Req: r}
server := metrics.WithServer(ctx)
if f.Match(state.Name()) {
BlockCount.WithLabelValues(server).Inc()
msg := createSyntheticResponse(r, f.ttl)
w.WriteMsg(msg) //nolint
return dns.RcodeSuccess, nil
}
if f.uncloak {
rw := &ResponseWriter{ResponseWriter: w, state: state, server: server, Filter: f}
return plugin.NextOrFailure(f.Name(), f.Next, ctx, rw, r)
}
return plugin.NextOrFailure(f.Name(), f.Next, ctx, w, r)
}
// Name implements the plugin.Handler interface.
func (f *Filter) Name() string {
return "filter"
}
// Match determines if the requested name should be blocked or allowed.
func (f *Filter) Match(name string) bool {
if f.allowlist.Match(name) {
return false
}
if f.denylist.Match(name) {
return true
}
return false
}
func (f *Filter) Load() error {
for _, src := range f.sources {
rc, err := src.Open()
if err != nil {
return err
}
defer rc.Close()
if src.IsBlock {
if err := f.denylist.LoadRules(rc); err != nil {
return err
}
} else {
if err := f.allowlist.LoadRules(rc); err != nil {
return err
}
}
}
return nil
}
// ResponseWriter is a response writer that performs response uncloaking.
type ResponseWriter struct {
dns.ResponseWriter
*Filter
state request.Request
server string
}
// WriteMsg implements the dns.ResponseWriter interface.
func (w *ResponseWriter) WriteMsg(m *dns.Msg) error {
if m.Rcode != dns.RcodeSuccess {
return w.ResponseWriter.WriteMsg(m)
}
if w.allowlist.Match(w.state.Name()) {
return w.ResponseWriter.WriteMsg(m)
}
for _, r := range m.Answer {
header := r.Header()
if header.Class != dns.ClassINET {
continue
}
var target string
switch header.Rrtype {
case dns.TypeCNAME:
target = r.(*dns.CNAME).Target //nolint
case dns.TypeSVCB:
target = r.(*dns.SVCB).Target //nolint
case dns.TypeHTTPS:
target = r.(*dns.HTTPS).Target //nolint
default:
continue
}
target = strings.TrimSuffix(target, ".")
if w.Match(target) {
BlockCount.WithLabelValues(w.server).Inc()
r := w.state.Req
msg := createSyntheticResponse(r, w.ttl)
w.WriteMsg(msg) //nolint
return nil
}
}
return w.ResponseWriter.WriteMsg(m)
}