Skip to content

Commit

Permalink
add config option for node hostname
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
willnorris and kdevan committed May 8, 2024
1 parent 3543703 commit 0c8166d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
8 changes: 8 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ type TSNode struct {
// Ephemeral specifies whether the node should be registered as ephemeral.
Ephemeral bool `json:"ephemeral,omitempty" caddy:"namespace=tailscale.ephemeral"`

// Hostname is the hostname to use when registering the node.
Hostname string `json:"hostname,omitempty" caddy:"namespace=tailscale.hostname"`

name string
}

Expand Down Expand Up @@ -121,6 +124,11 @@ func parseTSNode(d *caddyfile.Dispenser) (TSNode, error) {
node.AuthKey = segment.Val()
case "ephemeral":
node.Ephemeral = true
case "hostname":
if !segment.NextArg() {
return node, segment.ArgErr()
}
node.Hostname = segment.Val()
default:
return node, segment.Errf("unrecognized subdirective: %s", segment.Val())
}
Expand Down
17 changes: 16 additions & 1 deletion module.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,15 @@ func getNode(ctx caddy.Context, name string) (*tailscaleNode, error) {

s, _, err := nodes.LoadOrNew(name, func() (caddy.Destructor, error) {
s := &tsnet.Server{
Hostname: name,
Logf: func(format string, args ...any) {
app.logger.Sugar().Debugf(format, args...)
},
Ephemeral: getEphemeral(name, app),
}

if s.Hostname, err = getHostname(name, app); err != nil {
app.logger.Warn("error parsing hostname", zap.Error(err))
}
if s.AuthKey, err = getAuthKey(name, app); err != nil {
app.logger.Warn("error parsing auth key", zap.Error(err))
}
Expand Down Expand Up @@ -178,6 +180,19 @@ func getEphemeral(name string, app *TSApp) bool {
return app.Ephemeral
}

func getHostname(name string, app *TSApp) (string, error) {
if app == nil {
return name, nil
}
if node, ok := app.Nodes[name]; ok {
if node.Hostname != "" {
return repl.ReplaceOrErr(node.Hostname, true, true)
}
}

return name, nil
}

// tailscaleNode is a wrapper around a tsnet.Server that provides a fully self-contained Tailscale node.
// This node can listen on the tailscale network interface, or be used to connect to other nodes in the tailnet.
type tailscaleNode struct {
Expand Down

0 comments on commit 0c8166d

Please sign in to comment.