Skip to content
Merged
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
11 changes: 8 additions & 3 deletions internal/codespaces/codespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,14 @@ func ConnectToLiveshare(ctx context.Context, progress progressIndicator, session
})
}

// ListenTCP starts a localhost tcp listener and returns the listener and bound port
func ListenTCP(port int) (*net.TCPListener, int, error) {
addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("127.0.0.1:%d", port))
// ListenTCP starts a localhost tcp listener on 127.0.0.1 (unless allInterfaces is true) and returns the listener and bound port
func ListenTCP(port int, allInterfaces bool) (*net.TCPListener, int, error) {
host := "127.0.0.1"
if allInterfaces {
host = ""
}

addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", host, port))
if err != nil {
return nil, 0, fmt.Errorf("failed to build tcp address: %w", err)
}
Expand Down
1 change: 1 addition & 0 deletions internal/codespaces/rpc/invoker.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ func (i *invoker) StartSSHServerWithOptions(ctx context.Context, options StartSS
}

func listenTCP() (*net.TCPListener, error) {
// We will end up using this same address to connect, so specify the IP also or the connect will fail
addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:0")
if err != nil {
return nil, fmt.Errorf("failed to build tcp address: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion internal/codespaces/states.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func PollPostCreateStates(ctx context.Context, progress progressIndicator, apiCl
}()

// Ensure local port is listening before client (getPostCreateOutput) connects.
listen, localPort, err := ListenTCP(0)
listen, localPort, err := ListenTCP(0, false)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/codespace/jupyter.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (a *App) Jupyter(ctx context.Context, selector *CodespaceSelector) (err err
}

// Pass 0 to pick a random port
listen, _, err := codespaces.ListenTCP(0)
listen, _, err := codespaces.ListenTCP(0, false)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/codespace/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (a *App) Logs(ctx context.Context, selector *CodespaceSelector, follow bool
defer safeClose(session, &err)

// Ensure local port is listening before client (getPostCreateOutput) connects.
listen, localPort, err := codespaces.ListenTCP(0)
listen, localPort, err := codespaces.ListenTCP(0, false)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/codespace/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ func (a *App) ForwardPorts(ctx context.Context, selector *CodespaceSelector, por
for _, pair := range portPairs {
pair := pair
group.Go(func() error {
listen, _, err := codespaces.ListenTCP(pair.local)
listen, _, err := codespaces.ListenTCP(pair.local, true)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/codespace/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (a *App) SSH(ctx context.Context, sshArgs []string, opts sshOptions) (err e
// Ensure local port is listening before client (Shell) connects.
// Unless the user specifies a server port, localSSHServerPort is 0
// and thus the client will pick a random port.
listen, localSSHServerPort, err := codespaces.ListenTCP(localSSHServerPort)
listen, localSSHServerPort, err := codespaces.ListenTCP(localSSHServerPort, false)
if err != nil {
return err
}
Expand Down