Skip to content

Commit 968fa2a

Browse files
committed
feat: add port to global options for default node
- add global tailscale.port option - added proper handling for default node: If no name is specified it used hardcoded string "caddy" but the docs say that it is using the binary name Signed-off-by: khartahk <[email protected]>
1 parent 662ef34 commit 968fa2a

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

app.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ type App struct {
4444
// Tags to apply to all nodes when registered.
4545
Tags []string `json:"tags,omitempty" caddy:"namespace=tailscale.tags"`
4646

47+
// Port specifies the default UDP port for tsnet. When unset (0), tsnet will pick a random available port.
48+
Port uint16 `json:"port,omitempty" caddy:"namespace=tailscale.port"`
49+
4750
// Nodes is a map of per-node configuration which overrides global options.
4851
Nodes map[string]Node `json:"nodes,omitempty" caddy:"namespace=tailscale"`
4952

@@ -150,6 +153,16 @@ func parseAppConfig(d *caddyfile.Dispenser, _ any) (any, error) {
150153
}
151154
case "tags":
152155
app.Tags = d.RemainingArgs()
156+
case "port":
157+
if d.NextArg() {
158+
v, err := strconv.ParseUint(d.Val(), 10, 16)
159+
if err != nil {
160+
return nil, d.WrapErr(err)
161+
}
162+
app.Port = uint16(v)
163+
} else {
164+
app.Port = 0
165+
}
153166
default:
154167
node, err := parseNodeConfig(d)
155168
if app.Nodes == nil {

module.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ func getTCPListener(c context.Context, network string, host string, portRange st
6666
network = "tcp"
6767
}
6868

69+
// If host is empty (bind tailscale/), use default node name from binary name
70+
if host == "" {
71+
host = getDefaultNodeName()
72+
}
73+
6974
// Get node reference for this listener (increments node reference count)
7075
node, err := getNode(ctx, host)
7176
if err != nil {
@@ -117,6 +122,11 @@ func getTLSListener(c context.Context, network string, host string, portRange st
117122
network = "tcp"
118123
}
119124

125+
// If host is empty (bind tailscale/), use default node name from binary name
126+
if host == "" {
127+
host = getDefaultNodeName()
128+
}
129+
120130
// Get node reference for this listener (increments node reference count)
121131
node, err := getNode(ctx, host)
122132
if err != nil {
@@ -173,6 +183,11 @@ func getUDPListener(c context.Context, network string, host string, portRange st
173183
network = "udp"
174184
}
175185

186+
// If host is empty (bind tailscale/), use default node name from binary name
187+
if host == "" {
188+
host = getDefaultNodeName()
189+
}
190+
176191
// Get node reference for this listener (increments node reference count)
177192
node, err := getNode(ctx, host)
178193
if err != nil {
@@ -252,6 +267,12 @@ var nodes = caddy.NewUsagePool()
252267
// This ensures listeners are properly closed when removed from configuration.
253268
var tailscaleListeners = caddy.NewUsagePool()
254269

270+
// getDefaultNodeName returns the default node name when none is specified.
271+
// It uses the binary name (without path) as the default.
272+
func getDefaultNodeName() string {
273+
return filepath.Base(os.Args[0])
274+
}
275+
255276
// getNode returns a tailscale node for Caddy apps to interface with.
256277
//
257278
// The specified name will be used to lookup the node configuration from the tailscale caddy app,
@@ -366,7 +387,7 @@ func getPort(name string, app *App) uint16 {
366387
return node.Port
367388
}
368389

369-
return 0
390+
return app.Port
370391
}
371392

372393
func getStateDir(name string, app *App) (string, error) {

0 commit comments

Comments
 (0)