From b383ad24409692e64c505bc1fc881db861f5e27d Mon Sep 17 00:00:00 2001 From: Evan Simkowitz Date: Fri, 24 Jan 2025 13:44:01 -0800 Subject: [PATCH 1/6] Fix connparse for wsl connections --- pkg/remote/connparse/connparse.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/pkg/remote/connparse/connparse.go b/pkg/remote/connparse/connparse.go index 4f045e637c..1114c3dfd5 100644 --- a/pkg/remote/connparse/connparse.go +++ b/pkg/remote/connparse/connparse.go @@ -23,6 +23,7 @@ const ( ) var windowsDriveRegex = regexp.MustCompile(`^[a-zA-Z]:`) +var wslConnRegex = regexp.MustCompile(`^wsl://[^/]+`) type Connection struct { Scheme string @@ -117,12 +118,17 @@ func ParseURI(uri string) (*Connection, error) { remotePath = rest } } else { - split = strings.SplitN(rest, "/", 2) - host = split[0] - if len(split) > 1 { - remotePath = split[1] + if strings.HasPrefix(rest, "wsl://") { + host = wslConnRegex.FindString(rest) + remotePath = strings.TrimPrefix(rest, host) } else { - remotePath = "/" + split = strings.SplitN(rest, "/", 2) + host = split[0] + if len(split) > 1 { + remotePath = split[1] + } else { + remotePath = "/" + } } } From 0035b6c52cbc66eb7e3200ea712c12437b60d186 Mon Sep 17 00:00:00 2001 From: sawka Date: Fri, 24 Jan 2025 13:47:43 -0800 Subject: [PATCH 2/6] add jwt token back to wsl connections --- pkg/shellexec/shellexec.go | 4 ++++ pkg/util/shellutil/tokenswap.go | 1 - 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/shellexec/shellexec.go b/pkg/shellexec/shellexec.go index 1f9324116a..93c866126c 100644 --- a/pkg/shellexec/shellexec.go +++ b/pkg/shellexec/shellexec.go @@ -262,6 +262,10 @@ func StartWslShellProc(ctx context.Context, termSize waveobj.TermSize, cmdStr st conn.Debugf(ctx, "packed swaptoken %s\n", packedToken) cmdCombined = fmt.Sprintf(`%s=%s %s`, wavebase.WaveSwapTokenVarName, packedToken, cmdCombined) } + jwtToken := cmdOpts.SwapToken.Env[wavebase.WaveJwtTokenVarName] + if jwtToken != "" { + cmdCombined = fmt.Sprintf(`%s=%s %s`, wavebase.WaveJwtTokenVarName, jwtToken, cmdCombined) + } log.Printf("full combined command: %s", cmdCombined) ecmd := exec.Command("wsl.exe", "~", "-d", client.Name(), "--", "sh", "-c", cmdCombined) if termSize.Rows == 0 || termSize.Cols == 0 { diff --git a/pkg/util/shellutil/tokenswap.go b/pkg/util/shellutil/tokenswap.go index 89a16c6183..339ec6445f 100644 --- a/pkg/util/shellutil/tokenswap.go +++ b/pkg/util/shellutil/tokenswap.go @@ -20,7 +20,6 @@ type TokenSwapEntry struct { Token string `json:"token"` SockName string `json:"sockname,omitempty"` RpcContext *wshrpc.RpcContext `json:"rpccontext,omitempty"` - JwtToken string `json:"jwttoken,omitempty"` Env map[string]string `json:"env,omitempty"` ScriptText string `json:"scripttext,omitempty"` Exp time.Time `json:"-"` From d5fa489555b9e4b2dbba023e69384efd46cdfd30 Mon Sep 17 00:00:00 2001 From: sawka Date: Fri, 24 Jan 2025 14:01:09 -0800 Subject: [PATCH 3/6] implement authenticatetokencommand in wshserver.go --- pkg/wshrpc/wshserver/wshserver.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pkg/wshrpc/wshserver/wshserver.go b/pkg/wshrpc/wshserver/wshserver.go index edc7f76294..fd575872b9 100644 --- a/pkg/wshrpc/wshserver/wshserver.go +++ b/pkg/wshrpc/wshserver/wshserver.go @@ -28,6 +28,7 @@ import ( "github.com/wavetermdev/waveterm/pkg/remote/fileshare" "github.com/wavetermdev/waveterm/pkg/telemetry" "github.com/wavetermdev/waveterm/pkg/util/envutil" + "github.com/wavetermdev/waveterm/pkg/util/shellutil" "github.com/wavetermdev/waveterm/pkg/util/utilfn" "github.com/wavetermdev/waveterm/pkg/util/wavefileutil" "github.com/wavetermdev/waveterm/pkg/waveai" @@ -51,6 +52,19 @@ func (*WshServer) WshServerImpl() {} var WshServerImpl = WshServer{} +// TODO remove this after implementing in multiproxy, just for wsl +func (ws *WshServer) AuthenticateTokenCommand(ctx context.Context, data wshrpc.CommandAuthenticateTokenData) (wshrpc.CommandAuthenticateRtnData, error) { + entry := shellutil.GetAndRemoveTokenSwapEntry(data.Token) + if entry == nil { + return wshrpc.CommandAuthenticateRtnData{}, fmt.Errorf("invalid token") + } + rtn := wshrpc.CommandAuthenticateRtnData{ + Env: entry.Env, + InitScriptText: entry.ScriptText, + } + return rtn, nil +} + func (ws *WshServer) TestCommand(ctx context.Context, data string) error { defer func() { panichandler.PanicHandler("TestCommand", recover()) From cee9251214de49e203a1a7e41d760b570097429e Mon Sep 17 00:00:00 2001 From: sawka Date: Fri, 24 Jan 2025 14:11:09 -0800 Subject: [PATCH 4/6] allow windows paths for bg --- frontend/app/app-bg.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/app/app-bg.tsx b/frontend/app/app-bg.tsx index 8b1006de21..2a06f6d220 100644 --- a/frontend/app/app-bg.tsx +++ b/frontend/app/app-bg.tsx @@ -54,7 +54,7 @@ function processBackgroundUrls(cssText: string): string { return; } // allow absolute paths - if (originalUrl.startsWith("/") || originalUrl.startsWith("~/")) { + if (originalUrl.startsWith("/") || originalUrl.startsWith("~/") || /^[a-zA-Z]:(\/|\\)/.test(originalUrl)) { const newUrl = encodeFileURL(originalUrl); node.value = newUrl; return; From d4d23d8e15fd2d5aafb65c6c40e0fa879f85d4d5 Mon Sep 17 00:00:00 2001 From: Evan Simkowitz Date: Fri, 24 Jan 2025 14:11:30 -0800 Subject: [PATCH 5/6] save --- cmd/wsh/cmd/wshcmd-setbg.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/wsh/cmd/wshcmd-setbg.go b/cmd/wsh/cmd/wshcmd-setbg.go index bbd2071352..be57b9abd8 100644 --- a/cmd/wsh/cmd/wshcmd-setbg.go +++ b/cmd/wsh/cmd/wshcmd-setbg.go @@ -140,7 +140,7 @@ func setBgRun(cmd *cobra.Command, args []string) (rtnErr error) { } // Create URL-safe path - escapedPath := strings.ReplaceAll(absPath, "\\", "\\\\") + escapedPath := filepath.ToSlash(absPath) escapedPath = strings.ReplaceAll(escapedPath, "'", "\\'") bgStyle = fmt.Sprintf("url('%s')", escapedPath) From 9d445c37f59d531568454452cb76b2b789005cd8 Mon Sep 17 00:00:00 2001 From: Evan Simkowitz Date: Fri, 24 Jan 2025 14:52:29 -0800 Subject: [PATCH 6/6] fix connparse for windows/wsl --- cmd/wsh/cmd/wshcmd-file-util.go | 3 ++ pkg/remote/connparse/connparse.go | 55 ++++++++++++++++++------------- pkg/util/fileutil/fileutil.go | 6 ++-- 3 files changed, 37 insertions(+), 27 deletions(-) diff --git a/cmd/wsh/cmd/wshcmd-file-util.go b/cmd/wsh/cmd/wshcmd-file-util.go index 432cc1b1fd..119294035c 100644 --- a/cmd/wsh/cmd/wshcmd-file-util.go +++ b/cmd/wsh/cmd/wshcmd-file-util.go @@ -8,6 +8,7 @@ import ( "fmt" "io" "io/fs" + "log" "strings" "github.com/wavetermdev/waveterm/pkg/remote/connparse" @@ -210,10 +211,12 @@ func streamFileList(zoneId string, path string, recursive bool, filesOnly bool) } func fixRelativePaths(path string) (string, error) { + log.Printf("fixRelativePaths: path=%q", path) conn, err := connparse.ParseURI(path) if err != nil { return "", err } + log.Printf("fixRelativePaths: conn=%v", conn) if conn.Scheme == connparse.ConnectionTypeWsh { if conn.Host == connparse.ConnHostCurrent { conn.Host = RpcContext.Conn diff --git a/pkg/remote/connparse/connparse.go b/pkg/remote/connparse/connparse.go index 1114c3dfd5..bb37cdb843 100644 --- a/pkg/remote/connparse/connparse.go +++ b/pkg/remote/connparse/connparse.go @@ -6,6 +6,7 @@ package connparse import ( "context" "fmt" + "log" "regexp" "strings" @@ -87,29 +88,46 @@ func GetConnNameFromContext(ctx context.Context) (string, error) { // ParseURI parses a connection URI and returns the connection type, host/path, and parameters. func ParseURI(uri string) (*Connection, error) { - split := strings.SplitN(uri, "://", 2) + split := strings.SplitN(uri, "//", 2) var scheme string var rest string if len(split) > 1 { - scheme = split[0] + scheme = strings.TrimSuffix(split[0], ":") rest = split[1] } else { rest = split[0] } + log.Printf("parseURI: scheme=%q, rest=%q", scheme, rest) + var host string var remotePath string + + parseGenericPath := func() { + split = strings.SplitN(rest, "/", 2) + host = split[0] + if len(split) > 1 { + remotePath = split[1] + } else { + remotePath = "/" + } + log.Printf("parseGenericPath: host=%q, remotePath=%q", host, remotePath) + } + parseWshPath := func() { + if strings.HasPrefix(rest, "wsl://") { + host = wslConnRegex.FindString(rest) + remotePath = strings.TrimPrefix(rest, host) + log.Printf("parseWshPath: host=%q, remotePath=%q", host, remotePath) + } else { + parseGenericPath() + } + } + if scheme == "" { scheme = ConnectionTypeWsh - if strings.HasPrefix(rest, "//") { - rest = strings.TrimPrefix(rest, "//") - split = strings.SplitN(rest, "/", 2) - host = split[0] - if len(split) > 1 { - remotePath = split[1] - } else { - remotePath = "/" - } + if len(rest) != len(uri) { + // This accounts for when the uri starts with "//", which would get trimmed in the first split. + parseWshPath() } else if strings.HasPrefix(rest, "/~") { host = wshrpc.LocalConnName remotePath = rest @@ -117,19 +135,10 @@ func ParseURI(uri string) (*Connection, error) { host = ConnHostCurrent remotePath = rest } + } else if scheme == ConnectionTypeWsh { + parseWshPath() } else { - if strings.HasPrefix(rest, "wsl://") { - host = wslConnRegex.FindString(rest) - remotePath = strings.TrimPrefix(rest, host) - } else { - split = strings.SplitN(rest, "/", 2) - host = split[0] - if len(split) > 1 { - remotePath = split[1] - } else { - remotePath = "/" - } - } + parseGenericPath() } if scheme == ConnectionTypeWsh { diff --git a/pkg/util/fileutil/fileutil.go b/pkg/util/fileutil/fileutil.go index 84d9e1e88d..b5bbfbb1b3 100644 --- a/pkg/util/fileutil/fileutil.go +++ b/pkg/util/fileutil/fileutil.go @@ -19,7 +19,7 @@ import ( func FixPath(path string) (string, error) { if strings.HasPrefix(path, "~") { - return filepath.Join(wavebase.GetHomeDir(), path[1:]), nil + path = filepath.Join(wavebase.GetHomeDir(), path[1:]) } else if !filepath.IsAbs(path) { log.Printf("FixPath: path is not absolute: %s", path) path, err := filepath.Abs(path) @@ -27,10 +27,8 @@ func FixPath(path string) (string, error) { return "", err } log.Printf("FixPath: fixed path: %s", path) - return path, nil - } else { - return path, nil } + return path, nil } const (