-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
239 lines (198 loc) · 6.25 KB
/
main.js
File metadata and controls
239 lines (198 loc) · 6.25 KB
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
237
238
239
const { app, BrowserWindow, ipcMain, dialog } = require('electron');
const path = require('path');
const Store = require('electron-store');
const { Client } = require('ssh2');
const fs = require('fs');
const os = require('os');
// Configurar almacenamiento persistente
const store = new Store({
schema: {
sshProfiles: {
type: 'array',
default: [],
},
favorites: {
type: 'array',
default: [],
},
},
});
let mainWindow;
const activeConnections = new Map();
function createWindow() {
mainWindow = new BrowserWindow({
width: 1800,
height: 1169,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: false,
contextIsolation: true,
},
});
mainWindow.loadFile('index.html');
// Abrir DevTools en desarrollo (descomentar para depuración)
// mainWindow.webContents.openDevTools();
}
app.whenReady().then(() => {
createWindow();
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit();
});
// Manejar eventos desde el renderer
ipcMain.handle('get-ssh-profiles', async () => {
const profiles = store.get('sshProfiles', []);
return profiles;
});
ipcMain.handle('get-favorites', async () => {
return store.get('favorites', []);
});
ipcMain.handle('toggle-favorite', async (event, profileName) => {
const favorites = store.get('favorites', []);
const index = favorites.indexOf(profileName);
if (index === -1) {
// Añadir a favoritos
favorites.push(profileName);
} else {
// Quitar de favoritos
favorites.splice(index, 1);
}
store.set('favorites', favorites);
return favorites;
});
ipcMain.handle('save-ssh-profile', async (event, profile) => {
const profiles = store.get('sshProfiles', []);
// Si ya existe un perfil con el mismo nombre, actualizarlo
const existingIndex = profiles.findIndex(p => p.name === profile.name);
if (existingIndex >= 0) {
profiles[existingIndex] = profile;
} else {
profiles.push(profile);
}
store.set('sshProfiles', profiles);
return profiles;
});
ipcMain.handle('delete-ssh-profile', async (event, profileName) => {
const profiles = store.get('sshProfiles', []);
const updatedProfiles = profiles.filter(p => p.name !== profileName);
store.set('sshProfiles', updatedProfiles);
// También eliminar de favoritos si existe
const favorites = store.get('favorites', []);
if (favorites.includes(profileName)) {
const updatedFavorites = favorites.filter(name => name !== profileName);
store.set('favorites', updatedFavorites);
}
return updatedProfiles;
});
ipcMain.handle('select-key-file', async () => {
const result = await dialog.showOpenDialog({
properties: ['openFile'],
filters: [
{ name: 'Archivos de Clave', extensions: ['pem', 'key', 'ppk'] },
{ name: 'Todos los Archivos', extensions: ['*'] },
],
});
if (!result.canceled) {
return result.filePaths[0];
}
return null;
});
ipcMain.handle('connect-ssh', async (event, profile) => {
return new Promise((resolve, reject) => {
const conn = new Client();
conn.on('ready', () => {
// Guardar la conexión activa
activeConnections.set(profile.name, conn);
resolve({ success: true, message: 'Conexión establecida con éxito' });
});
conn.on('error', err => {
reject({ success: false, message: `Error al conectar: ${err.message}` });
});
const config = {
host: profile.host,
port: profile.port || 22,
username: profile.username,
keepaliveInterval: 10000, // Mantener conexión activa
};
// Configurar autenticación
if (profile.authType === 'password') {
config.password = profile.password;
} else if (profile.authType === 'keyFile') {
try {
config.privateKey = fs.readFileSync(profile.keyFile);
if (profile.passphrase) {
config.passphrase = profile.passphrase;
}
} catch (err) {
reject({ success: false, message: `Error al leer archivo de clave: ${err.message}` });
return;
}
}
// Intentar la conexión
conn.connect(config);
});
});
// Abrir una shell interactiva
ipcMain.handle('open-shell', async (event, profileName) => {
const conn = activeConnections.get(profileName);
if (!conn) {
return { success: false, message: 'No hay conexión activa para este perfil' };
}
return new Promise((resolve, reject) => {
conn.shell((err, stream) => {
if (err) {
reject({ success: false, message: `Error al abrir shell: ${err.message}` });
return;
}
// Almacenar el stream para este proceso de renderizado
const webContents = event.sender;
// Configurar el stream para reenviar datos al proceso de renderizado
stream.on('data', data => {
if (!webContents.isDestroyed()) {
webContents.send('terminal-data', data);
}
});
stream.on('close', () => {
if (!webContents.isDestroyed()) {
webContents.send('terminal-data', '\r\n\x1b[1;31mConexión cerrada\x1b[0m\r\n');
}
});
stream.on('error', err => {
if (!webContents.isDestroyed()) {
webContents.send('terminal-data', `\r\n\x1b[1;31mError: ${err.message}\x1b[0m\r\n`);
}
});
// Almacenar el stream para el sender actual
event.sender.sshStream = stream;
resolve({ success: true, message: 'Terminal abierta' });
});
});
});
// Enviar datos a la terminal
ipcMain.handle('terminal-input', (event, data) => {
const webContents = event.sender;
if (webContents && webContents.sshStream) {
webContents.sshStream.write(data);
return true;
}
return false;
});
// Cerrar conexión SSH
ipcMain.handle('disconnect-ssh', (event, profileName) => {
const conn = activeConnections.get(profileName);
if (conn) {
// Si hay un stream asociado al remitente, cerrarlo
if (event.sender.sshStream) {
event.sender.sshStream.end();
event.sender.sshStream = null;
}
// Cerrar la conexión
conn.end();
activeConnections.delete(profileName);
return { success: true, message: 'Desconectado' };
}
return { success: false, message: 'No hay conexión activa' };
});