-
Notifications
You must be signed in to change notification settings - Fork 0
/
sshTunnel.js
44 lines (38 loc) · 1.08 KB
/
sshTunnel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// sshTunnel.js
const { Client } = require('ssh2');
require('dotenv').config();
async function createSshTunnel() {
return new Promise((resolve, reject) => {
const conn = new Client();
conn.on('ready', () => {
conn.forwardOut(
'127.0.0.1',
process.env.SSH_LOCAL_PORT,
process.env.DB_HOST,
process.env.DB_PORT,
(err, stream) => {
if (err) {
conn.end();
reject(err);
return;
}
resolve(stream);
}
);
});
conn.on('error', (err) => {
console.error('SSH error:', err);
reject(err);
});
conn.connect({
host: process.env.SSH_HOST,
port: 22,
username: process.env.SSH_USER,
privateKey: require('fs').readFileSync(process.env.SSH_KEY),
readyTimeout: 30000
});
});
}
module.exports = {
createSshTunnel
};