-
Notifications
You must be signed in to change notification settings - Fork 12
/
upstream.go
614 lines (558 loc) · 14.1 KB
/
upstream.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
/*
* Created Feb 23, 2020
*/
package dnsredir
import (
"crypto/tls"
"errors"
"fmt"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
pkgtls "github.com/coredns/coredns/plugin/pkg/tls"
"github.com/coredns/coredns/plugin/pkg/transport"
"github.com/miekg/dns"
"net"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
type reloadableUpstream struct {
// Flag indicate match any request, i.e. the root zone "."
matchAny bool
*NameList
inline domainSet
ignored domainSet
*HealthCheck
// Bootstrap DNS in IP:Port combo
bootstrap []string
ipset interface{}
pf interface{}
noIPv6 bool
maxRetry int32
}
// reloadableUpstream implements Upstream interface
// Check if given name in upstream name list
// `name' is lower cased and without trailing dot(except for root zone)
func (u *reloadableUpstream) Match(name string) bool {
if u.matchAny {
if !plugin.Name(".").Matches(name) {
panic(fmt.Sprintf("Why %q doesn't match %q?!", name, "."))
}
ignored := u.ignored.Match(name)
if ignored {
log.Debugf("#0 Skip %q since it's ignored", name)
}
return !ignored
}
if !u.NameList.Match(name) && !u.inline.Match(name) {
return false
}
if u.ignored.Match(name) {
log.Debugf("#1 Skip %q since it's ignored", name)
return false
}
return true
}
func (u *reloadableUpstream) Start() error {
u.periodicUpdate(u.bootstrap)
u.HealthCheck.Start()
if err := ipsetSetup(u); err != nil {
return err
}
if err := pfSetup(u); err != nil {
return err
}
return nil
}
func (u *reloadableUpstream) Stop() error {
close(u.stopPathReload)
close(u.stopUrlReload)
u.HealthCheck.Stop()
if err := ipsetShutdown(u); err != nil {
return err
}
if err := pfShutdown(u); err != nil {
return err
}
return nil
}
// Parses Caddy config input and return a list of reloadable upstream for this plugin
func NewReloadableUpstreams(c *caddy.Controller) ([]Upstream, error) {
var ups []Upstream
for c.Next() {
u, err := newReloadableUpstream(c)
if err != nil {
return nil, err
}
ups = append(ups, u)
}
if ups == nil {
panic("Why upstream hosts is nil? it shouldn't happen.")
}
return ups, nil
}
// see: healthcheck.go/UpstreamHost.Dial()
func protoToNetwork(proto string) string {
if proto == "tls" {
return "tcp-tls"
}
return proto
}
func newReloadableUpstream(c *caddy.Controller) (Upstream, error) {
u := &reloadableUpstream{
NameList: &NameList{
pathReload: defaultPathReloadInterval,
stopPathReload: make(chan struct{}),
urlReload: defaultUrlReloadInterval,
urlReadTimeout: defaultUrlReadTimeout,
stopUrlReload: make(chan struct{}),
},
ignored: make(domainSet),
inline: make(domainSet),
maxRetry: defaultMaxRetry,
HealthCheck: &HealthCheck{
stop: make(chan struct{}),
maxFails: defaultMaxFails,
checkInterval: defaultHcInterval,
transport: &Transport{
expire: defaultConnExpire,
tlsConfig: new(tls.Config),
recursionDesired: true,
},
},
}
if err := parseFrom(c, u); err != nil {
return nil, err
}
for c.NextBlock() {
if err := parseBlock(c, u); err != nil {
return nil, err
}
}
if u.hosts == nil {
return nil, c.Errf("missing mandatory property: %q", "to")
}
for _, host := range u.hosts {
addr, tlsServerName := SplitByByte(host.addr, '@')
host.addr = addr
host.transport = newTransport()
// Inherit from global transport settings
host.transport.recursionDesired = u.transport.recursionDesired
host.transport.expire = u.transport.expire
if host.proto == transport.TLS {
// Deep copy
host.transport.tlsConfig = new(tls.Config)
host.transport.tlsConfig.Certificates = u.transport.tlsConfig.Certificates
host.transport.tlsConfig.RootCAs = u.transport.tlsConfig.RootCAs
// Don't set TLS server name if addr host part is already a domain name
if hostPortIsIpPort(addr) {
host.transport.tlsConfig.ServerName = u.transport.tlsConfig.ServerName
}
// TLS server name in tls:// takes precedence over the global one(if any)
if len(tlsServerName) != 0 {
tlsServerName = tlsServerName[1:]
serverName, ok := stringToDomain(tlsServerName)
if !ok {
return nil, c.Errf("invalid TLS server name %q", tlsServerName)
}
host.transport.tlsConfig.ServerName = serverName
}
}
network := protoToNetwork(host.proto)
if network == "dns" {
// Use classic DNS protocol for health checking
network = "udp"
}
host.c = &dns.Client{
Net: network,
TLSConfig: host.transport.tlsConfig,
Timeout: defaultHcTimeout,
}
host.InitDOH(u)
}
if err := u.inline.ForEachDomain(func(name string) error {
// except takes precedence over INLINE
if u.ignored.Match(name) {
return c.Errf("%q %v is conflict with %q", "INLINE", name, "except")
}
return nil
}); err != nil {
return nil, err
}
if u.matchAny {
if u.inline.Len() != 0 {
return nil, c.Errf("INLINE %q is forbidden since %q will match all requests", u.inline, ".")
}
if u.pathReload != 0 {
log.Debugf("Reset path_reload %v to zero since %q is matched", u.pathReload, ".")
u.pathReload = 0
}
if u.urlReload != 0 {
log.Debugf("Reset url_reload %v to zero since %q is matched", u.urlReload, ".")
u.urlReload = 0
}
} else {
hasPath := false
hasUrl := false
for _, item := range u.NameList.items {
switch item.whichType {
case NameItemTypePath:
hasPath = true
case NameItemTypeUrl:
hasUrl = true
default:
panic(fmt.Sprintf("Unexpected NameItem type %v", item.whichType))
}
}
if !hasPath {
log.Debugf("Reset path_reload %v to zero since no path found", u.pathReload)
u.NameList.pathReload = 0
}
if !hasUrl {
log.Debugf("Reset url_reload %v to zero since no url found", u.urlReload)
u.NameList.urlReload = 0
}
}
if u.inline.Len() != 0 {
log.Infof("inline: %v", u.inline)
}
return u, nil
}
func parseFrom(c *caddy.Controller, u *reloadableUpstream) error {
forms := c.RemainingArgs()
n := len(forms)
if n == 0 {
return c.ArgErr()
}
if n == 1 && forms[0] == "." {
u.matchAny = true
log.Infof("Match any")
return nil
}
config := dnsserver.GetConfig(c)
for _, from := range forms {
if strings.Index(from, "://") > 0 {
continue
}
if !filepath.IsAbs(from) && config.Root != "" {
from = filepath.Join(config.Root, from)
}
st, err := os.Stat(from)
if err != nil {
if os.IsNotExist(err) {
log.Warningf("File %q doesn't exist", from)
} else {
return err
}
} else if st != nil && !st.Mode().IsRegular() {
log.Warningf("File %q isn't a regular file", from)
}
}
items, err := NewNameItemsWithForms(forms)
if err != nil {
return err
}
u.items = items
log.Infof("FROM...: %v", forms)
return nil
}
func parseBlock(c *caddy.Controller, u *reloadableUpstream) error {
switch dir := c.Val(); dir {
case "path_reload":
dur, err := parseDuration(c)
if err != nil {
return err
}
if dur < minPathReloadInterval && dur != 0 {
return c.Errf("%v: minimal interval is %v", dir, minPathReloadInterval)
}
u.pathReload = dur
log.Infof("%v: %v", dir, u.pathReload)
case "url_reload":
args := c.RemainingArgs()
n := len(args)
if n != 1 && n != 2 {
return c.ArgErr()
}
dur, err := parseDuration0(dir, args[0])
if err != nil {
return c.Err(err.Error())
}
if dur < minUrlReloadInterval && dur != 0 {
return c.Errf("%v: minimal reload interval is %v", dir, minUrlReloadInterval)
}
if n == 2 {
dur, err := parseDuration0(dir, args[1])
if err != nil {
return c.Err(err.Error())
}
if dur < minUrlReadTimeout {
return c.Errf("%v: minimal read timeout is %v", dir, minUrlReadTimeout)
}
u.urlReadTimeout = dur
}
u.urlReload = dur
log.Infof("%v: %v %v", dir, u.urlReload, u.urlReadTimeout)
case "except":
// Multiple "except"s will be merged together
args := c.RemainingArgs()
if len(args) == 0 {
return c.ArgErr()
}
for _, name := range args {
if !u.ignored.Add(name) {
log.Warningf("%q isn't a domain name", name)
}
}
log.Infof("%v: %v", dir, u.ignored)
case "spray":
if len(c.RemainingArgs()) != 0 {
return c.ArgErr()
}
u.spray = &Spray{}
log.Infof("%v: enabled", dir)
case "policy":
arr := c.RemainingArgs()
if len(arr) != 1 {
return c.ArgErr()
}
policy, ok := SupportedPolicies[arr[0]]
if !ok {
return c.Errf("unknown policy: %q", arr[0])
}
u.policy = policy
log.Infof("%v: %v", dir, arr[0])
case "max_fails":
n, err := parseInt32(c)
if err != nil {
return err
}
u.maxFails = n
log.Infof("%v: %v", dir, n)
case "max_retry":
n, err := parseInt32(c)
if err != nil {
return err
}
u.maxRetry = n
log.Infof("%v: %v", dir, n)
case "health_check":
args := c.RemainingArgs()
n := len(args)
if n != 1 && n != 2 {
return c.ArgErr()
}
dur, err := parseDuration0(dir, args[0])
if err != nil {
return c.Err(err.Error())
}
if dur < minHcInterval && dur != 0 {
return c.Errf("%v: minimal interval is %v", dir, minHcInterval)
}
if n == 2 && args[1] != "no_rec" {
return c.Errf("%v: unknown option: %v", dir, args[1])
}
u.checkInterval = dur
u.transport.recursionDesired = n == 1
log.Infof("%v: %v %v", dir, u.checkInterval, u.transport.recursionDesired)
case "to":
// Multiple "to"s will be merged together
if err := parseTo(c, u); err != nil {
return err
}
case "expire":
dur, err := parseDuration(c)
if err != nil {
return err
}
if dur < minExpireInterval && dur != 0 {
return c.Errf("%v: minimal interval is %v", dir, minExpireInterval)
}
u.transport.expire = dur
log.Infof("%v: %v", dir, dur)
case "tls":
args := c.RemainingArgs()
if len(args) > 3 {
return c.ArgErr()
}
tlsConfig, err := pkgtls.NewTLSConfigFromArgs(args...)
if err != nil {
return err
}
// Merge server name if tls_servername set previously
tlsConfig.ServerName = u.transport.tlsConfig.ServerName
u.transport.tlsConfig = tlsConfig
log.Infof("%v: %v", dir, args)
case "tls_servername":
args := c.RemainingArgs()
if len(args) != 1 {
return c.ArgErr()
}
serverName, ok := stringToDomain(args[0])
if !ok {
return c.Errf("%v: %q isn't a valid domain name", dir, args[0])
}
u.transport.tlsConfig.ServerName = serverName
log.Infof("%v: %v", dir, serverName)
case "bootstrap":
if err := parseBootstrap(c, u); err != nil {
return err
}
case "ipset":
if err := ipsetParse(c, u); err != nil {
return err
}
case "pf":
if err := pfParse(c, u); err != nil {
return err
}
case "no_ipv6":
args := c.RemainingArgs()
if len(args) != 0 {
return c.ArgErr()
}
u.noIPv6 = true
log.Infof("%v: %v", dir, u.noIPv6)
default:
if len(c.RemainingArgs()) != 0 || !u.inline.Add(dir) {
return c.Errf("unknown property: %q", dir)
}
if u.ignored.Len() != 0 {
return c.Errf("%q must comes before %q", "INLINE", "except")
}
}
return nil
}
// Return a non-negative int32
// see: https://golang.org/pkg/builtin/#int
func parseInt32(c *caddy.Controller) (int32, error) {
dir := c.Val()
args := c.RemainingArgs()
if len(args) != 1 {
return 0, c.ArgErr()
}
n, err := strconv.Atoi(args[0])
if err != nil {
return 0, err
}
// In case of n is 64-bit
if n < 0 || n > 0x7fffffff {
return 0, c.Errf("%v: value %v of out of non-negative int32 range", dir, n)
}
return int32(n), nil
}
func parseDuration0(dir, arg string) (time.Duration, error) {
duration, err := time.ParseDuration(arg)
if err != nil {
return 0, err
}
if duration < 0 {
return 0, errors.New(fmt.Sprintf("%v: negative time duration %v", dir, arg))
}
return duration, nil
}
// Return a non-negative time.Duration and an error(if any)
func parseDuration(c *caddy.Controller) (time.Duration, error) {
dir := c.Val()
args := c.RemainingArgs()
if len(args) != 1 {
return 0, c.ArgErr()
}
dur, err := parseDuration0(dir, args[0])
if err == nil {
return dur, nil
}
return dur, c.Err(err.Error())
}
func parseTo(c *caddy.Controller, u *reloadableUpstream) error {
args := c.RemainingArgs()
if len(args) == 0 {
return c.ArgErr()
}
toHosts, err := HostPort(args)
if err != nil {
return err
}
for _, host := range toHosts {
trans, addr := SplitTransportHost(host)
log.Infof("Transport: %v Address: %v", trans, addr)
uh := &UpstreamHost{
proto: trans,
// Not an error, host and tls server name will be separated later
addr: addr,
downFunc: checkDownFunc(u),
}
u.hosts = append(u.hosts, uh)
log.Infof("Upstream: %v", uh)
}
return nil
}
func parseBootstrap(c *caddy.Controller, u *reloadableUpstream) error {
dir := c.Val()
args := c.RemainingArgs()
if len(args) == 0 {
return c.ArgErr()
}
var list []string
for _, hp := range args {
host, port, err := net.SplitHostPort(hp)
if err != nil {
if strings.Contains(err.Error(), "missing port in address") {
host = hp
port = "53"
} else {
return c.Errf("%v: %v", dir, err)
}
} else {
if port, err := strconv.Atoi(port); err != nil || port <= 0 {
if err == nil {
err = fmt.Errorf("non-positive port %v", port)
}
return c.Errf("%v: %v", dir, err)
}
}
if strings.HasPrefix(host, "[") {
if !strings.HasSuffix(host, "]") {
panic(fmt.Sprintf("Why %q doesn't have close bracket?!", host))
}
// Strip the brackets
host = host[1 : len(host)-1]
}
// XXX: Doesn't support IPv6 with zone
ip := net.ParseIP(host)
if ip == nil {
return c.Errf("%v: %q isn't a valid IP address", dir, host)
}
if ip.To4() != nil {
hp = ip.String() + ":" + port
} else {
if ip.To16() == nil {
panic(fmt.Sprintf("%v: expected an IPv6 address, got %v", dir, ip))
}
hp = "[" + ip.String() + "]:" + port
}
list = append(list, hp)
}
u.bootstrap = append(u.bootstrap, list...)
log.Infof("%v: %v", dir, list)
return nil
}
const (
defaultMaxFails = 3
defaultMaxRetry = 10
defaultPathReloadInterval = 2 * time.Second
defaultUrlReloadInterval = 30 * time.Minute
defaultUrlReadTimeout = 15 * time.Second
defaultHcInterval = 2000 * time.Millisecond
defaultHcTimeout = 5000 * time.Millisecond
)
const (
minPathReloadInterval = 1 * time.Second
minUrlReloadInterval = 15 * time.Second
minUrlReadTimeout = 3 * time.Second
minHcInterval = 1 * time.Second
minExpireInterval = 1 * time.Second
)