-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathconfig.go
More file actions
153 lines (127 loc) · 3.69 KB
/
config.go
File metadata and controls
153 lines (127 loc) · 3.69 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
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
package coredns_omada
import (
"strconv"
"time"
"github.com/coredns/caddy"
"github.com/go-playground/validator/v10"
)
type config struct {
Controller_url string `validate:"required,url"`
Site string `validate:"required"`
Username string `validate:"required"`
Password string `validate:"required"`
refresh_minutes int // update dns zones every x minutes
refresh_login_hours int // login and get a new session token every x hours
resolve_clients bool // resolve 'client' addresses
resolve_devices bool // resolve 'device' addresses
resolve_dhcp_reservations bool // resolve static 'dhcp reservations'
stale_record_duration time.Duration // duration to keep serving stale records for clients no longer present in the controller)
ignore_startup_errors bool // ignore any errors during the initial zone refresh
fallthrough_zones *[]string // list of fallthrough zones
}
func parse(c *caddy.Controller) (config config, err error) {
// defaults
config.refresh_minutes = 1
config.refresh_login_hours = 24
config.resolve_clients = true
config.resolve_devices = true
config.resolve_dhcp_reservations = true
config.stale_record_duration, _ = time.ParseDuration("10m")
config.ignore_startup_errors = false
for c.Next() {
for c.NextBlock() {
switch c.Val() {
case "controller_url":
if !c.NextArg() {
return config, c.ArgErr()
}
config.Controller_url = c.Val()
case "site":
if !c.NextArg() {
return config, c.ArgErr()
}
config.Site = c.Val()
case "username":
if !c.NextArg() {
return config, c.ArgErr()
}
config.Username = c.Val()
case "password":
if !c.NextArg() {
return config, c.ArgErr()
}
config.Password = c.Val()
case "refresh_minutes":
if !c.NextArg() {
return config, c.ArgErr()
}
config.refresh_minutes, err = strconv.Atoi(c.Val())
if err != nil {
return config, c.ArgErr()
}
case "refresh_login_hours":
if !c.NextArg() {
return config, c.ArgErr()
}
config.refresh_login_hours, err = strconv.Atoi(c.Val())
if err != nil {
return config, c.ArgErr()
}
case "resolve_clients":
if !c.NextArg() {
return config, c.ArgErr()
}
config.resolve_clients, err = strconv.ParseBool(c.Val())
if err != nil {
return config, c.ArgErr()
}
case "resolve_devices":
if !c.NextArg() {
return config, c.ArgErr()
}
config.resolve_devices, err = strconv.ParseBool(c.Val())
if err != nil {
return config, c.ArgErr()
}
case "resolve_dhcp_reservations":
if !c.NextArg() {
return config, c.ArgErr()
}
config.resolve_dhcp_reservations, err = strconv.ParseBool(c.Val())
if err != nil {
return config, c.ArgErr()
}
case "stale_record_duration":
if !c.NextArg() {
return config, c.ArgErr()
}
config.stale_record_duration, err = time.ParseDuration(c.Val())
if err != nil {
return config, c.ArgErr()
}
case "ignore_startup_errors":
if !c.NextArg() {
return config, c.ArgErr()
}
config.ignore_startup_errors, err = strconv.ParseBool(c.Val())
if err != nil {
return config, c.ArgErr()
}
case "fallthrough":
fallthroughZones := c.RemainingArgs()
config.fallthrough_zones = &fallthroughZones
if err != nil {
return config, c.ArgErr()
}
default:
return config, c.Errf("unknown property: %q", c.Val())
}
}
}
validate := validator.New()
if err := validate.Struct(config); err != nil {
log.Info("There is a Corefile configuration error:")
return config, err
}
return config, nil
}