Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ type App struct {
// Tags to apply to all nodes when registered.
Tags []string `json:"tags,omitempty" caddy:"namespace=tailscale.tags"`

// Port specifies the default UDP port for tsnet. When unset (0), tsnet will pick a random available port.
Port uint16 `json:"port,omitempty" caddy:"namespace=tailscale.port"`

// Nodes is a map of per-node configuration which overrides global options.
Nodes map[string]Node `json:"nodes,omitempty" caddy:"namespace=tailscale"`

Expand Down Expand Up @@ -150,6 +153,16 @@ func parseAppConfig(d *caddyfile.Dispenser, _ any) (any, error) {
}
case "tags":
app.Tags = d.RemainingArgs()
case "port":
if d.NextArg() {
v, err := strconv.ParseUint(d.Val(), 10, 16)
if err != nil {
return nil, d.WrapErr(err)
}
app.Port = uint16(v)
} else {
app.Port = 0
}
default:
node, err := parseNodeConfig(d)
if app.Nodes == nil {
Expand Down
23 changes: 22 additions & 1 deletion module.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ func getTCPListener(c context.Context, network string, host string, portRange st
network = "tcp"
}

// If host is empty (bind tailscale/), use default node name from binary name
if host == "" {
host = getDefaultNodeName()
}

// Get node reference for this listener (increments node reference count)
node, err := getNode(ctx, host)
if err != nil {
Expand Down Expand Up @@ -117,6 +122,11 @@ func getTLSListener(c context.Context, network string, host string, portRange st
network = "tcp"
}

// If host is empty (bind tailscale/), use default node name from binary name
if host == "" {
host = getDefaultNodeName()
}

// Get node reference for this listener (increments node reference count)
node, err := getNode(ctx, host)
if err != nil {
Expand Down Expand Up @@ -173,6 +183,11 @@ func getUDPListener(c context.Context, network string, host string, portRange st
network = "udp"
}

// If host is empty (bind tailscale/), use default node name from binary name
if host == "" {
host = getDefaultNodeName()
}

// Get node reference for this listener (increments node reference count)
node, err := getNode(ctx, host)
if err != nil {
Expand Down Expand Up @@ -252,6 +267,12 @@ var nodes = caddy.NewUsagePool()
// This ensures listeners are properly closed when removed from configuration.
var tailscaleListeners = caddy.NewUsagePool()

// getDefaultNodeName returns the default node name when none is specified.
// It uses the binary name (without path) as the default.
func getDefaultNodeName() string {
return filepath.Base(os.Args[0])
}

// getNode returns a tailscale node for Caddy apps to interface with.
//
// The specified name will be used to lookup the node configuration from the tailscale caddy app,
Expand Down Expand Up @@ -366,7 +387,7 @@ func getPort(name string, app *App) uint16 {
return node.Port
}

return 0
return app.Port
}

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