// server.js (Improved Version B) require('dotenv').config(); const express = require('express'); const path = require('path'); const bodyParser = require('body-parser'); const http = require('http'); const WebSocket = require('ws'); const { spawn } = require('child_process'); const indexRouter = require('./routes/index'); const app = express(); const PORT = process.env.PORT || 3000; // View engine app.set('view engine', 'ejs'); app.set('views', path.join(__dirname, 'views')); // Middleware app.use(express.static(path.join(__dirname, 'public'))); app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); // Routes app.use('/', indexRouter); // Error handler app.use((err, req, res, next) => { console.error(err); res.status(500).render('error', { message: 'Internal Server Error', detail: err.message }); }); // ----------------------------- // HTTP + WebSocket server // ----------------------------- const server = http.createServer(app); const wss = new WebSocket.Server({ server }); // Heartbeat for detecting dead connections function heartbeat() { this.isAlive = true; } wss.on('connection', ws => { ws.isAlive = true; ws.on('pong', heartbeat); let child = null; ws.send(JSON.stringify({ type: 'connected', message: 'ws ready' })); ws.on('message', msg => { let payload; try { payload = JSON.parse(msg.toString()); } catch { ws.send(JSON.stringify({ type: 'error', error: 'Invalid JSON' })); return; } // ----------------------------- // START LOGS // ----------------------------- if (payload.type === 'logs') { const name = payload.name || null; // Clean previous child if (child) { try { child.kill(); } catch {} child = null; } // Correct PM2 argument ordering const args = ['logs', '--raw', '--lines', '200']; if (name) args.splice(1, 0, name); try { child = spawn('pm2', args); child.stdout.on('data', data => { ws.send(JSON.stringify({ type: 'stdout', data: data.toString() })); }); child.stderr.on('data', data => { ws.send(JSON.stringify({ type: 'stderr', data: data.toString() })); }); child.on('close', code => { ws.send(JSON.stringify({ type: 'close', code })); child = null; }); child.on('error', err => { ws.send(JSON.stringify({ type: 'error', error: err.message })); child = null; }); ws.send(JSON.stringify({ type: 'info', message: `started logs for ${name || 'all'}` })); } catch (err) { ws.send(JSON.stringify({ type: 'error', error: err.message })); } return; } // ----------------------------- // STOP LOGS // ----------------------------- if (payload.type === 'stop-logs') { if (child) { try { child.kill(); } catch {} child = null; } ws.send(JSON.stringify({ type: 'info', message: 'logs stopped' })); return; } ws.send(JSON.stringify({ type: 'error', error: 'Unknown type' })); }); ws.on('close', () => { if (child) { try { child.kill(); } catch {} child = null; } }); }); // Ping-pong interval every 30s setInterval(() => { wss.clients.forEach(ws => { if (!ws.isAlive) return ws.terminate(); ws.isAlive = false; ws.ping(); // triggers 'pong' }); }, 30000); // Start server server.listen(PORT, () => console.log(`Proxer-Downloader running on http://localhost:${PORT}`) );