@lethevimlet/sshift 1.3.9 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lethevimlet/sshift",
3
- "version": "1.3.9",
3
+ "version": "1.4.0",
4
4
  "description": "Web-based SSH + SFTP terminal client with tabs and bookmarks",
5
5
  "main": "src/server/index.js",
6
6
  "bin": {
@@ -81,11 +81,60 @@ function registerSFTPHandlers(socket, io) {
81
81
  console.log('[SFTP] Emitting sftp-list-result for sessionId:', data.sessionId, 'files:', files.length);
82
82
  socket.emit('sftp-list-result', { path: data.path, files, sessionId: data.sessionId });
83
83
  } catch (err) {
84
- console.error('[SFTP] Error in sftp-list:', err.message);
85
- socket.emit('sftp-error', { message: err.message });
84
+ console.error('[SFTP] Error in sftp-list:', err.message, 'code:', err.code);
85
+
86
+ const homeDir = sftpManager.home(data.sessionId);
87
+ const isPermissionDenied = err.code === 3
88
+ || (err.message && (
89
+ err.message.includes('Permission denied')
90
+ || err.message.includes('permission denied')
91
+ || err.message.includes('EACCES')
92
+ ));
93
+
94
+ // If listing "/" fails with permission denied and we know the home
95
+ // directory, try falling back to it automatically. This handles the
96
+ // common case of chrooted SFTP servers that restrict root access.
97
+ if (isPermissionDenied && homeDir && data.path === '/') {
98
+ console.log('[SFTP] Permission denied on /, falling back to home directory:', homeDir);
99
+ try {
100
+ const homeFiles = await sftpManager.list(data.sessionId, homeDir);
101
+ socket.emit('sftp-list-result', {
102
+ path: homeDir,
103
+ files: homeFiles,
104
+ sessionId: data.sessionId,
105
+ redirectedFrom: '/',
106
+ homeDir
107
+ });
108
+ return;
109
+ } catch (homeErr) {
110
+ console.error('[SFTP] Home directory also failed:', homeErr.message);
111
+ socket.emit('sftp-error', {
112
+ message: homeErr.message,
113
+ sessionId: data.sessionId,
114
+ homeDir
115
+ });
116
+ return;
117
+ }
118
+ }
119
+
120
+ socket.emit('sftp-error', {
121
+ message: err.message,
122
+ sessionId: data.sessionId,
123
+ homeDir,
124
+ isPermissionDenied
125
+ });
86
126
  }
87
127
  });
88
128
 
129
+ // SFTP get home directory
130
+ socket.on('sftp-home', (data) => {
131
+ const homeDir = sftpManager.home(data.sessionId);
132
+ socket.emit('sftp-home-result', {
133
+ sessionId: data.sessionId,
134
+ homeDir
135
+ });
136
+ });
137
+
89
138
  // SFTP download (streamed)
90
139
  socket.on('sftp-download', async (data) => {
91
140
  try {
@@ -86,16 +86,30 @@ class SFTPManager {
86
86
  conn: conn,
87
87
  sftp: sftp,
88
88
  socket: socket,
89
- // Connection info for sessions API
90
89
  host: options.host,
91
90
  port: options.port || 22,
92
91
  username: options.username,
93
- connectedAt: Date.now()
92
+ connectedAt: Date.now(),
93
+ homeDir: null
94
94
  };
95
95
 
96
- this.sessions.set(sessionId, session);
97
- console.log(`[SFTP] SFTP session started: ${sessionId}`);
98
- resolve(sessionId);
96
+ // Resolve the user's home directory so we can fall back to it
97
+ // when listing "/" fails (e.g. chrooted SFTP or restricted
98
+ // server configs). realpath('.') returns the absolute path of
99
+ // the remote working directory, which defaults to the user's
100
+ // home on most SSH servers.
101
+ sftp.realpath('.', (realpathErr, resolvedPath) => {
102
+ if (!realpathErr && resolvedPath) {
103
+ session.homeDir = resolvedPath;
104
+ console.log(`[SFTP] Resolved home directory for ${options.username}: ${resolvedPath}`);
105
+ } else {
106
+ console.warn(`[SFTP] Could not resolve home directory: ${realpathErr?.message}`);
107
+ }
108
+
109
+ this.sessions.set(sessionId, session);
110
+ console.log(`[SFTP] SFTP session started: ${sessionId}`);
111
+ resolve(sessionId);
112
+ });
99
113
  });
100
114
  });
101
115
 
@@ -114,12 +128,36 @@ class SFTPManager {
114
128
  removeTab(sessionId);
115
129
  });
116
130
 
117
- try {
118
- conn.connect(config);
119
- } catch (err) {
120
- console.error(`[SFTP] Connect exception: ${err.message}`);
121
- reject(err);
131
+ try {
132
+ conn.connect(config);
133
+ } catch (err) {
134
+ console.error(`[SFTP] Connect exception: ${err.message}`);
135
+ reject(err);
136
+ }
137
+ });
138
+ }
139
+
140
+ home(sessionId) {
141
+ const session = this.sessions.get(sessionId);
142
+ if (!session) return null;
143
+ return session.homeDir || null;
144
+ }
145
+
146
+ realpath(sessionId, path) {
147
+ return new Promise((resolve, reject) => {
148
+ const session = this.sessions.get(sessionId);
149
+ if (!session) {
150
+ reject(new Error('Session not found'));
151
+ return;
122
152
  }
153
+
154
+ session.sftp.realpath(path, (err, resolvedPath) => {
155
+ if (err) {
156
+ reject(err);
157
+ return;
158
+ }
159
+ resolve(resolvedPath);
160
+ });
123
161
  });
124
162
  }
125
163
 
@@ -3000,12 +3000,16 @@ body.dragging-tabs .tabs-container {
3000
3000
  min-width: 200px;
3001
3001
  }
3002
3002
 
3003
+ .sftp-home-btn {
3004
+ border-radius: var(--radius-md) 0 0 var(--radius-md);
3005
+ padding: 8px 10px;
3006
+ }
3007
+
3003
3008
  .sftp-path-input {
3004
3009
  flex: 1;
3005
3010
  padding: 8px 12px;
3006
3011
  background: var(--bg-tertiary);
3007
3012
  border: 1px solid var(--border-color);
3008
- border-radius: var(--radius-md) 0 0 var(--radius-md);
3009
3013
  color: var(--text-primary);
3010
3014
  font-family: var(--font-mono);
3011
3015
  font-size: 13px;
@@ -4432,11 +4432,47 @@ const wheelHandler = (e) => {
4432
4432
  }
4433
4433
  });
4434
4434
 
4435
+ this.socket.on('sftp-home-result', (data) => {
4436
+ console.log('[SSHIFT] sftp-home-result received:', data);
4437
+ const session = this.sftpSessions.get(data.sessionId);
4438
+ if (session && data.homeDir) {
4439
+ session.homeDir = data.homeDir;
4440
+ console.log('[SSHIFT] Stored home directory for session:', data.homeDir);
4441
+
4442
+ // If the session still shows "/" as current path (initial load),
4443
+ // and listing "/" hasn't succeeded yet, navigate to home instead.
4444
+ // This gives users with chrooted SFTP a working starting directory.
4445
+ if (session.currentPath === '/' && !session._rootListSucceeded) {
4446
+ console.log('[SSHIFT] Navigating to home directory:', data.homeDir);
4447
+ session.currentPath = data.homeDir;
4448
+ const pathInput = document.querySelector(`#sftp-${data.sessionId} .sftp-path-input`);
4449
+ if (pathInput) pathInput.value = data.homeDir;
4450
+ this.socket.emit('sftp-list', { sessionId: data.sessionId, path: data.homeDir });
4451
+ }
4452
+ }
4453
+ });
4454
+
4435
4455
  this.socket.on('sftp-list-result', (data) => {
4436
4456
  console.log('[SSHIFT] sftp-list-result received:', data);
4437
4457
  console.log('[SSHIFT] sessionId from server:', data.sessionId);
4438
4458
  console.log('[SSHIFT] path:', data.path);
4439
4459
  console.log('[SSHIFT] files count:', data.files?.length || 0);
4460
+
4461
+ // Track whether root listing has succeeded (used by sftp-home-result
4462
+ // to decide whether to redirect to home directory).
4463
+ const session = this.sftpSessions.get(data.sessionId);
4464
+ if (session) {
4465
+ if (data.path === '/' && !data.redirectedFrom) {
4466
+ session._rootListSucceeded = true;
4467
+ }
4468
+ session.currentPath = data.path;
4469
+ }
4470
+
4471
+ // If listing was auto-redirected from "/" to home, notify the user.
4472
+ if (data.redirectedFrom) {
4473
+ this.showToast(`Root directory (${data.redirectedFrom}) is not accessible. Showing home directory instead.`, 'info');
4474
+ }
4475
+
4440
4476
  this.renderSFTPFileList(data.path, data.files, data.sessionId);
4441
4477
  });
4442
4478
 
@@ -4476,6 +4512,22 @@ const wheelHandler = (e) => {
4476
4512
  } else {
4477
4513
  this.showToast(data.message, 'error');
4478
4514
  }
4515
+ } else if (data.isPermissionDenied && data.homeDir) {
4516
+ // Permission denied with a known home directory — show a helpful
4517
+ // error with a "Go Home" action instead of a bare error toast.
4518
+ this.showToast(`Permission denied: ${data.message}. Try navigating to your home directory.`, 'error');
4519
+ // Auto-navigate to home directory if we're stuck at an inaccessible path
4520
+ const session = data?.sessionId ? this.sftpSessions.get(data.sessionId) : null;
4521
+ if (session) {
4522
+ session.homeDir = data.homeDir;
4523
+ const currentPath = session.currentPath || '/';
4524
+ // Only auto-redirect if we haven't already navigated away
4525
+ const pathInput = document.querySelector(`#sftp-${data.sessionId} .sftp-path-input`);
4526
+ if (pathInput && pathInput.value === currentPath) {
4527
+ console.log('[SSHIFT] Auto-navigating SFTP to home directory:', data.homeDir);
4528
+ this.navigateSFTPPath(data.homeDir, data.sessionId);
4529
+ }
4530
+ }
4479
4531
  } else {
4480
4532
  this.showToast(data.message, 'error');
4481
4533
  }
@@ -8035,6 +8087,8 @@ if (keepaliveCountMaxInput && this.sshKeepaliveCountMax) {
8035
8087
  name,
8036
8088
  type: 'sftp',
8037
8089
  currentPath: '/',
8090
+ homeDir: null, // Resolved from server via sftp-home
8091
+ _rootListSucceeded: false, // Whether listing "/" worked (used for auto-redirect)
8038
8092
  connectionData, // Store for sticky sessions
8039
8093
  fontSize: this.terminalFontSize, // Initialize with default font size
8040
8094
  isRestoring: !!restoreSessionId, // Flag to indicate if this is a restored session
@@ -8086,6 +8140,9 @@ if (keepaliveCountMaxInput && this.sshKeepaliveCountMax) {
8086
8140
  container.innerHTML = `
8087
8141
  <div class="sftp-toolbar">
8088
8142
  <div class="sftp-path">
8143
+ <button class="btn btn-sm sftp-home-btn" title="Home directory">
8144
+ <i class="fas fa-home"></i>
8145
+ </button>
8089
8146
  <input type="text" class="sftp-path-input" placeholder="/path/to/directory">
8090
8147
  <button class="btn btn-sm sftp-go-btn">
8091
8148
  <i class="fas fa-arrow-right"></i>
@@ -8108,11 +8165,18 @@ if (keepaliveCountMaxInput && this.sshKeepaliveCountMax) {
8108
8165
 
8109
8166
  // Add event listeners
8110
8167
  const pathInput = container.querySelector('.sftp-path-input');
8168
+ const homeBtn = container.querySelector('.sftp-home-btn');
8111
8169
  const goBtn = container.querySelector('.sftp-go-btn');
8112
8170
  const refreshBtn = container.querySelector('.sftp-refresh-btn');
8113
8171
  const mkdirBtn = container.querySelector('.sftp-mkdir-btn');
8114
8172
  const uploadBtn = container.querySelector('.sftp-upload-btn');
8115
8173
 
8174
+ homeBtn.addEventListener('click', () => {
8175
+ const session = this.sftpSessions.get(sessionId);
8176
+ const homePath = session?.homeDir || '~';
8177
+ this.navigateSFTPPath(homePath, sessionId);
8178
+ });
8179
+
8116
8180
  goBtn.addEventListener('click', () => {
8117
8181
  this.navigateSFTPPath(pathInput.value, sessionId);
8118
8182
  });
@@ -8187,7 +8251,12 @@ if (keepaliveCountMaxInput && this.sshKeepaliveCountMax) {
8187
8251
  console.log('[SSHIFT] SFTP container parent:', sftpContainer.parentElement?.id);
8188
8252
  }
8189
8253
 
8190
- // Update path input and list files
8254
+ // Request the server's home directory for this session so we can
8255
+ // start there instead of "/" (which may be denied by chroot / perms)
8256
+ this.socket.emit('sftp-home', { sessionId: data.sessionId });
8257
+
8258
+ // Also start listing the current path (or "/") immediately; if the
8259
+ // home-directory response arrives first we'll navigate there instead.
8191
8260
  const pathInput = document.querySelector(`#sftp-${data.sessionId} .sftp-path-input`);
8192
8261
  console.log('[SSHIFT] Path input found:', !!pathInput);
8193
8262
 
@@ -8197,7 +8266,6 @@ if (keepaliveCountMaxInput && this.sshKeepaliveCountMax) {
8197
8266
  console.error('[SSHIFT] Path input not found for session:', data.sessionId);
8198
8267
  }
8199
8268
 
8200
- // List files in the current directory
8201
8269
  console.log('[SSHIFT] Emitting sftp-list for path:', session.currentPath || '/');
8202
8270
  this.socket.emit('sftp-list', { sessionId: data.sessionId, path: session.currentPath || '/' });
8203
8271
  } else {
@@ -8330,6 +8398,16 @@ if (keepaliveCountMaxInput && this.sshKeepaliveCountMax) {
8330
8398
  navigateSFTPPath(path, sessionId) {
8331
8399
  const session = this.sftpSessions.get(sessionId);
8332
8400
  if (session) {
8401
+ // Resolve ~ to the user's home directory
8402
+ if (path === '~' || path.startsWith('~/')) {
8403
+ if (session.homeDir) {
8404
+ path = path === '~' ? session.homeDir : session.homeDir + path.slice(1);
8405
+ } else {
8406
+ // Ask the server to resolve ~ if we don't have the home dir yet
8407
+ this.socket.emit('sftp-home', { sessionId });
8408
+ }
8409
+ }
8410
+
8333
8411
  session.currentPath = path;
8334
8412
 
8335
8413
  const pathInput = document.querySelector(`#sftp-${sessionId} .sftp-path-input`);