refactor: Improve socket authentication error handling and message
This commit is contained in:
parent
1ecf19c5df
commit
d9931334de
1 changed files with 18 additions and 10 deletions
|
@ -59,11 +59,11 @@ function handleConnection(socket, config) {
|
||||||
function handleAuthentication(socket, creds, config) {
|
function handleAuthentication(socket, creds, config) {
|
||||||
console.log(`SOCKET AUTHENTICATE: ${socket.id}`)
|
console.log(`SOCKET AUTHENTICATE: ${socket.id}`)
|
||||||
if (isValidCredentials(creds)) {
|
if (isValidCredentials(creds)) {
|
||||||
console.log(`SOCKET AUTHENTICATE SUCCESS: ${socket.id}`)
|
console.log(`SOCKET CREDENTIALS VALID: ${socket.id}`)
|
||||||
initializeConnection(socket, creds, config)
|
initializeConnection(socket, creds, config)
|
||||||
} else {
|
} else {
|
||||||
console.log(`SOCKET AUTHENTICATE FAILED: ${socket.id}`)
|
console.log(`SOCKET CREDENTIALS INVALID: ${socket.id}`)
|
||||||
socket.emit('auth_result', { success: false, message: 'Invalid credentials' })
|
socket.emit('auth_result', { success: false, message: 'Invalid credentials format' })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,9 +77,9 @@ function handleConnection(socket, config) {
|
||||||
if (conn) {
|
if (conn) {
|
||||||
conn.end()
|
conn.end()
|
||||||
}
|
}
|
||||||
|
|
||||||
conn = new SSH()
|
conn = new SSH()
|
||||||
|
|
||||||
conn.on('ready', () => {
|
conn.on('ready', () => {
|
||||||
console.log(`SSH CONNECTION READY: ${socket.id}`)
|
console.log(`SSH CONNECTION READY: ${socket.id}`)
|
||||||
socket.emit('auth_result', { success: true })
|
socket.emit('auth_result', { success: true })
|
||||||
|
@ -88,12 +88,16 @@ function handleConnection(socket, config) {
|
||||||
setupSSHListeners(socket, creds)
|
setupSSHListeners(socket, creds)
|
||||||
initializeShell(socket, creds)
|
initializeShell(socket, creds)
|
||||||
})
|
})
|
||||||
|
|
||||||
conn.on('error', err => {
|
conn.on('error', err => {
|
||||||
console.log(`SSH CONNECTION ERROR: ${socket.id}`, err)
|
console.log(`SSH CONNECTION ERROR: ${socket.id}`, err)
|
||||||
handleError(socket, 'SSH CONNECTION ERROR', err)
|
if (err.level === 'client-authentication') {
|
||||||
|
socket.emit('auth_result', { success: false, message: 'Authentication failed' })
|
||||||
|
} else {
|
||||||
|
handleError(socket, 'SSH CONNECTION ERROR', err)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
conn.connect(getSSHConfig(creds, config))
|
conn.connect(getSSHConfig(creds, config))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -293,8 +297,12 @@ function handleConnection(socket, config) {
|
||||||
* @returns {boolean} Whether the credentials are valid
|
* @returns {boolean} Whether the credentials are valid
|
||||||
*/
|
*/
|
||||||
function isValidCredentials(credentials) {
|
function isValidCredentials(credentials) {
|
||||||
// Implement your credential validation logic here
|
// Basic format validation
|
||||||
return credentials && credentials.username && credentials.password
|
return credentials &&
|
||||||
|
typeof credentials.username === 'string' &&
|
||||||
|
typeof credentials.password === 'string' &&
|
||||||
|
typeof credentials.host === 'string' &&
|
||||||
|
typeof credentials.port === 'number'
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue