Skip to content

Commit 0c8166d

Browse files
willnorriskdevan
andcommitted
add config option for node hostname
this overrides the name used to refer to the node in the caddy config, and is mostly useful because it can include environment variables. Closes #18 Co-authored-by: kdevan <kaidevan@gmail.com> Signed-off-by: Will Norris <will@tailscale.com>
1 parent 3543703 commit 0c8166d

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

app.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ type TSNode struct {
4141
// Ephemeral specifies whether the node should be registered as ephemeral.
4242
Ephemeral bool `json:"ephemeral,omitempty" caddy:"namespace=tailscale.ephemeral"`
4343

44+
// Hostname is the hostname to use when registering the node.
45+
Hostname string `json:"hostname,omitempty" caddy:"namespace=tailscale.hostname"`
46+
4447
name string
4548
}
4649

@@ -121,6 +124,11 @@ func parseTSNode(d *caddyfile.Dispenser) (TSNode, error) {
121124
node.AuthKey = segment.Val()
122125
case "ephemeral":
123126
node.Ephemeral = true
127+
case "hostname":
128+
if !segment.NextArg() {
129+
return node, segment.ArgErr()
130+
}
131+
node.Hostname = segment.Val()
124132
default:
125133
return node, segment.Errf("unrecognized subdirective: %s", segment.Val())
126134
}

module.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,15 @@ func getNode(ctx caddy.Context, name string) (*tailscaleNode, error) {
103103

104104
s, _, err := nodes.LoadOrNew(name, func() (caddy.Destructor, error) {
105105
s := &tsnet.Server{
106-
Hostname: name,
107106
Logf: func(format string, args ...any) {
108107
app.logger.Sugar().Debugf(format, args...)
109108
},
110109
Ephemeral: getEphemeral(name, app),
111110
}
112111

112+
if s.Hostname, err = getHostname(name, app); err != nil {
113+
app.logger.Warn("error parsing hostname", zap.Error(err))
114+
}
113115
if s.AuthKey, err = getAuthKey(name, app); err != nil {
114116
app.logger.Warn("error parsing auth key", zap.Error(err))
115117
}
@@ -178,6 +180,19 @@ func getEphemeral(name string, app *TSApp) bool {
178180
return app.Ephemeral
179181
}
180182

183+
func getHostname(name string, app *TSApp) (string, error) {
184+
if app == nil {
185+
return name, nil
186+
}
187+
if node, ok := app.Nodes[name]; ok {
188+
if node.Hostname != "" {
189+
return repl.ReplaceOrErr(node.Hostname, true, true)
190+
}
191+
}
192+
193+
return name, nil
194+
}
195+
181196
// tailscaleNode is a wrapper around a tsnet.Server that provides a fully self-contained Tailscale node.
182197
// This node can listen on the tailscale network interface, or be used to connect to other nodes in the tailnet.
183198
type tailscaleNode struct {

0 commit comments

Comments
 (0)