-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup.go
179 lines (157 loc) · 3.73 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package temptxt
import (
"context"
"net"
"regexp"
"strings"
"time"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/miekg/dns"
)
const (
defaultAuthHeader = "X-Forwarded-User"
defaultMaxAge = 15 * time.Minute
defaultCleanInterval = 0
defaultListenAddr = ":8080"
)
var log = clog.NewWithPlugin("temptxt")
func init() {
plugin.Register("temptxt", setup)
}
func setup(c *caddy.Controller) error {
tt, err := parseConfig(c)
if err != nil {
return err
}
if tt.cleanInterval > 0 {
ctx, cancel := context.WithCancel(context.Background())
tt.Run(ctx)
c.OnShutdown(func() error { cancel(); return nil })
}
c.OnStartup(tt.OnStartup)
c.OnRestart(tt.OnFinalShutdown)
c.OnFinalShutdown(tt.OnFinalShutdown)
c.OnRestartFailed(tt.OnStartup)
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
tt.Next = next
return tt
})
return nil
}
func parseConfig(c *caddy.Controller) (*TempTxt, error) {
tt := &TempTxt{
authHeader: defaultAuthHeader,
maxAge: defaultMaxAge,
cleanInterval: defaultCleanInterval,
listenAddr: defaultListenAddr,
}
tt.records = make(map[string]*Record)
tt.aliases = make(map[string]*Record)
var prefix string
var suffix string
c.Next() // Skip "temptxt"
args := c.RemainingArgs()
if len(args) > 2 {
return nil, c.ArgErr()
}
if len(args) >= 1 {
prefix = args[0]
if prefix != "" && !strings.HasSuffix(prefix, ".") {
prefix += "."
}
}
if len(args) == 2 {
suffix = strings.TrimLeft(dns.Fqdn(args[1]), ".")
}
for c.NextBlock() {
switch c.Val() {
case "auth_header":
if !c.NextArg() {
return nil, c.ArgErr()
}
tt.authHeader = c.Val()
case "txt_alias":
err := addRecord(tt, c, prefix, suffix, true)
if err != nil {
return nil, err
}
case "txt":
err := addRecord(tt, c, prefix, suffix, false)
if err != nil {
return nil, err
}
case "max_age":
if !c.NextArg() {
return nil, c.ArgErr()
}
duration, err := time.ParseDuration(c.Val())
if err != nil {
return nil, c.Errf("Error parsing duration %q", c.Val())
}
tt.maxAge = duration
case "clean_interval":
if !c.NextArg() {
return nil, c.ArgErr()
}
duration, err := time.ParseDuration(c.Val())
if err != nil {
return nil, c.Errf("Error parsing duration %q", c.Val())
}
if duration < 60*time.Second {
return nil, c.Errf("clean_interval must be greater than 60.")
}
tt.cleanInterval = duration
case "listen":
if !c.NextArg() {
return nil, c.ArgErr()
}
if _, _, err := net.SplitHostPort(c.Val()); err != nil {
return nil, c.Errf("Invalid listen address: %v", err)
}
tt.listenAddr = c.Val()
default:
return nil, c.ArgErr()
}
}
return tt, nil
}
func addRecord(tt *TempTxt, c *caddy.Controller, prefix string, suffix string, hasAlias bool) error {
if !c.NextArg() {
return c.ArgErr()
}
r := &Record{}
alias := (dns.Fqdn(strings.ToLower(c.Val())))
fqdn := prefix + alias + suffix
if !hasAlias {
alias += suffix
}
if _, ok := tt.aliases[fqdn]; ok {
return c.Errf("Cannot have domain %q that is also in aliases", fqdn)
}
tt.records[fqdn] = r
if hasAlias {
if !c.NextArg() {
return c.ArgErr()
}
alias = dns.Fqdn(strings.ToLower(c.Val()))
if _, ok := tt.records[alias]; ok {
return c.Errf("Cannot have alias %q that is also in domains", fqdn)
}
}
tt.aliases[alias] = r
args := c.RemainingArgs()
if len(args) == 0 {
return c.ArgErr()
}
for _, u := range args {
regexp, err := regexp.Compile("^" + u + "$")
if err != nil {
return c.Errf("Unable to compile regexp: %v", err)
}
r.allowed = append(r.allowed, regexp)
}
return nil
}