From 61b824e22b25730161b94604a12c959a65a47400 Mon Sep 17 00:00:00 2001 From: Nishan Date: Sat, 12 Apr 2025 15:18:32 +0530 Subject: [PATCH] Fix: terminal disconnect issue --- app/config/config.js | 19 ---- app/middleware/errorHandler.js | 22 ----- app/middleware/proxy.js | 91 -------------------- app/public/js/components/terminal-service.js | 6 +- app/routes/api.js | 24 ------ nginx/default.conf | 25 +++--- 6 files changed, 19 insertions(+), 168 deletions(-) delete mode 100644 app/config/config.js delete mode 100644 app/middleware/errorHandler.js delete mode 100644 app/middleware/proxy.js delete mode 100644 app/routes/api.js diff --git a/app/config/config.js b/app/config/config.js deleted file mode 100644 index 62bf0f6..0000000 --- a/app/config/config.js +++ /dev/null @@ -1,19 +0,0 @@ -/** - * Configuration module for the application - * Centralizes all environment variables and configuration settings - */ - -// Server configuration -const PORT = process.env.PORT || 3000; - -// VNC service configuration -const VNC_SERVICE_HOST = process.env.VNC_SERVICE_HOST || 'remote-desktop-service'; -const VNC_SERVICE_PORT = process.env.VNC_SERVICE_PORT || 6901; -const VNC_PASSWORD = process.env.VNC_PASSWORD || 'bakku-the-wizard'; // Default password - -module.exports = { - PORT, - VNC_SERVICE_HOST, - VNC_SERVICE_PORT, - VNC_PASSWORD -}; \ No newline at end of file diff --git a/app/middleware/errorHandler.js b/app/middleware/errorHandler.js deleted file mode 100644 index 12dc989..0000000 --- a/app/middleware/errorHandler.js +++ /dev/null @@ -1,22 +0,0 @@ -/** - * Error Handler Middleware - * Centralized error handling for the application - */ - -const path = require('path'); - -/** - * Global error handler middleware - * @param {Error} err - The error object - * @param {Object} req - Express request object - * @param {Object} res - Express response object - * @param {Function} next - Express next function - */ -function errorHandler(err, req, res, next) { - console.error('Server error:', err); - - // Send a user-friendly error page - res.status(500).sendFile(path.join(__dirname, '..', 'public', '50x.html')); -} - -module.exports = errorHandler; \ No newline at end of file diff --git a/app/middleware/proxy.js b/app/middleware/proxy.js deleted file mode 100644 index a9c1781..0000000 --- a/app/middleware/proxy.js +++ /dev/null @@ -1,91 +0,0 @@ -/** - * Proxy middleware module - * Sets up the proxies for VNC connections - */ - -const { createProxyMiddleware } = require('http-proxy-middleware'); -const config = require('../config/config'); - -/** - * Creates the VNC proxy configuration object - * @returns {Object} Proxy configuration - */ -function createVncProxyConfig() { - return { - target: `http://${config.VNC_SERVICE_HOST}:${config.VNC_SERVICE_PORT}`, - changeOrigin: true, - ws: true, - secure: false, - pathRewrite: { - '^/vnc-proxy': '' - }, - onProxyReq: (proxyReq, req, res) => { - // Log HTTP requests being proxied - console.log(`Proxying HTTP request to VNC server: ${req.url}`); - }, - onProxyReqWs: (proxyReq, req, socket, options, head) => { - // Log WebSocket connections - console.log(`WebSocket connection established to VNC server: ${req.url}`); - }, - onProxyRes: (proxyRes, req, res) => { - // Log the responses from VNC server - console.log(`Received response from VNC server for: ${req.url}`); - }, - onError: (err, req, res) => { - console.error(`Proxy error: ${err.message}`); - if (res && res.writeHead) { - res.writeHead(500, { - 'Content-Type': 'text/plain' - }); - res.end(`Proxy error: ${err.message}`); - } - } - }; -} - -/** - * Sets up VNC proxy middleware on the Express app - * @param {Object} app - Express application - */ -function setupProxies(app) { - const vncProxyConfig = createVncProxyConfig(); - - // Middleware to enhance VNC URLs with authentication if needed - app.use('/vnc-proxy', (req, res, next) => { - // Check if the URL already has a password parameter - if (!req.query.password) { - // If no password provided, add default password - console.log('Adding default VNC password to request'); - const separator = req.url.includes('?') ? '&' : '?'; - req.url = `${req.url}${separator}password=${config.VNC_PASSWORD}`; - } - next(); - }, createProxyMiddleware(vncProxyConfig)); - - // Direct WebSocket proxy to handle the websockify endpoint - app.use('/websockify', createProxyMiddleware({ - ...vncProxyConfig, - pathRewrite: { - '^/websockify': '/websockify' - }, - ws: true, - onProxyReqWs: (proxyReq, req, socket, options, head) => { - // Log WebSocket connections to websockify - console.log(`WebSocket connection to websockify established: ${req.url}`); - - // Add additional headers if needed - proxyReq.setHeader('Origin', `http://${config.VNC_SERVICE_HOST}:${config.VNC_SERVICE_PORT}`); - }, - onError: (err, req, res) => { - console.error(`Websockify proxy error: ${err.message}`); - if (res && res.writeHead) { - res.writeHead(500, { - 'Content-Type': 'text/plain' - }); - res.end(`Websockify proxy error: ${err.message}`); - } - } - })); -} - -module.exports = setupProxies; \ No newline at end of file diff --git a/app/public/js/components/terminal-service.js b/app/public/js/components/terminal-service.js index cb62842..1ee1287 100644 --- a/app/public/js/components/terminal-service.js +++ b/app/public/js/components/terminal-service.js @@ -152,8 +152,8 @@ function connectToSocketIO() { // Connect to Socket.io server socket = io('/ssh', { forceNew: true, - reconnectionAttempts: 5, - timeout: 10000 + reconnectionAttempts: 1000, + timeout: 1000 }); console.log('Creating new socket connection to SSH server'); @@ -203,6 +203,8 @@ function connectToSocketIO() { // Handle SSH data with processing for ANSI codes socket.on('data', (data) => { + console.log('Received data from SSH server:', data); + // if data is a string, write it to the terminal if (terminal) { terminal.write(data); } diff --git a/app/routes/api.js b/app/routes/api.js deleted file mode 100644 index 43a23ea..0000000 --- a/app/routes/api.js +++ /dev/null @@ -1,24 +0,0 @@ -/** - * API Routes module - * Defines all API endpoints for the application - */ - -const express = require('express'); -const router = express.Router(); -const config = require('../config/config'); - -/** - * GET /api/vnc-info - * Returns information about the VNC server - */ -router.get('/vnc-info', (req, res) => { - res.json({ - host: config.VNC_SERVICE_HOST, - port: config.VNC_SERVICE_PORT, - wsUrl: `/websockify`, - defaultPassword: config.VNC_PASSWORD, - status: 'connected' - }); -}); - -module.exports = router; \ No newline at end of file diff --git a/nginx/default.conf b/nginx/default.conf index 0df1635..bd9cda8 100644 --- a/nginx/default.conf +++ b/nginx/default.conf @@ -20,16 +20,21 @@ server { proxy_cache_bypass $http_upgrade; } - location /ssh/ { - proxy_pass http://webapp:3000/ssh/; - proxy_http_version 1.1; - proxy_set_header Upgrade $http_upgrade; - proxy_set_header Connection $connection_upgrade; - proxy_set_header Host $host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Forwarded-Proto $scheme; - proxy_cache_bypass $http_upgrade; + location /ssh/socket.io/ { + proxy_pass http://webapp:3000/ssh/socket.io/; + + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + proxy_set_header Host $host; + proxy_cache_bypass $http_upgrade; + + proxy_read_timeout 86400; + proxy_send_timeout 86400; + + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Real-IP $remote_addr; + proxy_buffering off; } # Facilitator API endpoint