mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 11:03:34 +00:00
fix: SSH control socket path in WSL2 when runtime dir doesn't exist (fixes #319)
This commit is contained in:
@@ -45,10 +45,14 @@ func controlSocketPath() string {
|
|||||||
// of the ProxyJump option. This ensures that shared connections are uniquely identified.
|
// of the ProxyJump option. This ensures that shared connections are uniquely identified.
|
||||||
sockName := fmt.Sprintf("uc_control_%%C.sock")
|
sockName := fmt.Sprintf("uc_control_%%C.sock")
|
||||||
|
|
||||||
// Prefer XDG_RUNTIME_DIR if set, fall back to ~/.ssh if it exists.
|
// Prefer XDG_RUNTIME_DIR if set and the directory exists, fall back to ~/.ssh if it exists.
|
||||||
if dir := os.Getenv("XDG_RUNTIME_DIR"); dir != "" {
|
if dir := os.Getenv("XDG_RUNTIME_DIR"); dir != "" {
|
||||||
|
// On WSL2 without systemd, XDG_RUNTIME_DIR may be set to /run/user/$UID that doesn't actually exist,
|
||||||
|
// so existence must be verified before use: https://github.com/psviderski/uncloud/issues/319.
|
||||||
|
if fi, err := os.Stat(dir); err == nil && fi.IsDir() {
|
||||||
return filepath.Join(dir, sockName)
|
return filepath.Join(dir, sockName)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if home, err := os.UserHomeDir(); err == nil {
|
if home, err := os.UserHomeDir(); err == nil {
|
||||||
sshDir := filepath.Join(home, ".ssh")
|
sshDir := filepath.Join(home, ".ssh")
|
||||||
if fi, sErr := os.Stat(sshDir); sErr == nil && fi.IsDir() {
|
if fi, sErr := os.Stat(sshDir); sErr == nil && fi.IsDir() {
|
||||||
|
|||||||
@@ -109,11 +109,20 @@ func TestControlSocketPath(t *testing.T) {
|
|||||||
assert.True(t, strings.HasSuffix(path1, ".sock"))
|
assert.True(t, strings.HasSuffix(path1, ".sock"))
|
||||||
assert.Contains(t, path1, "%C")
|
assert.Contains(t, path1, "%C")
|
||||||
|
|
||||||
t.Run("uses XDG_RUNTIME_DIR when set", func(t *testing.T) {
|
t.Run("uses XDG_RUNTIME_DIR when set and exists", func(t *testing.T) {
|
||||||
runDir := "/user/runtime/dir"
|
runDir := t.TempDir()
|
||||||
t.Setenv("XDG_RUNTIME_DIR", runDir)
|
t.Setenv("XDG_RUNTIME_DIR", runDir)
|
||||||
|
|
||||||
path := controlSocketPath()
|
path := controlSocketPath()
|
||||||
assert.True(t, strings.HasPrefix(path, runDir))
|
assert.True(t, strings.HasPrefix(path, runDir))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("falls back when XDG_RUNTIME_DIR is set but missing", func(t *testing.T) {
|
||||||
|
// WSL2 without systemd sets XDG_RUNTIME_DIR to a path that doesn't exist.
|
||||||
|
t.Setenv("XDG_RUNTIME_DIR", "/nonexistent/uncloud-test-xdg")
|
||||||
|
|
||||||
|
path := controlSocketPath()
|
||||||
|
assert.NotEmpty(t, path)
|
||||||
|
assert.False(t, strings.HasPrefix(path, "/nonexistent/"))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user