2020-12-29 20:53:29 +01:00
|
|
|
import { app } from 'electron';
|
2018-02-15 21:27:56 +01:00
|
|
|
import * as path from 'path';
|
2018-01-16 21:58:17 +01:00
|
|
|
|
2018-02-14 05:38:18 +01:00
|
|
|
import { I18nService } from './services/i18n.service';
|
|
|
|
|
2018-02-08 19:10:13 +01:00
|
|
|
import { MenuMain } from './main/menu.main';
|
|
|
|
import { MessagingMain } from './main/messaging.main';
|
2018-02-11 05:24:22 +01:00
|
|
|
import { PowerMonitorMain } from './main/powerMonitor.main';
|
2018-04-24 22:30:47 +02:00
|
|
|
|
2018-04-25 05:53:20 +02:00
|
|
|
import { ConstantsService } from 'jslib/services/constants.service';
|
|
|
|
|
2020-07-23 19:32:36 +02:00
|
|
|
import { BiometricMain } from 'jslib/abstractions/biometric.main';
|
2019-02-02 18:27:06 +01:00
|
|
|
import { ElectronConstants } from 'jslib/electron/electronConstants';
|
2018-04-25 21:43:26 +02:00
|
|
|
import { KeytarStorageListener } from 'jslib/electron/keytarStorageListener';
|
2018-04-24 22:30:47 +02:00
|
|
|
import { ElectronLogService } from 'jslib/electron/services/electronLog.service';
|
2018-04-26 21:44:07 +02:00
|
|
|
import { ElectronMainMessagingService } from 'jslib/electron/services/electronMainMessaging.service';
|
2019-03-12 03:37:13 +01:00
|
|
|
import { ElectronStorageService } from 'jslib/electron/services/electronStorage.service';
|
2018-05-08 18:05:05 +02:00
|
|
|
import { TrayMain } from 'jslib/electron/tray.main';
|
2018-05-04 20:16:28 +02:00
|
|
|
import { UpdaterMain } from 'jslib/electron/updater.main';
|
2018-04-24 22:30:47 +02:00
|
|
|
import { WindowMain } from 'jslib/electron/window.main';
|
2020-10-12 21:18:28 +02:00
|
|
|
import { NativeMessagingMain } from './main/nativeMessaging.main';
|
2018-05-04 20:16:28 +02:00
|
|
|
|
2018-02-14 05:38:18 +01:00
|
|
|
export class Main {
|
2018-04-24 22:30:47 +02:00
|
|
|
logService: ElectronLogService;
|
2018-02-14 05:38:18 +01:00
|
|
|
i18nService: I18nService;
|
2019-03-12 03:37:13 +01:00
|
|
|
storageService: ElectronStorageService;
|
2018-04-26 21:44:07 +02:00
|
|
|
messagingService: ElectronMainMessagingService;
|
2018-04-25 21:43:26 +02:00
|
|
|
keytarStorageListener: KeytarStorageListener;
|
2018-02-14 05:38:18 +01:00
|
|
|
|
|
|
|
windowMain: WindowMain;
|
|
|
|
messagingMain: MessagingMain;
|
|
|
|
updaterMain: UpdaterMain;
|
|
|
|
menuMain: MenuMain;
|
|
|
|
powerMonitorMain: PowerMonitorMain;
|
2018-05-04 18:45:42 +02:00
|
|
|
trayMain: TrayMain;
|
2020-07-23 19:32:36 +02:00
|
|
|
biometricMain: BiometricMain;
|
2020-10-12 21:18:28 +02:00
|
|
|
nativeMessagingMain: NativeMessagingMain;
|
2018-02-14 05:38:18 +01:00
|
|
|
|
|
|
|
constructor() {
|
2018-02-15 21:27:56 +01:00
|
|
|
// Set paths for portable builds
|
2018-02-20 22:04:35 +01:00
|
|
|
let appDataPath = null;
|
|
|
|
if (process.env.BITWARDEN_APPDATA_DIR != null) {
|
|
|
|
appDataPath = process.env.BITWARDEN_APPDATA_DIR;
|
2018-02-23 23:11:18 +01:00
|
|
|
} else if (process.platform === 'win32' && process.env.PORTABLE_EXECUTABLE_DIR != null) {
|
2018-02-20 22:04:35 +01:00
|
|
|
appDataPath = path.join(process.env.PORTABLE_EXECUTABLE_DIR, 'bitwarden-appdata');
|
2018-02-23 23:11:18 +01:00
|
|
|
} else if (process.platform === 'linux' && process.env.SNAP_USER_DATA != null) {
|
2018-02-23 23:16:00 +01:00
|
|
|
appDataPath = path.join(process.env.SNAP_USER_DATA, 'appdata');
|
2018-02-20 22:04:35 +01:00
|
|
|
}
|
|
|
|
|
2019-12-16 14:59:19 +01:00
|
|
|
app.on('ready', () => {
|
2020-03-11 14:56:51 +01:00
|
|
|
/*
|
|
|
|
globalShortcut.register('CommandOrControl+Shift+L', async () => {
|
|
|
|
if (this.windowMain.win === null) {
|
|
|
|
await this.windowMain.createWindow();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.messagingService.send('focusSearch');
|
|
|
|
this.windowMain.win.show();
|
|
|
|
});
|
|
|
|
|
|
|
|
globalShortcut.register('CommandOrControl+Shift+G', async () => {
|
|
|
|
if (this.windowMain.win === null) {
|
|
|
|
await this.windowMain.createWindow();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.messagingService.send('openPasswordGenerator');
|
|
|
|
this.windowMain.win.show();
|
|
|
|
});
|
2020-03-12 14:50:19 +01:00
|
|
|
*/
|
2019-12-16 14:59:19 +01:00
|
|
|
});
|
|
|
|
|
2018-02-20 22:04:35 +01:00
|
|
|
if (appDataPath != null) {
|
2018-02-15 21:27:56 +01:00
|
|
|
app.setPath('userData', appDataPath);
|
|
|
|
}
|
2018-02-25 15:51:21 +01:00
|
|
|
app.setPath('logs', path.join(app.getPath('userData'), 'logs'));
|
2018-02-15 21:27:56 +01:00
|
|
|
|
2018-02-14 05:38:18 +01:00
|
|
|
const args = process.argv.slice(1);
|
2021-02-03 19:21:22 +01:00
|
|
|
const watch = args.some(val => val === '--watch');
|
2018-02-14 05:38:18 +01:00
|
|
|
|
|
|
|
if (watch) {
|
|
|
|
// tslint:disable-next-line
|
|
|
|
require('electron-reload')(__dirname, {});
|
|
|
|
}
|
|
|
|
|
2018-04-24 22:30:47 +02:00
|
|
|
this.logService = new ElectronLogService(null, app.getPath('userData'));
|
2018-02-14 05:38:18 +01:00
|
|
|
this.i18nService = new I18nService('en', './locales/');
|
2018-04-26 22:17:14 +02:00
|
|
|
|
|
|
|
const storageDefaults: any = {};
|
2020-04-01 17:18:36 +02:00
|
|
|
// Default vault timeout to "on restart", and action to "lock"
|
|
|
|
storageDefaults[ConstantsService.vaultTimeoutKey] = -1;
|
|
|
|
storageDefaults[ConstantsService.vaultTimeoutActionKey] = 'lock';
|
2019-03-12 03:37:13 +01:00
|
|
|
this.storageService = new ElectronStorageService(app.getPath('userData'), storageDefaults);
|
2018-02-14 05:38:18 +01:00
|
|
|
|
2020-08-21 15:50:36 +02:00
|
|
|
this.windowMain = new WindowMain(this.storageService, true, undefined, undefined,
|
2021-02-03 19:21:22 +01:00
|
|
|
arg => this.processDeepLink(arg), win => this.trayMain.setupWindowListeners(win));
|
2020-04-14 22:52:03 +02:00
|
|
|
this.messagingMain = new MessagingMain(this, this.storageService);
|
2018-05-04 20:16:28 +02:00
|
|
|
this.updaterMain = new UpdaterMain(this.i18nService, this.windowMain, 'desktop', () => {
|
|
|
|
this.menuMain.updateMenuItem.enabled = false;
|
|
|
|
}, () => {
|
|
|
|
this.menuMain.updateMenuItem.enabled = true;
|
|
|
|
}, () => {
|
|
|
|
this.menuMain.updateMenuItem.label = this.i18nService.t('restartToUpdate');
|
2018-11-27 14:32:04 +01:00
|
|
|
}, 'bitwarden');
|
2018-02-14 05:38:18 +01:00
|
|
|
this.menuMain = new MenuMain(this);
|
|
|
|
this.powerMonitorMain = new PowerMonitorMain(this);
|
2018-05-08 18:05:05 +02:00
|
|
|
this.trayMain = new TrayMain(this.windowMain, this.i18nService, this.storageService);
|
2018-04-25 21:43:26 +02:00
|
|
|
|
2021-02-03 19:21:22 +01:00
|
|
|
this.messagingService = new ElectronMainMessagingService(this.windowMain, message => {
|
2018-04-26 21:44:07 +02:00
|
|
|
this.messagingMain.onMessage(message);
|
|
|
|
});
|
|
|
|
|
2018-04-25 21:43:26 +02:00
|
|
|
this.keytarStorageListener = new KeytarStorageListener('Bitwarden');
|
2020-07-23 19:32:36 +02:00
|
|
|
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
const BiometricWindowsMain = require('jslib/electron/biometric.windows.main').default;
|
2020-12-21 11:31:33 +01:00
|
|
|
this.biometricMain = new BiometricWindowsMain(this.storageService, this.i18nService, this.windowMain);
|
2020-07-23 19:32:36 +02:00
|
|
|
} else if (process.platform === 'darwin') {
|
|
|
|
const BiometricDarwinMain = require('jslib/electron/biometric.darwin.main').default;
|
|
|
|
this.biometricMain = new BiometricDarwinMain(this.storageService, this.i18nService);
|
|
|
|
}
|
2020-10-05 15:11:37 +02:00
|
|
|
|
2020-10-12 21:18:28 +02:00
|
|
|
this.nativeMessagingMain = new NativeMessagingMain(this.logService, this.windowMain, app.getPath('userData'), app.getAppPath());
|
2018-02-14 05:38:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bootstrap() {
|
2018-04-25 21:43:26 +02:00
|
|
|
this.keytarStorageListener.init();
|
2018-04-25 05:53:20 +02:00
|
|
|
this.windowMain.init().then(async () => {
|
|
|
|
const locale = await this.storageService.get<string>(ConstantsService.localeKey);
|
|
|
|
await this.i18nService.init(locale != null ? locale : app.getLocale());
|
|
|
|
this.messagingMain.init();
|
|
|
|
this.menuMain.init();
|
2018-05-08 18:05:05 +02:00
|
|
|
await this.trayMain.init('Bitwarden', [{
|
2018-05-05 06:29:02 +02:00
|
|
|
label: this.i18nService.t('lockNow'),
|
|
|
|
enabled: false,
|
|
|
|
id: 'lockNow',
|
|
|
|
click: () => this.messagingService.send('lockVault'),
|
|
|
|
}]);
|
2019-02-02 18:27:06 +01:00
|
|
|
if (await this.storageService.get<boolean>(ElectronConstants.enableStartToTrayKey)) {
|
2019-02-02 18:16:32 +01:00
|
|
|
this.trayMain.hideToTray();
|
|
|
|
}
|
2019-03-12 04:18:04 +01:00
|
|
|
this.powerMonitorMain.init();
|
|
|
|
await this.updaterMain.init();
|
2020-07-23 19:32:36 +02:00
|
|
|
if (this.biometricMain != null) {
|
|
|
|
await this.biometricMain.init();
|
|
|
|
}
|
2020-08-21 15:50:36 +02:00
|
|
|
|
2020-10-07 15:11:01 +02:00
|
|
|
if (await this.storageService.get<boolean>(ElectronConstants.enableBrowserIntegration)) {
|
2020-10-12 21:18:28 +02:00
|
|
|
this.nativeMessagingMain.listen();
|
2020-10-07 15:11:01 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 15:50:36 +02:00
|
|
|
if (!app.isDefaultProtocolClient('bitwarden')) {
|
|
|
|
app.setAsDefaultProtocolClient('bitwarden');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process protocol for macOS
|
|
|
|
app.on('open-url', (event, url) => {
|
|
|
|
event.preventDefault();
|
|
|
|
this.processDeepLink([url]);
|
|
|
|
});
|
2020-11-04 18:09:21 +01:00
|
|
|
|
|
|
|
// Handle window visibility events
|
|
|
|
this.windowMain.win.on('hide', () => {
|
|
|
|
this.messagingService.send('windowHidden');
|
|
|
|
});
|
|
|
|
this.windowMain.win.on('minimize', () => {
|
|
|
|
this.messagingService.send('windowHidden');
|
|
|
|
});
|
2018-04-25 05:53:20 +02:00
|
|
|
}, (e: any) => {
|
|
|
|
// tslint:disable-next-line
|
|
|
|
console.error(e);
|
2018-02-14 05:38:18 +01:00
|
|
|
});
|
|
|
|
}
|
2020-08-21 15:50:36 +02:00
|
|
|
|
|
|
|
private processDeepLink(argv: string[]): void {
|
2021-02-03 19:21:22 +01:00
|
|
|
argv.filter(s => s.indexOf('bitwarden://') === 0).forEach(s => {
|
2020-08-21 15:50:36 +02:00
|
|
|
const url = new URL(s);
|
|
|
|
const code = url.searchParams.get('code');
|
|
|
|
const receivedState = url.searchParams.get('state');
|
|
|
|
if (code != null && receivedState != null) {
|
|
|
|
this.messagingService.send('ssoCallback', { code: code, state: receivedState });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-01-16 23:30:57 +01:00
|
|
|
}
|