tacticremote 1.6.0 → 1.6.1
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/README.md +8 -0
- package/bin/tactic-remote-server.js +628 -0
- package/lib/authChallengeManager.js +134 -0
- package/lib/bonjourPublisher.js +68 -0
- package/lib/errorCodes.js +127 -0
- package/lib/execSafe.js +202 -0
- package/lib/fileService.js +177 -0
- package/lib/hookInstaller.js +239 -0
- package/lib/outputParser.js +106 -0
- package/lib/rateLimiter.js +111 -0
- package/lib/tmuxManager.js +661 -0
- package/lib/uploadService.js +366 -0
- package/lib/validators.js +350 -0
- package/middleware/authMiddleware.js +152 -0
- package/package.json +32 -9
- package/routes/httpHandler.js +109 -0
- package/routes/wsHandler.js +1040 -0
- package/server.js +309 -0
|
@@ -0,0 +1,1040 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebSocket message handler for MacServer
|
|
3
|
+
* Supports both legacy protocol and new req/res/event frame protocol
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const tmux = require('../lib/tmuxManager');
|
|
7
|
+
const fileService = require('../lib/fileService');
|
|
8
|
+
const uploadService = require('../lib/uploadService');
|
|
9
|
+
const { ErrorCodes, createError } = require('../lib/errorCodes');
|
|
10
|
+
const { parseClaudeOutput } = require('../lib/outputParser');
|
|
11
|
+
const { tmuxCapture, tmuxGetPanePath, execSafe } = require('../lib/execSafe');
|
|
12
|
+
const { validatePath, validateSessionName } = require('../lib/validators');
|
|
13
|
+
const authChallengeManager = require('../lib/authChallengeManager');
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Create WebSocket message handler
|
|
17
|
+
* @param {Object} server - Server instance
|
|
18
|
+
* @returns {Object} Handler methods
|
|
19
|
+
*/
|
|
20
|
+
function createWsHandler(server) {
|
|
21
|
+
// Map: sessionName -> Map<ws, intervalId>
|
|
22
|
+
// Tracks which clients are monitoring each session (supports multiple subscribers)
|
|
23
|
+
const outputMonitors = new Map();
|
|
24
|
+
// Reverse map: ws -> Set<sessionName> for cleanup on disconnect
|
|
25
|
+
const clientMonitors = new Map();
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Get per-client current path (falls back to server default)
|
|
29
|
+
*/
|
|
30
|
+
function getClientPath(ws) {
|
|
31
|
+
const state = server.clientState.get(ws._clientId);
|
|
32
|
+
return state ? state.currentPath : server.currentPath;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Set per-client current path
|
|
37
|
+
*/
|
|
38
|
+
function setClientPath(ws, path) {
|
|
39
|
+
const state = server.clientState.get(ws._clientId);
|
|
40
|
+
if (state) {
|
|
41
|
+
state.currentPath = path;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Send message to a specific client
|
|
47
|
+
*/
|
|
48
|
+
function sendToClient(ws, message) {
|
|
49
|
+
if (ws.readyState === 1) {
|
|
50
|
+
ws.send(JSON.stringify(message));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Send streaming output to client with backpressure check.
|
|
56
|
+
* Drops the message if the socket buffer is full — safe because
|
|
57
|
+
* the next poll cycle will send fresh content.
|
|
58
|
+
*/
|
|
59
|
+
function sendOutputToClient(ws, message) {
|
|
60
|
+
if (ws.readyState === 1 && ws.bufferedAmount < 256 * 1024) {
|
|
61
|
+
ws.send(JSON.stringify(message));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Send a response to a request (new protocol)
|
|
67
|
+
* @param {WebSocket} ws - WebSocket connection
|
|
68
|
+
* @param {string} requestId - Original request ID
|
|
69
|
+
* @param {Object} payload - Response payload
|
|
70
|
+
*/
|
|
71
|
+
function sendResponse(ws, requestId, payload) {
|
|
72
|
+
sendToClient(ws, {
|
|
73
|
+
type: 'res',
|
|
74
|
+
id: requestId,
|
|
75
|
+
ok: true,
|
|
76
|
+
payload
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Send an error response to a request (new protocol)
|
|
82
|
+
* @param {WebSocket} ws - WebSocket connection
|
|
83
|
+
* @param {string} requestId - Original request ID
|
|
84
|
+
* @param {Object} error - Error object from createError
|
|
85
|
+
*/
|
|
86
|
+
function sendErrorResponse(ws, requestId, error) {
|
|
87
|
+
sendToClient(ws, {
|
|
88
|
+
type: 'res',
|
|
89
|
+
id: requestId,
|
|
90
|
+
ok: false,
|
|
91
|
+
error
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Send an event to client (new protocol)
|
|
97
|
+
* @param {WebSocket} ws - WebSocket connection
|
|
98
|
+
* @param {string} eventName - Event name
|
|
99
|
+
* @param {Object} payload - Event payload
|
|
100
|
+
*/
|
|
101
|
+
function sendEvent(ws, eventName, payload) {
|
|
102
|
+
sendToClient(ws, {
|
|
103
|
+
type: 'event',
|
|
104
|
+
event: eventName,
|
|
105
|
+
payload
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Broadcast event to all clients
|
|
111
|
+
* @param {string} eventName - Event name
|
|
112
|
+
* @param {Object} payload - Event payload
|
|
113
|
+
*/
|
|
114
|
+
function broadcastEvent(eventName, payload) {
|
|
115
|
+
server.clients.forEach((ws) => {
|
|
116
|
+
sendEvent(ws, eventName, payload);
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Wrap an operation that accesses a path which may be sensitive.
|
|
122
|
+
* If the path is in a sensitive directory and no grant exists,
|
|
123
|
+
* sends an auth_challenge event to the client and awaits authorization.
|
|
124
|
+
*
|
|
125
|
+
* @param {WebSocket} ws
|
|
126
|
+
* @param {string} userPath - The user-provided path
|
|
127
|
+
* @param {string} method - The operation method name (e.g. 'list_directory')
|
|
128
|
+
* @param {Object} validateOptions - Options for validatePath
|
|
129
|
+
* @param {Function} operation - async () => result to execute if authorized
|
|
130
|
+
* @returns {Object} The operation result, or an error object
|
|
131
|
+
*/
|
|
132
|
+
async function withSensitivePathAuth(ws, userPath, method, validateOptions, operation) {
|
|
133
|
+
const validation = validatePath(userPath, validateOptions);
|
|
134
|
+
console.log(`[DEBUG withSensitivePathAuth] method=${method}, userPath=${userPath}, valid=${validation.valid}, requiresAuth=${validation.requiresAuth}, error=${validation.error}`);
|
|
135
|
+
|
|
136
|
+
if (!validation.valid && validation.requiresAuth) {
|
|
137
|
+
const sensitiveDir = validation.sensitiveDir;
|
|
138
|
+
|
|
139
|
+
// Already granted?
|
|
140
|
+
if (authChallengeManager.isGranted(ws, sensitiveDir)) {
|
|
141
|
+
return await operation();
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Send challenge to client and wait for response
|
|
145
|
+
const { challengeId, expiresAt, promise } = authChallengeManager.createChallenge(
|
|
146
|
+
ws, userPath, sensitiveDir, method
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
sendEvent(ws, 'auth_challenge', {
|
|
150
|
+
challengeId,
|
|
151
|
+
path: userPath,
|
|
152
|
+
sensitiveDir,
|
|
153
|
+
method,
|
|
154
|
+
expiresAt
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
const granted = await promise;
|
|
158
|
+
if (granted) {
|
|
159
|
+
return await operation();
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return {
|
|
163
|
+
success: false,
|
|
164
|
+
error: {
|
|
165
|
+
type: 'error',
|
|
166
|
+
code: 'AUTH_DENIED',
|
|
167
|
+
error: `Access to ${sensitiveDir} was denied`,
|
|
168
|
+
retryable: false
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Not a sensitive dir issue — let the operation handle normal validation
|
|
174
|
+
return await operation();
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
async function collectGitInfo(cwd) {
|
|
178
|
+
try {
|
|
179
|
+
const [branch, gitStatus, diffStat, branchList] = await Promise.all([
|
|
180
|
+
execSafe('git', ['-C', cwd, 'branch', '--show-current'], { timeout: 5000 }).catch(() => ''),
|
|
181
|
+
execSafe('git', ['-C', cwd, 'status', '--short'], { timeout: 5000 }).catch(() => ''),
|
|
182
|
+
execSafe('git', ['-C', cwd, 'diff', '--stat', 'HEAD'], { timeout: 10000 }).catch(() => ''),
|
|
183
|
+
execSafe('git', ['-C', cwd, 'branch', '--format=%(refname:short)'], { timeout: 5000 }).catch(() => '')
|
|
184
|
+
]);
|
|
185
|
+
return {
|
|
186
|
+
cwd,
|
|
187
|
+
branch: branch.trim(),
|
|
188
|
+
branches: branchList.trim().split('\n').filter(Boolean),
|
|
189
|
+
git_status: gitStatus.trim(),
|
|
190
|
+
diff_stat: diffStat.trim(),
|
|
191
|
+
is_git_repo: true
|
|
192
|
+
};
|
|
193
|
+
} catch {
|
|
194
|
+
return { cwd, is_git_repo: false };
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Broadcast message to all clients
|
|
200
|
+
*/
|
|
201
|
+
function broadcast(message) {
|
|
202
|
+
server.clients.forEach((ws) => {
|
|
203
|
+
sendToClient(ws, message);
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Start monitoring output for a session
|
|
209
|
+
* Supports multiple clients subscribing to the same session
|
|
210
|
+
*/
|
|
211
|
+
function startOutputMonitor(ws, sessionName) {
|
|
212
|
+
// Track in reverse map
|
|
213
|
+
if (!clientMonitors.has(ws)) clientMonitors.set(ws, new Set());
|
|
214
|
+
clientMonitors.get(ws).add(sessionName);
|
|
215
|
+
|
|
216
|
+
// If this session already has an active monitor, just add this client as subscriber
|
|
217
|
+
if (outputMonitors.has(sessionName)) {
|
|
218
|
+
const monitor = outputMonitors.get(sessionName);
|
|
219
|
+
monitor.subscribers.set(ws, true);
|
|
220
|
+
monitor.checkCount = 0; // Reset timeout on re-subscribe
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
let lastOutput = '';
|
|
225
|
+
let stableCount = 0;
|
|
226
|
+
const maxChecks = 120; // 2 minutes max
|
|
227
|
+
|
|
228
|
+
const subscribers = new Map();
|
|
229
|
+
subscribers.set(ws, true);
|
|
230
|
+
|
|
231
|
+
const monitorState = { subscribers, intervalId: null, checkCount: 0 };
|
|
232
|
+
|
|
233
|
+
const intervalId = setInterval(async () => {
|
|
234
|
+
monitorState.checkCount++;
|
|
235
|
+
|
|
236
|
+
// Remove disconnected subscribers
|
|
237
|
+
for (const [subWs] of subscribers) {
|
|
238
|
+
if (subWs.readyState !== 1) {
|
|
239
|
+
subscribers.delete(subWs);
|
|
240
|
+
const sessions = clientMonitors.get(subWs);
|
|
241
|
+
if (sessions) sessions.delete(sessionName);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// Stop if no subscribers left
|
|
246
|
+
if (subscribers.size === 0 || monitorState.checkCount > maxChecks) {
|
|
247
|
+
clearInterval(intervalId);
|
|
248
|
+
outputMonitors.delete(sessionName);
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
try {
|
|
253
|
+
const output = await tmuxCapture(sessionName, 2000);
|
|
254
|
+
|
|
255
|
+
if (output !== lastOutput) {
|
|
256
|
+
lastOutput = output;
|
|
257
|
+
stableCount = 0;
|
|
258
|
+
|
|
259
|
+
const parsed = parseClaudeOutput(output);
|
|
260
|
+
for (const [subWs] of subscribers) {
|
|
261
|
+
sendOutputToClient(subWs, {
|
|
262
|
+
type: 'claude_output',
|
|
263
|
+
content: parsed.display,
|
|
264
|
+
session: sessionName
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
} else {
|
|
268
|
+
stableCount++;
|
|
269
|
+
|
|
270
|
+
if (stableCount >= 5) {
|
|
271
|
+
const isWaiting = tmux.isWaitingForInput(output);
|
|
272
|
+
|
|
273
|
+
if (isWaiting) {
|
|
274
|
+
const parsed = parseClaudeOutput(output);
|
|
275
|
+
for (const [subWs] of subscribers) {
|
|
276
|
+
sendToClient(subWs, {
|
|
277
|
+
type: 'status',
|
|
278
|
+
status: 'waiting'
|
|
279
|
+
});
|
|
280
|
+
sendToClient(subWs, {
|
|
281
|
+
type: 'claude_output',
|
|
282
|
+
content: parsed.display,
|
|
283
|
+
userQuestion: parsed.userQuestion,
|
|
284
|
+
claudeResponse: parsed.claudeResponse,
|
|
285
|
+
session: sessionName,
|
|
286
|
+
final: true
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
clearInterval(intervalId);
|
|
292
|
+
outputMonitors.delete(sessionName);
|
|
293
|
+
for (const [subWs] of subscribers) {
|
|
294
|
+
const sessions = clientMonitors.get(subWs);
|
|
295
|
+
if (sessions) sessions.delete(sessionName);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
} catch (error) {
|
|
300
|
+
console.error('Monitor error:', error.message);
|
|
301
|
+
}
|
|
302
|
+
}, 1000);
|
|
303
|
+
|
|
304
|
+
monitorState.intervalId = intervalId;
|
|
305
|
+
outputMonitors.set(sessionName, monitorState);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Stop monitor for a session
|
|
310
|
+
*/
|
|
311
|
+
function stopMonitor(sessionName) {
|
|
312
|
+
if (outputMonitors.has(sessionName)) {
|
|
313
|
+
const entry = outputMonitors.get(sessionName);
|
|
314
|
+
clearInterval(entry.intervalId);
|
|
315
|
+
for (const [subWs] of entry.subscribers) {
|
|
316
|
+
const sessions = clientMonitors.get(subWs);
|
|
317
|
+
if (sessions) sessions.delete(sessionName);
|
|
318
|
+
}
|
|
319
|
+
outputMonitors.delete(sessionName);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Remove a specific client from a session monitor (without stopping the whole monitor)
|
|
325
|
+
*/
|
|
326
|
+
function removeClientFromMonitor(ws, sessionName) {
|
|
327
|
+
if (outputMonitors.has(sessionName)) {
|
|
328
|
+
const entry = outputMonitors.get(sessionName);
|
|
329
|
+
entry.subscribers.delete(ws);
|
|
330
|
+
const sessions = clientMonitors.get(ws);
|
|
331
|
+
if (sessions) sessions.delete(sessionName);
|
|
332
|
+
// If no subscribers left, stop the monitor entirely
|
|
333
|
+
if (entry.subscribers.size === 0) {
|
|
334
|
+
clearInterval(entry.intervalId);
|
|
335
|
+
outputMonitors.delete(sessionName);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Clean up all monitors and auth state for a disconnected client
|
|
342
|
+
*/
|
|
343
|
+
function cleanupClient(ws) {
|
|
344
|
+
const sessions = clientMonitors.get(ws);
|
|
345
|
+
if (sessions) {
|
|
346
|
+
for (const sessionName of sessions) {
|
|
347
|
+
if (outputMonitors.has(sessionName)) {
|
|
348
|
+
const entry = outputMonitors.get(sessionName);
|
|
349
|
+
entry.subscribers.delete(ws);
|
|
350
|
+
if (entry.subscribers.size === 0) {
|
|
351
|
+
clearInterval(entry.intervalId);
|
|
352
|
+
outputMonitors.delete(sessionName);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
clientMonitors.delete(ws);
|
|
357
|
+
}
|
|
358
|
+
authChallengeManager.cleanupClient(ws);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Capture and broadcast output
|
|
363
|
+
*/
|
|
364
|
+
async function captureAndBroadcast(sessionName, reason = 'update', originWs = null) {
|
|
365
|
+
const result = await tmux.captureOutput(sessionName, 2000);
|
|
366
|
+
if (result.success) {
|
|
367
|
+
const message = {
|
|
368
|
+
type: 'claude_output',
|
|
369
|
+
content: result.content,
|
|
370
|
+
userQuestion: result.userQuestion,
|
|
371
|
+
claudeResponse: result.claudeResponse,
|
|
372
|
+
session: sessionName,
|
|
373
|
+
reason: reason
|
|
374
|
+
};
|
|
375
|
+
const monitor = outputMonitors.get(sessionName);
|
|
376
|
+
if (monitor) {
|
|
377
|
+
for (const [subWs] of monitor.subscribers) {
|
|
378
|
+
sendOutputToClient(subWs, message);
|
|
379
|
+
}
|
|
380
|
+
} else if (originWs) {
|
|
381
|
+
sendOutputToClient(originWs, message);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Handle incoming WebSocket message
|
|
388
|
+
* Supports both legacy protocol (type as method name) and new protocol (req/res/event)
|
|
389
|
+
*/
|
|
390
|
+
async function handleMessage(ws, message) {
|
|
391
|
+
// New protocol: handle 'req' type messages
|
|
392
|
+
if (message.type === 'req' && message.id && message.method) {
|
|
393
|
+
console.log(`📨 Request [${message.id}]: ${message.method}`);
|
|
394
|
+
await handleRequest(ws, message);
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
// Legacy protocol: type is the method name
|
|
399
|
+
console.log(`📨 Received: ${message.type}`);
|
|
400
|
+
await handleLegacyMessage(ws, message);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* Handle new protocol request messages
|
|
405
|
+
*/
|
|
406
|
+
async function handleRequest(ws, message) {
|
|
407
|
+
const { id, method, params = {} } = message;
|
|
408
|
+
|
|
409
|
+
try {
|
|
410
|
+
switch (method) {
|
|
411
|
+
case 'list_sessions': {
|
|
412
|
+
const result = await tmux.listSessions();
|
|
413
|
+
const sessions = result.sessions.map(s => ({
|
|
414
|
+
...s,
|
|
415
|
+
managed: server.managedSessions.has(s.name)
|
|
416
|
+
}));
|
|
417
|
+
sendResponse(ws, id, { sessions });
|
|
418
|
+
break;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
case 'send_to_claude': {
|
|
422
|
+
const result = await tmux.sendToClaude(params.content, params.session, params.path || getClientPath(ws), params.cols, params.rows);
|
|
423
|
+
if (result.success) {
|
|
424
|
+
sendResponse(ws, id, { status: 'sent' });
|
|
425
|
+
startOutputMonitor(ws, params.session || tmux.DEFAULT_SESSION);
|
|
426
|
+
} else {
|
|
427
|
+
sendErrorResponse(ws, id, result.error);
|
|
428
|
+
}
|
|
429
|
+
break;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
case 'change_path': {
|
|
433
|
+
const result = await withSensitivePathAuth(ws, params.path, 'change_path',
|
|
434
|
+
{ allowedBasePath: server.allowedBasePath, currentPath: getClientPath(ws) },
|
|
435
|
+
async () => fileService.changePath(params.path, server.allowedBasePath, getClientPath(ws))
|
|
436
|
+
);
|
|
437
|
+
if (result.success) {
|
|
438
|
+
setClientPath(ws, result.path);
|
|
439
|
+
sendResponse(ws, id, { path: result.path });
|
|
440
|
+
} else {
|
|
441
|
+
sendErrorResponse(ws, id, result.error);
|
|
442
|
+
}
|
|
443
|
+
break;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
case 'start_claude': {
|
|
447
|
+
console.log(`[DEBUG start_claude] params.session=${params.session}, params.command=${params.command}, params.path=${params.path}, clientPath=${getClientPath(ws)}`);
|
|
448
|
+
const result = await tmux.startClaude(params.session, params.command, params.path || getClientPath(ws), params.cols, params.rows);
|
|
449
|
+
console.log(`[DEBUG start_claude] result: success=${result.success}, isRunning=${result.isRunning}, alreadyRunning=${result.alreadyRunning}, error=${result.error}`);
|
|
450
|
+
if (result.success) {
|
|
451
|
+
sendResponse(ws, id, {
|
|
452
|
+
status: 'running',
|
|
453
|
+
isRunning: result.isRunning === true,
|
|
454
|
+
alreadyRunning: result.alreadyRunning === true
|
|
455
|
+
});
|
|
456
|
+
startOutputMonitor(ws, params.session || tmux.DEFAULT_SESSION);
|
|
457
|
+
} else {
|
|
458
|
+
sendErrorResponse(ws, id, result.error);
|
|
459
|
+
}
|
|
460
|
+
break;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
case 'stop_claude': {
|
|
464
|
+
const result = await tmux.stopClaude(params.session);
|
|
465
|
+
stopMonitor(params.session || tmux.DEFAULT_SESSION);
|
|
466
|
+
if (result.success) {
|
|
467
|
+
sendResponse(ws, id, { status: 'stopped' });
|
|
468
|
+
} else {
|
|
469
|
+
sendErrorResponse(ws, id, result.error);
|
|
470
|
+
}
|
|
471
|
+
break;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
case 'attach_session': {
|
|
475
|
+
// Remove this client from monitors for other sessions
|
|
476
|
+
const clientSessions = clientMonitors.get(ws);
|
|
477
|
+
if (clientSessions) {
|
|
478
|
+
for (const name of [...clientSessions]) {
|
|
479
|
+
if (name !== params.session) {
|
|
480
|
+
removeClientFromMonitor(ws, name);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
const result = await tmux.attachSession(params.session);
|
|
486
|
+
if (result.success) {
|
|
487
|
+
if (result.path) setClientPath(ws, result.path);
|
|
488
|
+
sendResponse(ws, id, {
|
|
489
|
+
path: getClientPath(ws),
|
|
490
|
+
isRunning: result.isRunning,
|
|
491
|
+
content: result.output?.content
|
|
492
|
+
});
|
|
493
|
+
if (result.isRunning) {
|
|
494
|
+
startOutputMonitor(ws, params.session);
|
|
495
|
+
}
|
|
496
|
+
} else {
|
|
497
|
+
sendErrorResponse(ws, id, result.error);
|
|
498
|
+
}
|
|
499
|
+
break;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
case 'create_session': {
|
|
503
|
+
console.log(`[DEBUG create_session] params.name=${params.name}, params.path=${params.path}, clientPath=${getClientPath(ws)}, allowedBasePath=${server.allowedBasePath}`);
|
|
504
|
+
const result = await withSensitivePathAuth(ws, params.path || getClientPath(ws), 'create_session',
|
|
505
|
+
{ allowedBasePath: server.allowedBasePath, currentPath: getClientPath(ws) },
|
|
506
|
+
async () => tmux.createSession(params.name, params.path, server.allowedBasePath, getClientPath(ws))
|
|
507
|
+
);
|
|
508
|
+
console.log(`[DEBUG create_session] result: success=${result.success}, path=${result.path}, error=${JSON.stringify(result.error)}`);
|
|
509
|
+
if (result.success) {
|
|
510
|
+
server.managedSessions.add(params.name);
|
|
511
|
+
setClientPath(ws, result.path);
|
|
512
|
+
const listResult = await tmux.listSessions();
|
|
513
|
+
sendResponse(ws, id, { path: result.path, sessions: listResult.sessions });
|
|
514
|
+
} else {
|
|
515
|
+
sendErrorResponse(ws, id, result.error);
|
|
516
|
+
}
|
|
517
|
+
break;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
case 'get_output': {
|
|
521
|
+
const result = await tmux.getSessionOutput(params.session);
|
|
522
|
+
if (result.success) {
|
|
523
|
+
sendResponse(ws, id, {
|
|
524
|
+
content: result.content,
|
|
525
|
+
isRunning: result.isRunning,
|
|
526
|
+
userQuestion: result.userQuestion,
|
|
527
|
+
claudeResponse: result.claudeResponse
|
|
528
|
+
});
|
|
529
|
+
} else {
|
|
530
|
+
sendErrorResponse(ws, id, result.error);
|
|
531
|
+
}
|
|
532
|
+
break;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
case 'send_key': {
|
|
536
|
+
const result = await tmux.sendKeyToSession(params.key, params.session);
|
|
537
|
+
if (result.success) {
|
|
538
|
+
sendResponse(ws, id, { status: 'key_sent' });
|
|
539
|
+
setTimeout(() => {
|
|
540
|
+
captureAndBroadcast(params.session || tmux.DEFAULT_SESSION, 'key_response', ws).catch(e => console.error('captureAndBroadcast error:', e.message));
|
|
541
|
+
}, 300);
|
|
542
|
+
} else {
|
|
543
|
+
sendErrorResponse(ws, id, result.error);
|
|
544
|
+
}
|
|
545
|
+
break;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
case 'send_sequence': {
|
|
549
|
+
if (!Array.isArray(params.keys)) {
|
|
550
|
+
sendErrorResponse(ws, id, { type: 'error', code: 'INVALID_PARAMS', error: 'keys must be an array', retryable: false });
|
|
551
|
+
break;
|
|
552
|
+
}
|
|
553
|
+
const result = await tmux.sendKeySequence(params.session, params.keys);
|
|
554
|
+
if (result.success) {
|
|
555
|
+
sendResponse(ws, id, { status: 'sequence_sent' });
|
|
556
|
+
setTimeout(() => {
|
|
557
|
+
captureAndBroadcast(params.session || tmux.DEFAULT_SESSION, 'key_response', ws).catch(e => console.error('captureAndBroadcast error:', e.message));
|
|
558
|
+
}, 300);
|
|
559
|
+
} else {
|
|
560
|
+
sendErrorResponse(ws, id, result.error);
|
|
561
|
+
}
|
|
562
|
+
break;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
case 'delete_session': {
|
|
566
|
+
const result = await tmux.deleteSession(params.session);
|
|
567
|
+
stopMonitor(params.session);
|
|
568
|
+
if (result.success) {
|
|
569
|
+
server.managedSessions.delete(params.session);
|
|
570
|
+
const listResult = await tmux.listSessions();
|
|
571
|
+
sendResponse(ws, id, { session: params.session, sessions: listResult.sessions });
|
|
572
|
+
} else {
|
|
573
|
+
sendErrorResponse(ws, id, result.error);
|
|
574
|
+
}
|
|
575
|
+
break;
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
case 'kill_session': {
|
|
579
|
+
const result = await tmux.killSession(params.session);
|
|
580
|
+
stopMonitor(params.session);
|
|
581
|
+
if (result.success) {
|
|
582
|
+
server.managedSessions.delete(params.session);
|
|
583
|
+
const listResult = await tmux.listSessions();
|
|
584
|
+
sendResponse(ws, id, { session: params.session, sessions: listResult.sessions });
|
|
585
|
+
} else {
|
|
586
|
+
sendErrorResponse(ws, id, result.error);
|
|
587
|
+
}
|
|
588
|
+
break;
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
case 'get_full_output': {
|
|
592
|
+
const result = await tmux.getFullSessionOutput(params.session, params.lines);
|
|
593
|
+
if (result.success) {
|
|
594
|
+
sendResponse(ws, id, { content: result.content, lines: params.lines });
|
|
595
|
+
} else {
|
|
596
|
+
sendErrorResponse(ws, id, result.error);
|
|
597
|
+
}
|
|
598
|
+
break;
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
case 'create_folder': {
|
|
602
|
+
const result = await withSensitivePathAuth(ws, params.path, 'create_folder',
|
|
603
|
+
{ allowedBasePath: server.allowedBasePath, currentPath: getClientPath(ws) },
|
|
604
|
+
async () => fileService.createFolder(params.path, server.allowedBasePath, getClientPath(ws))
|
|
605
|
+
);
|
|
606
|
+
if (result.success) {
|
|
607
|
+
sendResponse(ws, id, { path: result.path });
|
|
608
|
+
} else {
|
|
609
|
+
sendErrorResponse(ws, id, result.error);
|
|
610
|
+
}
|
|
611
|
+
break;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
case 'list_directory': {
|
|
615
|
+
const result = await withSensitivePathAuth(ws, params.path, 'list_directory',
|
|
616
|
+
{ allowBrowse: true, allowedBasePath: server.allowedBasePath, currentPath: getClientPath(ws) },
|
|
617
|
+
async () => fileService.listDirectory(params.path, server.allowedBasePath, getClientPath(ws))
|
|
618
|
+
);
|
|
619
|
+
if (result.success) {
|
|
620
|
+
sendResponse(ws, id, { path: result.path, items: result.items });
|
|
621
|
+
} else {
|
|
622
|
+
sendErrorResponse(ws, id, result.error);
|
|
623
|
+
}
|
|
624
|
+
break;
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
case 'upload_start': {
|
|
628
|
+
const result = uploadService.startUpload(
|
|
629
|
+
params.filename,
|
|
630
|
+
Number(params.size),
|
|
631
|
+
params.mimeType,
|
|
632
|
+
Number(params.totalChunks),
|
|
633
|
+
params.destPath,
|
|
634
|
+
server.allowedBasePath,
|
|
635
|
+
getClientPath(ws)
|
|
636
|
+
);
|
|
637
|
+
if (result.success) {
|
|
638
|
+
console.log(`📤 Upload started: ${params.filename} (${(Number(params.size) / 1024).toFixed(0)}KB, ${params.totalChunks} chunks) → uploadId: ${result.uploadId}`);
|
|
639
|
+
sendResponse(ws, id, { uploadId: result.uploadId, ready: true });
|
|
640
|
+
} else {
|
|
641
|
+
sendErrorResponse(ws, id, result.error);
|
|
642
|
+
}
|
|
643
|
+
break;
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
case 'upload_chunk': {
|
|
647
|
+
const result = uploadService.receiveChunk(
|
|
648
|
+
params.uploadId,
|
|
649
|
+
Number(params.chunkIndex),
|
|
650
|
+
params.data
|
|
651
|
+
);
|
|
652
|
+
if (result.success) {
|
|
653
|
+
if (result.complete) {
|
|
654
|
+
console.log(`✅ Upload complete: ${result.filePath} (${result.totalBytes} bytes)`);
|
|
655
|
+
sendResponse(ws, id, {
|
|
656
|
+
complete: true,
|
|
657
|
+
filePath: result.filePath,
|
|
658
|
+
totalBytes: result.totalBytes,
|
|
659
|
+
mimeType: result.mimeType
|
|
660
|
+
});
|
|
661
|
+
} else {
|
|
662
|
+
sendResponse(ws, id, {
|
|
663
|
+
chunkIndex: result.chunkIndex,
|
|
664
|
+
receivedBytes: result.receivedBytes
|
|
665
|
+
});
|
|
666
|
+
}
|
|
667
|
+
} else {
|
|
668
|
+
sendErrorResponse(ws, id, result.error);
|
|
669
|
+
}
|
|
670
|
+
break;
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
case 'upload_cancel': {
|
|
674
|
+
uploadService.cancelUpload(params.uploadId);
|
|
675
|
+
console.log(`🚫 Upload cancelled: ${params.uploadId}`);
|
|
676
|
+
sendResponse(ws, id, { cancelled: true });
|
|
677
|
+
break;
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
case 'resize_session': {
|
|
681
|
+
const result = await tmux.resizeSession(params.session, params.cols, params.rows);
|
|
682
|
+
if (result.success) {
|
|
683
|
+
sendResponse(ws, id, { cols: result.cols, rows: result.rows });
|
|
684
|
+
} else {
|
|
685
|
+
sendErrorResponse(ws, id, result.error);
|
|
686
|
+
}
|
|
687
|
+
break;
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
case 'auth_response': {
|
|
691
|
+
const resolved = authChallengeManager.resolveChallenge(
|
|
692
|
+
params.challengeId,
|
|
693
|
+
params.granted === true || params.granted === 'true'
|
|
694
|
+
);
|
|
695
|
+
if (resolved) {
|
|
696
|
+
sendResponse(ws, id, { status: 'received' });
|
|
697
|
+
} else {
|
|
698
|
+
sendErrorResponse(ws, id, {
|
|
699
|
+
type: 'error',
|
|
700
|
+
code: 'INVALID_CHALLENGE',
|
|
701
|
+
error: 'Challenge not found or already resolved',
|
|
702
|
+
retryable: false
|
|
703
|
+
});
|
|
704
|
+
}
|
|
705
|
+
break;
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
case 'git_info': {
|
|
709
|
+
const sessionCheck = validateSessionName(params.session);
|
|
710
|
+
if (!sessionCheck.valid) {
|
|
711
|
+
sendErrorResponse(ws, id, { type: 'error', code: 'INVALID_PARAMS', error: sessionCheck.error, retryable: false });
|
|
712
|
+
break;
|
|
713
|
+
}
|
|
714
|
+
let cwd;
|
|
715
|
+
try { cwd = (await tmuxGetPanePath(params.session)).trim(); } catch { cwd = getClientPath(ws); }
|
|
716
|
+
if (!cwd) {
|
|
717
|
+
cwd = getClientPath(ws);
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
const pathOptions = {
|
|
721
|
+
allowedBasePath: server.allowedBasePath,
|
|
722
|
+
currentPath: getClientPath(ws)
|
|
723
|
+
};
|
|
724
|
+
const result = await withSensitivePathAuth(ws, cwd, 'git_info', pathOptions, async () => {
|
|
725
|
+
const validation = validatePath(cwd, pathOptions);
|
|
726
|
+
if (!validation.valid) {
|
|
727
|
+
return {
|
|
728
|
+
success: false,
|
|
729
|
+
error: createError(ErrorCodes.PATH_ACCESS_DENIED, validation.error)
|
|
730
|
+
};
|
|
731
|
+
}
|
|
732
|
+
return {
|
|
733
|
+
success: true,
|
|
734
|
+
payload: await collectGitInfo(validation.path)
|
|
735
|
+
};
|
|
736
|
+
});
|
|
737
|
+
|
|
738
|
+
if (result.success) {
|
|
739
|
+
sendResponse(ws, id, result.payload);
|
|
740
|
+
} else {
|
|
741
|
+
sendErrorResponse(ws, id, result.error);
|
|
742
|
+
}
|
|
743
|
+
break;
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
default:
|
|
747
|
+
sendErrorResponse(ws, id, {
|
|
748
|
+
type: 'error',
|
|
749
|
+
code: 'INVALID_REQUEST',
|
|
750
|
+
error: `Unknown method: ${method}`,
|
|
751
|
+
retryable: false
|
|
752
|
+
});
|
|
753
|
+
}
|
|
754
|
+
} catch (err) {
|
|
755
|
+
console.error(`Request error [${id}]:`, err);
|
|
756
|
+
sendErrorResponse(ws, id, {
|
|
757
|
+
type: 'error',
|
|
758
|
+
code: 'INTERNAL_ERROR',
|
|
759
|
+
error: err.message,
|
|
760
|
+
retryable: true
|
|
761
|
+
});
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
/**
|
|
766
|
+
* Handle legacy protocol messages (backward compatibility)
|
|
767
|
+
*/
|
|
768
|
+
async function handleLegacyMessage(ws, message) {
|
|
769
|
+
try {
|
|
770
|
+
switch (message.type) {
|
|
771
|
+
case 'send_to_claude': {
|
|
772
|
+
const result = await tmux.sendToClaude(message.content, message.session, getClientPath(ws), message.cols, message.rows);
|
|
773
|
+
if (result.success) {
|
|
774
|
+
sendToClient(ws, { type: 'status', status: 'sent' });
|
|
775
|
+
startOutputMonitor(ws, message.session || tmux.DEFAULT_SESSION);
|
|
776
|
+
} else {
|
|
777
|
+
sendToClient(ws, result.error);
|
|
778
|
+
}
|
|
779
|
+
break;
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
case 'change_path': {
|
|
783
|
+
const result = await withSensitivePathAuth(ws, message.path, 'change_path',
|
|
784
|
+
{ allowedBasePath: server.allowedBasePath, currentPath: getClientPath(ws) },
|
|
785
|
+
async () => fileService.changePath(message.path, server.allowedBasePath, getClientPath(ws))
|
|
786
|
+
);
|
|
787
|
+
if (result.success) {
|
|
788
|
+
setClientPath(ws, result.path);
|
|
789
|
+
sendToClient(ws, { type: 'path_changed', path: result.path });
|
|
790
|
+
console.log(`📂 Changed path to: ${result.path} (client: ${ws._clientId})`);
|
|
791
|
+
} else {
|
|
792
|
+
sendToClient(ws, result.error);
|
|
793
|
+
}
|
|
794
|
+
break;
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
case 'start_claude': {
|
|
798
|
+
const result = await tmux.startClaude(message.session, message.command, getClientPath(ws), message.cols, message.rows);
|
|
799
|
+
if (result.success) {
|
|
800
|
+
sendToClient(ws, { type: 'status', status: 'running' });
|
|
801
|
+
startOutputMonitor(ws, message.session || tmux.DEFAULT_SESSION);
|
|
802
|
+
} else {
|
|
803
|
+
sendToClient(ws, result.error);
|
|
804
|
+
}
|
|
805
|
+
break;
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
case 'stop_claude': {
|
|
809
|
+
const result = await tmux.stopClaude(message.session);
|
|
810
|
+
stopMonitor(message.session || tmux.DEFAULT_SESSION);
|
|
811
|
+
if (result.success) {
|
|
812
|
+
sendToClient(ws, { type: 'status', status: 'stopped' });
|
|
813
|
+
} else {
|
|
814
|
+
sendToClient(ws, result.error);
|
|
815
|
+
}
|
|
816
|
+
break;
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
case 'list_sessions': {
|
|
820
|
+
const result = await tmux.listSessions();
|
|
821
|
+
const sessions = result.sessions.map(s => ({
|
|
822
|
+
...s,
|
|
823
|
+
managed: server.managedSessions.has(s.name)
|
|
824
|
+
}));
|
|
825
|
+
sendToClient(ws, { type: 'sessions', sessions });
|
|
826
|
+
break;
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
case 'attach_session': {
|
|
830
|
+
// Remove this client from monitors for other sessions
|
|
831
|
+
const legacyClientSessions = clientMonitors.get(ws);
|
|
832
|
+
if (legacyClientSessions) {
|
|
833
|
+
for (const name of [...legacyClientSessions]) {
|
|
834
|
+
if (name !== message.session) {
|
|
835
|
+
removeClientFromMonitor(ws, name);
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
const result = await tmux.attachSession(message.session);
|
|
841
|
+
if (result.success) {
|
|
842
|
+
if (result.path) setClientPath(ws, result.path);
|
|
843
|
+
sendToClient(ws, { type: 'path_changed', path: getClientPath(ws) });
|
|
844
|
+
console.log(`📎 Attached to session: ${message.session} at ${getClientPath(ws)}`);
|
|
845
|
+
|
|
846
|
+
if (result.output && result.output.success) {
|
|
847
|
+
sendToClient(ws, {
|
|
848
|
+
type: 'claude_output',
|
|
849
|
+
content: result.output.content,
|
|
850
|
+
session: message.session
|
|
851
|
+
});
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
sendToClient(ws, {
|
|
855
|
+
type: 'status',
|
|
856
|
+
status: result.isRunning ? 'running' : 'stopped'
|
|
857
|
+
});
|
|
858
|
+
|
|
859
|
+
if (result.isRunning) {
|
|
860
|
+
startOutputMonitor(ws, message.session);
|
|
861
|
+
}
|
|
862
|
+
} else {
|
|
863
|
+
sendToClient(ws, result.error);
|
|
864
|
+
}
|
|
865
|
+
break;
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
case 'create_session': {
|
|
869
|
+
const result = await withSensitivePathAuth(ws, message.path || getClientPath(ws), 'create_session',
|
|
870
|
+
{ allowedBasePath: server.allowedBasePath, currentPath: getClientPath(ws) },
|
|
871
|
+
async () => tmux.createSession(message.content, message.path, server.allowedBasePath, getClientPath(ws))
|
|
872
|
+
);
|
|
873
|
+
if (result.success) {
|
|
874
|
+
server.managedSessions.add(message.content);
|
|
875
|
+
const expandedPath = result.path;
|
|
876
|
+
setClientPath(ws, expandedPath);
|
|
877
|
+
sendToClient(ws, { type: 'path_changed', path: expandedPath });
|
|
878
|
+
const listResult = await tmux.listSessions();
|
|
879
|
+
sendToClient(ws, { type: 'sessions', sessions: listResult.sessions });
|
|
880
|
+
} else {
|
|
881
|
+
sendToClient(ws, result.error);
|
|
882
|
+
}
|
|
883
|
+
break;
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
case 'get_output': {
|
|
887
|
+
const result = await tmux.getSessionOutput(message.session);
|
|
888
|
+
if (result.success) {
|
|
889
|
+
sendToClient(ws, {
|
|
890
|
+
type: 'claude_output',
|
|
891
|
+
content: result.content,
|
|
892
|
+
userQuestion: result.userQuestion,
|
|
893
|
+
claudeResponse: result.claudeResponse,
|
|
894
|
+
session: message.session
|
|
895
|
+
});
|
|
896
|
+
} else {
|
|
897
|
+
sendToClient(ws, result.error);
|
|
898
|
+
}
|
|
899
|
+
break;
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
case 'subscribe_output': {
|
|
903
|
+
const session = message.session || tmux.DEFAULT_SESSION;
|
|
904
|
+
const result = await tmux.getSessionOutput(session);
|
|
905
|
+
if (result.success) {
|
|
906
|
+
sendToClient(ws, {
|
|
907
|
+
type: 'claude_output',
|
|
908
|
+
content: result.content,
|
|
909
|
+
session: session
|
|
910
|
+
});
|
|
911
|
+
}
|
|
912
|
+
startOutputMonitor(ws, session);
|
|
913
|
+
break;
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
case 'unsubscribe_output': {
|
|
917
|
+
removeClientFromMonitor(ws, message.session || tmux.DEFAULT_SESSION);
|
|
918
|
+
break;
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
case 'send_key': {
|
|
922
|
+
const result = await tmux.sendKeyToSession(message.content, message.session);
|
|
923
|
+
if (result.success) {
|
|
924
|
+
sendToClient(ws, { type: 'status', status: 'key_sent' });
|
|
925
|
+
setTimeout(() => {
|
|
926
|
+
captureAndBroadcast(message.session || tmux.DEFAULT_SESSION, 'key_response', ws).catch(e => console.error('captureAndBroadcast error:', e.message));
|
|
927
|
+
}, 300);
|
|
928
|
+
} else {
|
|
929
|
+
sendToClient(ws, result.error);
|
|
930
|
+
}
|
|
931
|
+
break;
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
case 'delete_session': {
|
|
935
|
+
const result = await tmux.deleteSession(message.session);
|
|
936
|
+
stopMonitor(message.session);
|
|
937
|
+
if (result.success) {
|
|
938
|
+
server.managedSessions.delete(message.session);
|
|
939
|
+
sendToClient(ws, { type: 'session_deleted', session: message.session });
|
|
940
|
+
const listResult = await tmux.listSessions();
|
|
941
|
+
sendToClient(ws, { type: 'sessions', sessions: listResult.sessions });
|
|
942
|
+
} else {
|
|
943
|
+
sendToClient(ws, result.error);
|
|
944
|
+
}
|
|
945
|
+
break;
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
case 'kill_session': {
|
|
949
|
+
const result = await tmux.killSession(message.session);
|
|
950
|
+
stopMonitor(message.session);
|
|
951
|
+
if (result.success) {
|
|
952
|
+
server.managedSessions.delete(message.session);
|
|
953
|
+
sendToClient(ws, { type: 'session_killed', session: message.session });
|
|
954
|
+
const listResult = await tmux.listSessions();
|
|
955
|
+
sendToClient(ws, { type: 'sessions', sessions: listResult.sessions });
|
|
956
|
+
} else {
|
|
957
|
+
sendToClient(ws, result.error);
|
|
958
|
+
}
|
|
959
|
+
break;
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
case 'get_full_output': {
|
|
963
|
+
const result = await tmux.getFullSessionOutput(message.session, message.lines);
|
|
964
|
+
if (result.success) {
|
|
965
|
+
sendToClient(ws, {
|
|
966
|
+
type: 'full_output',
|
|
967
|
+
content: result.content,
|
|
968
|
+
session: message.session,
|
|
969
|
+
lines: message.lines
|
|
970
|
+
});
|
|
971
|
+
} else {
|
|
972
|
+
sendToClient(ws, result.error);
|
|
973
|
+
}
|
|
974
|
+
break;
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
case 'create_folder': {
|
|
978
|
+
const result = await withSensitivePathAuth(ws, message.path, 'create_folder',
|
|
979
|
+
{ allowedBasePath: server.allowedBasePath, currentPath: getClientPath(ws) },
|
|
980
|
+
async () => fileService.createFolder(message.path, server.allowedBasePath, getClientPath(ws))
|
|
981
|
+
);
|
|
982
|
+
if (result.success) {
|
|
983
|
+
sendToClient(ws, { type: 'folder_created', path: result.path });
|
|
984
|
+
console.log(`📁 Created folder: ${result.path}`);
|
|
985
|
+
} else {
|
|
986
|
+
sendToClient(ws, result.error);
|
|
987
|
+
}
|
|
988
|
+
break;
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
case 'list_directory': {
|
|
992
|
+
const result = await withSensitivePathAuth(ws, message.path, 'list_directory',
|
|
993
|
+
{ allowBrowse: true, allowedBasePath: server.allowedBasePath, currentPath: getClientPath(ws) },
|
|
994
|
+
async () => fileService.listDirectory(message.path, server.allowedBasePath, getClientPath(ws))
|
|
995
|
+
);
|
|
996
|
+
if (result.success) {
|
|
997
|
+
sendToClient(ws, {
|
|
998
|
+
type: 'directory_list',
|
|
999
|
+
path: result.path,
|
|
1000
|
+
items: result.items
|
|
1001
|
+
});
|
|
1002
|
+
console.log(`📂 Listed directory: ${result.path} (${result.items.length} items)`);
|
|
1003
|
+
} else {
|
|
1004
|
+
sendToClient(ws, result.error);
|
|
1005
|
+
}
|
|
1006
|
+
break;
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1009
|
+
default:
|
|
1010
|
+
console.log(`Unknown message type: ${message.type}`);
|
|
1011
|
+
}
|
|
1012
|
+
} catch (err) {
|
|
1013
|
+
console.error(`Legacy message error [${message.type}]:`, err);
|
|
1014
|
+
sendToClient(ws, {
|
|
1015
|
+
type: 'error',
|
|
1016
|
+
error: err.message || 'Internal server error'
|
|
1017
|
+
});
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
|
|
1021
|
+
return {
|
|
1022
|
+
sendToClient,
|
|
1023
|
+
broadcast,
|
|
1024
|
+
broadcastEvent,
|
|
1025
|
+
sendEvent,
|
|
1026
|
+
sendResponse,
|
|
1027
|
+
sendErrorResponse,
|
|
1028
|
+
handleMessage,
|
|
1029
|
+
startOutputMonitor,
|
|
1030
|
+
stopMonitor,
|
|
1031
|
+
removeClientFromMonitor,
|
|
1032
|
+
cleanupClient,
|
|
1033
|
+
captureAndBroadcast,
|
|
1034
|
+
getClientPath,
|
|
1035
|
+
setClientPath,
|
|
1036
|
+
outputMonitors
|
|
1037
|
+
};
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
module.exports = { createWsHandler };
|