-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathmain.go
723 lines (694 loc) · 21 KB
/
main.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
package main
import (
"errors"
"fmt"
"net"
"net/url"
"os"
"time"
rdns "github.com/folbricht/routedns"
"github.com/heimdalr/dag"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
type options struct {
logLevel uint32
}
func main() {
var opt options
cmd := &cobra.Command{
Use: "routedns <config> [<config>..]",
Short: "DNS stub resolver, proxy and router",
Long: `DNS stub resolver, proxy and router.
Listens for incoming DNS requests, routes, modifies and
forwards to upstream resolvers. Supports plain DNS over
UDP and TCP as well as DNS-over-TLS and DNS-over-HTTPS
as listener and client protocols.
Routes can be defined to send requests for certain queries;
by record type, query name or client-IP to different modifiers
or upstream resolvers.
Configuration can be split over multiple files with listeners,
groups and routers defined in different files and provided as
arguments.
`,
Example: ` routedns config.toml`,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return start(opt, args)
},
SilenceUsage: true,
}
cmd.Flags().Uint32VarP(&opt.logLevel, "log-level", "l", 4, "log level; 0=None .. 6=Trace")
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
}
type Node struct {
id string
value interface{}
}
var _ dag.IDInterface = Node{}
func (n Node) ID() string {
return n.id
}
func start(opt options, args []string) error {
// Set the log level in the library package
if opt.logLevel > 6 {
return fmt.Errorf("invalid log level: %d", opt.logLevel)
}
rdns.Log.SetLevel(logrus.Level(opt.logLevel))
config, err := loadConfig(args...)
if err != nil {
return err
}
// Map to hold all the resolvers extracted from the config, key'ed by resolver ID. It
// holds configured resolvers, groups, as well as routers (since they all implement
// rdns.Resolver)
resolvers := make(map[string]rdns.Resolver)
// See if a bootstrap-resolver was defined in the config. If so, instantiate it,
// wrap it in a net.Resolver wrapper and replace the net.DefaultResolver with it
// for all other entities to use.
if config.BootstrapResolver.Address != "" {
if err := instantiateResolver("bootstrap-resolver", config.BootstrapResolver, resolvers); err != nil {
return fmt.Errorf("failed to instantiate bootstrap-resolver: %w", err)
}
net.DefaultResolver = rdns.NewNetResolver(resolvers["bootstrap-resolver"])
}
// Add all types of nodes to a DAG, this is to find duplicates. Then populate the edges (dependencies).
graph := dag.NewDAG()
edges := make(map[string][]string)
for id, v := range config.Resolvers {
node := &Node{id, v}
_, err := graph.AddVertex(node)
if err != nil {
return err
}
}
for id, v := range config.Groups {
node := &Node{id, v}
_, err := graph.AddVertex(node)
if err != nil {
return err
}
edges[id] = append(v.Resolvers, v.AllowListResolver, v.BlockListResolver, v.LimitResolver, v.RetryResolver)
}
for id, v := range config.Routers {
node := &Node{id, v}
_, err := graph.AddVertex(node)
if err != nil {
return err
}
// One router can have multiple edges to the same resolver.
// Dedup them before adding to the list of edges.
dep := make(map[string]struct{})
for _, route := range v.Routes {
dep[route.Resolver] = struct{}{}
}
for r := range dep {
edges[id] = append(edges[id], r)
}
}
// Add the edges to the DAG. This will fail if there are duplicate edges, recursion or missing nodes
for id, es := range edges {
for _, e := range es {
if e == "" {
continue
}
if err := graph.AddEdge(id, e); err != nil {
return err
}
}
}
// Instantiate the elements from leaves to the root nodes
for graph.GetOrder() > 0 {
leaves := graph.GetLeaves()
for id, v := range leaves {
node := v.(*Node)
if r, ok := node.value.(resolver); ok {
if err := instantiateResolver(id, r, resolvers); err != nil {
return err
}
}
if g, ok := node.value.(group); ok {
if err := instantiateGroup(id, g, resolvers); err != nil {
return err
}
}
if r, ok := node.value.(router); ok {
if err := instantiateRouter(id, r, resolvers); err != nil {
return err
}
}
if err := graph.DeleteVertex(id); err != nil {
return err
}
}
}
// Build the Listeners last as they can point to routers, groups or resolvers directly.
var listeners []rdns.Listener
for id, l := range config.Listeners {
resolver, ok := resolvers[l.Resolver]
// All Listeners should route queries (except the admin service).
if !ok && l.Protocol != "admin" {
return fmt.Errorf("listener '%s' references non-existant resolver, group or router '%s'", id, l.Resolver)
}
allowedNet, err := parseCIDRList(l.AllowedNet)
if err != nil {
return err
}
opt := rdns.ListenOptions{AllowedNet: allowedNet}
switch l.Protocol {
case "tcp":
listeners = append(listeners, rdns.NewDNSListener(id, l.Address, "tcp", opt, resolver))
case "udp":
listeners = append(listeners, rdns.NewDNSListener(id, l.Address, "udp", opt, resolver))
case "admin":
tlsConfig, err := rdns.TLSServerConfig(l.CA, l.ServerCrt, l.ServerKey, l.MutualTLS)
if err != nil {
return err
}
opt := rdns.AdminListenerOptions{
TLSConfig: tlsConfig,
ListenOptions: opt,
Transport: l.Transport,
}
ln, err := rdns.NewAdminListener(id, l.Address, opt)
if err != nil {
return err
}
listeners = append(listeners, ln)
case "dot":
tlsConfig, err := rdns.TLSServerConfig(l.CA, l.ServerCrt, l.ServerKey, l.MutualTLS)
if err != nil {
return err
}
ln := rdns.NewDoTListener(id, l.Address, rdns.DoTListenerOptions{TLSConfig: tlsConfig, ListenOptions: opt}, resolver)
listeners = append(listeners, ln)
case "dtls":
dtlsConfig, err := rdns.DTLSServerConfig(l.CA, l.ServerCrt, l.ServerKey, l.MutualTLS)
if err != nil {
return err
}
ln := rdns.NewDTLSListener(id, l.Address, rdns.DTLSListenerOptions{DTLSConfig: dtlsConfig, ListenOptions: opt}, resolver)
listeners = append(listeners, ln)
case "doh":
tlsConfig, err := rdns.TLSServerConfig(l.CA, l.ServerCrt, l.ServerKey, l.MutualTLS)
if err != nil {
return err
}
var httpProxyNet *net.IPNet
if l.Frontend.HTTPProxyNet != "" {
_, httpProxyNet, err = net.ParseCIDR(l.Frontend.HTTPProxyNet)
if err != nil {
return fmt.Errorf("listener '%s' trusted-proxy '%s': %v", id, l.Frontend.HTTPProxyNet, err)
}
}
opt := rdns.DoHListenerOptions{
TLSConfig: tlsConfig,
ListenOptions: opt,
Transport: l.Transport,
HTTPProxyNet: httpProxyNet,
}
ln, err := rdns.NewDoHListener(id, l.Address, opt, resolver)
if err != nil {
return err
}
listeners = append(listeners, ln)
case "doq":
tlsConfig, err := rdns.TLSServerConfig(l.CA, l.ServerCrt, l.ServerKey, l.MutualTLS)
if err != nil {
return err
}
ln := rdns.NewQUICListener(id, l.Address, rdns.DoQListenerOptions{TLSConfig: tlsConfig, ListenOptions: opt}, resolver)
listeners = append(listeners, ln)
default:
return fmt.Errorf("unsupported protocol '%s' for listener '%s'", l.Protocol, id)
}
}
// Start the listeners
for _, l := range listeners {
go func(l rdns.Listener) {
for {
err := l.Start()
rdns.Log.WithError(err).Error("listener failed")
time.Sleep(time.Second)
}
}(l)
}
select {}
}
// Instantiate a group object based on configuration and add to the map of resolvers by ID.
func instantiateGroup(id string, g group, resolvers map[string]rdns.Resolver) error {
var gr []rdns.Resolver
var err error
for _, rid := range g.Resolvers {
resolver, ok := resolvers[rid]
if !ok {
return fmt.Errorf("group '%s' references non-existant resolver or group '%s'", id, rid)
}
gr = append(gr, resolver)
}
switch g.Type {
case "lock-duplicate-request":
if len(gr) != 1 {
return fmt.Errorf("type lock-duplicate-request only supports one resolver in '%s'", id)
}
resolvers[id] = rdns.NewlockDuplicateRequest(id, gr[0])
case "round-robin":
resolvers[id] = rdns.NewRoundRobin(id, gr...)
case "fail-rotate":
opt := rdns.FailRotateOptions{
ServfailError: g.ServfailError,
}
resolvers[id] = rdns.NewFailRotate(id, opt, gr...)
case "fail-back":
opt := rdns.FailBackOptions{
ResetAfter: time.Duration(g.ResetAfter),
ServfailError: g.ServfailError,
}
resolvers[id] = rdns.NewFailBack(id, opt, gr...)
case "fastest":
resolvers[id] = rdns.NewFastest(id, gr...)
case "random":
opt := rdns.RandomOptions{
ResetAfter: time.Duration(g.ResetAfter),
ServfailError: g.ServfailError,
}
resolvers[id] = rdns.NewRandom(id, opt, gr...)
case "blocklist":
if len(gr) != 1 {
return fmt.Errorf("type blocklist only supports one resolver in '%s'", id)
}
if len(g.Blocklist) > 0 && g.Source != "" {
return fmt.Errorf("static blocklist can't be used with 'source' in '%s'", id)
}
blocklistDB, err := newBlocklistDB(list{Format: g.Format, Source: g.Source}, g.Blocklist)
if err != nil {
return err
}
opt := rdns.BlocklistOptions{
BlocklistDB: blocklistDB,
BlocklistRefresh: time.Duration(g.Refresh) * time.Second,
}
resolvers[id], err = rdns.NewBlocklist(id, gr[0], opt)
if err != nil {
return err
}
case "blocklist-v2":
if len(gr) != 1 {
return fmt.Errorf("type blocklist-v2 only supports one resolver in '%s'", id)
}
if len(g.Blocklist) > 0 && len(g.BlocklistSource) > 0 {
return fmt.Errorf("static blocklist can't be used with 'source' in '%s'", id)
}
if len(g.Allowlist) > 0 && len(g.AllowlistSource) > 0 {
return fmt.Errorf("static allowlist can't be used with 'source' in '%s'", id)
}
var blocklistDB rdns.BlocklistDB
if len(g.Blocklist) > 0 {
blocklistDB, err = newBlocklistDB(list{Format: g.BlocklistFormat}, g.Blocklist)
if err != nil {
return err
}
} else {
var dbs []rdns.BlocklistDB
for _, s := range g.BlocklistSource {
db, err := newBlocklistDB(s, nil)
if err != nil {
return fmt.Errorf("%s: %w", id, err)
}
dbs = append(dbs, db)
}
blocklistDB, err = rdns.NewMultiDB(dbs...)
if err != nil {
return err
}
}
var allowlistDB rdns.BlocklistDB
if len(g.Allowlist) > 0 {
allowlistDB, err = newBlocklistDB(list{Format: g.BlocklistFormat}, g.Allowlist)
if err != nil {
return err
}
} else {
var dbs []rdns.BlocklistDB
for _, s := range g.AllowlistSource {
db, err := newBlocklistDB(s, nil)
if err != nil {
return fmt.Errorf("%s: %w", id, err)
}
dbs = append(dbs, db)
}
allowlistDB, err = rdns.NewMultiDB(dbs...)
if err != nil {
return err
}
}
opt := rdns.BlocklistOptions{
BlocklistResolver: resolvers[g.BlockListResolver],
BlocklistDB: blocklistDB,
BlocklistRefresh: time.Duration(g.BlocklistRefresh) * time.Second,
AllowListResolver: resolvers[g.AllowListResolver],
AllowlistDB: allowlistDB,
AllowlistRefresh: time.Duration(g.AllowlistRefresh) * time.Second,
}
resolvers[id], err = rdns.NewBlocklist(id, gr[0], opt)
if err != nil {
return err
}
case "replace":
if len(gr) != 1 {
return fmt.Errorf("type replace only supports one resolver in '%s'", id)
}
resolvers[id], err = rdns.NewReplace(id, gr[0], g.Replace...)
if err != nil {
return err
}
case "ttl-modifier":
if len(gr) != 1 {
return fmt.Errorf("type ttl-modifier only supports one resolver in '%s'", id)
}
opt := rdns.TTLModifierOptions{
MinTTL: g.TTLMin,
MaxTTL: g.TTLMax,
}
resolvers[id] = rdns.NewTTLModifier(id, gr[0], opt)
case "truncate-retry":
if len(gr) != 1 {
return fmt.Errorf("type truncate-retry only supports one resolver in '%s'", id)
}
retryResolver := resolvers[g.RetryResolver]
if retryResolver == nil {
return errors.New("type truncate-retry requires 'retry-resolver' option")
}
opt := rdns.TruncateRetryOptions{}
resolvers[id] = rdns.NewTruncateRetry(id, gr[0], retryResolver, opt)
case "request-dedup":
if len(gr) != 1 {
return fmt.Errorf("type request-dedup only supports one resolver in '%s'", id)
}
resolvers[id] = rdns.NewRequestDedup(id, gr[0])
case "fastest-tcp":
if len(gr) != 1 {
return fmt.Errorf("type fastest-tcp only supports one resolver in '%s'", id)
}
opt := rdns.FastestTCPOptions{
Port: g.Port,
WaitAll: g.WaitAll,
SuccessTTLMin: g.SuccessTTLMin,
}
resolvers[id] = rdns.NewFastestTCP(id, gr[0], opt)
case "ecs-modifier":
if len(gr) != 1 {
return fmt.Errorf("type ecs-modifier only supports one resolver in '%s'", id)
}
var f rdns.ECSModifierFunc
switch g.ECSOp {
case "add":
f = rdns.ECSModifierAdd(g.ECSAddress, g.ECSPrefix4, g.ECSPrefix6)
case "delete":
f = rdns.ECSModifierDelete
case "privacy":
f = rdns.ECSModifierPrivacy(g.ECSPrefix4, g.ECSPrefix6)
case "":
default:
return fmt.Errorf("unsupported ecs-modifier operation '%s'", g.ECSOp)
}
resolvers[id], err = rdns.NewECSModifier(id, gr[0], f)
if err != nil {
return err
}
case "edns0-modifier":
if len(gr) != 1 {
return fmt.Errorf("type edns0-modifier only supports one resolver in '%s'", id)
}
var f rdns.EDNS0ModifierFunc
switch g.EDNS0Op {
case "add":
f = rdns.EDNS0ModifierAdd(g.EDNS0Code, g.EDNS0Data)
case "delete":
f = rdns.EDNS0ModifierDelete(g.EDNS0Code)
case "":
default:
return fmt.Errorf("unsupported edns0-modifier operation '%s'", g.EDNS0Op)
}
resolvers[id], err = rdns.NewEDNS0Modifier(id, gr[0], f)
if err != nil {
return err
}
case "cache":
var shuffleFunc rdns.AnswerShuffleFunc
switch g.CacheAnswerShuffle {
case "": // default
case "random":
shuffleFunc = rdns.AnswerShuffleRandon
case "round-robin":
shuffleFunc = rdns.AnswerShuffleRoundRobin
default:
return fmt.Errorf("unsupported shuffle function %q", g.CacheAnswerShuffle)
}
opt := rdns.CacheOptions{
GCPeriod: time.Duration(g.GCPeriod) * time.Second,
Capacity: g.CacheSize,
NegativeTTL: g.CacheNegativeTTL,
ShuffleAnswerFunc: shuffleFunc,
HardenBelowNXDOMAIN: g.CacheHardenBelowNXDOMAIN,
FlushQuery: g.CacheFlushQuery,
}
resolvers[id] = rdns.NewCache(id, gr[0], opt)
case "response-blocklist-ip", "response-blocklist-cidr": // "response-blocklist-cidr" has been retired/renamed to "response-blocklist-ip"
if len(gr) != 1 {
return fmt.Errorf("type response-blocklist-ip only supports one resolver in '%s'", id)
}
if len(g.Blocklist) > 0 && len(g.BlocklistSource) > 0 {
return fmt.Errorf("static blocklist can't be used with 'blocklist-source' in '%s'", id)
}
var blocklistDB rdns.IPBlocklistDB
if len(g.Blocklist) > 0 {
blocklistDB, err = newIPBlocklistDB(list{Format: g.BlocklistFormat}, g.LocationDB, g.Blocklist)
if err != nil {
return err
}
} else {
var dbs []rdns.IPBlocklistDB
for _, s := range g.BlocklistSource {
db, err := newIPBlocklistDB(s, g.LocationDB, nil)
if err != nil {
return fmt.Errorf("%s: %w", id, err)
}
dbs = append(dbs, db)
}
blocklistDB, err = rdns.NewMultiIPDB(dbs...)
if err != nil {
return err
}
}
opt := rdns.ResponseBlocklistIPOptions{
BlocklistResolver: resolvers[g.BlockListResolver],
BlocklistDB: blocklistDB,
BlocklistRefresh: time.Duration(g.BlocklistRefresh) * time.Second,
Filter: g.Filter,
}
resolvers[id], err = rdns.NewResponseBlocklistIP(id, gr[0], opt)
if err != nil {
return err
}
case "response-blocklist-name":
if len(gr) != 1 {
return fmt.Errorf("type response-blocklist-name only supports one resolver in '%s'", id)
}
if len(g.Blocklist) > 0 && len(g.BlocklistSource) > 0 {
return fmt.Errorf("static blocklist can't be used with 'blocklist-source' in '%s'", id)
}
var blocklistDB rdns.BlocklistDB
if len(g.Blocklist) > 0 {
blocklistDB, err = newBlocklistDB(list{Format: g.BlocklistFormat}, g.Blocklist)
if err != nil {
return err
}
} else {
var dbs []rdns.BlocklistDB
for _, s := range g.BlocklistSource {
db, err := newBlocklistDB(s, nil)
if err != nil {
return fmt.Errorf("%s: %w", id, err)
}
dbs = append(dbs, db)
}
blocklistDB, err = rdns.NewMultiDB(dbs...)
if err != nil {
return err
}
}
opt := rdns.ResponseBlocklistNameOptions{
BlocklistResolver: resolvers[g.BlockListResolver],
BlocklistDB: blocklistDB,
BlocklistRefresh: time.Duration(g.BlocklistRefresh) * time.Second,
}
resolvers[id], err = rdns.NewResponseBlocklistName(id, gr[0], opt)
if err != nil {
return err
}
case "client-blocklist":
if len(gr) != 1 {
return fmt.Errorf("type client-blocklist only supports one resolver in '%s'", id)
}
if len(g.Blocklist) > 0 && len(g.BlocklistSource) > 0 {
return fmt.Errorf("static blocklist can't be used with 'blocklist-source' in '%s'", id)
}
var blocklistDB rdns.IPBlocklistDB
if len(g.Blocklist) > 0 {
blocklistDB, err = newIPBlocklistDB(list{Format: g.BlocklistFormat}, g.LocationDB, g.Blocklist)
if err != nil {
return err
}
} else {
var dbs []rdns.IPBlocklistDB
for _, s := range g.BlocklistSource {
db, err := newIPBlocklistDB(s, g.LocationDB, nil)
if err != nil {
return fmt.Errorf("%s: %w", id, err)
}
dbs = append(dbs, db)
}
blocklistDB, err = rdns.NewMultiIPDB(dbs...)
if err != nil {
return err
}
}
opt := rdns.ClientBlocklistOptions{
BlocklistResolver: resolvers[g.BlockListResolver],
BlocklistDB: blocklistDB,
BlocklistRefresh: time.Duration(g.BlocklistRefresh) * time.Second,
}
resolvers[id], err = rdns.NewClientBlocklist(id, gr[0], opt)
if err != nil {
return err
}
case "static-responder":
opt := rdns.StaticResolverOptions{
Answer: g.Answer,
NS: g.NS,
Extra: g.Extra,
RCode: g.RCode,
}
resolvers[id], err = rdns.NewStaticResolver(id, opt)
if err != nil {
return err
}
case "response-minimize":
if len(gr) != 1 {
return fmt.Errorf("type response-minimize only supports one resolver in '%s'", id)
}
resolvers[id] = rdns.NewResponseMinimize(id, gr[0])
case "response-collapse":
if len(gr) != 1 {
return fmt.Errorf("type response-collapse only supports one resolver in '%s'", id)
}
opt := rdns.ResponseCollapsOptions{
NullRCode: g.NullRCode,
}
resolvers[id] = rdns.NewResponseCollapse(id, gr[0], opt)
case "drop":
resolvers[id] = rdns.NewDropResolver(id)
case "rate-limiter":
if len(gr) != 1 {
return fmt.Errorf("type rate-limiter only supports one resolver in '%s'", id)
}
opt := rdns.RateLimiterOptions{
Requests: g.Requests,
Window: g.Window,
Prefix4: g.Prefix4,
Prefix6: g.Prefix6,
LimitResolver: resolvers[g.LimitResolver],
}
resolvers[id] = rdns.NewRateLimiter(id, gr[0], opt)
default:
return fmt.Errorf("unsupported group type '%s' for group '%s'", g.Type, id)
}
return nil
}
// Instantiate a router object based on configuration and add to the map of resolvers by ID.
func instantiateRouter(id string, r router, resolvers map[string]rdns.Resolver) error {
router := rdns.NewRouter(id)
for _, route := range r.Routes {
resolver, ok := resolvers[route.Resolver]
if !ok {
return fmt.Errorf("router '%s' references non-existant resolver or group '%s'", id, route.Resolver)
}
types := route.Types
if route.Type != "" { // Support the deprecated "Type" by just adding it to "Types" if defined
types = append(types, route.Type)
}
r, err := rdns.NewRoute(route.Name, route.Class, types, route.Weekdays, route.Before, route.After, route.Source, resolver)
if err != nil {
return fmt.Errorf("failure parsing routes for router '%s' : %s", id, err.Error())
}
r.Invert(route.Invert)
router.Add(r)
}
resolvers[id] = router
return nil
}
func newBlocklistDB(l list, rules []string) (rdns.BlocklistDB, error) {
loc, err := url.Parse(l.Source)
if err != nil {
return nil, err
}
var loader rdns.BlocklistLoader
if len(rules) > 0 {
loader = rdns.NewStaticLoader(rules)
} else {
switch loc.Scheme {
case "http", "https":
opt := rdns.HTTPLoaderOptions{
CacheDir: l.CacheDir,
}
loader = rdns.NewHTTPLoader(l.Source, opt)
case "":
loader = rdns.NewFileLoader(l.Source)
default:
return nil, fmt.Errorf("unsupported scheme '%s' in '%s'", loc.Scheme, l.Source)
}
}
switch l.Format {
case "regexp", "":
return rdns.NewRegexpDB(loader)
case "domain":
return rdns.NewDomainDB(loader)
case "hosts":
return rdns.NewHostsDB(loader)
default:
return nil, fmt.Errorf("unsupported format '%s'", l.Format)
}
}
func newIPBlocklistDB(l list, locationDB string, rules []string) (rdns.IPBlocklistDB, error) {
loc, err := url.Parse(l.Source)
if err != nil {
return nil, err
}
var loader rdns.BlocklistLoader
if len(rules) > 0 {
loader = rdns.NewStaticLoader(rules)
} else {
switch loc.Scheme {
case "http", "https":
opt := rdns.HTTPLoaderOptions{
CacheDir: l.CacheDir,
}
loader = rdns.NewHTTPLoader(l.Source, opt)
case "":
loader = rdns.NewFileLoader(l.Source)
default:
return nil, fmt.Errorf("unsupported scheme '%s' in '%s'", loc.Scheme, l.Source)
}
}
switch l.Format {
case "cidr", "":
return rdns.NewCidrDB(loader)
case "location":
return rdns.NewGeoIPDB(loader, locationDB)
default:
return nil, fmt.Errorf("unsupported format '%s'", l.Format)
}
}