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

[v9] Error out if port is already bound (#13464) #13679

Merged
merged 1 commit into from
Jun 21, 2022
Merged
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
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