2018-02-15 21:27:56 +01:00
|
|
|
import * as path from "path";
|
2018-01-16 21:58:17 +01:00
|
|
|
|
2022-02-24 20:50:19 +01:00
|
|
|
import { app } from "electron";
|
2018-04-24 22:30:47 +02:00
|
|
|
|
2023-06-06 22:34:53 +02:00
|
|
|
import { StateFactory } from "@bitwarden/common/platform/factories/state-factory";
|
|
|
|
import { GlobalState } from "@bitwarden/common/platform/models/domain/global-state";
|
|
|
|
import { MemoryStorageService } from "@bitwarden/common/platform/services/memory-storage.service";
|
2021-12-15 23:32:00 +01:00
|
|
|
|
2022-04-05 16:54:44 +02:00
|
|
|
import { MenuMain } from "./main/menu/menu.main";
|
2022-02-24 20:50:19 +01:00
|
|
|
import { MessagingMain } from "./main/messaging.main";
|
2022-11-24 15:05:45 +01:00
|
|
|
import { NativeMessagingMain } from "./main/native-messaging.main";
|
|
|
|
import { PowerMonitorMain } from "./main/power-monitor.main";
|
2022-12-02 12:45:09 +01:00
|
|
|
import { TrayMain } from "./main/tray.main";
|
|
|
|
import { UpdaterMain } from "./main/updater.main";
|
|
|
|
import { WindowMain } from "./main/window.main";
|
2022-02-01 00:00:37 +01:00
|
|
|
import { Account } from "./models/account";
|
2023-06-06 22:34:53 +02:00
|
|
|
import { BiometricsService, BiometricsServiceAbstraction } from "./platform/main/biometric/index";
|
|
|
|
import { DesktopCredentialStorageListener } from "./platform/main/desktop-credential-storage-listener";
|
|
|
|
import { ElectronLogService } from "./platform/services/electron-log.service";
|
|
|
|
import { ElectronStateService } from "./platform/services/electron-state.service";
|
|
|
|
import { ElectronStorageService } from "./platform/services/electron-storage.service";
|
|
|
|
import { I18nService } from "./platform/services/i18n.service";
|
2022-12-02 12:45:09 +01:00
|
|
|
import { ElectronMainMessagingService } from "./services/electron-main-messaging.service";
|
2022-01-19 17:00:28 +01:00
|
|
|
|
2018-02-14 05:38:18 +01:00
|
|
|
export class Main {
|
2018-04-24 22:30:47 +02:00
|
|
|
logService: ElectronLogService;
|
2022-12-21 23:46:23 +01:00
|
|
|
i18nService: I18nService;
|
2019-03-12 03:37:13 +01:00
|
|
|
storageService: ElectronStorageService;
|
2022-06-27 19:38:12 +02:00
|
|
|
memoryStorageService: MemoryStorageService;
|
2018-04-26 21:44:07 +02:00
|
|
|
messagingService: ElectronMainMessagingService;
|
2023-04-18 15:09:47 +02:00
|
|
|
stateService: ElectronStateService;
|
2022-04-05 16:54:44 +02:00
|
|
|
desktopCredentialStorageListener: DesktopCredentialStorageListener;
|
2021-12-20 15:47:17 +01:00
|
|
|
|
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;
|
2023-03-10 21:32:26 +01:00
|
|
|
biometricsService: BiometricsServiceAbstraction;
|
2020-10-12 21:18:28 +02:00
|
|
|
nativeMessagingMain: NativeMessagingMain;
|
2021-12-20 15:47:17 +01:00
|
|
|
|
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
|
|
|
|
2018-02-20 22:04:35 +01:00
|
|
|
app.on("ready", () => {
|
2021-07-12 16:11:43 +02:00
|
|
|
// on ready stuff...
|
2018-02-25 15:51:21 +01:00
|
|
|
});
|
2018-02-15 21:27:56 +01:00
|
|
|
|
2018-02-14 05:38:18 +01:00
|
|
|
if (appDataPath != null) {
|
|
|
|
app.setPath("userData", appDataPath);
|
2021-12-20 15:47:17 +01:00
|
|
|
}
|
2021-02-03 19:21:22 +01:00
|
|
|
app.setPath("logs", path.join(app.getPath("userData"), "logs"));
|
2018-02-14 05:38:18 +01:00
|
|
|
|
|
|
|
const args = process.argv.slice(1);
|
|
|
|
const watch = args.some((val) => val === "--watch");
|
|
|
|
|
2021-12-15 23:32:00 +01:00
|
|
|
if (watch) {
|
2022-11-10 15:06:08 +01:00
|
|
|
const execName = process.platform === "win32" ? "electron.cmd" : "electron";
|
2022-02-24 20:50:19 +01:00
|
|
|
// eslint-disable-next-line
|
2022-11-10 15:06:08 +01:00
|
|
|
require("electron-reload")(__dirname, {
|
|
|
|
electron: path.join(__dirname, "../../../", "node_modules", ".bin", execName),
|
|
|
|
electronArgv: ["--inspect=5858", "--watch"],
|
|
|
|
});
|
2018-04-26 21:44:07 +02:00
|
|
|
}
|
|
|
|
|
2018-04-24 22:30:47 +02:00
|
|
|
this.logService = new ElectronLogService(null, app.getPath("userData"));
|
2022-12-21 23:46:23 +01:00
|
|
|
this.i18nService = new I18nService("en", "./locales/");
|
2021-12-20 15:47:17 +01:00
|
|
|
|
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"
|
2021-12-15 23:32:00 +01:00
|
|
|
storageDefaults["global.vaultTimeout"] = -1;
|
|
|
|
storageDefaults["global.vaultTimeoutAction"] = "lock";
|
2019-03-12 03:37:13 +01:00
|
|
|
this.storageService = new ElectronStorageService(app.getPath("userData"), storageDefaults);
|
2022-06-27 19:38:12 +02:00
|
|
|
this.memoryStorageService = new MemoryStorageService();
|
2021-12-20 15:47:17 +01:00
|
|
|
|
2021-12-15 23:32:00 +01:00
|
|
|
// TODO: this state service will have access to on disk storage, but not in memory storage.
|
|
|
|
// If we could get this to work using the stateService singleton that the rest of the app uses we could save
|
|
|
|
// ourselves from some hacks, like having to manually update the app menu vs. the menu subscribing to events.
|
2023-04-18 15:09:47 +02:00
|
|
|
this.stateService = new ElectronStateService(
|
2022-01-19 17:00:28 +01:00
|
|
|
this.storageService,
|
|
|
|
null,
|
2022-06-27 19:38:12 +02:00
|
|
|
this.memoryStorageService,
|
2022-01-19 17:00:28 +01:00
|
|
|
this.logService,
|
2022-03-15 14:25:43 +01:00
|
|
|
new StateFactory(GlobalState, Account),
|
|
|
|
false // Do not use disk caching because this will get out of sync with the renderer service
|
2022-01-19 17:00:28 +01:00
|
|
|
);
|
2021-12-20 15:47:17 +01:00
|
|
|
|
2021-12-15 23:32:00 +01:00
|
|
|
this.windowMain = new WindowMain(
|
|
|
|
this.stateService,
|
|
|
|
this.logService,
|
2023-07-14 13:10:40 +02:00
|
|
|
this.storageService,
|
2021-02-03 19:21:22 +01:00
|
|
|
(arg) => this.processDeepLink(arg),
|
|
|
|
(win) => this.trayMain.setupWindowListeners(win)
|
2021-12-20 15:47:17 +01:00
|
|
|
);
|
2021-12-15 23:32:00 +01:00
|
|
|
this.messagingMain = new MessagingMain(this, this.stateService);
|
2023-03-10 21:32:26 +01:00
|
|
|
this.updaterMain = new UpdaterMain(this.i18nService, this.windowMain);
|
2021-12-15 23:32:00 +01:00
|
|
|
this.trayMain = new TrayMain(this.windowMain, this.i18nService, this.stateService);
|
2021-12-20 15:47:17 +01: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);
|
2021-12-20 15:47:17 +01:00
|
|
|
});
|
2023-03-10 21:32:26 +01:00
|
|
|
this.powerMonitorMain = new PowerMonitorMain(this.messagingService);
|
|
|
|
this.menuMain = new MenuMain(
|
|
|
|
this.i18nService,
|
|
|
|
this.messagingService,
|
|
|
|
this.stateService,
|
|
|
|
this.windowMain,
|
|
|
|
this.updaterMain
|
|
|
|
);
|
2021-12-20 15:47:17 +01:00
|
|
|
|
2023-03-10 21:32:26 +01:00
|
|
|
this.biometricsService = new BiometricsService(
|
|
|
|
this.i18nService,
|
|
|
|
this.windowMain,
|
|
|
|
this.stateService,
|
|
|
|
this.logService,
|
|
|
|
this.messagingService,
|
|
|
|
process.platform
|
|
|
|
);
|
2020-07-23 19:32:36 +02:00
|
|
|
|
2022-04-05 16:54:44 +02:00
|
|
|
this.desktopCredentialStorageListener = new DesktopCredentialStorageListener(
|
|
|
|
"Bitwarden",
|
2023-04-18 15:09:47 +02:00
|
|
|
this.biometricsService,
|
|
|
|
this.logService
|
2022-04-05 16:54:44 +02:00
|
|
|
);
|
2021-12-20 15:47:17 +01:00
|
|
|
|
2021-12-15 23:32:00 +01:00
|
|
|
this.nativeMessagingMain = new NativeMessagingMain(
|
|
|
|
this.logService,
|
|
|
|
this.windowMain,
|
|
|
|
app.getPath("userData"),
|
2021-03-25 23:11:31 +01:00
|
|
|
app.getPath("exe")
|
2021-12-20 15:47:17 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-12-15 23:32:00 +01:00
|
|
|
bootstrap() {
|
2022-04-05 16:54:44 +02:00
|
|
|
this.desktopCredentialStorageListener.init();
|
2021-12-15 23:32:00 +01:00
|
|
|
this.windowMain.init().then(
|
2020-07-23 19:32:36 +02:00
|
|
|
async () => {
|
2021-12-15 23:32:00 +01:00
|
|
|
const locale = await this.stateService.getLocale();
|
|
|
|
await this.i18nService.init(locale != null ? locale : app.getLocale());
|
|
|
|
this.messagingMain.init();
|
2018-04-25 05:53:20 +02:00
|
|
|
this.menuMain.init();
|
2021-12-15 23:32:00 +01:00
|
|
|
await this.trayMain.init("Bitwarden", [
|
2021-12-20 15:47:17 +01:00
|
|
|
{
|
2022-02-14 14:40:06 +01:00
|
|
|
label: this.i18nService.t("lockVault"),
|
2021-12-15 23:32:00 +01:00
|
|
|
enabled: false,
|
2022-02-14 14:40:06 +01:00
|
|
|
id: "lockVault",
|
2018-05-05 06:29:02 +02:00
|
|
|
click: () => this.messagingService.send("lockVault"),
|
2021-12-20 15:47:17 +01:00
|
|
|
},
|
|
|
|
]);
|
2021-12-15 23:32:00 +01:00
|
|
|
if (await this.stateService.getEnableStartToTray()) {
|
2019-02-02 18:16:32 +01:00
|
|
|
this.trayMain.hideToTray();
|
2021-12-20 15:47:17 +01:00
|
|
|
}
|
2019-03-12 04:18:04 +01:00
|
|
|
this.powerMonitorMain.init();
|
2021-12-15 23:32:00 +01:00
|
|
|
await this.updaterMain.init();
|
2023-03-10 21:32:26 +01:00
|
|
|
if (this.biometricsService != null) {
|
|
|
|
await this.biometricsService.init();
|
2020-07-23 19:32:36 +02:00
|
|
|
}
|
2020-10-05 15:11:37 +02:00
|
|
|
|
2022-09-23 21:47:17 +02:00
|
|
|
if (
|
|
|
|
(await this.stateService.getEnableBrowserIntegration()) ||
|
|
|
|
(await this.stateService.getEnableDuckDuckGoBrowserIntegration())
|
|
|
|
) {
|
2021-06-07 23:16:45 +02:00
|
|
|
this.nativeMessagingMain.listen();
|
2021-12-20 15:47:17 +01:00
|
|
|
}
|
2021-06-07 23:16:45 +02:00
|
|
|
|
2021-03-25 23:11:31 +01:00
|
|
|
app.removeAsDefaultProtocolClient("bitwarden");
|
|
|
|
if (process.env.NODE_ENV === "development" && process.platform === "win32") {
|
|
|
|
// Fix development build on Windows requirering a different protocol client
|
|
|
|
app.setAsDefaultProtocolClient("bitwarden", process.execPath, [
|
2021-12-07 21:04:52 +01:00
|
|
|
process.argv[1],
|
2021-03-25 23:11:31 +01:00
|
|
|
path.resolve(process.argv[2]),
|
2021-12-20 15:47:17 +01:00
|
|
|
]);
|
|
|
|
} else {
|
2021-03-25 23:11:31 +01:00
|
|
|
app.setAsDefaultProtocolClient("bitwarden");
|
2018-02-14 05:38:18 +01:00
|
|
|
}
|
|
|
|
|
2021-12-15 23:32:00 +01:00
|
|
|
// Process protocol for macOS
|
2021-12-07 21:04:52 +01:00
|
|
|
app.on("open-url", (event, url) => {
|
2020-08-21 15:50:36 +02:00
|
|
|
event.preventDefault();
|
|
|
|
this.processDeepLink([url]);
|
2018-02-14 05:38:18 +01:00
|
|
|
});
|
2020-08-21 15:50:36 +02:00
|
|
|
|
2021-02-03 19:21:22 +01:00
|
|
|
// Handle window visibility events
|
|
|
|
this.windowMain.win.on("hide", () => {
|
2020-08-21 15:50:36 +02:00
|
|
|
this.messagingService.send("windowHidden");
|
|
|
|
});
|
2020-11-04 18:09:21 +01:00
|
|
|
this.windowMain.win.on("minimize", () => {
|
|
|
|
this.messagingService.send("windowHidden");
|
2021-12-20 15:47:17 +01:00
|
|
|
});
|
|
|
|
},
|
2018-04-25 05:53:20 +02:00
|
|
|
(e: any) => {
|
2022-02-24 20:50:19 +01:00
|
|
|
// eslint-disable-next-line
|
2018-04-25 05:53:20 +02:00
|
|
|
console.error(e);
|
2020-08-21 15:50:36 +02:00
|
|
|
}
|
2021-12-20 15:47:17 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-08-21 15:50:36 +02:00
|
|
|
private processDeepLink(argv: string[]): void {
|
2021-12-20 15:47:17 +01:00
|
|
|
argv
|
2021-02-03 19:21:22 +01:00
|
|
|
.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 });
|
2021-12-20 15:47:17 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-01-16 23:30:57 +01:00
|
|
|
}
|