-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathcaddyfile.go
163 lines (147 loc) · 3.84 KB
/
caddyfile.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
// Copyright 2020 Matthew Holt
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dynamicdns
import (
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
)
func init() {
httpcaddyfile.RegisterGlobalOption("dynamic_dns", parseApp)
}
// parseApp configures the "dynamic_dns" global option from Caddyfile.
// Syntax:
//
// dynamic_dns {
// domains {
// <zone> <names...>
// }
// check_interval <duration>
// provider <name> ...
// ip_source upnp|simple_http <endpoint>
// update_only
// dynamic_domains
// versions ipv4|ipv6
// ttl <duration>
// }
//
// If <names...> are omitted after <zone>, then "@" will be assumed.
func parseApp(d *caddyfile.Dispenser, _ interface{}) (interface{}, error) {
app := new(App)
// consume the option name
if !d.Next() {
return nil, d.ArgErr()
}
// handle the block
for d.NextBlock(0) {
switch d.Val() {
case "domains":
for nesting := d.Nesting(); d.NextBlock(nesting); {
zone := d.Val()
if zone == "" {
return nil, d.ArgErr()
}
names := d.RemainingArgs()
if len(names) == 0 {
names = []string{"@"}
}
if app.Domains == nil {
app.Domains = make(map[string][]string)
}
app.Domains[zone] = append(app.Domains[zone], names...)
}
case "update_only":
if d.NextArg() {
return nil, d.ArgErr()
}
app.UpdateOnly = true
case "dynamic_domains":
if d.NextArg() {
return nil, d.ArgErr()
}
app.DynamicDomains = true
case "check_interval":
if !d.NextArg() {
return nil, d.ArgErr()
}
dur, err := caddy.ParseDuration(d.Val())
if err != nil {
return nil, err
}
app.CheckInterval = caddy.Duration(dur)
case "provider":
if !d.NextArg() {
return nil, d.ArgErr()
}
provName := d.Val()
modID := "dns.providers." + provName
unm, err := caddyfile.UnmarshalModule(d, modID)
if err != nil {
return nil, err
}
app.DNSProviderRaw = caddyconfig.JSONModuleObject(unm, "name", provName, nil)
case "ip_source":
if !d.NextArg() {
return nil, d.ArgErr()
}
sourceType := d.Val()
modID := "dynamic_dns.ip_sources." + sourceType
unm, err := caddyfile.UnmarshalModule(d, modID)
if err != nil {
return nil, err
}
app.IPSourcesRaw = append(app.IPSourcesRaw, caddyconfig.JSONModuleObject(unm, "source", sourceType, nil))
case "versions":
args := d.RemainingArgs()
if len(args) == 0 {
return nil, d.Errf("Must specify at least one version")
}
// Set up defaults; if versions are specified,
// both versions start as false, then flipped
// to true otherwise.
falseBool := false
app.Versions = IPVersions{
IPv4: &falseBool,
IPv6: &falseBool,
}
trueBool := true
for _, arg := range args {
switch arg {
case "ipv4":
app.Versions.IPv4 = &trueBool
case "ipv6":
app.Versions.IPv6 = &trueBool
default:
return nil, d.Errf("Unsupported version: '%s'", arg)
}
}
case "ttl":
if !d.NextArg() {
return nil, d.ArgErr()
}
dur, err := caddy.ParseDuration(d.Val())
if err != nil {
return nil, err
}
app.TTL = caddy.Duration(dur)
default:
return nil, d.ArgErr()
}
}
return httpcaddyfile.App{
Name: "dynamic_dns",
Value: caddyconfig.JSON(app, nil),
}, nil
}