Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat(cli): implement ssh remote forward #8515

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 14 commits into from
Jul 20, 2023
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
Prev Previous commit
Next Next commit
basic implementation
  • Loading branch information
mtojek committed Jul 14, 2023
commit 6df2a9bc2f13daa804967bd83a94840c975b0039
55 changes: 50 additions & 5 deletions cli/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -40,6 +42,8 @@ import (
var (
workspacePollInterval = time.Minute
autostopNotifyCountdown = []time.Duration{30 * time.Minute}

remoteForwardRegex = regexp.MustCompile(`^\d+:\w+:\d+$`)
)

//nolint:gocyclo
Expand Down Expand Up @@ -123,7 +127,15 @@ func (r *RootCmd) ssh() *clibase.Cmd {
client.SetLogger(logger)
}

// TODO Validate -R and --stdio
if remoteForward != "" {
isValid := remoteForwardRegex.MatchString(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`)
}
}

workspace, workspaceAgent, err := getWorkspaceAndAgent(ctx, inv, client, codersdk.Me, inv.Args[0])
if err != nil {
Expand Down Expand Up @@ -303,7 +315,40 @@ func (r *RootCmd) ssh() *clibase.Cmd {
defer closer.Close()
}

// TODO if sshForwardRemote
if remoteForward != "" {
matches := remoteForwardRegex.FindStringSubmatch(remoteForward)

// Format:
// remote_port:local_address:local_port
remotePort, err := strconv.Atoi(matches[1])
if err != nil {
return xerrors.Errorf("remote port is invalid: %w", err)
}
localAddress, err := net.ResolveIPAddr("ip", matches[2])
if err != nil {
return xerrors.Errorf("local address is invalid: %w", err)
}
localPort, err := strconv.Atoi(matches[3])
if err != nil {
return xerrors.Errorf("local port is invalid: %w", err)
}

localAddr := &net.TCPAddr{
IP: localAddress.IP,
Port: localPort,
}

remoteAddr := &net.TCPAddr{
IP: net.ParseIP("127.0.0.1"),
Port: remotePort,
}

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

stdoutFile, validOut := inv.Stdout.(*os.File)
stdinFile, validIn := inv.Stdin.(*os.File)
Expand Down Expand Up @@ -765,18 +810,18 @@ func remoteGPGAgentSocket(sshClient *gossh.Client) (string, error) {
return string(bytes.TrimSpace(remoteSocket)), nil
}

// cookieAddr is a special net.Addr accepted by sshForward() which includes a
// cookieAddr is a special net.Addr accepted by sshRemoteForward() which includes a
// cookie which is written to the connection before forwarding.
type cookieAddr struct {
net.Addr
cookie []byte
}

// sshForwardRemote starts forwarding connections from a remote listener to a
// sshRemoteForward starts forwarding connections from a remote listener to a
// local address via SSH in a goroutine.
//
// Accepts a `cookieAddr` as the local address.
func sshForwardRemote(ctx context.Context, stderr io.Writer, sshClient *gossh.Client, localAddr, remoteAddr net.Addr) (io.Closer, error) {
func sshRemoteForward(ctx context.Context, stderr io.Writer, sshClient *gossh.Client, localAddr, remoteAddr net.Addr) (io.Closer, error) {
listener, err := sshClient.Listen(remoteAddr.Network(), remoteAddr.String())
if err != nil {
return nil, xerrors.Errorf("listen on remote SSH address %s: %w", remoteAddr.String(), err)
Expand Down
2 changes: 1 addition & 1 deletion cli/ssh_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ func forwardGPGAgent(ctx context.Context, stderr io.Writer, sshClient *gossh.Cli
Net: "unix",
}

return sshForwardRemote(ctx, stderr, sshClient, localAddr, remoteAddr)
return sshRemoteForward(ctx, stderr, sshClient, localAddr, remoteAddr)
}
2 changes: 1 addition & 1 deletion cli/ssh_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,5 @@ func forwardGPGAgent(ctx context.Context, stderr io.Writer, sshClient *gossh.Cli
Net: "unix",
}

return sshForwardRemote(ctx, stderr, sshClient, localAddr, remoteAddr)
return sshRemoteForward(ctx, stderr, sshClient, localAddr, remoteAddr)
}