Skip to content
This repository was archived by the owner on Jan 17, 2021. It is now read-only.

Commit 73309c7

Browse files
committed
fix msys-support completely, fully functional
1 parent b895d1b commit 73309c7

File tree

3 files changed

+83
-74
lines changed

3 files changed

+83
-74
lines changed

main.go

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@ package main
22

33
import (
44
"fmt"
5-
"log"
65
"math/rand"
76
"os"
8-
"os/exec"
9-
"path/filepath"
10-
"regexp"
117
"runtime"
12-
"sort"
138
"strings"
149
"time"
1510

@@ -29,8 +24,6 @@ var (
2924
helpTab = strings.Repeat(" ", helpTabWidth)
3025
// version is overwritten by ci/build.sh.
3126
version string
32-
res string
33-
dir string
3427
)
3528

3629
func main() {
@@ -129,50 +122,3 @@ Arguments:
129122
helpTab,
130123
)
131124
}
132-
133-
// This section translates a windows path such as "C:\Users\user" to "/Users/user"
134-
// and removes the default paths for mingw and git4windows to issues when you
135-
// specify a file path to start code-server in.
136-
func gitbashWindowsDir(dir string) string {
137-
if dir == "~" { //Special case
138-
return "~/"
139-
}
140-
mountPoints := gitbashMountPointsAndHome()
141-
142-
// Apply mount points
143-
absDir, _ := filepath.Abs(dir)
144-
absDir = filepath.ToSlash(absDir)
145-
for _, mp := range mountPoints {
146-
if strings.HasPrefix(absDir, mp[0]) {
147-
resolved := strings.Replace(absDir, mp[0], mp[1], 1)
148-
flog.Info("Resolved windows path '%s' to '%s", dir, resolved)
149-
return resolved
150-
}
151-
}
152-
return dir
153-
}
154-
155-
// This function returns an array with MINGW64 mount points including relative home dir
156-
func gitbashMountPointsAndHome() [][]string {
157-
// Initialize mount points with home dir
158-
mountPoints := [][]string{{filepath.ToSlash(os.Getenv("HOME")), "~"}}
159-
// Load mount points
160-
out, err := exec.Command("mount").Output()
161-
if err != nil {
162-
log.Fatal(err)
163-
}
164-
lines := strings.Split(string(out), "\n")
165-
var mountRx = regexp.MustCompile(`^(.*) on (.*) type`)
166-
for _, line := range lines {
167-
extract := mountRx.FindStringSubmatch(line)
168-
if len(extract) > 0 {
169-
mountPoints = append(mountPoints, []string{extract[1], extract[2]})
170-
}
171-
res = strings.TrimPrefix(dir, line)
172-
}
173-
// Sort by size to get more restrictive mount points first
174-
sort.Slice(mountPoints, func(i, j int) bool {
175-
return len(mountPoints[i][0]) > len(mountPoints[j][0])
176-
})
177-
return mountPoints
178-
}

settings.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,9 @@ func extensionsDir() (string, error) {
5050
}
5151

5252
func windowsVarFix(input string) string {
53-
var remoteSettingsDir = "~/.local/share/code-server/User/"
54-
var remoteExtensionsDir = "~/.local/share/code-server/extensions/"
5553
if runtime.GOOS == "windows" {
56-
remoteSettingsDir = ".local/share/code-server/User/"
57-
remoteExtensionsDir = ".local/share/code-server/extensions/"
54+
return input
5855
}
5956

60-
if input == "remoteSettingsDir" {
61-
return remoteSettingsDir
62-
} else if input == "remoteExtensionsDir" {
63-
return remoteExtensionsDir
64-
}
65-
return ""
57+
return "~/" + input
6658
}

sshcode.go

Lines changed: 81 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ package main
33
import (
44
"context"
55
"fmt"
6+
"log"
67
"math/rand"
78
"net"
89
"net/http"
910
"os"
1011
"os/exec"
1112
"os/signal"
1213
"path/filepath"
14+
"regexp"
1315
"runtime"
16+
"sort"
1417
"strconv"
1518
"strings"
1619
"syscall"
@@ -107,7 +110,6 @@ func sshCode(host, dir string, o options) error {
107110

108111
// Downloads the latest code-server and allows it to be executed.
109112
sshCmdStr := fmt.Sprintf("ssh %v %v '/usr/bin/env bash -l'", o.sshFlags, host)
110-
111113
sshCmd := exec.Command("sh", "-l", "-c", sshCmdStr)
112114
sshCmd.Stdout = os.Stdout
113115
sshCmd.Stderr = os.Stderr
@@ -146,10 +148,9 @@ func sshCode(host, dir string, o options) error {
146148
flog.Info("Tunneling remote port %v to %v", o.remotePort, o.bindAddr)
147149

148150
sshCmdStr :=
149-
fmt.Sprintf("ssh -tt -q -L %v:localhost:%v %v %v 'cd %v; %v --host 127.0.0.1 --auth none --port=%v'",
150-
o.bindAddr, o.remotePort, o.sshFlags, host, dir, codeServerPath, o.remotePort,
151+
fmt.Sprintf("ssh -tt -q -L %v:localhost:%v %v %v '%v %v --host 127.0.0.1 --auth none --port=%v'",
152+
o.bindAddr, o.remotePort, o.sshFlags, host, codeServerPath, dir, o.remotePort,
151153
)
152-
153154
// Starts code-server and forwards the remote port.
154155
sshCmd := exec.Command("sh", "-l", "-c", sshCmdStr)
155156
sshCmd.Stdin = os.Stdin
@@ -341,7 +342,6 @@ func randomPort() (string, error) {
341342
// checkSSHDirectory performs sanity and safety checks on sshDirectory, and
342343
// returns a new value for o.reuseConnection depending on the checks.
343344
func checkSSHDirectory(sshDirectory string, reuseConnection bool) bool {
344-
345345
if runtime.GOOS == "windows" {
346346
flog.Info("OS is windows, disabling connection reuse feature")
347347
return false
@@ -463,8 +463,8 @@ func syncUserSettings(sshFlags string, host string, back bool) error {
463463
return err
464464
}
465465

466-
//const remoteSettingsDir = ".local/share/code-server/User/"
467-
var remoteSettingsDir = windowsVarFix("remoteSettingsDir")
466+
var remoteSettingsDir = windowsVarFix(".local/share/code-server/User/")
467+
468468
var (
469469
src = localConfDir + "/"
470470
dest = host + ":" + remoteSettingsDir
@@ -489,8 +489,7 @@ func syncExtensions(sshFlags string, host string, back bool) error {
489489
return err
490490
}
491491

492-
//const remoteExtensionsDir = ".local/share/code-server/extensions/"
493-
var remoteExtensionsDir = windowsVarFix("remoteExtensionsDir")
492+
var remoteExtensionsDir = windowsVarFix(".local/share/code-server/extensions/")
494493

495494
var (
496495
src = localExtensionsDir + "/"
@@ -561,8 +560,10 @@ chmod +x %v`,
561560
func ensureDir(path string) error {
562561
_, err := os.Stat(path)
563562
if os.IsNotExist(err) {
563+
// This fixes a issue where GO reads `/c/` as `C:\c\` and creates
564+
// empty directories on HOST/CLIENT that dont need to exist
564565
if runtime.GOOS == "windows" && strings.HasPrefix(path, "/c/") {
565-
path = path[3:]
566+
path = "C:" + path[2:]
566567
}
567568
err = os.MkdirAll(path, 0750)
568569
}
@@ -624,3 +625,73 @@ func parseGCPSSHCmd(instance string) (ip, sshFlags string, err error) {
624625

625626
return strings.TrimSpace(userIP), sshFlags, nil
626627
}
628+
629+
// gitbashMountPoints returns all mount points in a msys2 system and then trunicates
630+
// the mount point for `/` from the path provided (dir), this is done to fix an
631+
// issue with how msys2 provides file paths to go, example. When you feed a filepath
632+
// as user input to go, `command /opt` for example, msys2 will pass `C:\<path to msys2 directory>\opt`
633+
// This causes an issue whith this program where when you run `sshcode.exe user@server /opt`,
634+
// GO and msys2 will attempt to pass `C:\<path to msys2 directory>\opt` to the server.
635+
// This function is used to prevent this.
636+
func gitbashWindowsDir(dir string) string {
637+
638+
// if dir is left empty, line82:main.go will set it to `~`, this makes it so that
639+
// if dir is `~`, return `~/` instead of continuing with the gitbashWindowsDir()
640+
// function. this prevens windows from trying to send a litteral instead of a relative
641+
if dir == "~" {
642+
return "~/"
643+
}
644+
645+
//If msys feeds a `C:` path, clense
646+
if strings.HasPrefix(dir, "C:") {
647+
mountPoints := gitbashMountPointsAndHome()
648+
649+
// Apply mount points
650+
absDir, _ := filepath.Abs(dir)
651+
absDir = filepath.ToSlash(absDir)
652+
for _, mp := range mountPoints {
653+
if strings.HasPrefix(absDir, mp[0]) {
654+
resolved := strings.Replace(absDir, mp[0], mp[1], 1)
655+
656+
// Sometimes the resolved path can go from user input `/Workspace`
657+
// to `//Workspace`, this if statement checks that and removes it
658+
if strings.HasPrefix(resolved, "//") {
659+
resolved = strings.TrimPrefix(resolved, "/")
660+
flog.Info("Resolved windows path '%s' to '%s", dir, resolved)
661+
return resolved
662+
}
663+
664+
flog.Info("Resolved windows path '%s' to '%s", dir, resolved)
665+
return resolved
666+
}
667+
}
668+
669+
}
670+
return dir
671+
}
672+
673+
// This function returns an array with MINGW64 mount points including relative home dir
674+
func gitbashMountPointsAndHome() [][]string {
675+
mountPoints := [][]string{{filepath.ToSlash(os.Getenv("HOME")), "~"}}
676+
677+
// Load mount points
678+
out, err := exec.Command("mount").Output()
679+
if err != nil {
680+
//log.Error(err)
681+
log.Println(err)
682+
}
683+
lines := strings.Split(string(out), "\n")
684+
var mountRx = regexp.MustCompile(`^(.*) on (.*) type`)
685+
for _, line := range lines {
686+
extract := mountRx.FindStringSubmatch(line)
687+
if len(extract) > 0 {
688+
mountPoints = append(mountPoints, []string{extract[1], extract[2]})
689+
}
690+
}
691+
692+
// Sort by size to get more restrictive mount points first
693+
sort.Slice(mountPoints, func(i, j int) bool {
694+
return len(mountPoints[i][0]) > len(mountPoints[j][0])
695+
})
696+
return mountPoints
697+
}

0 commit comments

Comments
 (0)