-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsecureBootstrap.js
More file actions
500 lines (423 loc) Β· 16.9 KB
/
secureBootstrap.js
File metadata and controls
500 lines (423 loc) Β· 16.9 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
const { app, BrowserWindow } = require('electron');
const path = require('path');
const fs = require('fs');
const { SecureAdapter } = require('./src/utils/secureAdapter.cjs');
// Global references
let passwordWindow = null;
let secureAdapter = null;
let mainProcessLoaded = false;
// Constants - Consistent path logic for both dev and packaged apps
// Use userData directory for persistent storage across updates
function getConfigPath() {
if (app.isPackaged) {
return path.join(app.getPath('userData'), 'config.json');
} else {
return path.join(__dirname, 'config.json');
}
}
function getWalletsPath() {
if (app.isPackaged) {
return path.join(app.getPath('userData'), 'wallets.json');
} else {
return path.join(__dirname, 'wallets.json');
}
}
const WALLETS_DB_PATH = getWalletsPath();
const CONFIG_PATH = getConfigPath();
/**
* Ensure config and wallets files exist by copying from .example files if needed
*/
function ensureConfigFilesExist() {
try {
// Ensure userData directory exists
if (app.isPackaged) {
const userDataDir = app.getPath('userData');
if (!fs.existsSync(userDataDir)) {
fs.mkdirSync(userDataDir, { recursive: true });
}
}
// Handle config.json - user file in userData, .example in app bundle
const configPath = getConfigPath(); // Now points to userData
const configExamplePath = app.isPackaged
? path.join(process.resourcesPath, 'app.asar.unpacked', 'config.example.json')
: path.join(__dirname, 'config.example.json');
if (!fs.existsSync(configPath)) {
if (fs.existsSync(configExamplePath)) {
fs.copyFileSync(configExamplePath, configPath);
console.log('β
[SECURE-BOOTSTRAP] Created config.json in userData from config.example.json');
} else {
console.warn('β οΈ [SECURE-BOOTSTRAP] config.example.json not found, will create default config');
}
}
// Handle wallets.json - user file in userData, .example in app bundle
const walletsPath = getWalletsPath(); // Now points to userData
const walletsExamplePath = app.isPackaged
? path.join(process.resourcesPath, 'app.asar.unpacked', 'wallets.example.json')
: path.join(__dirname, 'wallets.example.json');
if (!fs.existsSync(walletsPath)) {
if (fs.existsSync(walletsExamplePath)) {
fs.copyFileSync(walletsExamplePath, walletsPath);
console.log('β
[SECURE-BOOTSTRAP] Created wallets.json in userData from wallets.example.json');
} else {
console.warn('β οΈ [SECURE-BOOTSTRAP] wallets.example.json not found, will create default wallets');
}
}
} catch (error) {
console.error('β [SECURE-BOOTSTRAP] Error ensuring config files exist:', error);
}
}
/**
* Initialize the security adapter
*/
function initializeSecurityAdapter() {
secureAdapter = new SecureAdapter(WALLETS_DB_PATH, CONFIG_PATH);
return secureAdapter.initialize();
}
/**
* Create the password prompt window
*/
function createPasswordWindow() {
passwordWindow = new BrowserWindow({
width: 450,
height: 600,
resizable: false,
frame: true,
autoHideMenuBar: true,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
},
icon: path.join(__dirname, 'assets', 'icon.ico'), // Application icon
title: 'TRUSTBOT Security',
show: false
});
// Load password prompt HTML
passwordWindow.loadFile('password-prompt.html');
// Show window when ready
passwordWindow.once('ready-to-show', () => {
passwordWindow.show();
passwordWindow.focus();
});
// Prevent closing the password window (forces user to enter password or exit app)
passwordWindow.on('close', (event) => {
// If main window doesn't exist yet, exit the app
if (!mainProcessLoaded) {
app.exit(0);
}
});
}
/**
* Load the main process once authenticated
*/
// Create a hidden placeholder window to prevent app from exiting
let placeholderWindow = null;
function createPlaceholderWindow() {
placeholderWindow = new BrowserWindow({
width: 0,
height: 0,
show: false,
autoHideMenuBar: true,
icon: path.join(__dirname, 'assets', 'icon.ico'), // Application icon
webPreferences: { nodeIntegration: true }
});
// π― CRITICAL: Make placeholder window globally accessible for cleanup (COMPLIANCE WITH TRACE)
global.placeholderWindow = placeholderWindow;
console.log('Created hidden placeholder window to prevent app from exiting');
console.log('β
[SECURE-BOOTSTRAP] Placeholder window exposed globally for cleanup access');
}
function loadMainProcess(showWalletDialog = false) {
try {
// Mark as loaded so password window can close
mainProcessLoaded = true;
global.showWalletDialog = showWalletDialog;
global.deferAppStartup = true; // Flag to defer main window creation
// Share the master password with main.js process
if (secureAdapter && secureAdapter.masterPassword) {
global.masterPassword = secureAdapter.masterPassword;
console.log('Shared master password with main process');
} else {
console.warn('No master password available to share with main process');
}
console.log('Loading main process with wallet dialog:', showWalletDialog);
// Create a hidden placeholder window first to prevent app from exiting
createPlaceholderWindow();
// Load main.js first, THEN close the password window
console.log('Loading main.js first...');
require('./main.js');
console.log(`Main process loaded successfully${showWalletDialog ? ' (wallet dialog will open)' : ''}`);
// Now close the password window after main.js is loaded
if (passwordWindow && !passwordWindow.isDestroyed()) {
console.log('Closing password window...');
passwordWindow.close();
}
// Auto-updater is now initialized in main.js after window is ready
console.log('β
Main process loaded, auto-updater will be initialized by main.js');
} catch (error) {
console.error('β Error loading main process:', error);
app.exit(1);
}
}
// Register IPC handlers for password validation
const { ipcMain } = require('electron');
ipcMain.handle('check-password-setup', async (event) => {
// Check if this is the first time setup
const isFirstTimeSetup = !secureAdapter.isSecureConfigActive();
return { isFirstTimeSetup };
});
ipcMain.handle('validate-master-password', async (event, { password, isFirstTimeSetup }) => {
try {
if (isFirstTimeSetup) {
// Set up new password
secureAdapter.setMasterPassword(password, true);
// Migrate from legacy storage if it exists
if (fs.existsSync(WALLETS_DB_PATH)) {
await secureAdapter.migrateFromLegacy();
}
return { success: true };
} else {
// Validate existing password
const isValid = secureAdapter.validatePassword(password);
if (isValid) {
// Store password for this session
secureAdapter.setMasterPassword(password, false);
return { success: true };
} else {
return {
success: false,
error: 'Invalid password. Please try again.'
};
}
}
} catch (error) {
return {
success: false,
error: `Error: ${error.message}`
};
}
});
ipcMain.handle('password-accepted', async (event) => {
// Password accepted, load main process
loadMainProcess();
});
ipcMain.handle('exit-app', async (event) => {
console.log('π [SECURE-BOOTSTRAP] File > Exit triggered - comprehensive cleanup...');
try {
const { execSync } = require('child_process');
const ourPid = process.pid;
// π― CRITICAL: Close placeholder window if it exists (COMPLIANCE WITH TRACE)
try {
if (global.placeholderWindow && !global.placeholderWindow.isDestroyed()) {
console.log('π§ [SECURE-BOOTSTRAP] Closing placeholder window...');
global.placeholderWindow.close();
global.placeholderWindow = null;
console.log('β
[SECURE-BOOTSTRAP] Placeholder window closed');
} else {
console.log('βΉοΈ [SECURE-BOOTSTRAP] No placeholder window found or already destroyed');
}
} catch (error) {
console.log('β οΈ [SECURE-BOOTSTRAP] Error closing placeholder window:', error.message);
}
// Close console window if accessible
try {
const { consoleWindow } = require('./main.js');
if (consoleWindow && !consoleWindow.isDestroyed()) {
console.log('π§ [SECURE-BOOTSTRAP] Closing console window...');
consoleWindow.close();
console.log('β
[SECURE-BOOTSTRAP] Console window closed');
}
} catch (error) {
console.log('βΉοΈ [SECURE-BOOTSTRAP] Console window cleanup skipped:', error.message);
}
// Kill tracked child processes
try {
const { childProcesses } = require('./main.js');
if (childProcesses && childProcesses.length > 0) {
console.log(`π§ [SECURE-BOOTSTRAP] Cleaning up ${childProcesses.length} child processes...`);
childProcesses.forEach(process => {
try {
if (!process.killed) {
if (process.platform === 'win32') {
try {
execSync(`taskkill /pid ${process.pid} /t /f`, { stdio: 'ignore' });
console.log(`β
Killed child process tree: ${process.pid}`);
} catch (error) {
console.log(`βΉοΈ Process ${process.pid} already terminated`);
}
} else {
process.kill('SIGTERM');
setTimeout(() => {
if (!process.killed) {
process.kill('SIGKILL');
}
}, 1000);
}
}
} catch (error) {
console.log(`βΉοΈ Error killing process ${process.pid}:`, error.message);
}
});
// Clear the array
childProcesses.length = 0;
console.log('β
[SECURE-BOOTSTRAP] Child processes cleanup completed');
}
} catch (error) {
console.log('βΉοΈ [SECURE-BOOTSTRAP] Child processes cleanup skipped:', error.message);
}
// Kill any lingering processes (comprehensive approach)
if (process.platform === 'win32') {
try {
console.log('π§ [SECURE-BOOTSTRAP] Killing lingering node.exe processes...');
execSync(`taskkill /f /im node.exe /fi "PID ne ${ourPid}"`, { stdio: 'ignore' });
console.log('β
Cleaned up node processes');
} catch (error) {
console.log('βΉοΈ No node processes found to clean up');
}
try {
console.log('π§ [SECURE-BOOTSTRAP] Killing lingering cmd.exe processes...');
execSync(`taskkill /f /im cmd.exe`, { stdio: 'ignore' });
console.log('β
Cleaned up cmd processes');
} catch (error) {
console.log('βΉοΈ No cmd processes found to clean up');
}
// Additional cleanup for npm processes
try {
console.log('π§ [SECURE-BOOTSTRAP] Killing lingering npm processes...');
execSync(`taskkill /f /im npm.exe`, { stdio: 'ignore' });
console.log('β
Cleaned up npm processes');
} catch (error) {
console.log('βΉοΈ No npm processes found to clean up');
}
}
console.log('π [SECURE-BOOTSTRAP] File Exit comprehensive cleanup completed');
} catch (error) {
console.error('β [SECURE-BOOTSTRAP] Error during File Exit cleanup:', error.message);
}
// π― FINAL STEP: Centralized TRUSTBOT cleanup
try {
const { performFinalTrustbotCleanup } = require('./main.js');
performFinalTrustbotCleanup();
} catch (error) {
console.log('βΉοΈ [SECURE-BOOTSTRAP] Final cleanup function not available:', error.message);
}
// Exit after cleanup
console.log('πͺ [SECURE-BOOTSTRAP] Exiting application...');
app.exit(0);
});
// Reset account handler with clean restart logic
ipcMain.handle('reset-account', async (event, { restart = false } = {}) => {
console.log('π [SECURE-BOOTSTRAP] Account reset requested with restart:', restart);
try {
if (restart) {
// Show confirmation dialog
const { dialog } = require('electron');
const result = await dialog.showMessageBox(passwordWindow, {
type: 'info',
title: 'Account Reset Complete',
message: 'Your account has been reset successfully.',
detail: 'The application will automatically restart to complete the reset process.',
buttons: ['OK'],
defaultId: 0
});
console.log('π [SECURE-BOOTSTRAP] User confirmed restart, clearing config and relaunching app...');
// Clear the password/encryption data before restart
secureAdapter.resetSecureConfig();
console.log('β
[SECURE-BOOTSTRAP] Config reset complete, relaunching app...');
// Use Electron's built-in relaunch method for clean restart
app.relaunch();
// Exit current instance to complete the restart
console.log('πͺ [SECURE-BOOTSTRAP] Exiting for restart...');
app.exit(0);
}
return { success: true, message: 'Account reset completed' };
} catch (error) {
console.error('β [SECURE-BOOTSTRAP] Error during account reset:', error.message);
return { success: false, error: error.message };
}
});
// Handle successful password validation
ipcMain.handle('password-validated', async (event, { isFirstTimeSetup } = {}) => {
try {
console.log(`Password validated successfully, loading main process${isFirstTimeSetup ? ' with wallet dialog' : ''}...`);
// Make sure this is executed asynchronously to allow the response to be sent
setTimeout(() => {
// Show wallet dialog on first time setup
loadMainProcess(isFirstTimeSetup === true);
}, 200);
// Return success immediately so renderer gets a response
return { success: true };
} catch (error) {
console.error('Error in password-validated handler:', error);
return { success: false, error: error.message };
}
});
// Handle skip password authentication
ipcMain.handle('skip-password-auth', async (event) => {
try {
console.log('Skipping password authentication...');
// If we have a secureAdapter, disable it or set a bypass flag
if (secureAdapter) {
// Set a flag to indicate we're in insecure mode
secureAdapter.setInsecureMode(true);
console.log('β οΈ WARNING: Running in insecure mode - wallet keys will not be encrypted');
}
return { success: true };
} catch (error) {
console.error('β Error skipping password auth:', error);
return {
success: false,
error: error.message || 'Failed to skip password authentication'
};
}
});
// Make secureAdapter available to the main process
global.secureAdapter = secureAdapter;
// App startup
app.whenReady().then(async () => {
// Ensure config files exist before any initialization
ensureConfigFilesExist();
// Initialize secure adapter
initializeSecurityAdapter();
// Always show password prompt - it handles both first-time setup and returning users
createPasswordWindow();
// The password-prompt.html UI will check if it's first time setup
// and guide the user accordingly
});
// Prevent multiple instances
const gotTheLock = app.requestSingleInstanceLock();
console.log('Single instance lock result:', gotTheLock);
if (!gotTheLock) {
console.log('Failed to get single instance lock - another instance may be running');
console.log('Process PID:', process.pid);
// Temporarily comment out to allow app to start
// app.quit();
} else {
console.log('Successfully got single instance lock');
app.on('second-instance', (event, commandLine, workingDirectory) => {
// Someone tried to run a second instance, focus existing window
if (passwordWindow && !passwordWindow.isDestroyed()) {
if (passwordWindow.isMinimized()) passwordWindow.restore();
passwordWindow.focus();
}
});
}
// NUCLEAR CLEANUP SYSTEM - Emergency process termination for secureBootstrap
// Nuclear cleanup function removed to prevent interference with update installer process
// The aggressive process termination was killing the electron.exe update installer
// Relying on other cleanup handlers for graceful shutdown
// Nuclear cleanup removed - using graceful shutdown for update compatibility
app.on('before-quit', (event) => {
console.log('π [SECURE-BOOTSTRAP] before-quit event triggered - graceful shutdown...');
// Allow normal quit process for update installer compatibility
});
app.on('window-all-closed', () => {
console.log('π [SECURE-BOOTSTRAP] window-all-closed event triggered - graceful shutdown...');
// Nuclear cleanup removed for update installer compatibility
if (process.platform !== 'darwin') {
console.log('π [SECURE-BOOTSTRAP] Platform is not darwin, quitting app...');
app.quit();
}
});
app.on('will-quit', (event) => {
console.log('π [SECURE-BOOTSTRAP] will-quit event triggered - graceful shutdown...');
// Nuclear cleanup removed for update installer compatibility
});