Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit f98e94c

Browse files
authored
chore: Use webrtc for coder sh (#408)
- Use webrtc tunnel for `coder sh` command
1 parent 055521d commit f98e94c

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

internal/cmd/configssh.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ func makeSSHConfig(binPath, workspaceName, privateKeyFilepath string, additional
261261
}
262262
options = append(options,
263263
fmt.Sprintf("HostName coder.%s", workspaceName),
264-
fmt.Sprintf("ProxyCommand %q tunnel %s 12213 stdio", binPath, workspaceName),
264+
fmt.Sprintf("ProxyCommand %s", proxyCommand(binPath, workspaceName, true)),
265265
"StrictHostKeyChecking no",
266266
"ConnectTimeout=0",
267267
"IdentitiesOnly yes",
@@ -279,6 +279,13 @@ func makeSSHConfig(binPath, workspaceName, privateKeyFilepath string, additional
279279
return fmt.Sprintf("Host coder.%s\n\t%s\n\n", workspaceName, strings.Join(options, "\n\t"))
280280
}
281281

282+
func proxyCommand(binPath, workspaceName string, quoted bool) string {
283+
if quoted {
284+
binPath = fmt.Sprintf("%q", binPath)
285+
}
286+
return fmt.Sprintf(`%s tunnel %s 12213 stdio`, binPath, workspaceName)
287+
}
288+
282289
func writeStr(filename, data string) error {
283290
return ioutil.WriteFile(filename, []byte(data), 0777)
284291
}

internal/cmd/ssh.go

+13-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"fmt"
5-
"net/url"
65
"os"
76
"os/exec"
87
"os/user"
@@ -41,10 +40,7 @@ func shell(cmd *cobra.Command, args []string) error {
4140
if err != nil {
4241
return err
4342
}
44-
me, err := client.Me(ctx)
45-
if err != nil {
46-
return err
47-
}
43+
4844
workspace, err := findWorkspace(ctx, client, args[0], coder.Me)
4945
if err != nil {
5046
return err
@@ -60,9 +56,9 @@ func shell(cmd *cobra.Command, args []string) error {
6056
if err != nil {
6157
return err
6258
}
63-
u, err := url.Parse(wp.EnvproxyAccessURL)
64-
if err != nil {
65-
return err
59+
60+
if !wp.SSHEnabled {
61+
return clog.Error("SSH is disabled on this Workspace")
6662
}
6763

6864
usr, err := user.Current()
@@ -75,13 +71,21 @@ func shell(cmd *cobra.Command, args []string) error {
7571
if err != nil {
7672
return err
7773
}
74+
75+
binPath, err := binPath()
76+
if err != nil {
77+
return xerrors.Errorf("Failed to get executable path: %w", err)
78+
}
79+
7880
ssh := exec.CommandContext(ctx,
7981
"ssh", "-i"+privateKeyFilepath,
80-
fmt.Sprintf("%s-%s@%s", me.Username, workspace.Name, u.Hostname()),
82+
"-o"+fmt.Sprintf("ProxyCommand=%s", proxyCommand(binPath, workspace.Name, false)),
83+
workspace.Name,
8184
)
8285
if len(args) > 1 {
8386
ssh.Args = append(ssh.Args, args[1:]...)
8487
}
88+
8589
ssh.Stderr = os.Stderr
8690
ssh.Stdout = os.Stdout
8791
ssh.Stdin = os.Stdin

0 commit comments

Comments
 (0)