diff --git a/package-lock.json b/package-lock.json index 4ee0a01a..9fbb1d11 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7559,6 +7559,11 @@ "integrity": "sha512-GguP+DRY+pJ3soyIiGPTvdiVXjZ+DbXOxGpXn3eMvNW4x4irjqXm4wHKscC+TfxSJ0yw/S1F24tqdMNsMZTiLA==", "dev": true }, + "if-async": { + "version": "3.7.4", + "resolved": "https://registry.npmjs.org/if-async/-/if-async-3.7.4.tgz", + "integrity": "sha1-VYaN6wCT08Z79xZudFNT+5vLIaI=" + }, "iferr": { "version": "0.1.5", "resolved": "https://registry.npmjs.org/iferr/-/iferr-0.1.5.tgz", @@ -11339,6 +11344,49 @@ "integrity": "sha512-Ts1Y/anZELhSsjMcU605fU9RE4Oi3p5ORujwbIKXfWa+0Zxs510Qrmrce5/Jowq3cHSZSJqBjypxmHarc+vEWg==", "dev": true }, + "regedit": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/regedit/-/regedit-3.0.3.tgz", + "integrity": "sha512-SpHmMKOtiEYx0MiRRC48apBsmThoZ4svZNsYoK8leHd5bdUHV1nYb8pk8gh6Moou7/S9EDi1QsjBTpyXVQrPuQ==", + "requires": { + "debug": "^4.1.0", + "if-async": "^3.7.4", + "stream-slicer": "0.0.6", + "through2": "^0.6.3" + }, + "dependencies": { + "isarray": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz", + "integrity": "sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8=" + }, + "readable-stream": { + "version": "1.0.34", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.0.34.tgz", + "integrity": "sha1-Elgg40vIQtLyqq+v5MKRbuMsFXw=", + "requires": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.1", + "isarray": "0.0.1", + "string_decoder": "~0.10.x" + } + }, + "string_decoder": { + "version": "0.10.31", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz", + "integrity": "sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ=" + }, + "through2": { + "version": "0.6.5", + "resolved": "https://registry.npmjs.org/through2/-/through2-0.6.5.tgz", + "integrity": "sha1-QaucZ7KdVyCQcUEOHXp6lozTrUg=", + "requires": { + "readable-stream": ">=1.0.33-1 <1.1.0-0", + "xtend": ">=4.0.0 <4.1.0-0" + } + } + } + }, "regenerate": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/regenerate/-/regenerate-1.4.0.tgz", @@ -12425,6 +12473,11 @@ "integrity": "sha1-1cdSgl5TZ+eG944Y5EXqIjoVWVI=", "dev": true }, + "stream-slicer": { + "version": "0.0.6", + "resolved": "https://registry.npmjs.org/stream-slicer/-/stream-slicer-0.0.6.tgz", + "integrity": "sha1-+GsqxcJEC3oKh7cfM2ZcB4gEYTg=" + }, "strict-uri-encode": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/strict-uri-encode/-/strict-uri-encode-1.1.0.tgz", diff --git a/package.json b/package.json index 9a26468a..f41ca3cf 100644 --- a/package.json +++ b/package.json @@ -127,6 +127,13 @@ { "from": "build/proxys/app-win.exe", "to": "proxy.exe" + }, + { + "from": "node_modules/regedit/vbs", + "to": "regedit/vbs", + "filter": [ + "**/*" + ] } ] }, @@ -312,6 +319,7 @@ "node-ipc": "^9.1.1", "nord": "0.2.1", "papaparse": "4.6.0", + "regedit": "^3.0.3", "rxjs": "6.6.2", "sweetalert2": "9.8.1", "tslib": "^2.0.1", diff --git a/proxy/ipc.ts b/proxy/ipc.ts index 1737750b..a8be8ac1 100644 --- a/proxy/ipc.ts +++ b/proxy/ipc.ts @@ -1,8 +1,5 @@ /* tslint:disable:no-console */ import * as ipc from 'node-ipc'; -import { spawn } from 'child_process'; - -const StartDesktopCooldown = 60 * 1000; // 1 minute delay between attempts to start desktop ipc.config.id = 'proxy'; ipc.config.retry = 1500; @@ -10,7 +7,6 @@ ipc.config.logger = console.warn; // Stdout is used for native messaging export default class IPC { private connected = false; - private lastStartedDesktop = 0; connect() { ipc.connectTo('bitwarden', () => { @@ -32,30 +28,19 @@ export default class IPC { console.error('got a message from world : ', data); }); + /* ipc.of.bitwarden.on('error', (err: any) => { - if (err.syscall === 'connect' && this.lastStartedDesktop + StartDesktopCooldown < Date.now()) { - this.lastStartedDesktop = Date.now(); - console.error('Attempting to start Bitwarden desktop application'); - this.startDesktop(); - } console.error('error', err); }); + */ }); } - isConnected() { + isConnected(): boolean { return this.connected; } send(json: object) { ipc.of.bitwarden.emit('message', json); } - - // TODO: Do we want to start the desktop application? How do we get the install path? - private startDesktop() { - spawn( - 'C:\\Users\\Oscar\\Documents\\Projects\\Bitwarden\\desktop\\dist\\Bitwarden-Portable-1.22.2.exe', - { detached: true } - ); - } } diff --git a/scripts/chrome.json b/scripts/chrome.json deleted file mode 100644 index 15c5a491..00000000 --- a/scripts/chrome.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "name": "com.8bit.bitwarden", - "description": "Bitwarden Desktop Bridge", - "path": "C:\\Users\\Oscar\\Documents\\Projects\\Bitwarden\\desktop\\build\\proxys\\app-win.exe", - "type": "stdio", - "allowed_origins": ["chrome-extension://ijeheppnniijonkinoakkofcdhdfojda/"] -} diff --git a/src/main.ts b/src/main.ts index 0a12dc29..450ac644 100644 --- a/src/main.ts +++ b/src/main.ts @@ -119,7 +119,7 @@ export class Main { this.biometricMain = new BiometricDarwinMain(this.storageService, this.i18nService); } - this.nativeMessagingService = new NativeMessagingService(); + this.nativeMessagingService = new NativeMessagingService(app.getPath('userData'), app.getAppPath()); } bootstrap() { @@ -158,6 +158,8 @@ export class Main { console.error(e); }); this.nativeMessagingService.listen(); + this.nativeMessagingService.generateManifests(); + this.nativeMessagingService.enableManifest(); } private processDeepLink(argv: string[]): void { diff --git a/src/services/nativeMessaging.service.ts b/src/services/nativeMessaging.service.ts index 5af2647e..236f5ff4 100644 --- a/src/services/nativeMessaging.service.ts +++ b/src/services/nativeMessaging.service.ts @@ -1,8 +1,13 @@ +import * as fs from 'fs'; import * as ipc from 'node-ipc'; +import * as path from 'path'; +import * as util from 'util'; export class NativeMessagingService { private connected = false; + constructor(private userPath: string, private appPath: string) {} + listen() { ipc.config.id = 'bitwarden'; ipc.config.retry = 1500; @@ -30,4 +35,86 @@ export class NativeMessagingService { ipc.server.start(); } + + generateManifests() { + const baseJson = { + 'name': 'com.8bit.bitwarden', + 'description': 'Bitwarden desktop <-> browser bridge', + 'path': path.join(this.appPath, 'proxys', this.binaryName()), + 'type': 'stdio', + } + + const firefoxJson = {...baseJson, ...{ 'allowed_origins': ['446900e4-71c2-419f-a6a7-df9c091e268b']}} + const chromeJson = {...baseJson, ...{ 'allowed_origins': ['chrome-extension://ijeheppnniijonkinoakkofcdhdfojda/']}} + + fs.mkdir(path.join(this.userPath, 'browsers'), (err) => console.log); + + this.writeManifest('firefox.json', firefoxJson); + this.writeManifest('chrome.json', chromeJson); + } + + private writeManifest(filename: string, manifest: object) { + fs.writeFile( + path.join(this.userPath, 'browsers', filename), + JSON.stringify(manifest, null, 2), + (err) => console.log + ); + } + + private binaryName() { + switch (process.platform) { + case 'win32': + return 'app-win.exe' + case 'darwin': + return 'app-linux' + case 'linux': + default: + return 'app-macos' + } + } + + // Setup registry and/or directories + // TODO: Do other browsers use different directories? + enableManifest() { + switch (process.platform) { + case 'win32': + this.createWindowsRegistry('HKLM\\SOFTWARE\\Mozilla\\Firefox', 'HKCU\\SOFTWARE\\Mozilla\\NativeMessagingHosts\\com.8bit.bitwarden', 'firefox.json') + this.createWindowsRegistry('HKCU\\SOFTWARE\\Google\\Chrome', 'HKCU\\SOFTWARE\\Google\\Chrome\\NativeMessagingHosts\\com.8bit.bitwarden', 'chrome.json') + break; + case 'darwin': + break; + case 'linux': + default: + break; + } + } + + private createWindowsRegistry(check: string, location: string, jsonFile: string) { + const regedit = require('regedit'); + regedit.setExternalVBSLocation('resources/regedit/vbs'); + + const list = util.promisify(regedit.list); + const createKey = util.promisify(regedit.createKey); + const putValue = util.promisify(regedit.putValue); + + // Check installed + list(check) + .then(() => { + // Create path + return createKey(location); + }) + .then(() => { + // Insert path to manifest + const obj: any = {}; + obj[location] = { + 'default': { + value: path.join(this.userPath, 'browsers', jsonFile), + type: 'REG_DEFAULT', + }, + } + + return putValue(obj); + }) + .catch() + } }