-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
592 lines (518 loc) · 18.8 KB
/
server.js
File metadata and controls
592 lines (518 loc) · 18.8 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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
const express = require('express');
const http = require('http');
const { WebSocketServer } = require('ws');
const pty = require('node-pty');
const crypto = require('crypto');
const { execSync } = require('child_process');
const path = require('path');
const os = require('os');
const fs = require('fs');
const Database = require('better-sqlite3');
const bcrypt = require('bcryptjs');
const multer = require('multer');
const HOME_DIR = os.homedir();
const DATA_DIR = path.join(__dirname, 'data');
// Ensure data directory exists
fs.mkdirSync(DATA_DIR, { recursive: true });
// --- SQLite setup ---
const db = new Database(path.join(DATA_DIR, 'webshell.db'));
db.pragma('journal_mode = WAL');
db.pragma('foreign_keys = ON');
db.exec(`
CREATE TABLE IF NOT EXISTS settings (
key TEXT PRIMARY KEY,
value TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS projects (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE,
created_at INTEGER NOT NULL DEFAULT (strftime('%s','now'))
);
CREATE TABLE IF NOT EXISTS sessions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
project_id INTEGER NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
name TEXT NOT NULL,
tmux_name TEXT NOT NULL UNIQUE,
created_at INTEGER NOT NULL DEFAULT (strftime('%s','now'))
);
CREATE TABLE IF NOT EXISTS auth_tokens (
token TEXT PRIMARY KEY,
created_at INTEGER NOT NULL DEFAULT (strftime('%s','now'))
);
`);
// Migration: add directory column to projects
try {
db.exec("ALTER TABLE projects ADD COLUMN directory TEXT DEFAULT NULL");
} catch {
// column already exists
}
function getPasswordHash() {
const row = db.prepare("SELECT value FROM settings WHERE key = 'password_hash'").get();
return row ? row.value : null;
}
function setPasswordHash(hash) {
db.prepare("INSERT OR REPLACE INTO settings (key, value) VALUES ('password_hash', ?)").run(hash);
}
function isSetupDone() {
return getPasswordHash() !== null;
}
// --- Telegram ---
function getTelegramConfig() {
const botToken = db.prepare("SELECT value FROM settings WHERE key = 'telegram_bot_token'").get();
const chatId = db.prepare("SELECT value FROM settings WHERE key = 'telegram_chat_id'").get();
return {
bot_token: botToken?.value || '',
chat_id: chatId?.value || '',
configured: !!(botToken?.value && chatId?.value),
};
}
async function sendTelegramMessage(text) {
const config = getTelegramConfig();
if (!config.configured) return false;
try {
const res = await fetch(
`https://api.telegram.org/bot${config.bot_token}/sendMessage`,
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ chat_id: config.chat_id, text, parse_mode: 'HTML' }),
}
);
return res.ok;
} catch {
return false;
}
}
const app = express();
const server = http.createServer(app);
const wss = new WebSocketServer({ noServer: true });
const PORT = process.env.PORT || 3000;
// --- Token management (SQLite-backed) ---
const tokens = {
add(token) {
db.prepare('INSERT OR IGNORE INTO auth_tokens (token) VALUES (?)').run(token);
},
has(token) {
return !!db.prepare('SELECT 1 FROM auth_tokens WHERE token = ?').get(token);
},
delete(token) {
db.prepare('DELETE FROM auth_tokens WHERE token = ?').run(token);
},
clear() {
db.prepare('DELETE FROM auth_tokens').run();
},
};
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public')));
// --- Auth ---
// Check if first-time setup is needed
app.get('/api/setup-status', (req, res) => {
res.json({ setup_done: isSetupDone() });
});
// First-time password setup
app.post('/api/setup', (req, res) => {
if (isSetupDone()) {
return res.status(403).json({ error: 'Password already set.' });
}
const { password } = req.body || {};
if (!password || typeof password !== 'string' || password.length < 6) {
return res.status(400).json({ error: 'Password must be at least 6 characters.' });
}
const hash = bcrypt.hashSync(password, 12);
setPasswordHash(hash);
const token = crypto.randomBytes(32).toString('hex');
tokens.add(token);
res.json({ token });
});
app.post('/api/login', (req, res) => {
if (!isSetupDone()) {
return res.status(403).json({ error: 'Setup not complete.' });
}
const { password } = req.body || {};
if (typeof password === 'string' && bcrypt.compareSync(password, getPasswordHash())) {
const token = crypto.randomBytes(32).toString('hex');
tokens.add(token);
res.json({ token });
} else {
res.status(401).json({ error: 'Wrong password' });
}
});
app.post('/api/logout', (req, res) => {
const token = req.headers.authorization?.replace('Bearer ', '');
tokens.delete(token);
res.json({ ok: true });
});
function authMiddleware(req, res, next) {
const token = req.headers.authorization?.replace('Bearer ', '');
if (token && tokens.has(token)) return next();
res.status(401).json({ error: 'Unauthorized' });
}
// Change password (requires auth)
app.post('/api/change-password', authMiddleware, (req, res) => {
const { current_password, new_password } = req.body || {};
if (!current_password || !new_password) {
return res.status(400).json({ error: 'Both current and new password required.' });
}
if (typeof new_password !== 'string' || new_password.length < 6) {
return res.status(400).json({ error: 'New password must be at least 6 characters.' });
}
if (!bcrypt.compareSync(current_password, getPasswordHash())) {
return res.status(401).json({ error: 'Current password is wrong.' });
}
const hash = bcrypt.hashSync(new_password, 12);
setPasswordHash(hash);
// Invalidate all tokens except the current one
const currentToken = req.headers.authorization?.replace('Bearer ', '');
tokens.clear();
tokens.add(currentToken);
res.json({ ok: true });
});
// --- Telegram API ---
app.get('/api/settings/telegram', authMiddleware, (req, res) => {
const config = getTelegramConfig();
res.json({
bot_token: config.bot_token ? '***' + config.bot_token.slice(-4) : '',
chat_id: config.chat_id,
configured: config.configured,
});
});
app.post('/api/settings/telegram', authMiddleware, (req, res) => {
const { bot_token, chat_id } = req.body || {};
if (bot_token !== undefined) {
if (bot_token) {
db.prepare("INSERT OR REPLACE INTO settings (key, value) VALUES ('telegram_bot_token', ?)").run(bot_token);
} else {
db.prepare("DELETE FROM settings WHERE key = 'telegram_bot_token'").run();
}
}
if (chat_id !== undefined) {
if (chat_id) {
db.prepare("INSERT OR REPLACE INTO settings (key, value) VALUES ('telegram_chat_id', ?)").run(String(chat_id));
} else {
db.prepare("DELETE FROM settings WHERE key = 'telegram_chat_id'").run();
}
}
res.json({ ok: true, configured: getTelegramConfig().configured });
});
app.post('/api/settings/telegram/test', authMiddleware, async (req, res) => {
const ok = await sendTelegramMessage('🔔 Webshell test notification');
res.json({ ok });
});
// Notify endpoint (no auth — only accessible from localhost)
app.post('/api/notify', async (req, res) => {
const { message, session, command, duration } = req.body || {};
const cmdDisplay = command ? command.substring(0, 200) : '';
const text = message ||
`✅ Task completed in <b>${session || 'unknown'}</b>\n<code>${cmdDisplay}</code>\nDuration: ${duration || '?'}s`;
const ok = await sendTelegramMessage(text);
res.json({ ok });
});
// --- Validation ---
function isValidName(name) {
return /^[a-zA-Z0-9_-]{1,50}$/.test(name);
}
// --- tmux helpers ---
function getTmuxSessions() {
try {
const output = execSync(
'tmux list-sessions -F "#{session_name}||#{session_attached}"',
{ encoding: 'utf8', timeout: 5000 }
);
const map = {};
output.trim().split('\n').filter(Boolean).forEach(line => {
const [name, attached] = line.split('||');
map[name] = { attached: parseInt(attached, 10) > 0 };
});
return map;
} catch {
return {};
}
}
function killTmuxSession(tmuxName) {
try {
execSync(`tmux kill-session -t '${tmuxName}'`, { timeout: 5000 });
} catch {
// session may already be dead
}
}
// --- Projects API ---
app.get('/api/projects', authMiddleware, (req, res) => {
const projects = db.prepare(`
SELECT p.id, p.name, p.directory, p.created_at,
COUNT(s.id) as session_count
FROM projects p
LEFT JOIN sessions s ON s.project_id = p.id
GROUP BY p.id
ORDER BY p.created_at DESC
`).all();
res.json(projects);
});
app.post('/api/projects', authMiddleware, (req, res) => {
const { name, directory } = req.body || {};
if (!name || !isValidName(name)) {
return res.status(400).json({ error: 'Invalid name. Use alphanumeric, dash, underscore only.' });
}
if (directory && !fs.existsSync(directory)) {
return res.status(400).json({ error: 'Directory does not exist.' });
}
try {
const result = db.prepare('INSERT INTO projects (name, directory) VALUES (?, ?)').run(name, directory || null);
res.json({ id: result.lastInsertRowid, name, directory: directory || null });
} catch {
res.status(400).json({ error: 'Project already exists.' });
}
});
app.put('/api/projects/:id', authMiddleware, (req, res) => {
const id = parseInt(req.params.id, 10);
if (isNaN(id)) return res.status(400).json({ error: 'Invalid project id.' });
const project = db.prepare('SELECT * FROM projects WHERE id = ?').get(id);
if (!project) return res.status(404).json({ error: 'Project not found.' });
const { directory } = req.body || {};
if (directory !== undefined) {
if (directory && !fs.existsSync(directory)) {
return res.status(400).json({ error: 'Directory does not exist.' });
}
db.prepare('UPDATE projects SET directory = ? WHERE id = ?').run(directory || null, id);
}
res.json({ ok: true });
});
app.delete('/api/projects/:id', authMiddleware, (req, res) => {
const id = parseInt(req.params.id, 10);
if (isNaN(id)) return res.status(400).json({ error: 'Invalid project id.' });
// Kill all tmux sessions for this project
const sessions = db.prepare('SELECT tmux_name FROM sessions WHERE project_id = ?').all(id);
for (const s of sessions) {
killTmuxSession(s.tmux_name);
}
const result = db.prepare('DELETE FROM projects WHERE id = ?').run(id);
if (result.changes === 0) return res.status(404).json({ error: 'Project not found.' });
res.json({ ok: true });
});
// --- Sessions API (project-scoped) ---
app.get('/api/projects/:id/sessions', authMiddleware, (req, res) => {
const projectId = parseInt(req.params.id, 10);
if (isNaN(projectId)) return res.status(400).json({ error: 'Invalid project id.' });
const sessions = db.prepare(
'SELECT id, name, tmux_name, created_at FROM sessions WHERE project_id = ? ORDER BY created_at DESC'
).all(projectId);
const tmux = getTmuxSessions();
const result = sessions.map(s => ({
...s,
attached: tmux[s.tmux_name]?.attached || false,
alive: s.tmux_name in tmux,
}));
res.json(result);
});
app.post('/api/projects/:id/sessions', authMiddleware, (req, res) => {
const projectId = parseInt(req.params.id, 10);
if (isNaN(projectId)) return res.status(400).json({ error: 'Invalid project id.' });
const project = db.prepare('SELECT name, directory FROM projects WHERE id = ?').get(projectId);
if (!project) return res.status(404).json({ error: 'Project not found.' });
const name = req.body.name || `s-${Date.now()}`;
if (!isValidName(name)) {
return res.status(400).json({ error: 'Invalid name. Use alphanumeric, dash, underscore only.' });
}
const tmuxName = `${project.name}--${name}`;
const cwd = project.directory || HOME_DIR;
try {
const result = db.prepare(
'INSERT INTO sessions (project_id, name, tmux_name) VALUES (?, ?, ?)'
).run(projectId, name, tmuxName);
execSync(`tmux new-session -d -s '${tmuxName}' -c '${cwd}'`, { timeout: 5000 });
res.json({ id: result.lastInsertRowid, name, tmux_name: tmuxName });
} catch (e) {
// Clean up DB row if tmux failed
db.prepare('DELETE FROM sessions WHERE tmux_name = ?').run(tmuxName);
res.status(400).json({ error: 'Session already exists or failed to create.' });
}
});
app.delete('/api/sessions/:id', authMiddleware, (req, res) => {
const id = parseInt(req.params.id, 10);
if (isNaN(id)) return res.status(400).json({ error: 'Invalid session id.' });
const session = db.prepare('SELECT tmux_name FROM sessions WHERE id = ?').get(id);
if (!session) return res.status(404).json({ error: 'Session not found.' });
killTmuxSession(session.tmux_name);
db.prepare('DELETE FROM sessions WHERE id = ?').run(id);
res.json({ ok: true });
});
// --- Files API ---
const upload = multer({ dest: path.join(DATA_DIR, 'uploads_tmp'), limits: { fileSize: 100 * 1024 * 1024 } });
function safePath(base, rel) {
const resolved = path.resolve(base, rel || '');
if (!resolved.startsWith(base)) return null;
return resolved;
}
app.get('/api/files', authMiddleware, (req, res) => {
const dir = req.query.dir;
if (!dir) return res.status(400).json({ error: 'dir is required' });
try {
const resolved = path.resolve(dir);
const entries = fs.readdirSync(resolved, { withFileTypes: true });
const items = [];
for (const e of entries) {
if (e.name.startsWith('.')) continue; // skip hidden by default
try {
const fullPath = path.join(resolved, e.name);
const stat = fs.statSync(fullPath);
items.push({
name: e.name,
type: e.isDirectory() ? 'dir' : 'file',
size: e.isDirectory() ? null : stat.size,
modified: Math.floor(stat.mtimeMs / 1000),
});
} catch { /* skip inaccessible */ }
}
// Sort: dirs first, then files, alphabetical
items.sort((a, b) => {
if (a.type !== b.type) return a.type === 'dir' ? -1 : 1;
return a.name.localeCompare(b.name, undefined, { sensitivity: 'base' });
});
res.json({ path: resolved, items, showHidden: false });
} catch (e) {
res.status(400).json({ error: 'Cannot read directory.' });
}
});
app.get('/api/files/read', authMiddleware, (req, res) => {
const filePath = req.query.path;
if (!filePath) return res.status(400).json({ error: 'path is required' });
try {
const resolved = path.resolve(filePath);
const stat = fs.statSync(resolved);
if (stat.size > 2 * 1024 * 1024) {
return res.status(413).json({ error: 'File too large to preview (>2MB). Use download.' });
}
// Check if binary
const buf = Buffer.alloc(512);
const fd = fs.openSync(resolved, 'r');
const bytesRead = fs.readSync(fd, buf, 0, 512, 0);
fs.closeSync(fd);
let isBinary = false;
for (let i = 0; i < bytesRead; i++) {
if (buf[i] === 0) { isBinary = true; break; }
}
if (isBinary) {
return res.json({ binary: true, size: stat.size });
}
const content = fs.readFileSync(resolved, 'utf8');
res.json({ content, size: stat.size });
} catch {
res.status(400).json({ error: 'Cannot read file.' });
}
});
app.get('/api/files/download', (req, res, next) => {
// Allow token via query param for direct links (img src, download)
const t = req.query.token || req.headers.authorization?.replace('Bearer ', '');
if (t && tokens.has(t)) return next();
res.status(401).json({ error: 'Unauthorized' });
}, (req, res) => {
const filePath = req.query.path;
if (!filePath) return res.status(400).json({ error: 'path is required' });
try {
const resolved = path.resolve(filePath);
if (!fs.existsSync(resolved) || fs.statSync(resolved).isDirectory()) {
return res.status(404).json({ error: 'File not found.' });
}
res.download(resolved);
} catch {
res.status(400).json({ error: 'Cannot download file.' });
}
});
app.post('/api/files/upload', authMiddleware, upload.array('files', 20), (req, res) => {
const targetDir = req.body.dir;
if (!targetDir) {
// Clean up temp files
(req.files || []).forEach(f => { try { fs.unlinkSync(f.path); } catch {} });
return res.status(400).json({ error: 'dir is required' });
}
try {
const resolved = path.resolve(targetDir);
if (!fs.existsSync(resolved) || !fs.statSync(resolved).isDirectory()) {
(req.files || []).forEach(f => { try { fs.unlinkSync(f.path); } catch {} });
return res.status(400).json({ error: 'Target directory does not exist.' });
}
const uploaded = [];
for (const f of (req.files || [])) {
const dest = path.join(resolved, f.originalname);
fs.renameSync(f.path, dest);
uploaded.push(f.originalname);
}
res.json({ ok: true, uploaded });
} catch (e) {
(req.files || []).forEach(f => { try { fs.unlinkSync(f.path); } catch {} });
res.status(500).json({ error: 'Upload failed.' });
}
});
// --- WebSocket terminal ---
server.on('upgrade', (req, socket, head) => {
const url = new URL(req.url, 'http://localhost');
if (url.pathname !== '/ws') {
socket.destroy();
return;
}
const token = url.searchParams.get('token');
if (!token || !tokens.has(token)) {
socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
socket.destroy();
return;
}
wss.handleUpgrade(req, socket, head, (ws) => {
wss.emit('connection', ws, req);
});
});
wss.on('connection', (ws, req) => {
const url = new URL(req.url, 'http://localhost');
const tmuxName = url.searchParams.get('session');
// Validate: tmux_name format is "project--session", allow double dash
if (!tmuxName || !/^[a-zA-Z0-9_-]{1,50}--[a-zA-Z0-9_-]{1,50}$/.test(tmuxName)) {
ws.close(1008, 'Invalid session');
return;
}
// Ensure tmux session exists
try {
execSync(`tmux has-session -t '${tmuxName}'`, { timeout: 5000 });
} catch {
try {
execSync(`tmux new-session -d -s '${tmuxName}' -c '${HOME_DIR}'`, { timeout: 5000 });
} catch {
ws.close(1011, 'Failed to create session');
return;
}
}
const term = pty.spawn('tmux', ['attach-session', '-t', tmuxName], {
name: 'xterm-256color',
cols: 80,
rows: 24,
cwd: HOME_DIR,
env: { ...process.env, TERM: 'xterm-256color' },
});
term.onData((data) => {
if (ws.readyState === 1) {
ws.send(JSON.stringify({ type: 'output', data }));
}
});
ws.on('message', (raw) => {
try {
const msg = JSON.parse(raw);
if (msg.type === 'ping') {
if (ws.readyState === 1) ws.send(JSON.stringify({ type: 'pong' }));
} else if (msg.type === 'input') {
term.write(msg.data);
} else if (msg.type === 'resize' && msg.cols > 0 && msg.rows > 0) {
term.resize(Math.min(msg.cols, 500), Math.min(msg.rows, 200));
}
} catch {
// ignore malformed messages
}
});
const cleanup = () => {
try { term.kill(); } catch {}
};
ws.on('close', cleanup);
ws.on('error', cleanup);
term.onExit(() => {
if (ws.readyState === 1) ws.close();
});
});
server.listen(PORT, '127.0.0.1', () => {
console.log(`Terminal server running on http://127.0.0.1:${PORT}`);
});