chore: update webssh_client 0.2.18
This commit is contained in:
parent
b9ca79e7cf
commit
fc102380cb
3 changed files with 49 additions and 29 deletions
|
@ -3,7 +3,6 @@
|
||||||
const createDebug = require("debug")
|
const createDebug = require("debug")
|
||||||
var path = require("path")
|
var path = require("path")
|
||||||
var fs = require("fs")
|
var fs = require("fs")
|
||||||
const config = require("./config")
|
|
||||||
var extend = require("util")._extend
|
var extend = require("util")._extend
|
||||||
const debug = createDebug("webssh2:connectionHandler")
|
const debug = createDebug("webssh2:connectionHandler")
|
||||||
|
|
||||||
|
@ -20,24 +19,12 @@ function handleConnection(req, res, urlParams) {
|
||||||
'public'
|
'public'
|
||||||
)
|
)
|
||||||
|
|
||||||
const connectionParams = extend({}, urlParams)
|
const tempConfig = {
|
||||||
extend(connectionParams, req.query)
|
|
||||||
extend(connectionParams, req.body || {})
|
|
||||||
|
|
||||||
const sshCredentials = req.session.sshCredentials || {}
|
|
||||||
|
|
||||||
const config = {
|
|
||||||
socket: {
|
socket: {
|
||||||
url: req.protocol + '://' + req.get('host'),
|
url: req.protocol + '://' + req.get('host'),
|
||||||
path: '/ssh/socket.io'
|
path: '/ssh/socket.io'
|
||||||
},
|
},
|
||||||
ssh: {
|
autoConnect: true
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.readFile(
|
fs.readFile(
|
||||||
|
@ -55,7 +42,7 @@ function handleConnection(req, res, urlParams) {
|
||||||
|
|
||||||
modifiedHtml = modifiedHtml.replace(
|
modifiedHtml = modifiedHtml.replace(
|
||||||
'window.webssh2Config = null;',
|
'window.webssh2Config = null;',
|
||||||
'window.webssh2Config = ' + JSON.stringify(config) + ';'
|
'window.webssh2Config = ' + JSON.stringify(tempConfig) + ';'
|
||||||
)
|
)
|
||||||
|
|
||||||
res.send(modifiedHtml)
|
res.send(modifiedHtml)
|
||||||
|
|
|
@ -29,9 +29,13 @@ function handleConnection(socket, config) {
|
||||||
|
|
||||||
debug(`CONNECT: ${socket.id}, URL: ${socket.handshake.url}`)
|
debug(`CONNECT: ${socket.id}, URL: ${socket.handshake.url}`)
|
||||||
|
|
||||||
removeExistingListeners(socket)
|
// removeExistingListeners(socket)
|
||||||
setupInitialSocketListeners(socket, config)
|
setupInitialSocketListeners(socket, config)
|
||||||
|
debug(
|
||||||
|
`handleConnection: ${socket.id}, credentials: ${JSON.stringify(socket.handshake.session.sshCredentials)}`
|
||||||
|
)
|
||||||
|
|
||||||
|
// HTTP Basic Auth credentials
|
||||||
if (socket.handshake.session.sshCredentials) {
|
if (socket.handshake.session.sshCredentials) {
|
||||||
const creds = socket.handshake.session.sshCredentials
|
const creds = socket.handshake.session.sshCredentials
|
||||||
const { username, password, host, port } = creds
|
const { username, password, host, port } = creds
|
||||||
|
@ -97,7 +101,9 @@ function handleConnection(socket, config) {
|
||||||
* @param {Object} config - The configuration object
|
* @param {Object} config - The configuration object
|
||||||
*/
|
*/
|
||||||
function handleAuthentication(socket, creds, config) {
|
function handleAuthentication(socket, creds, config) {
|
||||||
|
debug("AUTHENTICATING: ", JSON.stringify(creds))
|
||||||
if (!creds.username && !creds.password) {
|
if (!creds.username && !creds.password) {
|
||||||
|
debug(`username and password isnt set: ${socket.id}, Host: ${creds.host}`)
|
||||||
creds.username = sshCredentials.username
|
creds.username = sshCredentials.username
|
||||||
creds.password = sshCredentials.password
|
creds.password = sshCredentials.password
|
||||||
creds.host = sshCredentials.host
|
creds.host = sshCredentials.host
|
||||||
|
@ -105,7 +111,14 @@ function handleConnection(socket, config) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// If reauth, creds from this function should take precedence
|
// 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
|
// Store new credentials in session, overriding any existing ones
|
||||||
socket.handshake.session.sshCredentials = {
|
socket.handshake.session.sshCredentials = {
|
||||||
username: creds.username,
|
username: creds.username,
|
||||||
|
@ -123,14 +136,20 @@ function handleConnection(socket, config) {
|
||||||
|
|
||||||
// Proceed with connection initialization using the new credentials
|
// Proceed with connection initialization using the new credentials
|
||||||
initializeConnection(socket, creds, config)
|
initializeConnection(socket, creds, config)
|
||||||
} else {
|
return
|
||||||
// Handle invalid credentials scenario
|
|
||||||
debug(`CREDENTIALS INVALID: ${socket.id}, Host: ${creds.host}`)
|
|
||||||
socket.emit("authentication", {
|
|
||||||
success: false,
|
|
||||||
message: "Invalid credentials format"
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
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 {import('socket.io').Socket} socket - The Socket.IO socket
|
||||||
* @param {Credentials} creds - The user credentials
|
* @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(
|
conn.shell(
|
||||||
{
|
{
|
||||||
term: creds.term || 'vt69', // config.ssh.term,
|
term: creds.term, // config.ssh.term,
|
||||||
cols: creds.cols,
|
cols: creds.cols,
|
||||||
rows: creds.rows
|
rows: creds.rows
|
||||||
},
|
},
|
||||||
|
@ -317,9 +337,22 @@ function handleConnection(socket, config) {
|
||||||
* @param {number} data.cols - The number of columns
|
* @param {number} data.cols - The number of columns
|
||||||
*/
|
*/
|
||||||
function handleResize(stream, data) {
|
function handleResize(stream, data) {
|
||||||
|
debug(`Resizing terminal to ${data.rows}x${data.cols}`)
|
||||||
|
|
||||||
if (stream) {
|
if (stream) {
|
||||||
stream.setWindow(data.rows, data.cols)
|
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -43,7 +43,7 @@
|
||||||
"read-config-ng": "~3.0.7",
|
"read-config-ng": "~3.0.7",
|
||||||
"socket.io": "~2.2.0",
|
"socket.io": "~2.2.0",
|
||||||
"ssh2": "~0.8.9",
|
"ssh2": "~0.8.9",
|
||||||
"webssh2_client": "^0.2.17"
|
"webssh2_client": "^0.2.18"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node index.js",
|
"start": "node index.js",
|
||||||
|
|
Loading…
Reference in a new issue