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

fix: Networking V2 tunnel on Windows #356

Merged
merged 1 commit into from
May 21, 2021
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
24 changes: 15 additions & 9 deletions cmd/coder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,21 @@ func main() {
}()
}

stdoutState, err := xterminal.MakeOutputRaw(os.Stdout.Fd())
if err != nil {
clog.Log(clog.Fatal(fmt.Sprintf("set output to raw: %s", err)))
cancel()
os.Exit(1)
}
restoreTerminal := func() {
// Best effort. Would result in broken terminal on window but nothing we can do about it.
_ = xterminal.Restore(os.Stdout.Fd(), stdoutState)
restoreTerminal := func() {}

// Janky, but SSH on windows sets the output to raw.
// If we set it ourselves, SSH fails because the FD isn't found.
if len(os.Args) >= 2 && os.Args[1] != "tunnel" {
state, err := xterminal.MakeOutputRaw(os.Stdout.Fd())
if err != nil {
clog.Log(clog.Fatal(fmt.Sprintf("set output to raw: %s", err)))
cancel()
os.Exit(1)
}
restoreTerminal = func() {
// Best effort. Would result in broken terminal on window but nothing we can do about it.
_ = xterminal.Restore(os.Stdout.Fd(), state)
}
}

app := cmd.Make()
Expand Down
17 changes: 11 additions & 6 deletions internal/cmd/configssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,12 @@ func configSSH(configpath *string, remove *bool, next *bool) func(cmd *cobra.Com
}
}

newConfig := makeNewConfigs(user.Username, workspacesWithProviders, privateKeyFilepath, p2p)
binPath, err := os.Executable()
if err != nil {
return xerrors.Errorf("Failed to get executable path: %w", err)
}

newConfig := makeNewConfigs(binPath, user.Username, workspacesWithProviders, privateKeyFilepath, p2p)

err = os.MkdirAll(filepath.Dir(*configpath), os.ModePerm)
if err != nil {
Expand Down Expand Up @@ -193,7 +198,7 @@ func writeSSHKey(ctx context.Context, client coder.Client, privateKeyPath string
return ioutil.WriteFile(privateKeyPath, []byte(key.PrivateKey), 0600)
}

func makeNewConfigs(userName string, workspaces []coderutil.WorkspaceWithWorkspaceProvider, privateKeyFilepath string, p2p bool) string {
func makeNewConfigs(binPath, userName string, workspaces []coderutil.WorkspaceWithWorkspaceProvider, privateKeyFilepath string, p2p bool) string {
newConfig := fmt.Sprintf("\n%s\n%s\n\n", sshStartToken, sshStartMessage)

sort.Slice(workspaces, func(i, j int) bool { return workspaces[i].Workspace.Name < workspaces[j].Workspace.Name })
Expand All @@ -213,24 +218,24 @@ func makeNewConfigs(userName string, workspaces []coderutil.WorkspaceWithWorkspa
}

useTunnel := workspace.WorkspaceProvider.BuiltIn && p2p
newConfig += makeSSHConfig(u.Host, userName, workspace.Workspace.Name, privateKeyFilepath, useTunnel)
newConfig += makeSSHConfig(binPath, u.Host, userName, workspace.Workspace.Name, privateKeyFilepath, useTunnel)
}
newConfig += fmt.Sprintf("\n%s\n", sshEndToken)

return newConfig
}

func makeSSHConfig(host, userName, workspaceName, privateKeyFilepath string, tunnel bool) string {
func makeSSHConfig(binPath, host, userName, workspaceName, privateKeyFilepath string, tunnel bool) string {
if tunnel {
return fmt.Sprintf(
`Host coder.%s
HostName coder.%s
ProxyCommand coder tunnel %s 12213 stdio
ProxyCommand %s tunnel %s 12213 stdio
StrictHostKeyChecking no
ConnectTimeout=0
IdentitiesOnly yes
IdentityFile="%s"
`, workspaceName, workspaceName, workspaceName, privateKeyFilepath)
`, workspaceName, workspaceName, binPath, workspaceName, privateKeyFilepath)
}

return fmt.Sprintf(
Expand Down