Skip to content

Commit

Permalink
Error out if port is already bound (#13679)
Browse files Browse the repository at this point in the history
  • Loading branch information
EdwardDowling authored Jun 21, 2022
1 parent 6d8004b commit 834aeda
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions lib/client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1705,7 +1705,9 @@ func (tc *TeleportClient) SSH(ctx context.Context, command []string, runLocally
defer nodeClient.Close()

// If forwarding ports were specified, start port forwarding.
tc.startPortForwarding(ctx, nodeClient)
if err := tc.startPortForwarding(ctx, nodeClient); err != nil {
return trace.Wrap(err)
}

// If no remote command execution was requested, block on the context which
// will unblock upon error or SIGINT.
Expand Down Expand Up @@ -1746,14 +1748,13 @@ func (tc *TeleportClient) SSH(ctx context.Context, command []string, runLocally
return tc.runShell(ctx, nodeClient, types.SessionPeerMode, nil, nil)
}

func (tc *TeleportClient) startPortForwarding(ctx context.Context, nodeClient *NodeClient) {
func (tc *TeleportClient) startPortForwarding(ctx context.Context, nodeClient *NodeClient) error {
if len(tc.Config.LocalForwardPorts) > 0 {
for _, fp := range tc.Config.LocalForwardPorts {
addr := net.JoinHostPort(fp.SrcIP, strconv.Itoa(fp.SrcPort))
socket, err := net.Listen("tcp", addr)
if err != nil {
log.Errorf("Failed to bind to %v: %v.", addr, err)
continue
return trace.Errorf("Failed to bind to %v: %v.", addr, err)
}
go nodeClient.listenAndForward(ctx, socket, net.JoinHostPort(fp.DestHost, strconv.Itoa(fp.DestPort)))
}
Expand All @@ -1763,12 +1764,12 @@ func (tc *TeleportClient) startPortForwarding(ctx context.Context, nodeClient *N
addr := net.JoinHostPort(fp.SrcIP, strconv.Itoa(fp.SrcPort))
socket, err := net.Listen("tcp", addr)
if err != nil {
log.Errorf("Failed to bind to %v: %v.", addr, err)
continue
return trace.Errorf("Failed to bind to %v: %v.", addr, err)
}
go nodeClient.dynamicListenAndForward(ctx, socket)
}
}
return nil
}

// Join connects to the existing/active SSH session
Expand Down Expand Up @@ -1850,7 +1851,9 @@ func (tc *TeleportClient) Join(ctx context.Context, mode types.SessionParticipan
defer nc.Close()

// Start forwarding ports if configured.
tc.startPortForwarding(ctx, nc)
if err := tc.startPortForwarding(ctx, nc); err != nil {
return trace.Wrap(err)
}

presenceCtx, presenceCancel := context.WithCancel(ctx)
defer presenceCancel()
Expand Down

0 comments on commit 834aeda

Please sign in to comment.