mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-22 01:45:59 +00:00
71 lines
2.0 KiB
JavaScript
71 lines
2.0 KiB
JavaScript
const fs = require('fs-extra');
|
|
const platformInfo = require('../utility/platformInfo');
|
|
const childProcessChecker = require('../utility/childProcessChecker');
|
|
const { handleProcessCommunication } = require('../utility/processComm');
|
|
const { SSHConnection } = require('../utility/SSHConnection');
|
|
|
|
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) {
|
|
console.log('Error creating SSH tunnel connection:', err.message);
|
|
|
|
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 };
|