diff --git a/app/connectionHandler.js b/app/connectionHandler.js index 4c52493..4057348 100644 --- a/app/connectionHandler.js +++ b/app/connectionHandler.js @@ -3,7 +3,6 @@ const createDebug = require("debug") var path = require("path") var fs = require("fs") -const config = require("./config") var extend = require("util")._extend const debug = createDebug("webssh2:connectionHandler") @@ -19,25 +18,13 @@ function handleConnection(req, res, urlParams) { 'client', 'public' ) - - const connectionParams = extend({}, urlParams) - extend(connectionParams, req.query) - extend(connectionParams, req.body || {}) - - const sshCredentials = req.session.sshCredentials || {} - - const config = { + + const tempConfig = { socket: { url: req.protocol + '://' + req.get('host'), path: '/ssh/socket.io' }, - ssh: { - host: urlParams.host || sshCredentials.host || '', - port: urlParams.port || sshCredentials.port || 22, - username: sshCredentials.username || '', - term: urlParams.sshTerm || sshCredentials.term || config.ssh.term - }, - autoConnect: !!req.session.sshCredentials + autoConnect: true } fs.readFile( @@ -55,7 +42,7 @@ function handleConnection(req, res, urlParams) { modifiedHtml = modifiedHtml.replace( 'window.webssh2Config = null;', - 'window.webssh2Config = ' + JSON.stringify(config) + ';' + 'window.webssh2Config = ' + JSON.stringify(tempConfig) + ';' ) res.send(modifiedHtml) diff --git a/app/socket.js b/app/socket.js index 2d1c209..8dfbb35 100644 --- a/app/socket.js +++ b/app/socket.js @@ -29,9 +29,13 @@ function handleConnection(socket, config) { debug(`CONNECT: ${socket.id}, URL: ${socket.handshake.url}`) - removeExistingListeners(socket) + // removeExistingListeners(socket) setupInitialSocketListeners(socket, config) + debug( + `handleConnection: ${socket.id}, credentials: ${JSON.stringify(socket.handshake.session.sshCredentials)}` + ) + // HTTP Basic Auth credentials if (socket.handshake.session.sshCredentials) { const creds = socket.handshake.session.sshCredentials const { username, password, host, port } = creds @@ -97,7 +101,9 @@ function handleConnection(socket, config) { * @param {Object} config - The configuration object */ function handleAuthentication(socket, creds, config) { + debug("AUTHENTICATING: ", JSON.stringify(creds)) if (!creds.username && !creds.password) { + debug(`username and password isnt set: ${socket.id}, Host: ${creds.host}`) creds.username = sshCredentials.username creds.password = sshCredentials.password creds.host = sshCredentials.host @@ -105,7 +111,14 @@ function handleConnection(socket, config) { } // If reauth, creds from this function should take precedence - if (creds && isValidCredentials(creds)) { + if ( + !socket.handshake.session.sshCredentials && + creds && + isValidCredentials(creds) + ) { + debug( + `REAUTH CREDENTIALS VALID: ${socket.id}, socket.handshake.session.sshCredentials: ${JSON.stringify(socket.handshake.session.sshCredentials)}` + ) // Store new credentials in session, overriding any existing ones socket.handshake.session.sshCredentials = { username: creds.username, @@ -123,14 +136,20 @@ function handleConnection(socket, config) { // Proceed with connection initialization using the new credentials initializeConnection(socket, creds, config) - } else { - // Handle invalid credentials scenario - debug(`CREDENTIALS INVALID: ${socket.id}, Host: ${creds.host}`) - socket.emit("authentication", { - success: false, - message: "Invalid credentials format" - }) + return } + if (isValidCredentials(socket.handshake.session.sshCredentials)) { + debug(`CREDENTIALS VALID: ${socket.id}, Host: ${creds.host}`) + initializeConnection(socket, creds, config) + return + } + + // Handle invalid credentials scenario + debug(`CREDENTIALS INVALID: ${socket.id}, Host: ${creds.host}`) + socket.emit("authentication", { + success: false, + message: "Invalid credentials format" + }) } /** @@ -210,10 +229,11 @@ function handleConnection(socket, config) { * @param {import('socket.io').Socket} socket - The Socket.IO socket * @param {Credentials} creds - The user credentials */ - function initializeShell (socket, creds) { + function initializeShell(socket, creds) { + debug(`INITIALIZING SHELL: ${socket.id}, creds: ${JSON.stringify(creds)}`) conn.shell( { - term: creds.term || 'vt69', // config.ssh.term, + term: creds.term, // config.ssh.term, cols: creds.cols, rows: creds.rows }, @@ -317,9 +337,22 @@ function handleConnection(socket, config) { * @param {number} data.cols - The number of columns */ function handleResize(stream, data) { + debug(`Resizing terminal to ${data.rows}x${data.cols}`) + if (stream) { stream.setWindow(data.rows, data.cols) + return } + + socket.handshake.session.sshCredentials.rows = data.rows + socket.handshake.session.sshCredentials.cols = data.cols + + // Save the session after modification + socket.handshake.session.save((err) => { + if (err) { + console.error(`Failed to save session for ${socket.id}:`, err) + } + }) } /** diff --git a/package.json b/package.json index 8f16d1c..7244e64 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ "read-config-ng": "~3.0.7", "socket.io": "~2.2.0", "ssh2": "~0.8.9", - "webssh2_client": "^0.2.17" + "webssh2_client": "^0.2.18" }, "scripts": { "start": "node index.js",