mirror of
https://github.com/bitwarden/browser
synced 2025-12-18 01:03:35 +00:00
*Wip* Run proxy app through electron
This commit is contained in:
11
src/main.ts
11
src/main.ts
@@ -19,6 +19,7 @@ import { TrayMain } from 'jslib/electron/tray.main';
|
||||
import { UpdaterMain } from 'jslib/electron/updater.main';
|
||||
import { WindowMain } from 'jslib/electron/window.main';
|
||||
import { NativeMessagingMain } from './main/nativeMessaging.main';
|
||||
import { NativeMessagingProxy } from './proxy/native-messaging-proxy';
|
||||
|
||||
export class Main {
|
||||
logService: ElectronLogService;
|
||||
@@ -183,5 +184,11 @@ export class Main {
|
||||
}
|
||||
}
|
||||
|
||||
const main = new Main();
|
||||
main.bootstrap();
|
||||
console.error(process.argv);
|
||||
if (process.argv.some(arg => arg.indexOf('chrome-extension://') !== -1 || arg.indexOf('{') !== -1)) {
|
||||
const proxy = new NativeMessagingProxy();
|
||||
proxy.run();
|
||||
} else {
|
||||
const main = new Main();
|
||||
main.bootstrap();
|
||||
}
|
||||
|
||||
51
src/proxy/ipc.ts
Normal file
51
src/proxy/ipc.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
/* tslint:disable:no-console */
|
||||
import * as ipc from 'node-ipc';
|
||||
|
||||
ipc.config.id = 'proxy';
|
||||
ipc.config.retry = 1500;
|
||||
ipc.config.logger = console.warn; // Stdout is used for native messaging
|
||||
|
||||
export default class IPC {
|
||||
onMessage: (message: object) => void
|
||||
|
||||
private connected = false;
|
||||
|
||||
connect() {
|
||||
ipc.connectTo('bitwarden', () => {
|
||||
ipc.of.bitwarden.on('connect', () => {
|
||||
this.connected = true;
|
||||
console.error(
|
||||
'## connected to bitwarden desktop ##',
|
||||
ipc.config.delay
|
||||
);
|
||||
|
||||
// Notify browser extension, connection is established to desktop application.
|
||||
this.onMessage({command: 'connected'})
|
||||
});
|
||||
|
||||
ipc.of.bitwarden.on('disconnect', () => {
|
||||
this.connected = false;
|
||||
console.error('disconnected from world');
|
||||
|
||||
// Notify browser extension, no connection to desktop application.
|
||||
this.onMessage({command: 'disconnected'})
|
||||
});
|
||||
|
||||
ipc.of.bitwarden.on('message', (message: any) => {
|
||||
this.onMessage(message);
|
||||
});
|
||||
|
||||
ipc.of.bitwarden.on('error', (err: any) => {
|
||||
console.error('error', err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
isConnected(): boolean {
|
||||
return this.connected;
|
||||
}
|
||||
|
||||
send(json: object) {
|
||||
ipc.of.bitwarden.emit('message', json);
|
||||
}
|
||||
}
|
||||
23
src/proxy/native-messaging-proxy.ts
Normal file
23
src/proxy/native-messaging-proxy.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import NativeMessage from './nativemessage';
|
||||
import IPC from './ipc';
|
||||
|
||||
// Proxy is a lightweight application which provides bi-directional communication
|
||||
// between the browser extension and a running desktop application.
|
||||
//
|
||||
// Browser extension <-[native messaging]-> proxy <-[ipc]-> desktop
|
||||
export class NativeMessagingProxy {
|
||||
private ipc: IPC;
|
||||
private nativeMessage: NativeMessage;
|
||||
|
||||
constructor() {
|
||||
this.ipc = new IPC();
|
||||
this.nativeMessage = new NativeMessage(this.ipc);
|
||||
}
|
||||
|
||||
run() {
|
||||
this.ipc.connect();
|
||||
this.nativeMessage.listen();
|
||||
|
||||
this.ipc.onMessage = this.nativeMessage.send;
|
||||
}
|
||||
}
|
||||
84
src/proxy/nativemessage.ts
Normal file
84
src/proxy/nativemessage.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
/* tslint:disable:no-console */
|
||||
import IPC from './ipc';
|
||||
|
||||
// Mostly based on the example from MDN,
|
||||
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Native_messaging
|
||||
export default class NativeMessage {
|
||||
ipc: IPC;
|
||||
|
||||
constructor(ipc: IPC) {
|
||||
this.ipc = ipc;
|
||||
}
|
||||
|
||||
send(message: object) {
|
||||
const messageBuffer = Buffer.from(JSON.stringify(message));
|
||||
|
||||
const headerBuffer = Buffer.alloc(4);
|
||||
headerBuffer.writeUInt32LE(messageBuffer.length, 0);
|
||||
|
||||
process.stdout.write(Buffer.concat([headerBuffer, messageBuffer]));
|
||||
}
|
||||
|
||||
listen() {
|
||||
let payloadSize: number = null;
|
||||
|
||||
// A queue to store the chunks as we read them from stdin.
|
||||
// This queue can be flushed when `payloadSize` data has been read
|
||||
const chunks: any = [];
|
||||
|
||||
// Only read the size once for each payload
|
||||
const sizeHasBeenRead = () => Boolean(payloadSize);
|
||||
|
||||
// All the data has been read, reset everything for the next message
|
||||
const flushChunksQueue = () => {
|
||||
payloadSize = null;
|
||||
chunks.splice(0);
|
||||
};
|
||||
|
||||
const processData = () => {
|
||||
// Create one big buffer with all all the chunks
|
||||
const stringData = Buffer.concat(chunks);
|
||||
console.error(stringData);
|
||||
|
||||
// The browser will emit the size as a header of the payload,
|
||||
// if it hasn't been read yet, do it.
|
||||
// The next time we'll need to read the payload size is when all of the data
|
||||
// of the current payload has been read (ie. data.length >= payloadSize + 4)
|
||||
if (!sizeHasBeenRead()) {
|
||||
payloadSize = stringData.readUInt32LE(0);
|
||||
}
|
||||
|
||||
// If the data we have read so far is >= to the size advertised in the header,
|
||||
// it means we have all of the data sent.
|
||||
// We add 4 here because that's the size of the bytes that old the payloadSize
|
||||
if (stringData.length >= payloadSize + 4) {
|
||||
// Remove the header
|
||||
const contentWithoutSize = stringData
|
||||
.slice(4, payloadSize + 4)
|
||||
.toString();
|
||||
|
||||
// Reset the read size and the queued chunks
|
||||
flushChunksQueue();
|
||||
|
||||
const json = JSON.parse(contentWithoutSize);
|
||||
|
||||
// Forward to desktop application
|
||||
this.ipc.send(json);
|
||||
}
|
||||
};
|
||||
|
||||
process.stdin.on('readable', () => {
|
||||
// A temporary variable holding the nodejs.Buffer of each
|
||||
// chunk of data read off stdin
|
||||
let chunk = null;
|
||||
|
||||
// Read all of the available data
|
||||
// tslint:disable-next-line:no-conditional-assignment
|
||||
while ((chunk = process.stdin.read()) !== null) {
|
||||
chunks.push(chunk);
|
||||
}
|
||||
|
||||
processData();
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user