From 169f610b2ee4857661ec3da7f04b628fec21f1f0 Mon Sep 17 00:00:00 2001 From: Fabio Di Stasio Date: Mon, 13 Nov 2023 18:08:29 +0100 Subject: [PATCH] fix: error with multiple sessions in non-dev environment --- src/main/main.ts | 121 ++++++++++++++--------------- src/renderer/stores/connections.ts | 17 ++-- 2 files changed, 67 insertions(+), 71 deletions(-) diff --git a/src/main/main.ts b/src/main/main.ts index 1dda7c4f..91cdfd00 100644 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -78,87 +78,80 @@ async function createMainWindow () { return window; } -if (!gotTheLock && !safeStorage.isEncryptionAvailable()) // Disable multiple instances if is not possible to share session keys - app.quit(); -else { - require('@electron/remote/main').initialize(); - - // Initialize ipcHandlers - ipcHandlers(); - - ipcMain.on('refresh-theme-settings', () => { - const appTheme = settingsStore.get('application_theme'); - if (isWindows && mainWindow) { - mainWindow.setTitleBarOverlay({ - color: appTheme === 'dark' ? '#3f3f3f' : '#fff', - symbolColor: appTheme === 'dark' ? '#fff' : '#000' - }); - } - }); +require('@electron/remote/main').initialize(); - ipcMain.on('change-window-title', (_, title: string) => { - if (mainWindow) mainWindow.setTitle(title); - }); +// Initialize ipcHandlers +ipcHandlers(); - // quit application when all windows are closed - app.on('window-all-closed', () => { - // on macOS it is common for applications to stay open until the user explicitly quits - if (!isMacOS) app.quit(); - }); +ipcMain.on('refresh-theme-settings', () => { + const appTheme = settingsStore.get('application_theme'); + if (isWindows && mainWindow) { + mainWindow.setTitleBarOverlay({ + color: appTheme === 'dark' ? '#3f3f3f' : '#fff', + symbolColor: appTheme === 'dark' ? '#fff' : '#000' + }); + } +}); - app.on('activate', async () => { - // on macOS it is common to re-create a window even after all windows have been closed - if (mainWindow === null) - mainWindow = await createMainWindow(); - }); +ipcMain.on('change-window-title', (_, title: string) => { + if (mainWindow) mainWindow.setTitle(title); +}); - // create main BrowserWindow when electron is ready - app.on('ready', async () => { - mainWindowState = windowStateKeeper({ - defaultWidth: 1024, - defaultHeight: 800 - }); +// quit application when all windows are closed +app.on('window-all-closed', () => { + // on macOS it is common for applications to stay open until the user explicitly quits + if (!isMacOS) app.quit(); +}); +app.on('activate', async () => { + // on macOS it is common to re-create a window even after all windows have been closed + if (mainWindow === null) mainWindow = await createMainWindow(); - createAppMenu(); +}); + +// create main BrowserWindow when electron is ready +app.on('ready', async () => { + if (!gotTheLock && !safeStorage.isEncryptionAvailable()) // Disable multiple instances if is not possible to share session keys + app.quit(); - if (isWindows) - mainWindow.show(); + mainWindowState = windowStateKeeper({ + defaultWidth: 1024, + defaultHeight: 800 + }); - // if (isDevelopment) - // mainWindow.webContents.openDevTools(); + mainWindow = await createMainWindow(); + createAppMenu(); - // const key = safeStorage.encryptString('godisnothere'); - // console.log('KEY:', key.toString('hex')); + if (isWindows) + mainWindow.show(); - // const decrypted = safeStorage.decryptString(key); - // console.log(decrypted.toString()); + // if (isDevelopment) + // mainWindow.webContents.openDevTools(); - process.on('uncaughtException', error => { - mainWindow.webContents.send('unhandled-exception', error); - }); + process.on('uncaughtException', error => { + mainWindow.webContents.send('unhandled-exception', error); + }); - process.on('unhandledRejection', error => { - mainWindow.webContents.send('unhandled-exception', error); - }); + process.on('unhandledRejection', error => { + mainWindow.webContents.send('unhandled-exception', error); }); +}); - app.on('browser-window-created', (event, window) => { - if (isDevelopment) { - const { antares } = require('../../package.json'); - const extensionPath = path.resolve(__dirname, `../../misc/${antares.devtoolsId}`); - window.webContents.session.loadExtension(extensionPath, { allowFileAccess: true }).catch(console.error); - } +app.on('browser-window-created', (event, window) => { + if (isDevelopment) { + const { antares } = require('../../package.json'); + const extensionPath = path.resolve(__dirname, `../../misc/${antares.devtoolsId}`); + window.webContents.session.loadExtension(extensionPath, { allowFileAccess: true }).catch(console.error); + } - window.webContents.on('will-navigate', (e) => { // Prevent browser navigation - e.preventDefault(); - }); + window.webContents.on('will-navigate', (e) => { // Prevent browser navigation + e.preventDefault(); + }); - window.webContents.on('did-create-window', (w) => { // Close new windows - w.close(); - }); + window.webContents.on('did-create-window', (w) => { // Close new windows + w.close(); }); -} +}); function createAppMenu () { const menuTemplate: OsMenu = { diff --git a/src/renderer/stores/connections.ts b/src/renderer/stores/connections.ts index 7f796736..647264be 100644 --- a/src/renderer/stores/connections.ts +++ b/src/renderer/stores/connections.ts @@ -6,7 +6,7 @@ import * as Store from 'electron-store'; import { defineStore } from 'pinia'; import { useWorkspacesStore } from '@/stores/workspaces'; -const key = localStorage.getItem('key'); +let key = localStorage.getItem('key'); export interface SidebarElement { isFolder: boolean; @@ -18,18 +18,21 @@ export interface SidebarElement { icon?: null | string; } -if (!key) { - const storedKey = ipcRenderer.sendSync('get-key'); +if (!key) { // If no key in local storace + const storedKey = ipcRenderer.sendSync('get-key');// Ask for key stored on disk - if (!storedKey) { + if (!storedKey) { // Of nop stored key on disk const newKey = crypto.randomBytes(16).toString('hex'); localStorage.setItem('key', newKey); + key = newKey; } - else + else { localStorage.setItem('key', storedKey); + key = storedKey; + } } - -ipcRenderer.send('set-key', key); +else + ipcRenderer.send('set-key', key); const persistentStore = new Store({ name: 'connections',