-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtunnelManager.js
50 lines (47 loc) · 1.39 KB
/
tunnelManager.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
45
46
47
48
49
50
const { exec } = require('child_process');
const db = require('./database');
function startTunnel(id, callback) {
db.get('SELECT * FROM tunnels WHERE id = ?', [id], (err, tunnel) => {
if (err) {
console.error(err);
return;
}
const command = `ssh -f -N -L ${tunnel.local_port}:${tunnel.remote_host}:${tunnel.remote_port} ${tunnel.ssh_username}@${tunnel.remote_host}`;
exec(command, (error) => {
if (error) {
console.error(`Error starting tunnel: ${error.message}`);
return;
}
db.run('UPDATE tunnels SET status = ? WHERE id = ?', ['running', id], (err) => {
if (err) {
console.error(err);
return;
}
callback();
});
});
});
}
function stopTunnel(id, callback) {
db.get('SELECT * FROM tunnels WHERE id = ?', [id], (err, tunnel) => {
if (err) {
console.error(err);
return;
}
const command = `pkill -f 'ssh -f -N -L ${tunnel.local_port}:${tunnel.remote_host}:${tunnel.remote_port}'`;
exec(command, (error) => {
if (error) {
console.error(`Error stopping tunnel: ${error.message}`);
return;
}
db.run('UPDATE tunnels SET status = ? WHERE id = ?', ['stopped', id], (err) => {
if (err) {
console.error(err);
return;
}
callback();
});
});
});
}
module.exports = { startTunnel, stopTunnel };