// server.js const express = require('express'); const http = require('http'); const WebSocket = require('ws'); const path = require('path'); const app = express(); const port = 7860; // Serve static files (if any) app.use(express.static('public')); // Data structures to store client data const clients = {}; // HTTP server const server = http.createServer(app); // WebSocket server const wss = new WebSocket.Server({ server }); // Handle new WebSocket connections wss.on('connection', (ws) => { const clientId = generateId(); clients[clientId] = { ws: ws, html: '', css: '', url: '' }; console.log(`Client ${clientId} connected`); ws.on('message', (message) => { try { const data = JSON.parse(message); if (data.type === 'sessionInfo') { if(data.sessionId){ clients[clientId].sessionId = data.sessionId; } } if (data.type === 'pageContent') { clients[clientId].html = data.html; clients[clientId].css = data.css; clients[clientId].url = data.url; } else if (data.type === 'userInteraction') { // Handle user interactions if needed console.log(`Interaction from ${clientId}:`, data); } } catch (e) { console.error('Error parsing message:', e); } }); ws.on('close', () => { console.log(`Client ${clientId} disconnected`); delete clients[clientId]; }); }); // Generate a unique client ID function generateId() { return Math.random().toString(36).substr(2, 9); } // Tailwind Play CDN for including Tailwind CSS const tailwindCDN = ``; // Route to list all active clients app.get('/', (req, res) => { // Get the list of active clients const clientKeys = Object.keys(clients); // If no clients are available if (clientKeys.length === 0) { return res.send(` ${tailwindCDN}

No Active Clients

`); } // Render the list of active clients using Tailwind let clientListHTML = ` ${tailwindCDN}

Active Clients

`; // Send the rendered HTML response res.send(clientListHTML); }); // Route to render the client's page app.get('/client/:id', (req, res) => { const client = clients[req.params.id]; if (client) { res.send(client.html); } else { res.status(404).send('Client not found'); } }); app.get('/clientCSS/:id', (req, res) => { const client = clients[req.params.id]; if (client) { let htmlContent = client.html; // Inject the CSS into a style tag in the head const cssTag = ``; htmlContent = htmlContent.replace('', `${cssTag}`); //remove all script tags htmlContent = htmlContent.replace(/)<[^<]*)*<\/script>/gi, ""); //enable copy paste htmlContent = htmlContent.replace(/-webkit-user-select: none;/gi, ""); htmlContent = htmlContent.replace(/user-select: none;/gi, ""); htmlContent = htmlContent.replace(/-moz-user-select: none;/gi, ""); htmlContent = htmlContent.replace(/-ms-user-select: none;/gi, ""); htmlContent = htmlContent.replace(/-o-user-select: none;/gi, ""); //enable right click htmlContent = htmlContent.replace(/oncontextmenu="return false;"/gi, ""); htmlContent = htmlContent.replace(/onselectstart="return false;"/gi, ""); htmlContent = htmlContent.replace(/onselect="return false;"/gi, ""); htmlContent = htmlContent.replace(/ondragstart="return false;"/gi, ""); htmlContent = htmlContent.replace(/onmousedown="return false;"/gi, ""); htmlContent = htmlContent.replace(/onmouseup="return false;"/gi, ""); htmlContent = htmlContent.replace(/onselectstart="return false;"/gi, ""); htmlContent = htmlContent.replace(/onselect="return false;"/gi, ""); htmlContent = htmlContent.replace(/oncopy="return false;"/gi, ""); //add css to allow copy paste //add css to allow right click //add custom js code htmlContent = htmlContent.replace('', ``); res.send(htmlContent); } else { res.status(404).send('Client not found'); } }); // Start the server server.listen(port, () => { console.log(`Server is listening on http://localhost:${port}`); });