ssh forward moved to extra process #253

This commit is contained in:
Jan Prochazka
2022-04-03 09:40:46 +02:00
parent 1a76cc0979
commit 1a32d88312
2 changed files with 100 additions and 28 deletions

View File

@@ -0,0 +1,68 @@
const fs = require('fs-extra');
const platformInfo = require('../utility/platformInfo');
const childProcessChecker = require('../utility/childProcessChecker');
const { SSHConnection } = require('node-ssh-forward');
const { handleProcessCommunication } = require('../utility/processComm');
async function getSshConnection(connection) {
const sshConfig = {
endHost: connection.sshHost || '',
endPort: connection.sshPort || 22,
bastionHost: connection.sshBastionHost || '',
agentForward: connection.sshMode == 'agent',
passphrase: connection.sshMode == 'keyFile' ? connection.sshKeyfilePassword : undefined,
username: connection.sshLogin,
password: connection.sshMode == 'userPassword' ? connection.sshPassword : undefined,
agentSocket: connection.sshMode == 'agent' ? platformInfo.sshAuthSock : undefined,
privateKey:
connection.sshMode == 'keyFile' && connection.sshKeyfile ? await fs.readFile(connection.sshKeyfile) : undefined,
skipAutoPrivateKey: true,
noReadline: true,
};
const sshConn = new SSHConnection(sshConfig);
return sshConn;
}
async function handleStart({ connection, tunnelConfig }) {
try {
const sshConn = await getSshConnection(connection);
await sshConn.forward(tunnelConfig);
process.send({
msgtype: 'connected',
connection,
tunnelConfig,
});
} catch (err) {
process.send({
msgtype: 'error',
connection,
tunnelConfig,
errorMessage: err.message,
});
}
}
const messageHandlers = {
connect: handleStart,
};
async function handleMessage({ msgtype, ...other }) {
const handler = messageHandlers[msgtype];
await handler(other);
}
function start() {
childProcessChecker();
process.on('message', async message => {
if (handleProcessCommunication(message)) return;
try {
await handleMessage(message);
} catch (e) {
console.error('sshForwardProcess - unhandled error', e);
}
});
}
module.exports = { start };