chore: refactoring

This commit is contained in:
Bill Church 2024-08-21 20:00:34 +00:00
parent 01ddac958e
commit 2bd225e12e
No known key found for this signature in database
2 changed files with 67 additions and 74 deletions

View file

@ -95,8 +95,7 @@ class WebSSH2Socket {
this.ssh this.ssh
.connect(creds) .connect(creds)
.then( .then(() => {
function() {
this.sessionState = Object.assign({}, this.sessionState, { this.sessionState = Object.assign({}, this.sessionState, {
authenticated: true, authenticated: true,
username: creds.username, username: creds.username,
@ -123,10 +122,8 @@ class WebSSH2Socket {
} }
this.socket.emit("getTerminal", true) this.socket.emit("getTerminal", true)
}.bind(this) })
) .catch(err => {
.catch(
function(err) {
debug( debug(
`initializeConnection: SSH CONNECTION ERROR: ${this.socket.id}, Host: ${creds.host}, Error: ${err.message}` `initializeConnection: SSH CONNECTION ERROR: ${this.socket.id}, Host: ${creds.host}, Error: ${err.message}`
) )
@ -134,8 +131,7 @@ class WebSSH2Socket {
new SSHConnectionError(`SSH CONNECTION ERROR: ${err.message}`) new SSHConnectionError(`SSH CONNECTION ERROR: ${err.message}`)
) )
this.socket.emit("ssherror", `SSH CONNECTION ERROR: ${err.message}`) this.socket.emit("ssherror", `SSH CONNECTION ERROR: ${err.message}`)
}.bind(this) })
)
} }
/** /**
@ -164,37 +160,35 @@ class WebSSH2Socket {
rows: this.sessionState.rows rows: this.sessionState.rows
}) })
.then(stream => { .then(stream => {
stream.on("data", data => stream.on("data", data => {
this.socket.emit("data", data.toString("utf-8")) this.socket.emit("data", data.toString("utf-8"))
) })
stream.stderr.on("data", data => debug(`STDERR: ${data}`)) stream.stderr.on("data", data => debug(`STDERR: ${data}`))
stream.on("close", (code, signal) => stream.on("close", (code, signal) => {
this.handleConnectionClose(code, signal) this.handleConnectionClose(code, signal)
) })
this.socket.on("data", data => stream.write(data)) this.socket.on("data", data => {
this.socket.on("control", controlData => stream.write(data)
})
this.socket.on("control", controlData => {
this.handleControl(controlData) this.handleControl(controlData)
) })
this.socket.on("resize", data => this.handleResize(data)) this.socket.on("resize", data => {
this.handleResize(data)
})
}) })
.catch(err => this.handleError("SHELL ERROR", err)) .catch(err => this.handleError("SHELL ERROR", err))
} }
/**
* Handles the resize event for the terminal.
* @param {Object} data - The resize data.
*/
handleResize(data) { handleResize(data) {
const { rows, cols } = data const { rows, cols } = data
if (this.ssh.stream) {
if (rows && validator.isInt(rows.toString())) if (rows && validator.isInt(rows.toString()))
this.sessionState.rows = parseInt(rows, 10) this.sessionState.rows = parseInt(rows, 10)
if (cols && validator.isInt(cols.toString())) if (cols && validator.isInt(cols.toString()))
this.sessionState.cols = parseInt(cols, 10) this.sessionState.cols = parseInt(cols, 10)
this.ssh.resizeTerminal(this.sessionState.rows, this.sessionState.cols) this.ssh.resizeTerminal(this.sessionState.rows, this.sessionState.cols)
} }
}
/** /**
* Handles control commands. * Handles control commands.
@ -261,9 +255,11 @@ class WebSSH2Socket {
* Handles the closure of the connection. * Handles the closure of the connection.
* @param {string} reason - The reason for the closure. * @param {string} reason - The reason for the closure.
*/ */
handleConnectionClose(reason) { handleConnectionClose(code, signal) {
if (this.ssh) this.ssh.end() this.ssh.end()
debug(`handleConnectionClose: ${this.socket.id}, Reason: ${reason}`) debug(
`handleConnectionClose: ${this.socket.id}, Code: ${code}, Signal: ${signal}`
)
this.socket.disconnect(true) this.socket.disconnect(true)
} }

View file

@ -15,24 +15,22 @@ function SSHConnection(config) {
} }
SSHConnection.prototype.connect = function(creds) { SSHConnection.prototype.connect = function(creds) {
const self = this
return new Promise(function(resolve, reject) {
debug("connect: %O", maskSensitiveData(creds)) debug("connect: %O", maskSensitiveData(creds))
return new Promise((resolve, reject) => {
if (self.conn) { if (this.conn) {
self.conn.end() this.conn.end()
} }
self.conn = new SSH() this.conn = new SSH()
const sshConfig = self.getSSHConfig(creds) const sshConfig = this.getSSHConfig(creds)
self.conn.on("ready", function() { this.conn.on("ready", () => {
debug(`connect: ready: ${creds.host}`) debug(`connect: ready: ${creds.host}`)
resolve() resolve(this.conn)
}) })
self.conn.on("error", function(err) { this.conn.on("error", err => {
const error = new SSHConnectionError( const error = new SSHConnectionError(
`SSH Connection error: ${err.message}` `SSH Connection error: ${err.message}`
) )
@ -40,7 +38,7 @@ SSHConnection.prototype.connect = function(creds) {
reject(error) reject(error)
}) })
self.conn.connect(sshConfig) this.conn.connect(sshConfig)
}) })
} }
@ -65,13 +63,12 @@ SSHConnection.prototype.getSSHConfig = function(creds) {
} }
SSHConnection.prototype.shell = function(options) { SSHConnection.prototype.shell = function(options) {
const self = this return new Promise((resolve, reject) => {
return new Promise(function(resolve, reject) { this.conn.shell(options, (err, stream) => {
self.conn.shell(options, function(err, stream) {
if (err) { if (err) {
reject(err) reject(err)
} else { } else {
self.stream = stream this.stream = stream
resolve(stream) resolve(stream)
} }
}) })