Browse Source

Implement host chaining as per d937234

master
KaKi87 2 months ago
parent
commit
66be32b978
1 changed files with 23 additions and 7 deletions
  1. +23
    -7
      lib/tui/index.js

+ 23
- 7
lib/tui/index.js View File

@ -244,7 +244,7 @@ const getViews = screen => {
else
addButton.focus();
},
setHost: ({ name, address, port, username, password } = {}, callback) => {
setHost: ({ name, address, port, username, password, chain } = {}, callback) => {
let action;
const hostForm = form();
text({ content: 'Add host' }, { borderBottom: true }, hostForm);
@ -259,6 +259,8 @@ const getViews = screen => {
const userInput = textbox({ top: n += 2, name: 'username', content: username }, hostForm);
text({ top: n += 4, content: 'Password :' }, hostForm);
textbox({ top: n += 2, name: 'password', censor: true, content: password }, hostForm);
text({ top: n += 4, content: 'Chain (optional, comma-separated) :' }, hostForm);
textbox({ top: n += 2, name: 'chain', content: chain }, hostForm);
const actions = flexContainer({ top: n += 4, width: 100 }, hostForm);
button({ content: 'Cancel' }, undefined, actions)
.on('press', () => callback({ action: 'cancel' }));
@ -281,7 +283,7 @@ const getViews = screen => {
{ color: 'red' },
hostForm
);
hostForm.on('submit', ({ name, address, port, username, password }) => {
hostForm.on('submit', ({ name, address, port, username, password, chain }) => {
port = parseInt(port);
if(action === 'test'){
successText.hide();
@ -292,7 +294,7 @@ const getViews = screen => {
// noinspection JSUnusedGlobalSymbols
callback({
action,
host: { name, address, port, username, password },
host: { name, address, port, username, password, chain },
res: res => {
if(action === 'test'){
testButton.setContent('Test');
@ -304,7 +306,8 @@ const getViews = screen => {
successText.hide();
const error = ({
WRONG_CREDENTIALS: 'wrong username or password',
WRONG_DESTINATION: 'wrong address or port'
WRONG_DESTINATION: 'wrong address or port',
INVALID_CHAIN: 'invalid hosts chain'
})[res];
errorText.setContent(`SSH connection failed : ${error || `unknown error (${res})`}`);
errorText.show();
@ -513,9 +516,16 @@ const getControllers = (stream, end) => {
switch(action2){
case 'cancel': return _controllers.main(user);
case 'test': {
const { address, port, username, password } = host2;
const
{ address, port, username, password, chain } = host2,
hosts = user.getHosts(),
hostsChain = [
...(chain ? chain.split(',').map(name => hosts.find(host => host.name === name)) : []),
{ address, port, username, password }
];
if(hostsChain.includes(undefined)) return res('INVALID_CHAIN');
ssh.chainedSession(
[{ address, port, username, password }],
hostsChain,
() => res(true),
undefined,
({ level: error }) => {
@ -551,7 +561,13 @@ const getControllers = (stream, end) => {
return _controllers.main(user);
}
case 'connect': {
return ssh.chainedSession([host], clientStream => {
const
{ chain } = host,
hosts = user.getHosts();
return ssh.chainedSession([
...(chain ? chain.split(',').map(name => hosts.find(host => host.name === name)) : []),
host
], clientStream => {
screen.destroy();
clientStream.stdout.pipe(stream.stdin);
stream.stdin.pipe(clientStream.stdout);


Loading…
Cancel
Save