forked from mholt/caddy-dynamicdns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
caddyfile.go
128 lines (114 loc) · 2.91 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
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>
// versions ipv4|ipv6
// }
//
// 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] = names
}
case "dynamic_domains":
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)
}
}
default:
return nil, d.ArgErr()
}
}
return httpcaddyfile.App{
Name: "dynamic_dns",
Value: caddyconfig.JSON(app, nil),
}, nil
}