-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
410 lines (344 loc) · 10.7 KB
/
server.js
File metadata and controls
410 lines (344 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const cors = require('cors');
const pty = require('node-pty');
const multer = require('multer');
const path = require('path');
const os = require('os');
const fs = require('fs');
const { v4: uuidv4 } = require('uuid');
const app = express();
const server = http.createServer(app);
const io = socketIo(server, {
cors: {
origin: "*",
methods: ["GET", "POST"]
}
});
const PORT = process.env.PORT || 3000;
app.use(cors());
app.use(express.static(__dirname));
app.use(express.json({ limit: '50mb' }));
// Session management
const sessions = new Map();
const BUFFER_MAX_LINES = 1000; // Keep last 1000 lines per session
// Ensure uploads directory exists in home directory
const uploadsDir = path.join(os.homedir(), 'wt_upload');
if (!fs.existsSync(uploadsDir)) {
fs.mkdirSync(uploadsDir, { recursive: true });
}
// Configure multer for file uploads
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, uploadsDir);
},
filename: function (req, file, cb) {
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);
const ext = path.extname(file.originalname);
const name = path.basename(file.originalname, ext);
const safeName = name
.replace(/[^a-zA-Z0-9._-]/g, '_')
.replace(/_{2,}/g, '_')
.replace(/^_|_$/g, '');
const finalName = safeName || 'file';
cb(null, `${uniqueSuffix}-${finalName}${ext}`);
}
});
const upload = multer({
storage: storage,
limits: {
fileSize: 100 * 1024 * 1024 // 100MB limit
}
});
// Session class
class TerminalSession {
constructor(id, name = null) {
this.id = id;
this.name = name || `Session ${sessions.size + 1}`;
this.createdAt = new Date();
this.lastAccessedAt = new Date();
this.buffer = [];
this.subscribedSockets = new Set(); // Changed from single socket to Set of sockets
this.ptyProcess = null;
this.isActive = false;
this.initPty();
}
initPty() {
const shell = os.platform() === 'win32' ? 'powershell.exe' : 'bash';
this.ptyProcess = pty.spawn(shell, [], {
name: 'xterm-color',
cols: 80,
rows: 30,
cwd: os.homedir(), // Start in home directory
env: {
...process.env,
TERM: 'xterm-256color'
}
});
this.isActive = true;
// Store output in buffer
this.ptyProcess.onData((data) => {
// Add to buffer
this.buffer.push({
data: data,
timestamp: new Date()
});
// Limit buffer size
if (this.buffer.length > BUFFER_MAX_LINES) {
this.buffer = this.buffer.slice(-BUFFER_MAX_LINES);
}
// Send to all subscribed clients with session ID
this.subscribedSockets.forEach(socket => {
socket.emit('terminal-data', {
sessionId: this.id,
data: data
});
});
});
this.ptyProcess.onExit((exitCode) => {
console.log(`Session ${this.id} PTY process exited with code: ${exitCode}`);
this.isActive = false;
// Notify all subscribed clients
this.subscribedSockets.forEach(socket => {
socket.emit('terminal-exit', {
sessionId: this.id,
exitCode: exitCode
});
});
});
}
subscribe(socket) {
// Check if already subscribed
const wasSubscribed = this.subscribedSockets.has(socket);
// Add socket to subscribers
this.subscribedSockets.add(socket);
this.lastAccessedAt = new Date();
// Only send buffer history if this is a new subscription
if (!wasSubscribed) {
const recentBuffer = this.buffer.slice(-BUFFER_MAX_LINES); // Send full buffer
socket.emit('buffer-history', {
sessionId: this.id,
data: recentBuffer.map(b => b.data).join('')
});
}
}
unsubscribe(socket) {
this.subscribedSockets.delete(socket);
}
write(data) {
if (this.ptyProcess && this.isActive) {
this.ptyProcess.write(data);
this.lastAccessedAt = new Date();
}
}
resize(cols, rows) {
if (this.ptyProcess && this.isActive) {
this.ptyProcess.resize(cols, rows);
}
}
rename(newName) {
this.name = newName;
this.lastAccessedAt = new Date();
}
terminate() {
if (this.ptyProcess) {
this.ptyProcess.kill();
this.isActive = false;
}
// Notify all subscribers
this.subscribedSockets.forEach(socket => {
socket.emit('session-terminated', { sessionId: this.id });
});
this.subscribedSockets.clear();
}
toJSON() {
return {
id: this.id,
name: this.name,
createdAt: this.createdAt,
lastAccessedAt: this.lastAccessedAt,
isActive: this.isActive,
subscriberCount: this.subscribedSockets.size
};
}
}
// Socket.io connection handling
io.on('connection', (socket) => {
console.log('New client connected:', socket.id);
// List all sessions
socket.on('list-sessions', () => {
const sessionList = Array.from(sessions.values()).map(s => s.toJSON());
socket.emit('sessions-list', sessionList);
});
// Create new session
socket.on('create-session', (data) => {
const sessionId = uuidv4();
const session = new TerminalSession(sessionId, data?.name);
sessions.set(sessionId, session);
session.subscribe(socket);
socket.emit('session-created', {
sessionId: sessionId,
sessionInfo: session.toJSON()
});
// Broadcast to all clients that a new session was created
io.emit('session-list-updated');
console.log(`Created new session: ${sessionId} (${session.name})`);
});
// Subscribe to session
socket.on('subscribe-to-session', (sessionId) => {
const session = sessions.get(sessionId);
if (!session) {
socket.emit('session-not-found', { sessionId });
return;
}
if (!session.isActive) {
socket.emit('session-inactive', { sessionId });
return;
}
session.subscribe(socket);
socket.emit('session-subscribed', {
sessionId: sessionId,
sessionInfo: session.toJSON()
});
console.log(`Client ${socket.id} subscribed to session ${sessionId}`);
});
// Request buffer for a session (used when switching to already-subscribed session)
socket.on('request-session-buffer', (sessionId) => {
const session = sessions.get(sessionId);
if (session && session.subscribedSockets.has(socket)) {
const recentBuffer = session.buffer.slice(-BUFFER_MAX_LINES);
socket.emit('buffer-history', {
sessionId: session.id,
data: recentBuffer.map(b => b.data).join('')
});
}
});
// Unsubscribe from session
socket.on('unsubscribe-from-session', (sessionId) => {
const session = sessions.get(sessionId);
if (session) {
session.unsubscribe(socket);
console.log(`Client ${socket.id} unsubscribed from session ${sessionId}`);
}
});
// Rename session
socket.on('rename-session', ({ sessionId, newName }) => {
const session = sessions.get(sessionId);
if (session) {
session.rename(newName);
socket.emit('session-renamed', {
sessionId: sessionId,
newName: newName
});
// Broadcast to all clients
io.emit('session-list-updated');
}
});
// Terminal input
socket.on('terminal-input', ({ sessionId, data }) => {
const session = sessions.get(sessionId);
if (session) {
session.write(data);
}
});
// Terminal paste - handle multiline input
socket.on('terminal-paste', ({ sessionId, data }) => {
const session = sessions.get(sessionId);
if (session) {
session.write(data);
}
});
// Terminal resize
socket.on('terminal-resize', ({ sessionId, cols, rows }) => {
const session = sessions.get(sessionId);
if (session) {
session.resize(cols, rows);
}
});
// Terminate session
socket.on('terminate-session', (sessionId) => {
const session = sessions.get(sessionId);
if (session) {
session.terminate();
sessions.delete(sessionId);
socket.emit('session-terminated', { sessionId });
// Broadcast to all clients
io.emit('session-list-updated');
console.log(`Session terminated: ${sessionId}`);
}
});
// Handle disconnect
socket.on('disconnect', () => {
console.log('Client disconnected:', socket.id);
// Unsubscribe from all sessions
for (const session of sessions.values()) {
session.unsubscribe(socket);
}
});
});
// File upload endpoint
app.post('/upload', upload.single('file'), (req, res) => {
try {
if (!req.file) {
return res.status(400).json({ error: 'No file uploaded' });
}
// Return path as ~/wt_upload/filename
const fileName = path.basename(req.file.path);
const filePath = `~/wt_upload/${fileName}`;
console.log(`File uploaded: ${req.file.originalname} -> ${filePath}`);
res.json({
success: true,
path: filePath,
originalName: req.file.originalname,
size: req.file.size,
mimetype: req.file.mimetype
});
} catch (error) {
console.error('Upload error:', error);
res.status(500).json({ error: 'Upload failed: ' + error.message });
}
});
// List uploaded files
app.get('/uploads', (req, res) => {
try {
const files = fs.readdirSync(uploadsDir).map(filename => {
const filepath = path.join(uploadsDir, filename);
const stats = fs.statSync(filepath);
return {
name: filename,
path: `~/wt_upload/${filename}`,
size: stats.size,
modified: stats.mtime
};
});
res.json(files);
} catch (error) {
res.status(500).json({ error: 'Failed to list files: ' + error.message });
}
});
// Delete uploaded file
app.delete('/uploads/:filename', (req, res) => {
try {
const filename = req.params.filename;
const filepath = path.join(uploadsDir, filename);
if (!fs.existsSync(filepath)) {
return res.status(404).json({ error: 'File not found' });
}
fs.unlinkSync(filepath);
console.log(`File deleted: ${filename}`);
res.json({ success: true, message: 'File deleted' });
} catch (error) {
res.status(500).json({ error: 'Failed to delete file: ' + error.message });
}
});
// Serve main page
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
// Auto-cleanup removed - manage sessions manually
server.listen(PORT, () => {
console.log(`WebTerm Server running on http://localhost:${PORT}`);
console.log(`Uploads directory: ~/wt_upload/ (${uploadsDir})`);
console.log('Features: Persistent sessions, Multi-session support, Session management');
});