mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-19 04:56:00 +00:00
ssh tunnel - reuse SSH connection + local port for multiple DB connections
This commit is contained in:
@@ -1,33 +1,36 @@
|
||||
const { SSHConnection } = require('node-ssh-forward');
|
||||
const portfinder = require('portfinder');
|
||||
const { decryptConnection } = require('./crypting');
|
||||
const { getSshTunnel } = require('./sshTunnel');
|
||||
const { getSshTunnelProxy } = require('./sshTunnelProxy');
|
||||
|
||||
async function connectUtility(driver, storedConnection) {
|
||||
let connection = decryptConnection(storedConnection);
|
||||
if (connection.useSshTunnel) {
|
||||
const sshConfig = {
|
||||
endHost: connection.sshHost || '',
|
||||
endPort: connection.sshPort || 22,
|
||||
bastionHost: '',
|
||||
agentForward: false,
|
||||
passphrase: undefined,
|
||||
username: connection.sshLogin,
|
||||
password: connection.sshPassword,
|
||||
skipAutoPrivateKey: true,
|
||||
noReadline: true,
|
||||
};
|
||||
const localPort = await getSshTunnelProxy(connection);
|
||||
// const sshConfig = {
|
||||
// endHost: connection.sshHost || '',
|
||||
// endPort: connection.sshPort || 22,
|
||||
// bastionHost: '',
|
||||
// agentForward: false,
|
||||
// passphrase: undefined,
|
||||
// username: connection.sshLogin,
|
||||
// password: connection.sshPassword,
|
||||
// skipAutoPrivateKey: true,
|
||||
// noReadline: true,
|
||||
// };
|
||||
|
||||
const sshConn = new SSHConnection(sshConfig);
|
||||
const localPort = await portfinder.getPortPromise({ port: 10000, stopPort: 60000 });
|
||||
// workaround for `getPortPromise` not releasing the port quickly enough
|
||||
await new Promise(resolve => setTimeout(resolve, 500));
|
||||
const tunnelConfig = {
|
||||
fromPort: localPort,
|
||||
toPort: connection.port,
|
||||
toHost: connection.server,
|
||||
};
|
||||
const tunnel = await sshConn.forward(tunnelConfig);
|
||||
console.log(`Created SSH tunnel to ${connection.sshHost}-${connection.server}:${connection.port}, using local port ${localPort}`)
|
||||
// const sshConn = new SSHConnection(sshConfig);
|
||||
// const localPort = await portfinder.getPortPromise({ port: 10000, stopPort: 60000 });
|
||||
// // workaround for `getPortPromise` not releasing the port quickly enough
|
||||
// await new Promise(resolve => setTimeout(resolve, 500));
|
||||
// const tunnelConfig = {
|
||||
// fromPort: localPort,
|
||||
// toPort: connection.port,
|
||||
// toHost: connection.server,
|
||||
// };
|
||||
// const tunnel = await sshConn.forward(tunnelConfig);
|
||||
// console.log(`Created SSH tunnel to ${connection.sshHost}-${connection.server}:${connection.port}, using local port ${localPort}`)
|
||||
|
||||
connection = {
|
||||
...connection,
|
||||
|
||||
Reference in New Issue
Block a user