1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-12-03 13:33:32 +01:00
bitwarden-browser/src/services/nativeMessaging.service.ts

127 lines
4.1 KiB
TypeScript
Raw Normal View History

2020-10-05 20:05:48 +02:00
import { promises as fs, existsSync } from 'fs';
import * as ipc from 'node-ipc';
import * as path from 'path';
import * as util from 'util';
2020-10-05 20:05:48 +02:00
import { LogService } from 'jslib/abstractions/log.service';
export class NativeMessagingService {
private connected = false;
2020-10-05 20:05:48 +02:00
constructor(private logService: LogService, private userPath: string, private appPath: string) {}
listen() {
ipc.config.id = 'bitwarden';
ipc.config.retry = 1500;
ipc.serve(() => {
ipc.server.on('message', (data: any, socket: any) => {
ipc.log('got a message : ', data);
ipc.server.emit(socket, 'message', data + ' world!');
});
ipc.server.on('connect', () => {
this.connected = true;
})
ipc.server.on(
'socket.disconnected',
(socket: any, destroyedSocketID: any) => {
this.connected = false;
ipc.log(
'client ' + destroyedSocketID + ' has disconnected!'
);
}
);
});
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/']}}
2020-10-05 20:05:48 +02:00
if (!existsSync(path.join(this.userPath, 'browsers'))) {
fs.mkdir(path.join(this.userPath, 'browsers'))
.catch(this.logService.error)
}
this.writeManifest('firefox.json', firefoxJson);
this.writeManifest('chrome.json', chromeJson);
}
2020-10-05 20:05:48 +02:00
// 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 writeManifest(filename: string, manifest: object) {
fs.writeFile(
path.join(this.userPath, 'browsers', filename),
2020-10-05 20:05:48 +02:00
JSON.stringify(manifest, null, 2)
).catch(this.logService.error);
}
private binaryName() {
switch (process.platform) {
case 'win32':
return 'app-win.exe'
case 'darwin':
return 'app-linux'
case 'linux':
default:
return 'app-macos'
}
}
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);
2020-10-05 20:05:48 +02:00
this.logService.debug(`Adding registry: ${location}`)
// 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);
})
2020-10-05 20:05:48 +02:00
.catch(this.logService.error)
}
}