2022-02-24 20:50:19 +01:00
|
|
|
/* eslint-disable no-console */
|
2023-04-11 16:45:24 +02:00
|
|
|
import { createHash } from "crypto";
|
|
|
|
import { existsSync, mkdirSync } from "fs";
|
2021-01-26 18:01:46 +01:00
|
|
|
import { homedir } from "os";
|
2023-04-11 16:45:24 +02:00
|
|
|
import { join as path_join } from "path";
|
2020-10-05 15:11:37 +02:00
|
|
|
|
2022-02-24 20:50:19 +01:00
|
|
|
import * as ipc from "node-ipc";
|
|
|
|
|
2023-04-11 16:45:24 +02:00
|
|
|
export function getIpcSocketRoot(): string | null {
|
|
|
|
let socketRoot = null;
|
|
|
|
|
|
|
|
switch (process.platform) {
|
|
|
|
case "darwin": {
|
|
|
|
const ipcSocketRootDir = path_join(homedir(), "tmp");
|
|
|
|
if (!existsSync(ipcSocketRootDir)) {
|
|
|
|
mkdirSync(ipcSocketRootDir);
|
|
|
|
}
|
|
|
|
socketRoot = ipcSocketRootDir + "/";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "win32": {
|
|
|
|
// Let node-ipc use a unique IPC pipe //./pipe/xxxxxxxxxxxxxxxxx.app.bitwarden per user.
|
|
|
|
// Hashing prevents problems with reserved characters and file length limitations.
|
|
|
|
socketRoot = createHash("sha1").update(homedir()).digest("hex") + ".";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return socketRoot;
|
|
|
|
}
|
|
|
|
|
2020-10-05 15:11:37 +02:00
|
|
|
ipc.config.id = "proxy";
|
|
|
|
ipc.config.retry = 1500;
|
|
|
|
ipc.config.logger = console.warn; // Stdout is used for native messaging
|
2023-04-11 16:45:24 +02:00
|
|
|
const ipcSocketRoot = getIpcSocketRoot();
|
|
|
|
if (ipcSocketRoot != null) {
|
|
|
|
ipc.config.socketRoot = ipcSocketRoot;
|
2021-01-26 19:11:36 +01:00
|
|
|
}
|
2020-10-05 15:11:37 +02:00
|
|
|
|
|
|
|
export default class IPC {
|
2021-02-03 19:21:22 +01:00
|
|
|
onMessage: (message: object) => void;
|
2020-11-23 18:37:04 +01:00
|
|
|
|
2020-10-05 15:11:37 +02:00
|
|
|
private connected = false;
|
|
|
|
|
|
|
|
connect() {
|
|
|
|
ipc.connectTo("bitwarden", () => {
|
|
|
|
ipc.of.bitwarden.on("connect", () => {
|
|
|
|
this.connected = true;
|
2021-05-03 09:59:40 +02:00
|
|
|
console.error("## connected to bitwarden desktop ##");
|
2020-10-21 16:48:40 +02:00
|
|
|
|
|
|
|
// Notify browser extension, connection is established to desktop application.
|
2021-02-03 19:21:22 +01:00
|
|
|
this.onMessage({ command: "connected" });
|
2020-10-05 15:11:37 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
ipc.of.bitwarden.on("disconnect", () => {
|
|
|
|
this.connected = false;
|
|
|
|
console.error("disconnected from world");
|
2020-10-21 16:48:40 +02:00
|
|
|
|
|
|
|
// Notify browser extension, no connection to desktop application.
|
2021-02-03 19:21:22 +01:00
|
|
|
this.onMessage({ command: "disconnected" });
|
2020-10-05 15:11:37 +02:00
|
|
|
});
|
|
|
|
|
2020-10-11 20:41:10 +02:00
|
|
|
ipc.of.bitwarden.on("message", (message: any) => {
|
|
|
|
this.onMessage(message);
|
2020-10-05 15:11:37 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
ipc.of.bitwarden.on("error", (err: any) => {
|
|
|
|
console.error("error", err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-05 19:48:51 +02:00
|
|
|
isConnected(): boolean {
|
2020-10-05 15:11:37 +02:00
|
|
|
return this.connected;
|
|
|
|
}
|
|
|
|
|
|
|
|
send(json: object) {
|
|
|
|
ipc.of.bitwarden.emit("message", json);
|
|
|
|
}
|
|
|
|
}
|