Skip to content

Commit decd1c4

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 9fa40cb commit decd1c4

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
@@ -47,6 +47,9 @@ type TSNode struct {
4747
// Ephemeral specifies whether the node should be registered as ephemeral.
4848
Ephemeral bool `json:"ephemeral,omitempty" caddy:"namespace=tailscale.ephemeral"`
4949

50+
// Hostname is the hostname to use when registering the node.
51+
Hostname string `json:"hostname,omitempty" caddy:"namespace=tailscale.hostname"`
52+
5053
name string
5154
}
5255

@@ -132,6 +135,11 @@ func parseTSNode(d *caddyfile.Dispenser) (TSNode, error) {
132135
node.ControlURL = segment.Val()
133136
case "ephemeral":
134137
node.Ephemeral = true
138+
case "hostname":
139+
if !segment.NextArg() {
140+
return node, segment.ArgErr()
141+
}
142+
node.Hostname = segment.Val()
135143
default:
136144
return node, segment.Errf("unrecognized subdirective: %s", segment.Val())
137145
}

module.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ 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
},
@@ -116,6 +115,9 @@ func getNode(ctx caddy.Context, name string) (*tailscaleNode, error) {
116115
if s.ControlURL, err = getControlURL(name, app); err != nil {
117116
return nil, err
118117
}
118+
if s.Hostname, err = getHostname(name, app); err != nil {
119+
return nil, err
120+
}
119121

120122
if name != "" {
121123
// Set config directory for tsnet. By default, tsnet will use the name of the
@@ -182,6 +184,19 @@ func getEphemeral(name string, app *TSApp) bool {
182184
return app.Ephemeral
183185
}
184186

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

0 commit comments

Comments
 (0)