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

View file

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