alwaysai
Version:
The alwaysAI command-line interface (CLI)
71 lines • 2.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SecureTunnelInteractiveSsh = void 0;
const alwayscli_1 = require("@alwaysai/alwayscli");
const child_process_1 = require("child_process");
const environment_1 = require("../environment");
const paths_1 = require("../paths");
const logger_1 = require("./logger");
const rKill = require("tree-kill");
class SecureTunnelInteractiveSsh {
constructor(opts) {
const { targetHost, sshPort } = opts;
this.targetHost = targetHost;
this.sshPort = sshPort;
}
/**
* processArgs - process arguments for the ssh
* @param {string[]} args - arguments to process
* @returns {string[]} - processed arguments
*/
processArgs(args) {
logger_1.logger.debug(`-> SecureTunnelInteractiveSsh:processArgs(args = ${args})`);
const processedArgs = [
'-i',
paths_1.PRIVATE_KEY_FILE_PATH,
'-o',
'StrictHostKeyChecking=no',
'-t',
...(this.sshPort > 0 ? ['-p', `${this.sshPort}`] : []),
...args,
this.targetHost
];
logger_1.logger.debug(`<- SecureTunnelInteractiveSsh:processArgs(processedArgs = ${processedArgs})`);
return processedArgs;
}
/**
* runInteractiveSshAsync - run the ssh command in interactive mode
* @param {string[]} args - arguments to use with ssh
*/
async runInteractiveSshAsync(args) {
logger_1.logger.debug(`-> SecureTunnelInteractiveSsh:runInteractiveSshAsync(args = ${args})`);
const sshCommand = `ssh ${args.join(' ')}`;
const sshSession = (0, child_process_1.spawn)(sshCommand, {
stdio: [process.stdin, process.stdout, process.stderr],
shell: true
});
sshSession.on('close', (code) => {
console.log(`SSH session closed with code ${code}`);
});
await new Promise((resolve) => {
sshSession.on('exit', () => {
if (environment_1.ALWAYSAI_OS_PLATFORM === 'darwin') {
process.on('SIGINT', () => {
if (sshSession.pid) {
rKill(sshSession.pid, 'SIGINT', (error) => {
if (error) {
throw new alwayscli_1.CliTerseError(`Failed to kill child process ${error.name} ${error.message}`);
}
});
}
});
}
resolve();
});
});
logger_1.logger.debug('<- SecureTunnelInteractiveSsh:runInteractiveSshAsync()');
return sshSession;
}
}
exports.SecureTunnelInteractiveSsh = SecureTunnelInteractiveSsh;
//# sourceMappingURL=secure-tunnel-ssh.js.map