-
Notifications
You must be signed in to change notification settings - Fork 45
/
app.go
224 lines (191 loc) · 5.85 KB
/
app.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: Apache-2.0
package tscaddy
// app.go contains App and Node, which provide global configuration for registering Tailscale nodes.
import (
"strconv"
"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"
"go.uber.org/zap"
"tailscale.com/types/opt"
)
func init() {
caddy.RegisterModule(App{})
httpcaddyfile.RegisterGlobalOption("tailscale", parseAppConfig)
}
// App is the Tailscale Caddy app used to configure Tailscale nodes.
// Nodes can be used to serve sites privately on a Tailscale network,
// or to connect to other Tailnet nodes as upstream proxy backend.
type App struct {
// DefaultAuthKey is the default auth key to use for Tailscale if no other auth key is specified.
DefaultAuthKey string `json:"auth_key,omitempty" caddy:"namespace=tailscale.auth_key"`
// ControlURL specifies the default control URL to use for nodes.
ControlURL string `json:"control_url,omitempty" caddy:"namespace=tailscale.control_url"`
// Ephemeral specifies whether Tailscale nodes should be registered as ephemeral.
Ephemeral bool `json:"ephemeral,omitempty" caddy:"namespace=tailscale.ephemeral"`
// StateDir specifies the default state directory for Tailscale nodes.
// Each node will have a subdirectory under this parent directory for its state.
StateDir string `json:"state_dir,omitempty" caddy:"namespace=tailscale.state_dir"`
// WebUI specifies whether Tailscale nodes should run the Web UI for remote management.
WebUI bool `json:"webui,omitempty" caddy:"namespace=tailscale.webui"`
// Nodes is a map of per-node configuration which overrides global options.
Nodes map[string]Node `json:"nodes,omitempty" caddy:"namespace=tailscale"`
logger *zap.Logger
}
// Node is a Tailscale node configuration.
// A single node can be used to serve multiple sites on different domains or ports,
// and/or to connect to other Tailscale nodes.
type Node struct {
// AuthKey is the Tailscale auth key used to register the node.
AuthKey string `json:"auth_key,omitempty" caddy:"namespace=auth_key"`
// ControlURL specifies the control URL to use for the node.
ControlURL string `json:"control_url,omitempty" caddy:"namespace=tailscale.control_url"`
// Ephemeral specifies whether the node should be registered as ephemeral.
Ephemeral opt.Bool `json:"ephemeral,omitempty" caddy:"namespace=tailscale.ephemeral"`
// WebUI specifies whether the node should run the Web UI for remote management.
WebUI opt.Bool `json:"webui,omitempty" caddy:"namespace=tailscale.webui"`
// Hostname is the hostname to use when registering the node.
Hostname string `json:"hostname,omitempty" caddy:"namespace=tailscale.hostname"`
// StateDir specifies the state directory for the node.
StateDir string `json:"state_dir,omitempty" caddy:"namespace=tailscale.state_dir"`
name string
}
func (App) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "tailscale",
New: func() caddy.Module { return new(App) },
}
}
func (t *App) Provision(ctx caddy.Context) error {
t.logger = ctx.Logger(t)
return nil
}
func (t *App) Start() error {
return nil
}
func (t *App) Stop() error {
return nil
}
func parseAppConfig(d *caddyfile.Dispenser, _ any) (any, error) {
app := &App{
Nodes: make(map[string]Node),
}
if !d.Next() {
return app, d.ArgErr()
}
for d.NextBlock(0) {
val := d.Val()
switch val {
case "auth_key":
if !d.NextArg() {
return nil, d.ArgErr()
}
app.DefaultAuthKey = d.Val()
case "control_url":
if !d.NextArg() {
return nil, d.ArgErr()
}
app.ControlURL = d.Val()
case "ephemeral":
if d.NextArg() {
v, err := strconv.ParseBool(d.Val())
if err != nil {
return nil, d.WrapErr(err)
}
app.Ephemeral = v
} else {
app.Ephemeral = true
}
case "state_dir":
if !d.NextArg() {
return nil, d.ArgErr()
}
app.StateDir = d.Val()
case "webui":
if d.NextArg() {
v, err := strconv.ParseBool(d.Val())
if err != nil {
return nil, d.WrapErr(err)
}
app.WebUI = v
} else {
app.WebUI = true
}
default:
node, err := parseNodeConfig(d)
if app.Nodes == nil {
app.Nodes = map[string]Node{}
}
if err != nil {
return nil, err
}
app.Nodes[node.name] = node
}
}
return httpcaddyfile.App{
Name: "tailscale",
Value: caddyconfig.JSON(app, nil),
}, nil
}
func parseNodeConfig(d *caddyfile.Dispenser) (Node, error) {
name := d.Val()
segment := d.NewFromNextSegment()
if !segment.Next() {
return Node{}, d.ArgErr()
}
node := Node{name: name}
for nesting := segment.Nesting(); segment.NextBlock(nesting); {
val := segment.Val()
switch val {
case "auth_key":
if !segment.NextArg() {
return node, segment.ArgErr()
}
node.AuthKey = segment.Val()
case "control_url":
if !segment.NextArg() {
return node, segment.ArgErr()
}
node.ControlURL = segment.Val()
case "ephemeral":
if segment.NextArg() {
v, err := strconv.ParseBool(segment.Val())
if err != nil {
return node, segment.WrapErr(err)
}
node.Ephemeral = opt.NewBool(v)
} else {
node.Ephemeral = opt.NewBool(true)
}
case "hostname":
if !segment.NextArg() {
return node, segment.ArgErr()
}
node.Hostname = segment.Val()
case "state_dir":
if !segment.NextArg() {
return node, segment.ArgErr()
}
node.StateDir = segment.Val()
case "webui":
if segment.NextArg() {
v, err := strconv.ParseBool(segment.Val())
if err != nil {
return node, segment.WrapErr(err)
}
node.WebUI = opt.NewBool(v)
} else {
node.WebUI = opt.NewBool(true)
}
default:
return node, segment.Errf("unrecognized subdirective: %s", segment.Val())
}
}
return node, nil
}
var (
_ caddy.App = (*App)(nil)
_ caddy.Provisioner = (*App)(nil)
)