From a92d1bc80b683c0386eaf916ec13081b39666b77 Mon Sep 17 00:00:00 2001 From: Will Norris Date: Wed, 8 May 2024 12:19:42 -0700 Subject: [PATCH] add config option for control URL also return any replacer errors, and remove app nil check since we always check prior to calling these config funcs. Closes #22 Co-authored-by: ChibangLW Signed-off-by: Will Norris --- app.go | 16 ++++++++++++++++ module.go | 22 +++++++++++++--------- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/app.go b/app.go index 5327bdd..483908a 100644 --- a/app.go +++ b/app.go @@ -22,6 +22,9 @@ 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"` @@ -38,6 +41,9 @@ 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 bool `json:"ephemeral,omitempty" caddy:"namespace=tailscale.ephemeral"` @@ -82,6 +88,11 @@ func parseAppConfig(d *caddyfile.Dispenser, _ any) (any, error) { return nil, d.ArgErr() } app.DefaultAuthKey = d.Val() + case "control_url": + if !d.NextArg() { + return nil, d.ArgErr() + } + app.ControlURL = d.Val() case "ephemeral": app.Ephemeral = true default: @@ -119,6 +130,11 @@ func parseNodeConfig(d *caddyfile.Dispenser) (Node, error) { return node, segment.ArgErr() } node.AuthKey = segment.Val() + case "control_url": + if !segment.NextArg() { + return node, segment.ArgErr() + } + node.ControlURL = segment.Val() case "ephemeral": node.Ephemeral = true default: diff --git a/module.go b/module.go index abc7a46..167e210 100644 --- a/module.go +++ b/module.go @@ -111,7 +111,10 @@ func getNode(ctx caddy.Context, name string) (*tailscaleNode, error) { } if s.AuthKey, err = getAuthKey(name, app); err != nil { - app.logger.Warn("error parsing auth key", zap.Error(err)) + return nil, err + } + if s.ControlURL, err = getControlURL(name, app); err != nil { + return nil, err } if name != "" { @@ -142,10 +145,6 @@ func getNode(ctx caddy.Context, name string) (*tailscaleNode, error) { var repl = caddy.NewReplacer() func getAuthKey(name string, app *App) (string, error) { - if app == nil { - return "", nil - } - if node, ok := app.Nodes[name]; ok { if node.AuthKey != "" { return repl.ReplaceOrErr(node.AuthKey, true, true) @@ -167,14 +166,19 @@ func getAuthKey(name string, app *App) (string, error) { return os.Getenv("TS_AUTHKEY"), nil } -func getEphemeral(name string, app *App) bool { - if app == nil { - return false +func getControlURL(name string, app *App) (string, error) { + if node, ok := app.Nodes[name]; ok { + if node.ControlURL != "" { + return repl.ReplaceOrErr(node.ControlURL, true, true) + } } + return repl.ReplaceOrErr(app.ControlURL, true, true) +} + +func getEphemeral(name string, app *App) bool { if node, ok := app.Nodes[name]; ok { return node.Ephemeral } - return app.Ephemeral }