Skip to content

Commit

Permalink
feat: ability to open multiple app sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio286 committed Nov 12, 2023
1 parent 664b218 commit 075f542
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
36 changes: 34 additions & 2 deletions src/main/ipc-handlers/application.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,46 @@
import { app, dialog, ipcMain } from 'electron';
import { app, dialog, ipcMain, safeStorage } from 'electron';
import * as Store from 'electron-store';

import { validateSender } from '../libs/misc/validateSender';
import { ShortcutRegister } from '../libs/ShortcutRegister';

export default () => {
ipcMain.on('close-app', (event) => {
if (!validateSender(event.senderFrame)) return { status: 'error', response: 'Unauthorized process' };
if (!validateSender(event.senderFrame)) {
return {
status: 'error',
response: 'Unauthorized process'
};
}
app.exit();
});

ipcMain.on('set-key', (event, key) => {
if (safeStorage.isEncryptionAvailable()) {
const sessionStore = new Store({
name: 'session',
fileExtension: ''
});
const encrypted = safeStorage.encryptString(key);
sessionStore.set('key', encrypted);
event.returnValue = true;
}
});

ipcMain.on('get-key', (event) => {
if (!safeStorage.isEncryptionAvailable()) {
event.returnValue = false;
return;
}
const sessionStore = new Store({
name: 'session',
fileExtension: ''
});
const encrypted = sessionStore.get('key') as string;
const key = safeStorage.decryptString(Buffer.from(encrypted, 'utf-8'));
event.returnValue = key;
});

ipcMain.handle('show-open-dialog', (event, options) => {
if (!validateSender(event.senderFrame)) return { status: 'error', response: 'Unauthorized process' };
return dialog.showOpenDialog(options);
Expand Down
11 changes: 9 additions & 2 deletions src/main/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as remoteMain from '@electron/remote/main';
import { app, BrowserWindow, ipcMain, nativeImage } from 'electron';
import { app, BrowserWindow, ipcMain, nativeImage, safeStorage } from 'electron';
import * as Store from 'electron-store';
import * as windowStateKeeper from 'electron-window-state';
import * as path from 'path';
Expand Down Expand Up @@ -78,7 +78,8 @@ async function createMainWindow () {
return window;
}

if (!gotTheLock) app.quit();
if (!gotTheLock && !safeStorage.isEncryptionAvailable()) // Disable multiple instances if is not possible to share session keys
app.quit();
else {
require('@electron/remote/main').initialize();

Expand Down Expand Up @@ -127,6 +128,12 @@ else {
// if (isDevelopment)
// mainWindow.webContents.openDevTools();

// const key = safeStorage.encryptString('godisnothere');
// console.log('KEY:', key.toString('hex'));

// const decrypted = safeStorage.decryptString(key);
// console.log(decrypted.toString());

process.on('uncaughtException', error => {
mainWindow.webContents.send('unhandled-exception', error);
});
Expand Down
17 changes: 13 additions & 4 deletions src/renderer/stores/connections.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ConnectionParams } from 'common/interfaces/antares';
import { uidGen } from 'common/libs/uidGen';
import * as crypto from 'crypto';
import { ipcRenderer } from 'electron';
import * as Store from 'electron-store';
import { defineStore } from 'pinia';

Expand All @@ -17,10 +18,18 @@ export interface SidebarElement {
icon?: null | string;
}

if (!key)
localStorage.setItem('key', crypto.randomBytes(16).toString('hex'));
else
localStorage.setItem('key', key);
if (!key) {
const storedKey = ipcRenderer.sendSync('get-key');

if (!storedKey) {
const newKey = crypto.randomBytes(16).toString('hex');
localStorage.setItem('key', newKey);
}
else
localStorage.setItem('key', storedKey);
}

ipcRenderer.send('set-key', key);

const persistentStore = new Store({
name: 'connections',
Expand Down

0 comments on commit 075f542

Please sign in to comment.