-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
111 lines (99 loc) · 2.24 KB
/
setup.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
package boost
import (
"fmt"
"github.com/caddyserver/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
"strconv"
"time"
)
func init() {
plugin.Register("boost", setup)
}
func setup(c *caddy.Controller) error {
boost, err := parse(c)
if err != nil {
return plugin.Error("boost", err)
}
config := dnsserver.GetConfig(c)
config.AddPlugin(func(next plugin.Handler) plugin.Handler {
boost.Debug = config.Debug
boost.Next = next
return boost
})
return nil
}
func parse(c *caddy.Controller) (*Boost, error) {
boost := New()
// cache [zones..]
c.Next()
origins := make([]string, len(c.ServerBlockKeys))
copy(origins, c.ServerBlockKeys)
args := c.RemainingArgs()
if len(args) > 0 {
copy(origins, args)
}
for i := range origins {
origins[i] = plugin.Host(origins[i]).Normalize()
}
boost.Zones = origins
for c.NextBlock() {
switch c.Val() {
case "method":
args := c.RemainingArgs()
if len(args) != 1 {
return nil, c.ArgErr()
}
method, ok := argsMethods[args[0]]
if !ok {
return nil, c.ArgErr()
}
boost.Method = method
case "ping_count":
args := c.RemainingArgs()
if len(args) != 1 {
return nil, c.ArgErr()
}
pingCount, err := strconv.Atoi(args[0])
if err != nil {
return nil, err
}
if pingCount < 1 {
return nil, fmt.Errorf("ping_count can not be less than 1")
}
boost.PingCount = pingCount
case "ping_interval":
args := c.RemainingArgs()
if len(args) != 1 {
return nil, c.ArgErr()
}
pingInterval, err := strconv.ParseFloat(args[0], 64)
if err != nil {
return nil, err
}
if pingInterval <= 0.0 {
return nil, fmt.Errorf("ping_interval must be greater than 0")
}
boost.PingInterval = time.Duration(pingInterval * float64(time.Second))
case "ping_timeout":
args := c.RemainingArgs()
if len(args) != 1 {
return nil, c.ArgErr()
}
pingTimeout, err := strconv.ParseFloat(args[0], 64)
if err != nil {
return nil, err
}
if pingTimeout <= 0.0 {
return nil, fmt.Errorf("ping_timeout must be greater than 0")
}
boost.PingTimeout = time.Duration(pingTimeout * float64(time.Second))
}
}
return boost, nil
}
var (
argsMethods = map[string]Method{
"ping": MethodPing,
}
)