@lethevimlet/sshift 1.4.8 → 1.4.10
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 +4 -1
- package/src/server/endpoints/ws/sftp.js +44 -8
- package/src/server/endpoints/ws/ssh.js +139 -11
- package/src/server/endpoints/ws/tabs.js +49 -19
- package/src/server/services/ssh-manager.js +61 -7
- package/src/webapp/js/app.js +352 -211
- package/src/webapp/js/terminal-worker.js +36 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lethevimlet/sshift",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.10",
|
|
4
4
|
"description": "Web-based SSH + SFTP terminal client with tabs and bookmarks",
|
|
5
5
|
"main": "src/server/index.js",
|
|
6
6
|
"bin": {
|
|
@@ -16,6 +16,9 @@
|
|
|
16
16
|
"test:integration": "jest --testPathPatterns='src/tests/integration' --forceExit",
|
|
17
17
|
"test:e2e": "jest --testPathPatterns='src/tests/e2e' --forceExit --runInBand",
|
|
18
18
|
"test:browser": "jest --testPathPatterns='src/tests/browser' --forceExit --runInBand",
|
|
19
|
+
"test:ssh:up": "docker compose -f docker/test-ssh/docker-compose.yml up -d --build",
|
|
20
|
+
"test:ssh:down": "docker compose -f docker/test-ssh/docker-compose.yml down -v",
|
|
21
|
+
"test:ssh:logs": "docker compose -f docker/test-ssh/docker-compose.yml logs -f",
|
|
19
22
|
"prepublishOnly": "echo 'Publishing to npm...'",
|
|
20
23
|
"install:safe": "sfw pnpm install"
|
|
21
24
|
},
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
const { loadConfig, getStickySetting } = require('../../utils/config');
|
|
6
|
-
const { addTab, removeTab, addSocketToTab, removeSocketFromTab } = require('../../utils/tab-manager');
|
|
6
|
+
const { addTab, getTab, removeTab, addSocketToTab, removeSocketFromTab, clearCloseTimer, setCloseTimer } = require('../../utils/tab-manager');
|
|
7
7
|
const { sftpManager } = require('../../services');
|
|
8
8
|
|
|
9
9
|
/**
|
|
@@ -44,13 +44,13 @@ function registerSFTPHandlers(socket, io) {
|
|
|
44
44
|
sticky: sticky
|
|
45
45
|
});
|
|
46
46
|
|
|
47
|
-
|
|
47
|
+
socket.broadcast.emit('tab-opened', {
|
|
48
48
|
sessionId,
|
|
49
49
|
name: data.name || 'SFTP',
|
|
50
50
|
type: 'sftp',
|
|
51
51
|
connectionData: data
|
|
52
52
|
});
|
|
53
|
-
|
|
53
|
+
|
|
54
54
|
io.emit('sessions-updated');
|
|
55
55
|
|
|
56
56
|
socket.emit('sftp-connected', { sessionId });
|
|
@@ -274,14 +274,50 @@ function registerSFTPHandlers(socket, io) {
|
|
|
274
274
|
}
|
|
275
275
|
});
|
|
276
276
|
|
|
277
|
-
// SFTP disconnect
|
|
277
|
+
// SFTP disconnect — mirror the ssh-disconnect viewer/sticky-aware teardown
|
|
278
|
+
// so the closing client's view is removed without tearing down the
|
|
279
|
+
// underlying SFTP stream for other viewers or non-sticky-none-viewers.
|
|
278
280
|
socket.on('sftp-disconnect', (data) => {
|
|
281
|
+
console.log('[SFTP] sftp-disconnect event received for', data.sessionId, 'from socket', socket.id);
|
|
282
|
+
|
|
283
|
+
const tab = getTab(data.sessionId);
|
|
284
|
+
if (tab) {
|
|
285
|
+
removeSocketFromTab(data.sessionId, socket.id);
|
|
286
|
+
|
|
287
|
+
if (tab.activeSockets.size > 0) {
|
|
288
|
+
console.log('[SFTP] sftp-disconnect from', socket.id, 'but', tab.activeSockets.size, 'viewer(s) remain; keeping:', data.sessionId);
|
|
289
|
+
io.emit('sessions-updated');
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// No remaining viewers. Honor sticky flag (same semantics as ssh).
|
|
294
|
+
clearCloseTimer(data.sessionId);
|
|
295
|
+
if (tab.sticky) {
|
|
296
|
+
console.log('[SFTP] Sticky session remains open on server (no viewers):', data.sessionId);
|
|
297
|
+
io.emit('sessions-updated');
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
console.log('[SFTP] Non-sticky session, scheduling close after grace:', data.sessionId);
|
|
302
|
+
const gracePeriod = 5000;
|
|
303
|
+
setCloseTimer(data.sessionId, setTimeout(() => {
|
|
304
|
+
const currentTab = getTab(data.sessionId);
|
|
305
|
+
if (currentTab && currentTab.activeSockets.size === 0) {
|
|
306
|
+
console.log('[SFTP] Grace expired, closing session:', data.sessionId);
|
|
307
|
+
sftpManager.disconnect(data.sessionId);
|
|
308
|
+
removeTab(data.sessionId);
|
|
309
|
+
io.emit('tab-closed', { sessionId: data.sessionId });
|
|
310
|
+
io.emit('sessions-updated');
|
|
311
|
+
} else {
|
|
312
|
+
console.log('[SFTP] Session has new sockets, canceling close:', data.sessionId);
|
|
313
|
+
}
|
|
314
|
+
}, gracePeriod));
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// No tab record — broadcast close so stale client UIs clear it.
|
|
279
319
|
sftpManager.disconnect(data.sessionId);
|
|
280
|
-
|
|
281
|
-
// Remove from open tabs
|
|
282
320
|
removeTab(data.sessionId);
|
|
283
|
-
|
|
284
|
-
// Broadcast to all clients that tab was closed
|
|
285
321
|
io.emit('tab-closed', { sessionId: data.sessionId });
|
|
286
322
|
io.emit('sessions-updated');
|
|
287
323
|
});
|
|
@@ -6,6 +6,20 @@ const { loadConfig, getStickySetting, getScrollback } = require('../../utils/con
|
|
|
6
6
|
const { addTab, getTab, addSocketToTab, removeSocketFromTab, removeTab, getTabOrder, setCloseTimer, clearCloseTimer, getCloseTimer } = require('../../utils/tab-manager');
|
|
7
7
|
const { sshManager } = require('../../services');
|
|
8
8
|
|
|
9
|
+
// Per-session rate limiting for ssh-request-sync. A non-controller could
|
|
10
|
+
// otherwise trigger a 1MB serialization + base64 + emit on every
|
|
11
|
+
// keystroke, easily saturating the server's main thread and bandwidth.
|
|
12
|
+
// Allow at most one request per session every 2 seconds.
|
|
13
|
+
const _syncLastSent = new Map(); // sessionId -> last sync emit timestamp (ms)
|
|
14
|
+
const SYNC_RATE_LIMIT_MS = 2000;
|
|
15
|
+
|
|
16
|
+
// Acceptable client-supplied terminal dimensions. Guards against
|
|
17
|
+
// malformed clients passing 0 or huge values that would crash the
|
|
18
|
+
// headless xterm.js terminal.resize() or the remote PTY.
|
|
19
|
+
const MIN_DIM = 1;
|
|
20
|
+
const MAX_COLS = 400;
|
|
21
|
+
const MAX_ROWS = 200;
|
|
22
|
+
|
|
9
23
|
/**
|
|
10
24
|
* Register SSH WebSocket handlers
|
|
11
25
|
* @param {Object} socket - Socket.IO socket
|
|
@@ -53,13 +67,16 @@ function registerSSHHandlers(socket, io) {
|
|
|
53
67
|
sticky: sticky
|
|
54
68
|
});
|
|
55
69
|
|
|
56
|
-
|
|
70
|
+
// Use socket.broadcast.emit so the originator doesn't receive its own
|
|
71
|
+
// tab-opened echo. The originator already created the tab locally via
|
|
72
|
+
// createSSHTab, so relying on a client-side guard was fragile.
|
|
73
|
+
socket.broadcast.emit('tab-opened', {
|
|
57
74
|
sessionId,
|
|
58
75
|
name: data.name || 'SSH',
|
|
59
76
|
type: 'ssh',
|
|
60
77
|
connectionData: data
|
|
61
78
|
});
|
|
62
|
-
|
|
79
|
+
|
|
63
80
|
io.emit('sessions-updated');
|
|
64
81
|
|
|
65
82
|
socket.emit('ssh-connected', { sessionId });
|
|
@@ -85,6 +102,26 @@ function registerSSHHandlers(socket, io) {
|
|
|
85
102
|
// SSH request screen sync (for manual refresh or reconnection)
|
|
86
103
|
socket.on('ssh-request-sync', (data) => {
|
|
87
104
|
console.log('[SSH] ssh-request-sync event received for session:', data.sessionId);
|
|
105
|
+
|
|
106
|
+
// Rate-limit per session to prevent abuse / accidental DoS.
|
|
107
|
+
// NOTE: this emits an `ssh-error` with `advisory: true` so the
|
|
108
|
+
// client's ssh-error handler can distinguish a soft advisory from
|
|
109
|
+
// a hard connection failure (which would warrant closing the tab).
|
|
110
|
+
// Without the `advisory: true` flag, the client would
|
|
111
|
+
// `closeTab()` on every rate-limited sync — killing the very tab
|
|
112
|
+
// the user was trying to refresh.
|
|
113
|
+
const now = Date.now();
|
|
114
|
+
const last = _syncLastSent.get(data.sessionId) || 0;
|
|
115
|
+
if ((now - last) < SYNC_RATE_LIMIT_MS) {
|
|
116
|
+
socket.emit('ssh-error', {
|
|
117
|
+
sessionId: data.sessionId,
|
|
118
|
+
message: 'Sync rate limit reached, please wait',
|
|
119
|
+
advisory: true
|
|
120
|
+
});
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
_syncLastSent.set(data.sessionId, now);
|
|
124
|
+
|
|
88
125
|
const state = sshManager.getTerminalState(data.sessionId);
|
|
89
126
|
if (state) {
|
|
90
127
|
// Base64 encode to avoid Socket.IO binary detection issues
|
|
@@ -109,7 +146,27 @@ function registerSSHHandlers(socket, io) {
|
|
|
109
146
|
console.log(`[SSH] Ignoring input from non-controller socket ${socket.id}`);
|
|
110
147
|
return;
|
|
111
148
|
}
|
|
112
|
-
|
|
149
|
+
// Validate payload type — a non-string `data.data` would throw deep
|
|
150
|
+
// inside the ssh2 stream.write and crash the socket handler.
|
|
151
|
+
if (typeof data.data !== 'string') {
|
|
152
|
+
console.warn(`[SSH] ssh-data: invalid payload type from socket ${socket.id}`);
|
|
153
|
+
socket.emit('ssh-error', {
|
|
154
|
+
sessionId: data.sessionId,
|
|
155
|
+
message: 'Invalid input payload',
|
|
156
|
+
advisory: true
|
|
157
|
+
});
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
try {
|
|
161
|
+
sshManager.write(data.sessionId, data.data);
|
|
162
|
+
} catch (err) {
|
|
163
|
+
console.error(`[SSH] ssh-data: write failed for ${data.sessionId}:`, err.message);
|
|
164
|
+
socket.emit('ssh-error', {
|
|
165
|
+
sessionId: data.sessionId,
|
|
166
|
+
message: 'Failed to write input: ' + err.message,
|
|
167
|
+
advisory: true
|
|
168
|
+
});
|
|
169
|
+
}
|
|
113
170
|
});
|
|
114
171
|
|
|
115
172
|
// SSH take control
|
|
@@ -145,24 +202,95 @@ function registerSSHHandlers(socket, io) {
|
|
|
145
202
|
console.log(`[SSH] Ignoring resize from non-controller socket ${socket.id}`);
|
|
146
203
|
return;
|
|
147
204
|
}
|
|
148
|
-
|
|
149
|
-
|
|
205
|
+
// Validate dimensions. cols=0/rows=0 (or NaN, negative, huge) would
|
|
206
|
+
// crash headless terminal.resize() inside the SSH manager.
|
|
207
|
+
const cols = Number(data.cols);
|
|
208
|
+
const rows = Number(data.rows);
|
|
209
|
+
if (!Number.isFinite(cols) || !Number.isFinite(rows) ||
|
|
210
|
+
cols < MIN_DIM || rows < MIN_DIM ||
|
|
211
|
+
cols > MAX_COLS || rows > MAX_ROWS) {
|
|
212
|
+
console.warn(`[SSH] ssh-resize: invalid dimensions (${data.cols}x${data.rows}) from socket ${socket.id}`);
|
|
213
|
+
socket.emit('ssh-error', {
|
|
214
|
+
sessionId: data.sessionId,
|
|
215
|
+
message: `Invalid terminal dimensions: ${data.cols}x${data.rows}`,
|
|
216
|
+
advisory: true
|
|
217
|
+
});
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
try {
|
|
221
|
+
sshManager.resize(data.sessionId, cols, rows);
|
|
222
|
+
} catch (err) {
|
|
223
|
+
console.error(`[SSH] ssh-resize: resize failed for ${data.sessionId}:`, err.message);
|
|
224
|
+
socket.emit('ssh-error', {
|
|
225
|
+
sessionId: data.sessionId,
|
|
226
|
+
message: 'Failed to resize: ' + err.message,
|
|
227
|
+
advisory: true
|
|
228
|
+
});
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
|
|
150
232
|
// Broadcast resize to all other clients in the session
|
|
151
233
|
// This ensures all clients stay in sync with the server terminal size
|
|
152
234
|
socket.to(`session-${data.sessionId}`).emit('ssh-resize-sync', {
|
|
153
235
|
sessionId: data.sessionId,
|
|
154
|
-
cols
|
|
155
|
-
rows
|
|
236
|
+
cols,
|
|
237
|
+
rows
|
|
156
238
|
});
|
|
157
239
|
});
|
|
158
240
|
|
|
159
|
-
// SSH disconnect
|
|
241
|
+
// SSH disconnect — historically meant "I'm done with this session locally".
|
|
242
|
+
// Treat it identically to tab-close: viewer-aware + sticky-aware teardown.
|
|
243
|
+
// Only destroy the underlying SSH stream when no other clients remain AND
|
|
244
|
+
// the session is non-sticky. This lets the orchestrator close their view
|
|
245
|
+
// without yanking the session out from under sticky observers (or shedding
|
|
246
|
+
// a session the user wants to rejoin from the Sessions modal).
|
|
160
247
|
socket.on('ssh-disconnect', (data) => {
|
|
248
|
+
console.log('[SSH] ssh-disconnect event received for', data.sessionId, 'from socket', socket.id);
|
|
249
|
+
|
|
250
|
+
// Remove this socket from the SSH session room and reassign control if needed.
|
|
251
|
+
sshManager.leaveSession(socket, data.sessionId);
|
|
252
|
+
|
|
253
|
+
// Also remove from the tab's active-socket set so viewer counting
|
|
254
|
+
// reflects reality.
|
|
255
|
+
const tab = getTab(data.sessionId);
|
|
256
|
+
if (tab) {
|
|
257
|
+
removeSocketFromTab(data.sessionId, socket.id);
|
|
258
|
+
|
|
259
|
+
if (tab.activeSockets.size > 0) {
|
|
260
|
+
// Other viewers still active — keep the session alive for them.
|
|
261
|
+
console.log('[SSH] ssh-disconnect from', socket.id, 'but', tab.activeSockets.size, 'viewer(s) remain; keeping:', data.sessionId);
|
|
262
|
+
io.emit('sessions-updated');
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// No remaining viewers. Honor sticky flag.
|
|
267
|
+
clearCloseTimer(data.sessionId);
|
|
268
|
+
if (tab.sticky) {
|
|
269
|
+
console.log('[SSH] Sticky session remains open on server (no viewers):', data.sessionId);
|
|
270
|
+
io.emit('sessions-updated');
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
console.log('[SSH] Non-sticky session, scheduling close after grace:', data.sessionId);
|
|
275
|
+
const gracePeriod = 5000;
|
|
276
|
+
setCloseTimer(data.sessionId, setTimeout(() => {
|
|
277
|
+
const currentTab = getTab(data.sessionId);
|
|
278
|
+
if (currentTab && currentTab.activeSockets.size === 0) {
|
|
279
|
+
console.log('[SSH] Grace expired, closing session:', data.sessionId);
|
|
280
|
+
sshManager.disconnect(data.sessionId);
|
|
281
|
+
removeTab(data.sessionId);
|
|
282
|
+
io.emit('tab-closed', { sessionId: data.sessionId });
|
|
283
|
+
io.emit('sessions-updated');
|
|
284
|
+
} else {
|
|
285
|
+
console.log('[SSH] Session has new sockets, canceling close:', data.sessionId);
|
|
286
|
+
}
|
|
287
|
+
}, gracePeriod));
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// No tab record — broadcast close so stale client UIs clear it.
|
|
161
292
|
sshManager.disconnect(data.sessionId);
|
|
162
|
-
|
|
163
|
-
// Remove from open tabs and order
|
|
164
293
|
removeTab(data.sessionId);
|
|
165
|
-
|
|
166
294
|
io.emit('tab-closed', { sessionId: data.sessionId });
|
|
167
295
|
io.emit('sessions-updated');
|
|
168
296
|
});
|
|
@@ -32,34 +32,64 @@ function registerTabHandlers(socket, io) {
|
|
|
32
32
|
// Tab close event (client-side tab close, not full disconnect)
|
|
33
33
|
socket.on('tab-close', (data) => {
|
|
34
34
|
console.log('[TAB] Tab closed:', data.sessionId, 'by socket:', socket.id);
|
|
35
|
-
|
|
35
|
+
|
|
36
36
|
// Remove this socket from the tab's active sockets
|
|
37
37
|
const tab = getTab(data.sessionId);
|
|
38
38
|
if (tab) {
|
|
39
39
|
removeSocketFromTab(data.sessionId, socket.id);
|
|
40
|
-
|
|
41
|
-
//
|
|
42
|
-
//
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
console.log('[TAB] Tab
|
|
48
|
-
|
|
49
|
-
//
|
|
40
|
+
|
|
41
|
+
// If other sockets are still viewing this tab, the originating
|
|
42
|
+
// client's close must NOT tear down the session for them.
|
|
43
|
+
// Just notify remaining viewers to refresh their sessions list.
|
|
44
|
+
// The originating client already removed the tab locally via
|
|
45
|
+
// closeTab() (app.js), so it does NOT need a tab-closed echo.
|
|
46
|
+
if (tab.activeSockets.size > 0) {
|
|
47
|
+
console.log('[TAB] Tab closed by', socket.id, 'but', tab.activeSockets.size, 'viewer(s) remain; keeping session:', data.sessionId);
|
|
48
|
+
// SSH: drop this socket from the session room so it stops
|
|
49
|
+
// receiving broadcasts from this session.
|
|
50
50
|
if (tab.type === 'ssh') {
|
|
51
|
-
sshManager.
|
|
52
|
-
} else if (tab.type === 'sftp') {
|
|
53
|
-
sftpManager.disconnect(data.sessionId);
|
|
51
|
+
sshManager.leaveSession(socket, data.sessionId);
|
|
54
52
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
// Broadcast to all clients that tab was closed
|
|
58
|
-
io.emit('tab-closed', { sessionId: data.sessionId });
|
|
53
|
+
// Tell remaining clients to refresh any open "manage sessions"
|
|
54
|
+
// modal — they may want to reflect the new viewer count.
|
|
59
55
|
io.emit('sessions-updated');
|
|
56
|
+
return;
|
|
60
57
|
}
|
|
58
|
+
|
|
59
|
+
// No more viewers left after this close. Honor the sticky flag:
|
|
60
|
+
// sticky sessions stay alive on the server indefinitely (until
|
|
61
|
+
// explicitly killed by another path or server restart). Non-
|
|
62
|
+
// sticky sessions get the same 5s grace path used by
|
|
63
|
+
// handleTabDisconnect, so a transient close-then-reopen round-
|
|
64
|
+
// trip (e.g. closing and reloading a devtools session) doesn't
|
|
65
|
+
// yank the SSH connection out from under the user.
|
|
66
|
+
clearCloseTimer(data.sessionId);
|
|
67
|
+
if (tab.sticky) {
|
|
68
|
+
console.log('[TAB] Sticky session remains open on server (no viewers):', data.sessionId);
|
|
69
|
+
io.emit('sessions-updated');
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
console.log('[TAB] Non-sticky session, scheduling close after grace period:', data.sessionId);
|
|
74
|
+
const gracePeriod = 5000;
|
|
75
|
+
setCloseTimer(data.sessionId, setTimeout(() => {
|
|
76
|
+
const currentTab = getTab(data.sessionId);
|
|
77
|
+
if (currentTab && currentTab.activeSockets.size === 0) {
|
|
78
|
+
console.log('[TAB] Grace period expired, closing session:', data.sessionId);
|
|
79
|
+
if (currentTab.type === 'ssh') {
|
|
80
|
+
sshManager.disconnect(data.sessionId);
|
|
81
|
+
} else if (currentTab.type === 'sftp') {
|
|
82
|
+
sftpManager.disconnect(data.sessionId);
|
|
83
|
+
}
|
|
84
|
+
removeTab(data.sessionId);
|
|
85
|
+
io.emit('tab-closed', { sessionId: data.sessionId });
|
|
86
|
+
io.emit('sessions-updated');
|
|
87
|
+
} else {
|
|
88
|
+
console.log('[TAB] Session has new sockets, canceling close:', data.sessionId);
|
|
89
|
+
}
|
|
90
|
+
}, gracePeriod));
|
|
61
91
|
} else {
|
|
62
|
-
//
|
|
92
|
+
// No tab record — broadcast close so stale client UIs clear it.
|
|
63
93
|
io.emit('tab-closed', { sessionId: data.sessionId });
|
|
64
94
|
io.emit('sessions-updated');
|
|
65
95
|
}
|
|
@@ -189,6 +189,10 @@ class SSHManager {
|
|
|
189
189
|
|
|
190
190
|
stream.on('close', () => {
|
|
191
191
|
console.log(`[SSH] Stream closed: ${sessionId}`);
|
|
192
|
+
if (session.exitTimer) {
|
|
193
|
+
clearTimeout(session.exitTimer);
|
|
194
|
+
session.exitTimer = null;
|
|
195
|
+
}
|
|
192
196
|
this.flushData(sessionId);
|
|
193
197
|
this.broadcastToSession(sessionId, 'ssh-disconnected', { sessionId });
|
|
194
198
|
this.disconnect(sessionId);
|
|
@@ -202,6 +206,21 @@ class SSHManager {
|
|
|
202
206
|
stream.on('exit', (code, signal) => {
|
|
203
207
|
console.log(`[SSH] Stream exit: ${sessionId}, code: ${code}, signal: ${signal}`);
|
|
204
208
|
this.broadcastToSession(sessionId, 'ssh-exit', { sessionId, code, signal });
|
|
209
|
+
// Remote process exited. The ssh2 channel normally fires
|
|
210
|
+
// `close` shortly after, but if it doesn't (network drop,
|
|
211
|
+
// half-open socket) the session would linger in the map
|
|
212
|
+
// forever, freezing sticky clients that reattach to it.
|
|
213
|
+
// Arm a 500ms safety timer to force teardown if `close`
|
|
214
|
+
// doesn't fire.
|
|
215
|
+
if (session.exitTimer) clearTimeout(session.exitTimer);
|
|
216
|
+
session.exitTimer = setTimeout(() => {
|
|
217
|
+
console.warn(`[SSH] Stream close did not fire after exit; forcing disconnect for ${sessionId}`);
|
|
218
|
+
this.disconnect(sessionId);
|
|
219
|
+
if (this.io) {
|
|
220
|
+
this.io.emit('tab-closed', { sessionId });
|
|
221
|
+
this.io.emit('sessions-updated');
|
|
222
|
+
}
|
|
223
|
+
}, 500);
|
|
205
224
|
});
|
|
206
225
|
|
|
207
226
|
stream.on('error', (err) => {
|
|
@@ -544,8 +563,23 @@ class SSHManager {
|
|
|
544
563
|
if (session && session.stream) {
|
|
545
564
|
const canWrite = session.stream.write(data);
|
|
546
565
|
if (!canWrite) {
|
|
547
|
-
// Stream buffer is full — queue write and wait for drain
|
|
566
|
+
// Stream buffer is full — queue write and wait for drain.
|
|
567
|
+
// Cap the queue to avoid OOM on a slow/half-open remote;
|
|
568
|
+
// 64 chunks is enough headroom for normal paste flows
|
|
569
|
+
// while bounding memory in pathological cases.
|
|
548
570
|
if (!session.writeQueue) session.writeQueue = [];
|
|
571
|
+
if (session.writeQueue.length >= 64) {
|
|
572
|
+
console.warn(`[SSH] writeQueue overflow for ${sessionId}; dropping ${session.writeQueue.length} queued chunks`);
|
|
573
|
+
session.writeQueue = [];
|
|
574
|
+
if (this.io) {
|
|
575
|
+
this.io.to(`session-${sessionId}`).emit('ssh-error', {
|
|
576
|
+
sessionId,
|
|
577
|
+
message: 'bufferFull',
|
|
578
|
+
advisory: true
|
|
579
|
+
});
|
|
580
|
+
}
|
|
581
|
+
return;
|
|
582
|
+
}
|
|
549
583
|
session.writeQueue.push(data);
|
|
550
584
|
if (!session.drainListener) {
|
|
551
585
|
session.drainListener = true;
|
|
@@ -567,13 +601,23 @@ class SSHManager {
|
|
|
567
601
|
resize(sessionId, cols, rows) {
|
|
568
602
|
const session = this.sessions.get(sessionId);
|
|
569
603
|
if (session && session.stream) {
|
|
570
|
-
|
|
604
|
+
try {
|
|
605
|
+
session.stream.setWindow(rows, cols);
|
|
606
|
+
} catch (err) {
|
|
607
|
+
console.error(`[SSH] stream.setWindow failed for ${sessionId}:`, err.message);
|
|
608
|
+
}
|
|
571
609
|
session.cols = cols;
|
|
572
610
|
session.rows = rows;
|
|
573
|
-
|
|
574
|
-
// Also resize the headless terminal
|
|
611
|
+
|
|
612
|
+
// Also resize the headless terminal. xterm.js v6 throws on
|
|
613
|
+
// resize(0, 0) or out-of-range values — wrap so a malformed
|
|
614
|
+
// payload doesn't tear down the entire session.
|
|
575
615
|
if (session.terminal) {
|
|
576
|
-
|
|
616
|
+
try {
|
|
617
|
+
session.terminal.resize(cols, rows);
|
|
618
|
+
} catch (err) {
|
|
619
|
+
console.error(`[SSH] headless terminal.resize failed for ${sessionId}:`, err.message);
|
|
620
|
+
}
|
|
577
621
|
}
|
|
578
622
|
}
|
|
579
623
|
}
|
|
@@ -619,6 +663,11 @@ class SSHManager {
|
|
|
619
663
|
clearTimeout(session.batchTimer);
|
|
620
664
|
session.batchTimer = null;
|
|
621
665
|
}
|
|
666
|
+
// Clear any pending exit-safety timer
|
|
667
|
+
if (session.exitTimer) {
|
|
668
|
+
clearTimeout(session.exitTimer);
|
|
669
|
+
session.exitTimer = null;
|
|
670
|
+
}
|
|
622
671
|
// Flush any remaining incomplete UTF-8 bytes from the streaming
|
|
623
672
|
// decoder. Passing { stream: false } (or no option) signals the
|
|
624
673
|
// decoder that the stream has ended, emitting any buffered bytes
|
|
@@ -626,10 +675,15 @@ class SSHManager {
|
|
|
626
675
|
// disconnect, but this prevents a memory leak.
|
|
627
676
|
try { session.decoder.decode(new Uint8Array(0)); } catch (_) {}
|
|
628
677
|
if (session.stream) {
|
|
629
|
-
session.stream.end();
|
|
678
|
+
try { session.stream.end(); } catch (_) {}
|
|
679
|
+
// Also call destroy() to force teardown of half-open sockets.
|
|
680
|
+
// end() only signals EOF and waits for the peer to acknowledge;
|
|
681
|
+
// if the remote is unreachable, end() alone can leave a
|
|
682
|
+
// half-open socket pinning the session in the map until GC.
|
|
683
|
+
try { session.stream.destroy(); } catch (_) {}
|
|
630
684
|
}
|
|
631
685
|
if (session.conn) {
|
|
632
|
-
session.conn.end();
|
|
686
|
+
try { session.conn.end(); } catch (_) {}
|
|
633
687
|
}
|
|
634
688
|
// Dispose of the headless terminal
|
|
635
689
|
if (session.terminal) {
|