-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
236 lines (201 loc) · 7.24 KB
/
main.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
const { app, BrowserWindow, ipcMain, Menu } = require('electron');
const path = require('path');
const db = require('./database');
const { startTunnel, stopTunnel } = require('./tunnelManager');
const { exec, spawn } = require('child_process');
let mainWindow;
function createWindow() {
mainWindow = new BrowserWindow({
width: 1200,
height: 1000,
icon: path.join(__dirname, 'assets', 'app_icon.ico'),
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
nodeIntegration: false
}
});
mainWindow.loadFile('index.html');
Menu.setApplicationMenu(null);
}
app.on('ready', createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
ipcMain.on('add-tunnel', (event, tunnelData) => {
const { name, remoteHost, sshUsername, localPort, remotePort } = tunnelData;
db.run('INSERT INTO tunnels (name, remote_host, ssh_username, local_port, remote_port) VALUES (?, ?, ?, ?, ?)', [name, remoteHost, sshUsername, localPort, remotePort], (err) => {
if (err) {
console.error(err);
return;
}
});
});
ipcMain.handle('load-tunnels', async () => {
return new Promise((resolve, reject) => {
db.all('SELECT * FROM tunnels', (err, rows) => {
if (err) {
reject(err);
} else {
resolve(rows);
}
});
});
});
// ipcMain.on('start-tunnel', (event, id) => {
// startTunnel(id, () => {
// console.log(`Tunnel ${id} started`);
// });
// });
// ipcMain.on('stop-tunnel', (event, id) => {
// stopTunnel(id, () => {
// console.log(`Tunnel ${id} stopped`);
// });
// });
ipcMain.handle('delete-tunnel', async (event, id) => {
return new Promise((resolve, reject) => {
db.get('SELECT * FROM tunnels WHERE id = ?', [id], (err, tunnel) => {
if (err) {
console.error(`Failed to fetch tunnel from database: ${err.message}`);
reject(err);
return;
}
// If the tunnel is running, stop it first
if (tunnel.status === 'running' && tunnel.pid) {
const command = `taskkill /PID ${tunnel.pid} /F`;
exec(command, (killError) => {
if (killError) {
console.error(`Failed to kill SSH process: ${killError.message}`);
reject(killError);
return;
}
console.log(`Tunnel ${id} stopped successfully. Proceeding to delete from database.`);
// Proceed to delete the tunnel from the database
db.run('DELETE FROM tunnels WHERE id = ?', [id], (deleteErr) => {
if (deleteErr) {
console.error(`Failed to delete tunnel from database: ${deleteErr.message}`);
reject(deleteErr);
} else {
console.log(`Tunnel ${id} deleted successfully from database.`);
resolve();
}
});
});
} else {
// If the tunnel is not running, delete it directly from the database
db.run('DELETE FROM tunnels WHERE id = ?', [id], (deleteErr) => {
if (deleteErr) {
console.error(`Failed to delete tunnel from database: ${deleteErr.message}`);
reject(deleteErr);
} else {
console.log(`Tunnel ${id} deleted successfully from database.`);
resolve();
}
});
}
});
});
});
ipcMain.handle('start-tunnel', async (event, id) => {
return new Promise((resolve, reject) => {
db.get('SELECT * FROM tunnels WHERE id = ?', [id], (err, tunnel) => {
if (err) {
console.error(`Failed to fetch tunnel from database: ${err.message}`);
reject(err);
return;
}
// Spawn SSH tunnel command to start the tunnel
const args = [
'-f', '-N',
'-L', `${tunnel.local_port}:${tunnel.remote_host}:${tunnel.remote_port}`,
`${tunnel.ssh_username}@${tunnel.remote_host}`
];
const sshProcess = spawn('ssh', args, {
detached: true, // Run the process independently
stdio: 'ignore' // Ignore stdio so the process can run in the background
});
sshProcess.unref(); // Allow the SSH process to keep running after Node.js exits
sshProcess.on('error', (error) => {
console.error(`Failed to start SSH tunnel: ${error.message}`);
reject(error);
});
sshProcess.on('spawn', () => {
const pid = sshProcess.pid;
// Save the PID in the database
db.run('UPDATE tunnels SET status = ?, pid = ? WHERE id = ?', ['running', pid, id], (updateErr) => {
if (updateErr) {
console.error(`Failed to update tunnel status in database: ${updateErr.message}`);
reject(updateErr);
} else {
console.log(`Tunnel ${id} started successfully with PID ${pid}`);
resolve();
}
});
});
});
});
});
ipcMain.handle('stop-tunnel', async (event, id) => {
return new Promise((resolve, reject) => {
db.get('SELECT * FROM tunnels WHERE id = ?', [id], (err, tunnel) => {
if (err) {
console.error(`Failed to fetch tunnel from database: ${err.message}`);
reject(err);
return;
}
if (!tunnel.pid) {
console.error(`No PID found for tunnel ${id}`);
resolve(); // Tunnel is already stopped if no PID is available
return;
}
// Kill the process using taskkill with the stored PID
const command = `taskkill /PID ${tunnel.pid} /F`;
exec(command, (killError) => {
if (killError) {
console.error(`Failed to kill SSH process: ${killError.message}`);
reject(killError);
return;
}
// Update the tunnel status in the database
db.run('UPDATE tunnels SET status = ?, pid = NULL WHERE id = ?', ['stopped', id], (updateErr) => {
if (updateErr) {
console.error(`Failed to update tunnel status in database: ${updateErr.message}`);
reject(updateErr);
} else {
console.log(`Tunnel ${id} stopped successfully and status updated in database`);
resolve();
}
});
});
});
});
});
ipcMain.handle('check-tunnel-status', async (event, id) => {
return new Promise((resolve) => {
db.get('SELECT * FROM tunnels WHERE id = ?', [id], (err, tunnel) => {
if (err) {
console.error(`Failed to fetch tunnel from database: ${err.message}`);
resolve('stopped');
return;
}
// Use netstat command to check if the port is open on Windows
const command = `netstat -ano | findstr :${tunnel.local_port}`;
exec(command, (error, stdout) => {
if (error || !stdout.includes(tunnel.local_port)) {
// If there's an error or no output, assume the tunnel is not running
resolve('stopped');
} else {
// If there's output, the tunnel is running
resolve('running');
}
});
});
});
});