diff --git a/src/shared/utils/machineId.ts b/src/shared/utils/machineId.ts index 02a2fff9..82b7ea4b 100644 --- a/src/shared/utils/machineId.ts +++ b/src/shared/utils/machineId.ts @@ -1,17 +1,52 @@ -import { machineIdSync } from "node-machine-id"; +import { execSync, execFileSync } from "child_process"; + +function getMachineIdRaw(): string { + if (process.platform === "win32") { + const sysRoot = process.env.SystemRoot || process.env.windir || "C:\\Windows"; + const output = execFileSync( + `${sysRoot}\\System32\\REG.exe`, + ["QUERY", "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography", "/v", "MachineGuid"], + { encoding: "utf8" } + ); + return ( + output + .toString() + .split("REG_SZ")[1] + ?.replace(/\r+|\n+|\s+/gi, "") + ?.toLowerCase() ?? "" + ); + } + if (process.platform === "darwin") { + const output = execSync("ioreg -rd1 -c IOPlatformExpertDevice", { encoding: "utf8" }); + return ( + output + .split("IOPlatformUUID")[1] + ?.split("\n")[0] + ?.replace(/\=|\s+|\"/gi, "") + ?.toLowerCase() ?? "" + ); + } + const output = execSync( + "( cat /var/lib/dbus/machine-id /etc/machine-id 2> /dev/null || hostname ) | head -n 1 || :", + { encoding: "utf8" } + ); + return output + .toString() + .replace(/\r+|\n+|\s+/gi, "") + .toLowerCase(); +} /** - * Get consistent machine ID using node-machine-id with salt + * Get consistent machine ID using native registry/OS query with salt * This ensures the same physical machine gets the same ID across runs * * @param {string} salt - Optional salt to use (defaults to environment variable) * @returns {Promise} Machine ID (16-character base32) */ export async function getConsistentMachineId(salt = null) { - // For server-side, use node-machine-id with salt const saltValue = salt || process.env.MACHINE_ID_SALT || "endpoint-proxy-salt"; try { - const rawMachineId = machineIdSync(); + const rawMachineId = getMachineIdRaw(); // Create consistent ID using salt const crypto = await import("crypto"); const hashedMachineId = crypto @@ -41,9 +76,8 @@ export async function getConsistentMachineId(salt = null) { * @returns {Promise} Raw machine ID */ export async function getRawMachineId() { - // For server-side, use raw node-machine-id try { - return machineIdSync(); + return getMachineIdRaw(); } catch (error) { console.log("Error getting raw machine ID:", error); // Fallback to random ID if node-machine-id fails