-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.go
More file actions
104 lines (99 loc) · 2.17 KB
/
setup.go
File metadata and controls
104 lines (99 loc) · 2.17 KB
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
package coredns
import (
"errors"
"net/url"
"strings"
"sync"
"github.com/console-dns/client"
"github.com/console-dns/spec/models"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
"github.com/robfig/cron/v3"
)
// init registers this plugin.
func init() {
plugin.Register("console", setup)
}
func setup(c *caddy.Controller) error {
config, err := parseConfig(c)
if err != nil {
return plugin.Error("console", err)
}
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
config.Next = next
return config
})
return nil
}
func parseConfig(c *caddy.Controller) (*ConsoleDns, error) {
result := &ConsoleDns{
client: make([]*client.ConsoleDnsClient, 0),
Next: nil,
Zones: models.NewZones(),
cron: cron.New(),
logLevel: 3,
rwLock: &sync.RWMutex{},
}
services := make([]string, 0)
var token string
for c.Next() {
if c.NextBlock() {
for {
switch c.Val() {
case "server":
for c.NextArg() {
_, err := url.Parse(c.Val())
if err != nil {
return nil, errors.Join(c.ArgErr(), err)
} else {
services = append(services, c.Val())
}
}
case "token":
if !c.NextArg() {
return nil, c.ArgErr()
}
token = c.Val()
case "cache":
if !c.NextArg() {
return nil, c.ArgErr()
}
result.Cache = c.Val()
case "log":
if !c.NextArg() {
return nil, c.ArgErr()
}
switch strings.ToUpper(c.Val()) {
case "DEBUG":
result.logLevel = 1
case "INFO":
result.logLevel = 2
case "ERROR":
result.logLevel = 3
default:
return nil, c.ArgErr()
}
default:
if c.Val() != "}" {
return nil, c.Errf("unknown property '%s'", c.Val())
}
}
if !c.Next() {
break
}
}
}
}
if len(services) > 1 {
result.Info("当前已配置故障转移服务 %v", services)
}
if len(services) == 0 || token == "" {
return nil, errors.New("server 或 token 不能为空")
}
for _, service := range services {
result.client = append(result.client, client.NewConsoleDnsClient(service, token))
}
result.Start()
return result, nil
}