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,47 +95,43 @@ 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, password: creds.password,
password: creds.password, host: creds.host,
host: creds.host, port: creds.port
port: creds.port })
})
const authResult = { action: "auth_result", success: true } const authResult = { action: "auth_result", success: true }
this.socket.emit("authentication", authResult) this.socket.emit("authentication", authResult)
const permissions = { const permissions = {
autoLog: this.config.options.autoLog || false, autoLog: this.config.options.autoLog || false,
allowReplay: this.config.options.allowReplay || false, allowReplay: this.config.options.allowReplay || false,
allowReconnect: this.config.options.allowReconnect || false, allowReconnect: this.config.options.allowReconnect || false,
allowReauth: this.config.options.allowReauth || false allowReauth: this.config.options.allowReauth || false
} }
this.socket.emit("permissions", permissions) this.socket.emit("permissions", permissions)
this.updateElement("footer", `ssh://${creds.host}:${creds.port}`) this.updateElement("footer", `ssh://${creds.host}:${creds.port}`)
if (this.config.header && this.config.header.text !== null) { if (this.config.header && this.config.header.text !== null) {
this.updateElement("header", this.config.header.text) this.updateElement("header", this.config.header.text)
} }
this.socket.emit("getTerminal", true) this.socket.emit("getTerminal", true)
}.bind(this) })
) .catch(err => {
.catch( debug(
function(err) { `initializeConnection: SSH CONNECTION ERROR: ${this.socket.id}, Host: ${creds.host}, Error: ${err.message}`
debug( )
`initializeConnection: SSH CONNECTION ERROR: ${this.socket.id}, Host: ${creds.host}, Error: ${err.message}` handleError(
) new SSHConnectionError(`SSH CONNECTION ERROR: ${err.message}`)
handleError( )
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,36 +160,34 @@ 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)
}
} }
/** /**
@ -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 debug("connect: %O", maskSensitiveData(creds))
return new Promise(function(resolve, reject) { return new Promise((resolve, reject) => {
debug("connect: %O", maskSensitiveData(creds)) if (this.conn) {
this.conn.end()
if (self.conn) {
self.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)
} }
}) })