1
0
mirror of https://github.com/bitwarden/desktop synced 2026-01-06 02:23:24 +00:00

Disconnect active connections when closing native messaging. Fix exe path on windows (#865)

This commit is contained in:
Oscar Hinton
2021-05-03 09:59:40 +02:00
committed by GitHub
parent 163fa2aa41
commit 20b8a83dd8
5 changed files with 28 additions and 14 deletions

4
src/global.d.ts vendored
View File

@@ -1,6 +1,2 @@
declare function escape(s: string): string;
declare function unescape(s: string): string;
declare module 'node-ipc' {
const x: any;
export = x;
}

View File

@@ -1,4 +1,5 @@
import { existsSync, promises as fs } from 'fs';
import { Socket } from 'net';
import * as ipc from 'node-ipc';
import { homedir, userInfo } from 'os';
import * as path from 'path';
@@ -9,7 +10,7 @@ import { LogService } from 'jslib/abstractions/log.service';
import { WindowMain } from 'jslib/electron/window.main';
export class NativeMessagingMain {
private connected = false;
private connected: Socket[] = [];
private socket: any;
constructor(private logService: LogService, private windowMain: WindowMain, private userPath: string, private exePath: string) {}
@@ -36,14 +37,18 @@ export class NativeMessagingMain {
}
});
ipc.server.on('connect', () => {
this.connected = true;
ipc.server.on('connect', (socket: Socket) => {
this.connected.push(socket);
});
ipc.server.on(
'socket.disconnected',
(socket: any, destroyedSocketID: any) => {
this.connected = false;
(socket, destroyedSocketID) => {
const index = this.connected.indexOf(socket);
if (index > -1) {
this.connected.splice(index, 1);
}
this.socket = null;
ipc.log(
'client ' + destroyedSocketID + ' has disconnected!'
@@ -57,6 +62,12 @@ export class NativeMessagingMain {
stop() {
ipc.server.stop();
// Kill all existing connections
this.connected.forEach(socket => {
if (!socket.destroyed) {
socket.destroy();
}
});
}
send(message: object, socket: any) {
@@ -184,7 +195,7 @@ export class NativeMessagingMain {
private binaryPath() {
if (process.platform === 'win32') {
return path.join(path.basename(this.exePath), 'resources', 'native-messaging.bat');
return path.join(path.dirname(this.exePath), 'resources', 'native-messaging.bat');
}
return this.exePath;

View File

@@ -18,10 +18,7 @@ export default class IPC {
ipc.connectTo('bitwarden', () => {
ipc.of.bitwarden.on('connect', () => {
this.connected = true;
console.error(
'## connected to bitwarden desktop ##',
ipc.config.delay
);
console.error('## connected to bitwarden desktop ##');
// Notify browser extension, connection is established to desktop application.
this.onMessage({command: 'connected'});