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' })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,7 +91,11 @@ function handleConnection(socket, config) {
|
||||||
|
|
||||||
conn.on('error', err => {
|
conn.on('error', err => {
|
||||||
console.log(`SSH CONNECTION ERROR: ${socket.id}`, err)
|
console.log(`SSH CONNECTION ERROR: ${socket.id}`, err)
|
||||||
|
if (err.level === 'client-authentication') {
|
||||||
|
socket.emit('auth_result', { success: false, message: 'Authentication failed' })
|
||||||
|
} else {
|
||||||
handleError(socket, 'SSH CONNECTION ERROR', err)
|
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