Fix: terminal disconnect issue

This commit is contained in:
Nishan
2025-04-12 15:18:32 +05:30
parent 89808a64e3
commit 61b824e22b
6 changed files with 19 additions and 168 deletions

View File

@@ -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
};

View File

@@ -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;

View File

@@ -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;

View File

@@ -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);
}

View File

@@ -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;

View File

@@ -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