-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUnifiedWebServer.js
More file actions
566 lines (471 loc) · 18.8 KB
/
Copy pathUnifiedWebServer.js
File metadata and controls
566 lines (471 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
const express = require('express');
const path = require('path');
const fs = require('fs').promises;
const { exec } = require('child_process');
const { promisify } = require('util');
const WebServerSecurity = require('./WebServerSecurity');
const SmartPortResolver = require('./SmartPortResolver');
const QTunnel = require('./QTunnel');
const execAsync = promisify(exec);
/**
* Unified Web Server - Single server for all Mini App pages (File Browser + Git Diff)
* Replaces separate FileBrowserServer and GitDiffServer with unified approach
*
* TO DISABLE: Pass { disabled: true } in options parameter to constructor
* Example: new UnifiedWebServer(projectRoot, botInstance, security, { disabled: true })
*/
class UnifiedWebServer {
constructor(projectRoot = process.cwd(), botInstance = 'bot1', security = null, options = {}) {
this.app = express();
this.server = null;
this.projectRoot = projectRoot;
this.botInstance = botInstance;
this.port = null;
this.isStarting = false;
this.isStarted = false;
// Flag to disable web server functionality entirely
this.disabled = options.disabled || false;
// Use provided security system or create new one
this.security = security || new WebServerSecurity(botInstance);
// Smart port resolver with large random range (8000-9999)
this.portResolver = new SmartPortResolver({
minPort: 8000,
maxPort: 9999,
maxAttempts: 50
});
// QTunnel adapter - HTTP/2 for better network resilience
this.tunnelAdapter = new QTunnel({
protocol: options.qTunnelProtocol || 'http2',
token: options.qTunnelToken || null,
botInstance: botInstance
});
this.publicUrl = null;
this.setupRoutes();
}
setupRoutes() {
// Apply security middleware to all routes
this.app.use(this.security.getMiddleware());
// Serve static assets
this.app.use('/static', express.static(path.join(__dirname, 'public')));
// Configure EJS as template engine
this.app.set('view engine', 'ejs');
this.app.set('views', path.join(__dirname, 'views'));
// ========== MAIN MENU (Vue.js Default) ==========
this.app.get('/', async (req, res) => {
res.render('v2/main-menu', {
botInstance: this.botInstance,
currentVersion: 'vue'
});
});
// ========== FILE BROWSER ROUTES (Vue.js) ==========
this.app.get('/files', async (req, res) => {
const currentPath = req.query.path || '';
const fullPath = path.join(this.projectRoot, currentPath);
try {
await this.validatePath(fullPath);
const content = await this.generateFileList(fullPath, currentPath);
res.render('v2/file-browser-simple', {
...content,
currentPath,
botInstance: this.botInstance
});
} catch (error) {
res.status(404).render('v2/error', {
message: error.message,
botInstance: this.botInstance
});
}
});
this.app.get('/files/view', async (req, res) => {
const filePath = req.query.path;
if (!filePath) {
return res.status(400).render('v2/error', {
message: 'File path required',
botInstance: this.botInstance
});
}
const fullPath = path.join(this.projectRoot, filePath);
try {
await this.validatePath(fullPath);
const stats = await fs.stat(fullPath);
if (stats.isDirectory()) {
return res.redirect(`/files?path=${encodeURIComponent(filePath)}`);
}
const content = await fs.readFile(fullPath, 'utf8');
const fileExtension = path.extname(fullPath).toLowerCase();
res.render('v2/file-viewer-simple', {
content,
filePath,
fileExtension,
language: this.getLanguageForHighlighting(fileExtension),
botInstance: this.botInstance
});
} catch (error) {
res.status(404).render('v2/error', {
message: error.message,
botInstance: this.botInstance
});
}
});
// ========== GIT DIFF ROUTES (Vue.js) ==========
this.app.get('/git', async (req, res) => {
try {
const changedFiles = await this.getChangedFiles();
res.render('v2/git-diff-simple', {
files: changedFiles,
botInstance: this.botInstance
});
} catch (error) {
res.status(500).render('v2/error', {
message: error.message,
botInstance: this.botInstance
});
}
});
this.app.get('/git/diff', async (req, res) => {
const filePath = req.query.file;
if (!filePath) {
return res.status(400).render('v2/error', {
message: 'File path required',
botInstance: this.botInstance
});
}
try {
const diff = await this.getFileDiff(filePath);
const highlightedDiff = this.highlightDiff(diff);
res.render('v2/diff-viewer-simple', {
diff: highlightedDiff,
filePath,
botInstance: this.botInstance
});
} catch (error) {
res.status(500).render('v2/error', {
message: error.message,
botInstance: this.botInstance
});
}
});
this.app.get('/git/status', async (req, res) => {
try {
const status = await this.getGitStatus();
res.render('v2/git-status-simple', {
status,
botInstance: this.botInstance
});
} catch (error) {
res.status(500).render('v2/error', {
message: error.message,
botInstance: this.botInstance
});
}
});
// ========== INFO ROUTE (Vue.js) ==========
this.app.get('/info', async (req, res) => {
const stats = this.tunnelAdapter.getTunnelStats();
res.render('v2/info-simple', {
stats,
botInstance: this.botInstance
});
});
}
// ========== PATH VALIDATION ==========
async validatePath(fullPath) {
const resolvedPath = path.resolve(fullPath);
const resolvedRoot = path.resolve(this.projectRoot);
if (!resolvedPath.startsWith(resolvedRoot)) {
throw new Error('Access denied: Path outside project directory');
}
await fs.access(resolvedPath);
}
// ========== FILE BROWSER FUNCTIONALITY ==========
async generateFileList(dirPath, currentPath) {
const items = await fs.readdir(dirPath, { withFileTypes: true });
const files = [];
const directories = [];
for (const item of items) {
const itemPath = path.join(currentPath, item.name);
const fullItemPath = path.join(dirPath, item.name);
if (item.name.startsWith('.') || item.name === 'node_modules') {
continue;
}
try {
const stats = await fs.stat(fullItemPath);
const size = stats.isFile() ? this.formatFileSize(stats.size) : '-';
const modified = stats.mtime.toLocaleDateString();
if (item.isDirectory()) {
directories.push({
name: item.name,
path: itemPath,
type: 'directory',
size,
modified
});
} else {
files.push({
name: item.name,
path: itemPath,
type: 'file',
size,
modified
});
}
} catch (error) {
continue;
}
}
return { directories, files, currentPath };
}
formatFileSize(bytes) {
if (bytes === 0) return '0 B';
const k = 1024;
const sizes = ['B', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + ' ' + sizes[i];
}
getFileIcon(filename) {
const ext = path.extname(filename).toLowerCase();
const iconMap = {
'.js': '📄', '.json': '📄', '.md': '📝', '.txt': '📄',
'.html': '🌐', '.css': '🎨', '.png': '🖼️', '.jpg': '🖼️',
'.jpeg': '🖼️', '.gif': '🖼️', '.pdf': '📕', '.zip': '📦',
'.log': '📊', '.yml': '⚙️', '.yaml': '⚙️', '.xml': '📄'
};
return iconMap[ext] || '📄';
}
// ========== GIT FUNCTIONALITY ==========
async getChangedFiles() {
try {
const { stdout } = await execAsync('git status --porcelain', {
cwd: this.projectRoot,
maxBuffer: 1024 * 1024
});
const files = [];
const lines = stdout.trim().split('\n').filter(line => line.length > 0);
for (const line of lines) {
const status = line.substring(0, 2);
const filePath = line.substring(3);
let changeType = 'unknown';
let icon = '📄';
if (status.includes('M')) {
changeType = 'modified';
icon = '📝';
} else if (status.includes('A')) {
changeType = 'added';
icon = '➕';
} else if (status.includes('D')) {
changeType = 'deleted';
icon = '🗑️';
} else if (status.includes('R')) {
changeType = 'renamed';
icon = '🔄';
} else if (status.includes('??')) {
changeType = 'untracked';
icon = '❓';
}
files.push({
path: filePath,
status: status.trim(),
changeType,
icon
});
}
return files;
} catch (error) {
console.error('Error getting changed files:', error);
return [];
}
}
async getFileDiff(filePath) {
try {
let cmd = `git diff HEAD -- "${filePath}"`;
try {
const { stdout: staged } = await execAsync(`git diff --cached --name-only "${filePath}"`, {
cwd: this.projectRoot
});
if (staged.trim()) {
cmd = `git diff --cached -- "${filePath}"`;
}
} catch (e) {
// File might be untracked
}
const { stdout } = await execAsync(cmd, {
cwd: this.projectRoot,
maxBuffer: 1024 * 1024
});
if (!stdout.trim()) {
try {
const { stdout: content } = await execAsync(`cat "${filePath}"`, {
cwd: this.projectRoot,
maxBuffer: 1024 * 1024
});
return this.formatAsNewFile(content, filePath);
} catch (e) {
return 'No diff available for this file';
}
}
return stdout;
} catch (error) {
console.error('Error getting file diff:', error);
return `Error getting diff: ${error.message}`;
}
}
formatAsNewFile(content, filePath) {
const lines = content.split('\n');
let diff = `diff --git a/${filePath} b/${filePath}\n`;
diff += `new file mode 100644\n`;
diff += `index 0000000..0000000\n`;
diff += `--- /dev/null\n`;
diff += `+++ b/${filePath}\n`;
diff += `@@ -0,0 +1,${lines.length} @@\n`;
for (const line of lines) {
diff += `+${line}\n`;
}
return diff;
}
async getGitStatus() {
try {
const { stdout } = await execAsync('git status', {
cwd: this.projectRoot,
maxBuffer: 1024 * 1024
});
return stdout;
} catch (error) {
return `Error getting git status: ${error.message}`;
}
}
// ========== UTILITY METHODS ==========
getLanguageForHighlighting(extension) {
const langMap = {
'.js': 'javascript', '.json': 'json', '.md': 'markdown',
'.html': 'html', '.css': 'css', '.yml': 'yaml',
'.yaml': 'yaml', '.xml': 'xml', '.sh': 'bash'
};
return langMap[extension] || 'text';
}
highlightDiff(diff) {
const lines = diff.split('\n');
let html = '';
let lineNumber = 1;
for (const line of lines) {
let className = 'context';
if (line.startsWith('+++') || line.startsWith('---') || line.startsWith('diff --git')) {
className = 'header';
} else if (line.startsWith('@@')) {
className = 'hunk';
} else if (line.startsWith('+')) {
className = 'added';
} else if (line.startsWith('-')) {
className = 'removed';
}
const escapedLine = this.escapeHtml(line);
html += `<div class="diff-line ${className}"><span class="line-number">${lineNumber}</span>${escapedLine}</div>\n`;
lineNumber++;
}
return html;
}
escapeHtml(text) {
const map = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
return text.replace(/[&<>"']/g, m => map[m]);
}
// ========== PROJECT ROOT MANAGEMENT ==========
updateProjectRoot(newProjectRoot) {
if (newProjectRoot && newProjectRoot !== this.projectRoot) {
const oldRoot = this.projectRoot;
this.projectRoot = newProjectRoot;
console.log(`[${this.botInstance}] Updated UnifiedWebServer project root: ${oldRoot} → ${newProjectRoot}`);
}
}
// ========== SERVER MANAGEMENT ==========
async findAvailablePort() {
return await this.portResolver.findAvailablePort(`Unified-${this.botInstance}`);
}
async start() {
try {
// Early return if web server is disabled
if (this.disabled) {
console.log(`[${this.botInstance}] Unified web server is DISABLED - skipping startup`);
return null;
}
if (this.isStarting) {
console.log(`[${this.botInstance}] Unified web server is already starting...`);
return null;
}
if (this.isStarted) {
console.log(`[${this.botInstance}] Unified web server is already running on port ${this.port}`);
return this.publicUrl;
}
this.isStarting = true;
console.log(`[${this.botInstance}] Starting unified web server...`);
if (!this.port) {
this.port = await this.findAvailablePort();
}
this.server = this.app.listen(this.port, 'localhost', () => {
console.log(`[${this.botInstance}] 🚀 Unified web server running on http://localhost:${this.port}`);
});
const localUrl = `http://localhost:${this.port}`;
// Create QTunnel WebSocket tunnel
try {
console.log(`[${this.botInstance}] Creating QTunnel...`);
const publicUrl = await this.tunnelAdapter.createTunnel(this.port, 'unified');
this.publicUrl = publicUrl;
console.log(`[${this.botInstance}] ✅ Unified server public URL: ${publicUrl}`);
} catch (tunnelError) {
console.log(`[${this.botInstance}] ⚠️ QTunnel failed, using local access only: ${tunnelError.message}`);
this.publicUrl = localUrl;
}
this.isStarted = true;
this.isStarting = false;
return this.publicUrl;
} catch (error) {
this.isStarting = false;
console.error(`[${this.botInstance}] Failed to start unified web server:`, error);
throw error;
}
}
getSecurePublicUrl() {
// Return null if web server is disabled
if (this.disabled) {
return null;
}
if (!this.publicUrl || !this.isStarted) {
return null;
}
// Return direct URL since we use QTunnel
const secureUrl = this.security.secureExternalUrl(this.publicUrl, '', {});
return secureUrl;
}
async stop() {
try {
console.log(`[${this.botInstance}] Stopping unified web server...`);
// Close all QTunnels
await this.tunnelAdapter.closeAllTunnels();
if (this.server) {
await new Promise((resolve) => {
this.server.close(resolve);
});
this.server = null;
console.log(`[${this.botInstance}] Unified web server stopped`);
}
// Release port allocation
if (this.port) {
this.portResolver.releasePort(this.port, `Unified-${this.botInstance}`);
}
this.isStarted = false;
this.isStarting = false;
this.port = null;
this.publicUrl = null;
} catch (error) {
console.error(`[${this.botInstance}] Error stopping unified web server:`, error);
this.isStarted = false;
this.isStarting = false;
throw error;
}
}
}
module.exports = UnifiedWebServer;