-
Notifications
You must be signed in to change notification settings - Fork 67
/
filter.go
316 lines (248 loc) · 6.61 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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package main
import (
"context"
"fmt"
"net"
"strings"
"time"
"github.com/miekg/dns"
)
const (
isatapHostname = "isatap"
defaultLookupTimeout = 500 * time.Millisecond
)
func containsDomain(haystack []string, needle string) bool {
needle = strings.ToLower(strings.TrimRight(needle, "."))
for _, el := range haystack {
// dot matches non-fqdns
if el == "." {
if !strings.Contains(needle, ".") {
return true
}
continue
}
el := strings.ToLower(strings.TrimRight(el, "."))
if strings.HasPrefix(el, ".") && strings.HasSuffix(needle, strings.TrimLeft(el, ".")) {
return true
} else if strings.EqualFold(el, needle) {
return true
}
}
return false
}
func shouldRespondToNameResolutionQuery(config Config, host string, queryType uint16,
from net.IP, fromHostnames []string,
) (bool, string) {
if config.DryMode {
return false, "dry mode"
}
if strings.HasPrefix(strings.ToLower(host), isatapHostname) {
return false, "ISATAP is always ignored"
}
if queryType == dns.TypeSOA && config.SOAHostname == "" {
return false, "no SOA hostname configured"
}
if len(config.SpoofFor) > 0 && !containsIP(config.SpoofFor, from) &&
!containsAnyHostname(config.SpoofFor, fromHostnames) {
return false, "host address and name not in spoof-for list"
}
if len(config.DontSpoofFor) > 0 {
if containsIP(config.DontSpoofFor, from) {
return false, "host address included in dont-spoof-for list"
}
if containsAnyHostname(config.DontSpoofFor, fromHostnames) {
return false, "hostname included in dont-spoof-for list"
}
}
if len(config.Spoof) > 0 && !containsDomain(config.Spoof, host) {
return false, "domain not in spoof list"
}
if len(config.DontSpoof) > 0 && containsDomain(config.DontSpoof, host) {
return false, "domain included in dont-spoof list"
}
if !config.SpoofTypes.ShouldSpoof(queryType) {
return false, fmt.Sprintf("type %s is not in spoof-types", dnsQueryType(queryType))
}
switch {
case queryType == dns.TypeA && config.RelayIPv4 == nil:
return false, "no IPv4 relay address configured"
case queryType == dns.TypeAAAA && config.RelayIPv6 == nil:
return false, "no IPv6 relay address configured"
}
return true, ""
}
func shouldRespondToDHCP(config Config, from peerInfo) (bool, string) {
if config.DryMode && !config.DryWithDHCPv6Mode {
return false, "dry mode"
}
if len(from.Hostnames) == 0 && config.IgnoreDHCPv6NoFQDN {
return false, "no FQDN in DHCPv6 message"
}
if len(config.SpoofFor) > 0 && !containsPeer(config.SpoofFor, from) {
return false, "host not in spoof-for list"
}
if len(config.DontSpoofFor) > 0 && containsPeer(config.DontSpoofFor, from) {
return false, "host included in dont-spoof-for list"
}
return true, ""
}
type hostMatcher struct {
IPs []net.IP
Hostname string
}
var hostMatcherLookupFunction = lookupIPWithTimeout
func newHostMatcher(hostnameOrIP string, dnsTimeout time.Duration) *hostMatcher {
ip := net.ParseIP(hostnameOrIP)
if ip != nil { // hostnameOrIP is an IP
return &hostMatcher{IPs: []net.IP{ip}}
}
// domain is a wildcard
if strings.HasPrefix(hostnameOrIP, ".") {
return &hostMatcher{Hostname: hostnameOrIP}
}
// hostnameOrIP is not an IP
ips, _ := hostMatcherLookupFunction(hostnameOrIP, dnsTimeout)
return &hostMatcher{
IPs: ips,
Hostname: hostnameOrIP,
}
}
func asHostMatchers(hostnamesOrIPs []string, dnsTimeout time.Duration) []*hostMatcher {
hosts := make([]*hostMatcher, 0, len(hostnamesOrIPs))
for _, hostnameOrIP := range hostnamesOrIPs {
hosts = append(hosts, newHostMatcher(strings.TrimSpace(hostnameOrIP), dnsTimeout))
}
return hosts
}
func normalizeHostname(hostname string) string {
return strings.ToLower(strings.TrimRight(hostname, "."))
}
// Matches determines whether or not the host matches any of the provided hostnames.
func (h *hostMatcher) MatchesAnyHostname(hostnames ...string) bool {
if h.Hostname == "" {
return false
}
thisHostname := normalizeHostname(h.Hostname)
for _, hostname := range hostnames {
otherHostname := normalizeHostname(hostname)
// hostname matches
if thisHostname == otherHostname {
return true
}
// subdomain matches
if strings.HasPrefix(thisHostname, ".") && strings.HasSuffix(otherHostname, thisHostname) {
return true
}
}
return false
}
func (h *hostMatcher) String() string {
if len(h.IPs) == 0 && h.Hostname == "" {
return "no host"
}
ipStrings := make([]string, 0, len(h.IPs))
for _, ip := range h.IPs {
ipStrings = append(ipStrings, ip.String())
}
ipsString := strings.Join(ipStrings, ", ")
if h.Hostname == "" {
return ipsString
}
if len(h.IPs) == 0 {
return h.Hostname
}
return fmt.Sprintf("%s (%s)", h.Hostname, ipsString)
}
type spoofTypes struct {
A bool
AAAA bool
ANY bool
SOA bool
}
func parseSpoofTypes(spoofTypesStrings []string) (*spoofTypes, error) {
if len(spoofTypesStrings) == 0 {
return nil, nil //nolint:nilnil
}
st := &spoofTypes{}
for _, spoofType := range spoofTypesStrings {
switch strings.ToLower(spoofType) {
case "a":
st.A = true
case "aaaa":
st.AAAA = true
case "any":
st.ANY = true
case "soa":
st.SOA = true
default:
return nil, fmt.Errorf("unknown query type: %s", spoofType)
}
}
return st, nil
}
func (st *spoofTypes) ShouldSpoof(qType uint16) bool {
if st == nil {
return true
}
switch {
case qType == typeNetBios:
return true
case qType == dns.TypeA && st.A:
return true
case qType == dns.TypeAAAA && st.AAAA:
return true
case qType == dns.TypeANY && st.ANY:
return true
case qType == dns.TypeSOA && st.SOA:
return true
default:
return false
}
}
func containsPeer(hosts []*hostMatcher, peer peerInfo) bool {
for _, host := range hosts {
if host.MatchesAnyHostname(peer.Hostnames...) {
return true
}
for _, ip := range host.IPs {
if peer.IP.Equal(ip) {
return true
}
}
}
return false
}
func containsIP(haystack []*hostMatcher, needle net.IP) bool {
for _, el := range haystack {
for _, ip := range el.IPs {
if needle.Equal(ip) {
return true
}
}
}
return false
}
func containsAnyHostname(haystack []*hostMatcher, needles []string) bool {
for _, el := range haystack {
if el.MatchesAnyHostname(needles...) {
return true
}
}
return false
}
func lookupIPWithTimeout(hostname string, timeout time.Duration) ([]net.IP, error) {
if hostname == "" {
return nil, nil
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
addrs, err := net.DefaultResolver.LookupIPAddr(ctx, hostname)
if err != nil {
return nil, err
}
ips := make([]net.IP, 0, len(addrs))
for _, addr := range addrs {
ips = append(ips, addr.IP)
}
return ips, nil
}