Skip to content

Commit 32b202f

Browse files
chrishoagewillnorris
authored andcommitted
allow port to be configured with direct assignment
Signed-off-by: Chris Hoage <iam@chrishoage.com>
1 parent 642f61f commit 32b202f

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

app.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ type Node struct {
6666
// Hostname is the hostname to use when registering the node.
6767
Hostname string `json:"hostname,omitempty" caddy:"namespace=tailscale.hostname"`
6868

69+
Port uint16 `json:"port,omitempty" caddy:"namespace=tailscale.port"`
70+
6971
// StateDir specifies the state directory for the node.
7072
StateDir string `json:"state_dir,omitempty" caddy:"namespace=tailscale.state_dir"`
7173

@@ -190,6 +192,17 @@ func parseNodeConfig(d *caddyfile.Dispenser) (Node, error) {
190192
} else {
191193
node.Ephemeral = opt.NewBool(true)
192194
}
195+
case "port":
196+
if segment.NextArg() {
197+
v, err := strconv.ParseUint(segment.Val(), 10, 16)
198+
if err != nil {
199+
return node, segment.WrapErr(err)
200+
}
201+
node.Port = uint16(v)
202+
} else {
203+
node.Port = 0
204+
}
205+
193206
case "hostname":
194207
if !segment.NextArg() {
195208
return node, segment.ArgErr()

module.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ func getTCPListener(c context.Context, network string, host string, portRange st
4949
if !ok {
5050
return nil, fmt.Errorf("context is not a caddy.Context: %T", c)
5151
}
52-
52+
5353
na, err := caddy.ParseNetworkAddress(caddy.JoinNetworkAddress(network, host, portRange))
5454
if err != nil {
5555
return nil, err
5656
}
57-
57+
5858
addr := na.JoinHostPort(portOffset)
5959
network, host, port, err := caddy.SplitNetworkAddress(addr)
6060
if err != nil {
@@ -82,7 +82,7 @@ func getTLSListener(c context.Context, network string, host string, portRange st
8282
if err != nil {
8383
return nil, err
8484
}
85-
85+
8686
addr := na.JoinHostPort(portOffset)
8787
network, host, port, err := caddy.SplitNetworkAddress(addr)
8888
if err != nil {
@@ -121,7 +121,7 @@ func getUDPListener(c context.Context, network string, host string, portRange st
121121
if err != nil {
122122
return nil, err
123123
}
124-
124+
125125
addr := na.JoinHostPort(portOffset)
126126
network, host, port, err := caddy.SplitNetworkAddress(addr)
127127
if err != nil {
@@ -183,6 +183,7 @@ func getNode(ctx caddy.Context, name string) (*tailscaleNode, error) {
183183
},
184184
Ephemeral: getEphemeral(name, app),
185185
RunWebClient: getWebUI(name, app),
186+
Port: getPort(name, app),
186187
}
187188

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

272+
func getPort(name string, app *App) uint16 {
273+
if node, ok := app.Nodes[name]; ok {
274+
return node.Port
275+
}
276+
277+
return 0
278+
}
279+
271280
func getStateDir(name string, app *App) (string, error) {
272281
if node, ok := app.Nodes[name]; ok {
273282
if node.StateDir != "" {

module_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,34 @@ func Test_GetHostname(t *testing.T) {
221221
}
222222
}
223223

224+
func Test_GetPort(t *testing.T) {
225+
app := &App{
226+
Nodes: map[string]Node{
227+
"empty": {},
228+
"port": {Port: 3000},
229+
},
230+
}
231+
if err := app.Provision(caddy.Context{}); err != nil {
232+
t.Fatal(err)
233+
}
234+
235+
got := getPort("noconfig", &App{})
236+
if want := uint16(0); got != want {
237+
t.Errorf("GetPort() = %v, want %v", got, want)
238+
}
239+
240+
got = getPort("empty", app)
241+
if want := uint16(0); got != want {
242+
t.Errorf("GetPort() = %v, want %v", got, want)
243+
}
244+
245+
got = getPort("port", app)
246+
if want := uint16(3000); got != want {
247+
t.Errorf("GetPort() = %v, want %v", got, want)
248+
}
249+
250+
}
251+
224252
func Test_GetStateDir(t *testing.T) {
225253
const nodeName = "node"
226254
configDir := must.Get(os.UserConfigDir())

0 commit comments

Comments
 (0)