Skip to content

feat(cli/ssh): allow multiple remote forwards and allow missing local file #11648

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

Merged
merged 4 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat(cli/ssh): allow multiple remote forwards
  • Loading branch information
mafredri committed Jan 19, 2024
commit d192887d4ac9032f91be2264d776d1f202641b6c
13 changes: 3 additions & 10 deletions cli/remoteforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io"
"net"
"os"
"regexp"
"strconv"

Expand Down Expand Up @@ -67,19 +66,13 @@ func parseRemoteForwardTCP(matches []string) (net.Addr, net.Addr, error) {
return localAddr, remoteAddr, nil
}

// parseRemoteForwardUnixSocket parses a remote forward flag. Note that
// we don't verify that the local socket path exists because the user
// may create it later. This behavior matches OpenSSH.
func parseRemoteForwardUnixSocket(matches []string) (net.Addr, net.Addr, error) {
remoteSocket := matches[1]
localSocket := matches[2]

fileInfo, err := os.Stat(localSocket)
if err != nil {
return nil, nil, err
}

if fileInfo.Mode()&os.ModeSocket == 0 {
return nil, nil, xerrors.New("File is not a Unix domain socket file")
}

remoteAddr := &net.UnixAddr{
Name: remoteSocket,
Net: "unix",
Expand Down
44 changes: 24 additions & 20 deletions cli/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (r *RootCmd) ssh() *clibase.Cmd {
waitEnum string
noWait bool
logDirPath string
remoteForward string
remoteForwards []string
disableAutostart bool
)
client := new(codersdk.Client)
Expand Down Expand Up @@ -135,13 +135,15 @@ func (r *RootCmd) ssh() *clibase.Cmd {
stack := newCloserStack(ctx, logger)
defer stack.close(nil)

if remoteForward != "" {
isValid := validateRemoteForward(remoteForward)
if !isValid {
return xerrors.Errorf(`invalid format of remote-forward, expected: remote_port:local_address:local_port`)
}
if isValid && stdio {
return xerrors.Errorf(`remote-forward can't be enabled in the stdio mode`)
if len(remoteForwards) > 0 {
for _, remoteForward := range remoteForwards {
isValid := validateRemoteForward(remoteForward)
if !isValid {
return xerrors.Errorf(`invalid format of remote-forward, expected: remote_port:local_address:local_port`)
}
if isValid && stdio {
return xerrors.Errorf(`remote-forward can't be enabled in the stdio mode`)
}
}
}

Expand Down Expand Up @@ -311,18 +313,20 @@ func (r *RootCmd) ssh() *clibase.Cmd {
}
}

if remoteForward != "" {
localAddr, remoteAddr, err := parseRemoteForward(remoteForward)
if err != nil {
return err
}
if len(remoteForwards) > 0 {
for _, remoteForward := range remoteForwards {
localAddr, remoteAddr, err := parseRemoteForward(remoteForward)
if err != nil {
return err
}

closer, err := sshRemoteForward(ctx, inv.Stderr, sshClient, localAddr, remoteAddr)
if err != nil {
return xerrors.Errorf("ssh remote forward: %w", err)
}
if err = stack.push("sshRemoteForward", closer); err != nil {
return err
closer, err := sshRemoteForward(ctx, inv.Stderr, sshClient, localAddr, remoteAddr)
if err != nil {
return xerrors.Errorf("ssh remote forward: %w", err)
}
if err = stack.push("sshRemoteForward", closer); err != nil {
return err
}
}
}

Expand Down Expand Up @@ -460,7 +464,7 @@ func (r *RootCmd) ssh() *clibase.Cmd {
Description: "Enable remote port forwarding (remote_port:local_address:local_port).",
Env: "CODER_SSH_REMOTE_FORWARD",
FlagShorthand: "R",
Value: clibase.StringOf(&remoteForward),
Value: clibase.StringArrayOf(&remoteForwards),
},
sshDisableAutostartOption(clibase.BoolOf(&disableAutostart)),
}
Expand Down