♻️ Merge 'test' & 'chainedSession' methods

dev
KaKi87 2 years ago
parent d937234a80
commit e8c0a8d60e

@ -1,28 +1,7 @@
const { Client } = require('ssh2');
module.exports = {
test: ({ address, port, user, password }) => new Promise((resolve, reject) => {
const client = new Client();
client.on('error', ({ level: error }) => {
if(error === 'client-authentication')
reject('WRONG_CREDENTIALS');
else if(['client-socket', 'client-timeout'].includes(error))
reject('WRONG_DESTINATION');
else
reject();
});
client.on('ready', () => {
client.end();
resolve();
});
client.connect({
host: address,
port,
username: user,
password
});
}),
chainedSession: (hostsChain, onReady, onClose, onError) => {
chainedSession: (hostsChain, onReady, onClose, onError, isTest) => {
const clients = [];
(async () => {
for(let i = 0; i < hostsChain.length; i++){
@ -36,7 +15,11 @@ module.exports = {
client.on('ready', () => {
if(nextHost) resolve();
else {
client.shell((error, clientStream) => {
if(isTest){
client.end();
onReady();
}
else client.shell((error, clientStream) => {
if(error) return onError(error);
clientStream.on('unpipe', () => {
onClose(clientStream);

@ -512,7 +512,20 @@ const getControllers = (stream, end) => {
case 'cancel': return _controllers.main(user);
case 'test': {
const { address, port, user, password } = host2;
ssh.test({ address, port, user, password }).then(() => res(true)).catch(res);
ssh.chainedSession(
[{ address, port, user, password }],
() => res(true),
undefined,
({ level: error }) => {
if(error === 'client-authentication')
res('WRONG_CREDENTIALS');
else if(['client-socket', 'client-timeout'].includes(error))
res('WRONG_DESTINATION');
else
res();
},
true
);
return;
}
case 'submit': {

Loading…
Cancel
Save