Introduce host chaining w/ 'chainedSession' method + remove old 'session' method

dev
KaKi87 2 years ago
parent 34ebec80aa
commit d937234a80

@ -22,22 +22,49 @@ module.exports = {
password
});
}),
session: ({ address, port, user, password }, onReady, onClose, onError) => {
const client = new Client();
client.on('ready', () => client.shell((error, clientStream) => {
if(error) return onError(error);
clientStream.on('unpipe', () => {
onClose(clientStream);
client.end();
});
onReady(clientStream);
}));
client.on('error', onError);
client.connect({
host: address,
port,
username: user,
password
});
chainedSession: (hostsChain, onReady, onClose, onError) => {
const clients = [];
(async () => {
for(let i = 0; i < hostsChain.length; i++){
const
prevClient = clients[i-1],
{ address, port, user, password } = hostsChain[i],
nextHost = hostsChain[i+1],
client = clients[i] = new Client();
client.on('error', onError);
await new Promise(resolve => {
client.on('ready', () => {
if(nextHost) resolve();
else {
client.shell((error, clientStream) => {
if(error) return onError(error);
clientStream.on('unpipe', () => {
onClose(clientStream);
client.end();
});
onReady(clientStream);
});
}
});
if(prevClient){
client.on('end', () => prevClient.end());
prevClient.forwardOut('127.0.0.1', 12345 + i, address, port, (error, prevClientStream) => {
if(error) onError(error);
client.connect({
sock: prevClientStream,
username: user,
password
});
});
}
else client.connect({
host: address,
port,
username: user,
password
});
});
}
})();
}
};

@ -536,7 +536,7 @@ const getControllers = (stream, end) => {
return _controllers.main(user);
}
case 'connect': {
return ssh.session(host, clientStream => {
return ssh.chainedSession([host], clientStream => {
screen.destroy();
clientStream.stdout.pipe(stream.stdin);
stream.stdin.pipe(clientStream.stdout);

Loading…
Cancel
Save