Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow port to be configured with direct assignment #94

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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 @@ -66,6 +66,8 @@ type Node struct {
// Hostname is the hostname to use when registering the node.
Hostname string `json:"hostname,omitempty" caddy:"namespace=tailscale.hostname"`

Port uint16 `json:"port,omitempty" caddy:"namespace=tailscale.port"`

// StateDir specifies the state directory for the node.
StateDir string `json:"state_dir,omitempty" caddy:"namespace=tailscale.state_dir"`

Expand Down Expand Up @@ -190,6 +192,17 @@ func parseNodeConfig(d *caddyfile.Dispenser) (Node, error) {
} else {
node.Ephemeral = opt.NewBool(true)
}
case "port":
if segment.NextArg() {
v, err := strconv.ParseUint(segment.Val(), 10, 16)
if err != nil {
return node, segment.WrapErr(err)
}
node.Port = uint16(v)
} else {
node.Port = 0
}

case "hostname":
if !segment.NextArg() {
return node, segment.ArgErr()
Expand Down
17 changes: 13 additions & 4 deletions module.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ func getTCPListener(c context.Context, network string, host string, portRange st
if !ok {
return nil, fmt.Errorf("context is not a caddy.Context: %T", c)
}

na, err := caddy.ParseNetworkAddress(caddy.JoinNetworkAddress(network, host, portRange))
if err != nil {
return nil, err
}

addr := na.JoinHostPort(portOffset)
network, host, port, err := caddy.SplitNetworkAddress(addr)
if err != nil {
Expand Down Expand Up @@ -82,7 +82,7 @@ func getTLSListener(c context.Context, network string, host string, portRange st
if err != nil {
return nil, err
}

addr := na.JoinHostPort(portOffset)
network, host, port, err := caddy.SplitNetworkAddress(addr)
if err != nil {
Expand Down Expand Up @@ -121,7 +121,7 @@ func getUDPListener(c context.Context, network string, host string, portRange st
if err != nil {
return nil, err
}

addr := na.JoinHostPort(portOffset)
network, host, port, err := caddy.SplitNetworkAddress(addr)
if err != nil {
Expand Down Expand Up @@ -183,6 +183,7 @@ func getNode(ctx caddy.Context, name string) (*tailscaleNode, error) {
},
Ephemeral: getEphemeral(name, app),
RunWebClient: getWebUI(name, app),
Port: getPort(name, app),
}

if s.AuthKey, err = getAuthKey(name, app); err != nil {
Expand Down Expand Up @@ -268,6 +269,14 @@ func getHostname(name string, app *App) (string, error) {
return name, nil
}

func getPort(name string, app *App) uint16 {
if node, ok := app.Nodes[name]; ok {
return node.Port
}

return 0
}

func getStateDir(name string, app *App) (string, error) {
if node, ok := app.Nodes[name]; ok {
if node.StateDir != "" {
Expand Down
28 changes: 28 additions & 0 deletions module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,34 @@ func Test_GetHostname(t *testing.T) {
}
}

func Test_GetPort(t *testing.T) {
app := &App{
Nodes: map[string]Node{
"empty": {},
"port": {Port: 3000},
},
}
if err := app.Provision(caddy.Context{}); err != nil {
t.Fatal(err)
}

got := getPort("noconfig", &App{})
if want := uint16(0); got != want {
t.Errorf("GetPort() = %v, want %v", got, want)
}

got = getPort("empty", app)
if want := uint16(0); got != want {
t.Errorf("GetPort() = %v, want %v", got, want)
}

got = getPort("port", app)
if want := uint16(3000); got != want {
t.Errorf("GetPort() = %v, want %v", got, want)
}

}

func Test_GetStateDir(t *testing.T) {
const nodeName = "node"
configDir := must.Get(os.UserConfigDir())
Expand Down