tacticremote 1.6.5 → 1.7.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/lib/authChallengeManager.js +1 -134
- package/lib/claudeSessionManager.js +1 -0
- package/lib/claudeTransport.js +1 -0
- package/lib/errorCodes.js +1 -127
- package/lib/execSafe.js +1 -218
- package/lib/fileService.js +1 -177
- package/lib/hookInstaller.js +21 -7
- package/lib/outputParser.js +1 -106
- package/lib/pushClient.js +1 -0
- package/lib/rateLimiter.js +1 -111
- package/lib/serverIdentity.js +1 -0
- package/lib/streamJsonParser.js +1 -0
- package/lib/tmuxManager.js +1 -671
- package/lib/uploadService.js +1 -366
- package/lib/validators.js +1 -350
- package/package.json +2 -1
- package/routes/httpHandler.js +3 -1
- package/routes/wsHandler.js +418 -364
- package/server.js +67 -152
- package/shared/server-core/authChallengeManager.js +108 -0
- package/shared/server-core/claudeSessionManager.js +222 -0
- package/shared/server-core/claudeTransport.js +176 -0
- package/shared/server-core/errorCodes.js +91 -0
- package/shared/server-core/execSafe.js +185 -0
- package/shared/server-core/fileService.js +155 -0
- package/shared/server-core/outputParser.js +174 -0
- package/shared/server-core/protocol-manifest.json +59 -0
- package/shared/server-core/protocol-manifest.schema.json +90 -0
- package/shared/server-core/protocolManifest.js +42 -0
- package/shared/server-core/pushClient.js +198 -0
- package/shared/server-core/rateLimiter.js +98 -0
- package/shared/server-core/serverConnectionLifecycle.js +118 -0
- package/shared/server-core/serverHandshake.js +72 -0
- package/shared/server-core/serverIdentity.js +102 -0
- package/shared/server-core/streamJsonParser.js +321 -0
- package/shared/server-core/tmuxManager.js +596 -0
- package/shared/server-core/unixProcessLifecycle.js +96 -0
- package/shared/server-core/unixServerHooks.js +148 -0
- package/shared/server-core/unixWsRuntime.js +412 -0
- package/shared/server-core/uploadService.js +321 -0
- package/shared/server-core/validators.js +274 -0
- package/shared/server-core/windowsServerHooks.js +174 -0
- package/shared/server-core/windowsWsRuntime.js +413 -0
package/lib/uploadService.js
CHANGED
|
@@ -1,366 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* File upload service for MacServer
|
|
3
|
-
* Handles chunked file uploads from iOS clients via WebSocket
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
const fs = require('fs');
|
|
7
|
-
const path = require('path');
|
|
8
|
-
const crypto = require('crypto');
|
|
9
|
-
const { validatePath } = require('./validators');
|
|
10
|
-
const { ErrorCodes, createError } = require('./errorCodes');
|
|
11
|
-
|
|
12
|
-
const MAX_FILE_SIZE = 50 * 1024 * 1024; // 50MB
|
|
13
|
-
const CHUNK_TIMEOUT_MS = 60000; // 60 seconds without a chunk → auto-cancel
|
|
14
|
-
const MAX_FILENAME_LENGTH = 255;
|
|
15
|
-
const MAX_ACTIVE_UPLOADS = 4;
|
|
16
|
-
|
|
17
|
-
const ALLOWED_MIME_TYPES = new Set([
|
|
18
|
-
// Images
|
|
19
|
-
'image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/svg+xml',
|
|
20
|
-
'image/bmp', 'image/tiff', 'image/heic', 'image/heif',
|
|
21
|
-
// Documents
|
|
22
|
-
'application/pdf',
|
|
23
|
-
'application/msword',
|
|
24
|
-
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
25
|
-
'application/vnd.ms-excel',
|
|
26
|
-
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
27
|
-
'application/vnd.ms-powerpoint',
|
|
28
|
-
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
|
29
|
-
// Text & Code
|
|
30
|
-
'text/plain', 'text/html', 'text/css', 'text/javascript', 'text/markdown',
|
|
31
|
-
'text/csv', 'text/xml', 'text/yaml',
|
|
32
|
-
'application/json', 'application/xml', 'application/javascript',
|
|
33
|
-
'application/typescript', 'application/x-yaml',
|
|
34
|
-
// Archives
|
|
35
|
-
'application/zip', 'application/gzip', 'application/x-tar',
|
|
36
|
-
'application/x-7z-compressed',
|
|
37
|
-
// Other
|
|
38
|
-
'application/octet-stream'
|
|
39
|
-
]);
|
|
40
|
-
|
|
41
|
-
// Active uploads: uploadId → { metadata, tempPath, writeStream, receivedChunks, receivedBytes, timer }
|
|
42
|
-
const activeUploads = new Map();
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Sanitize filename — remove path traversal, special chars, limit length
|
|
46
|
-
*/
|
|
47
|
-
function sanitizeFilename(filename) {
|
|
48
|
-
if (!filename || typeof filename !== 'string') {
|
|
49
|
-
return null;
|
|
50
|
-
}
|
|
51
|
-
// Remove path separators and traversal
|
|
52
|
-
let clean = filename.replace(/[/\\]/g, '_').replace(/\.\./g, '_');
|
|
53
|
-
// Remove control characters and problematic chars
|
|
54
|
-
clean = clean.replace(/[\x00-\x1f\x7f<>:"|?*]/g, '_');
|
|
55
|
-
// Trim whitespace and dots from edges
|
|
56
|
-
clean = clean.replace(/^[\s.]+|[\s.]+$/g, '');
|
|
57
|
-
// Limit length
|
|
58
|
-
if (clean.length > MAX_FILENAME_LENGTH) {
|
|
59
|
-
const ext = path.extname(clean);
|
|
60
|
-
const base = path.basename(clean, ext);
|
|
61
|
-
clean = base.substring(0, MAX_FILENAME_LENGTH - ext.length) + ext;
|
|
62
|
-
}
|
|
63
|
-
return clean || null;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Generate a unique filename if a file with the same name already exists
|
|
68
|
-
*/
|
|
69
|
-
function resolveFilename(destDir, filename) {
|
|
70
|
-
let candidate = path.join(destDir, filename);
|
|
71
|
-
if (!fs.existsSync(candidate)) {
|
|
72
|
-
return candidate;
|
|
73
|
-
}
|
|
74
|
-
const ext = path.extname(filename);
|
|
75
|
-
const base = path.basename(filename, ext);
|
|
76
|
-
let counter = 1;
|
|
77
|
-
while (fs.existsSync(candidate) && counter < 1000) {
|
|
78
|
-
candidate = path.join(destDir, `${base} (${counter})${ext}`);
|
|
79
|
-
counter++;
|
|
80
|
-
}
|
|
81
|
-
return candidate;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Start a new upload
|
|
86
|
-
*/
|
|
87
|
-
function startUpload(filename, size, mimeType, totalChunks, destPath, allowedBasePath, currentPath) {
|
|
88
|
-
// Validate filename
|
|
89
|
-
const cleanFilename = sanitizeFilename(filename);
|
|
90
|
-
if (!cleanFilename) {
|
|
91
|
-
return {
|
|
92
|
-
success: false,
|
|
93
|
-
error: createError(ErrorCodes.UPLOAD_INVALID_TYPE, 'Invalid filename')
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// Validate size
|
|
98
|
-
if (!size || typeof size !== 'number' || size <= 0) {
|
|
99
|
-
return {
|
|
100
|
-
success: false,
|
|
101
|
-
error: createError(ErrorCodes.UPLOAD_TOO_LARGE, 'Invalid file size')
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
if (size > MAX_FILE_SIZE) {
|
|
105
|
-
return {
|
|
106
|
-
success: false,
|
|
107
|
-
error: createError(ErrorCodes.UPLOAD_TOO_LARGE,
|
|
108
|
-
`File too large: ${(size / 1024 / 1024).toFixed(1)}MB exceeds ${MAX_FILE_SIZE / 1024 / 1024}MB limit`)
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// Validate MIME type
|
|
113
|
-
if (!mimeType || !ALLOWED_MIME_TYPES.has(mimeType)) {
|
|
114
|
-
return {
|
|
115
|
-
success: false,
|
|
116
|
-
error: createError(ErrorCodes.UPLOAD_INVALID_TYPE, `Unsupported file type: ${mimeType}`)
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
// Validate totalChunks
|
|
121
|
-
if (!totalChunks || typeof totalChunks !== 'number' || totalChunks <= 0) {
|
|
122
|
-
return {
|
|
123
|
-
success: false,
|
|
124
|
-
error: createError(ErrorCodes.INVALID_REQUEST, 'Invalid chunk count')
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
if (activeUploads.size >= MAX_ACTIVE_UPLOADS) {
|
|
129
|
-
return {
|
|
130
|
-
success: false,
|
|
131
|
-
error: createError(ErrorCodes.RATE_LIMITED, 'Too many concurrent uploads. Wait for an active upload to finish and try again.')
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// Resolve destination directory
|
|
136
|
-
const destDir = destPath || currentPath || allowedBasePath;
|
|
137
|
-
const pathValidation = validatePath(destDir, { allowedBasePath, currentPath });
|
|
138
|
-
if (!pathValidation.valid) {
|
|
139
|
-
return {
|
|
140
|
-
success: false,
|
|
141
|
-
error: createError(ErrorCodes.UPLOAD_PATH_DENIED, pathValidation.error)
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
const resolvedDestDir = pathValidation.path;
|
|
146
|
-
if (!fs.existsSync(resolvedDestDir) || !fs.statSync(resolvedDestDir).isDirectory()) {
|
|
147
|
-
return {
|
|
148
|
-
success: false,
|
|
149
|
-
error: createError(ErrorCodes.UPLOAD_PATH_DENIED, 'Destination directory does not exist')
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
// Generate upload ID and temp file
|
|
154
|
-
const uploadId = crypto.randomUUID();
|
|
155
|
-
const finalPath = resolveFilename(resolvedDestDir, cleanFilename);
|
|
156
|
-
const tempPath = finalPath + '.upload-tmp';
|
|
157
|
-
|
|
158
|
-
try {
|
|
159
|
-
// Pre-allocate by creating an empty file
|
|
160
|
-
fs.writeFileSync(tempPath, Buffer.alloc(0));
|
|
161
|
-
} catch (err) {
|
|
162
|
-
return {
|
|
163
|
-
success: false,
|
|
164
|
-
error: createError(ErrorCodes.INTERNAL_ERROR, `Failed to create temp file: ${err.message}`)
|
|
165
|
-
};
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
// Set up timeout timer
|
|
169
|
-
const timer = setTimeout(() => {
|
|
170
|
-
cancelUpload(uploadId);
|
|
171
|
-
}, CHUNK_TIMEOUT_MS);
|
|
172
|
-
|
|
173
|
-
activeUploads.set(uploadId, {
|
|
174
|
-
filename: cleanFilename,
|
|
175
|
-
finalPath,
|
|
176
|
-
tempPath,
|
|
177
|
-
size,
|
|
178
|
-
mimeType,
|
|
179
|
-
totalChunks,
|
|
180
|
-
receivedChunks: new Set(),
|
|
181
|
-
receivedBytes: 0,
|
|
182
|
-
timer,
|
|
183
|
-
createdAt: Date.now()
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
return {
|
|
187
|
-
success: true,
|
|
188
|
-
uploadId,
|
|
189
|
-
ready: true
|
|
190
|
-
};
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
/**
|
|
194
|
-
* Receive a chunk of data
|
|
195
|
-
*/
|
|
196
|
-
function receiveChunk(uploadId, chunkIndex, base64Data) {
|
|
197
|
-
const upload = activeUploads.get(uploadId);
|
|
198
|
-
if (!upload) {
|
|
199
|
-
return {
|
|
200
|
-
success: false,
|
|
201
|
-
error: createError(ErrorCodes.UPLOAD_NOT_FOUND, 'Upload not found or expired')
|
|
202
|
-
};
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
if (typeof chunkIndex !== 'number' || chunkIndex < 0 || chunkIndex >= upload.totalChunks) {
|
|
206
|
-
return {
|
|
207
|
-
success: false,
|
|
208
|
-
error: createError(ErrorCodes.UPLOAD_CHUNK_ERROR, `Invalid chunk index: ${chunkIndex}`)
|
|
209
|
-
};
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
if (upload.receivedChunks.has(chunkIndex)) {
|
|
213
|
-
// Duplicate chunk — idempotent, just ack
|
|
214
|
-
return {
|
|
215
|
-
success: true,
|
|
216
|
-
chunkIndex,
|
|
217
|
-
receivedBytes: upload.receivedBytes
|
|
218
|
-
};
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
// Enforce strict sequential order (WebSocket guarantees order, but defend against bugs)
|
|
222
|
-
if (chunkIndex !== upload.receivedChunks.size) {
|
|
223
|
-
return {
|
|
224
|
-
success: false,
|
|
225
|
-
error: createError(ErrorCodes.UPLOAD_CHUNK_ERROR, `Expected chunk ${upload.receivedChunks.size}, got ${chunkIndex}`)
|
|
226
|
-
};
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
// Decode base64
|
|
230
|
-
let buffer;
|
|
231
|
-
try {
|
|
232
|
-
buffer = Buffer.from(base64Data, 'base64');
|
|
233
|
-
} catch (err) {
|
|
234
|
-
return {
|
|
235
|
-
success: false,
|
|
236
|
-
error: createError(ErrorCodes.UPLOAD_CHUNK_ERROR, 'Invalid base64 data')
|
|
237
|
-
};
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
// Write chunk at correct offset
|
|
241
|
-
// For simplicity, we append sequentially and track order
|
|
242
|
-
try {
|
|
243
|
-
fs.appendFileSync(upload.tempPath, buffer);
|
|
244
|
-
} catch (err) {
|
|
245
|
-
return {
|
|
246
|
-
success: false,
|
|
247
|
-
error: createError(ErrorCodes.UPLOAD_CHUNK_ERROR, `Failed to write chunk: ${err.message}`)
|
|
248
|
-
};
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
upload.receivedChunks.add(chunkIndex);
|
|
252
|
-
upload.receivedBytes += buffer.length;
|
|
253
|
-
|
|
254
|
-
// Reset timeout
|
|
255
|
-
clearTimeout(upload.timer);
|
|
256
|
-
upload.timer = setTimeout(() => {
|
|
257
|
-
cancelUpload(uploadId);
|
|
258
|
-
}, CHUNK_TIMEOUT_MS);
|
|
259
|
-
|
|
260
|
-
// Check if upload is complete
|
|
261
|
-
if (upload.receivedChunks.size === upload.totalChunks) {
|
|
262
|
-
return completeUpload(uploadId);
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
return {
|
|
266
|
-
success: true,
|
|
267
|
-
chunkIndex,
|
|
268
|
-
receivedBytes: upload.receivedBytes
|
|
269
|
-
};
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
/**
|
|
273
|
-
* Complete an upload — verify and move temp file to final location
|
|
274
|
-
*/
|
|
275
|
-
function completeUpload(uploadId) {
|
|
276
|
-
const upload = activeUploads.get(uploadId);
|
|
277
|
-
if (!upload) {
|
|
278
|
-
return {
|
|
279
|
-
success: false,
|
|
280
|
-
error: createError(ErrorCodes.UPLOAD_NOT_FOUND, 'Upload not found')
|
|
281
|
-
};
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
clearTimeout(upload.timer);
|
|
285
|
-
|
|
286
|
-
// Verify size (allow small variance for base64 padding)
|
|
287
|
-
const sizeVariance = Math.abs(upload.receivedBytes - upload.size);
|
|
288
|
-
if (sizeVariance > upload.totalChunks * 3) {
|
|
289
|
-
// More than 3 bytes per chunk variance (base64 padding) is suspicious
|
|
290
|
-
cleanupTempFile(upload.tempPath);
|
|
291
|
-
activeUploads.delete(uploadId);
|
|
292
|
-
return {
|
|
293
|
-
success: false,
|
|
294
|
-
error: createError(ErrorCodes.UPLOAD_CHUNK_ERROR,
|
|
295
|
-
`Size mismatch: expected ${upload.size}, received ${upload.receivedBytes}`)
|
|
296
|
-
};
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
// Move temp file to final path
|
|
300
|
-
try {
|
|
301
|
-
fs.renameSync(upload.tempPath, upload.finalPath);
|
|
302
|
-
} catch (err) {
|
|
303
|
-
cleanupTempFile(upload.tempPath);
|
|
304
|
-
activeUploads.delete(uploadId);
|
|
305
|
-
return {
|
|
306
|
-
success: false,
|
|
307
|
-
error: createError(ErrorCodes.INTERNAL_ERROR, `Failed to finalize file: ${err.message}`)
|
|
308
|
-
};
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
activeUploads.delete(uploadId);
|
|
312
|
-
|
|
313
|
-
return {
|
|
314
|
-
success: true,
|
|
315
|
-
complete: true,
|
|
316
|
-
filePath: upload.finalPath,
|
|
317
|
-
totalBytes: upload.receivedBytes,
|
|
318
|
-
mimeType: upload.mimeType
|
|
319
|
-
};
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
/**
|
|
323
|
-
* Cancel an upload and clean up
|
|
324
|
-
*/
|
|
325
|
-
function cancelUpload(uploadId) {
|
|
326
|
-
const upload = activeUploads.get(uploadId);
|
|
327
|
-
if (!upload) {
|
|
328
|
-
return { success: true }; // Already gone
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
clearTimeout(upload.timer);
|
|
332
|
-
cleanupTempFile(upload.tempPath);
|
|
333
|
-
activeUploads.delete(uploadId);
|
|
334
|
-
|
|
335
|
-
return { success: true };
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
function cleanupTempFile(tempPath) {
|
|
339
|
-
try {
|
|
340
|
-
if (fs.existsSync(tempPath)) {
|
|
341
|
-
fs.unlinkSync(tempPath);
|
|
342
|
-
}
|
|
343
|
-
} catch (e) {
|
|
344
|
-
console.error('Failed to cleanup temp file:', e.message);
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
/**
|
|
349
|
-
* Get count of active uploads (for monitoring)
|
|
350
|
-
*/
|
|
351
|
-
function getActiveUploadCount() {
|
|
352
|
-
return activeUploads.size;
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
module.exports = {
|
|
356
|
-
startUpload,
|
|
357
|
-
receiveChunk,
|
|
358
|
-
cancelUpload,
|
|
359
|
-
completeUpload,
|
|
360
|
-
getActiveUploadCount,
|
|
361
|
-
sanitizeFilename,
|
|
362
|
-
resolveFilename,
|
|
363
|
-
MAX_FILE_SIZE,
|
|
364
|
-
MAX_ACTIVE_UPLOADS,
|
|
365
|
-
ALLOWED_MIME_TYPES
|
|
366
|
-
};
|
|
1
|
+
module.exports = require('../shared/server-core/uploadService');
|