| import { execSync, execFileSync } from "child_process"; |
| import { existsSync, readFileSync } from "fs"; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function getMachineIdRaw(): string { |
| |
| try { |
| if (process.platform !== "win32") { |
| throw new Error("Not Windows"); |
| } |
| const sysRoot = process.env.SystemRoot || process.env.windir || "C:\\Windows"; |
| const regPath = `${sysRoot}\\System32\\REG.exe`; |
| if (existsSync(regPath)) { |
| const output = execFileSync( |
| regPath, |
| ["QUERY", "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography", "/v", "MachineGuid"], |
| { encoding: "utf8", timeout: 5000 } |
| ); |
| const id = output |
| .split("REG_SZ")[1] |
| ?.replace(/\r+|\n+|\s+/gi, "") |
| ?.toLowerCase(); |
| if (id && id.length > 8) return id; |
| } |
| } catch { |
| |
| } |
|
|
| |
| try { |
| if (process.platform !== "darwin") { |
| throw new Error("Not macOS"); |
| } |
| const output = execSync("ioreg -rd1 -c IOPlatformExpertDevice", { |
| encoding: "utf8", |
| timeout: 5000, |
| }); |
| if (output.includes("IOPlatformUUID")) { |
| const id = output |
| .split("IOPlatformUUID")[1] |
| ?.split("\n")[0] |
| ?.replace(/=|\s+|"/gi, "") |
| ?.toLowerCase(); |
| if (id && id.length > 8) return id; |
| } |
| } catch { |
| |
| } |
|
|
| |
| try { |
| for (const filePath of ["/etc/machine-id", "/var/lib/dbus/machine-id"]) { |
| if (existsSync(filePath)) { |
| const content = readFileSync(filePath, "utf8").trim().toLowerCase(); |
| if (content.length > 8) return content; |
| } |
| } |
| } catch { |
| |
| } |
|
|
| |
| try { |
| const hostname = execSync("hostname", { encoding: "utf8", timeout: 5000 }); |
| const id = hostname.trim().toLowerCase(); |
| if (id) return id; |
| } catch { |
| |
| } |
|
|
| |
| try { |
| const os = require("os"); |
| return os.hostname().toLowerCase(); |
| } catch { |
| |
| } |
|
|
| return "unknown-machine"; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export async function getConsistentMachineId(salt = null) { |
| const saltValue = salt || process.env.MACHINE_ID_SALT || "endpoint-proxy-salt"; |
| try { |
| const rawMachineId = getMachineIdRaw(); |
| |
| const crypto = await import("crypto"); |
| const hashedMachineId = crypto |
| .createHash("sha256") |
| .update(rawMachineId + saltValue) |
| .digest("hex"); |
| |
| return hashedMachineId.substring(0, 16); |
| } catch (error) { |
| console.log("Error getting machine ID:", error); |
| |
| try { |
| const cryptoFallback = await import("crypto"); |
| return cryptoFallback.randomUUID(); |
| } catch { |
| return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) { |
| const r = (Math.random() * 16) | 0; |
| const v = c == "x" ? r : (r & 0x3) | 0x8; |
| return v.toString(16); |
| }); |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| export async function getRawMachineId() { |
| try { |
| return getMachineIdRaw(); |
| } catch (error) { |
| console.log("Error getting raw machine ID:", error); |
| |
| try { |
| const cryptoFallback = await import("crypto"); |
| return cryptoFallback.randomUUID(); |
| } catch { |
| return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) { |
| const r = (Math.random() * 16) | 0; |
| const v = c == "x" ? r : (r & 0x3) | 0x8; |
| return v.toString(16); |
| }); |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| export function isBrowser() { |
| return typeof window !== "undefined"; |
| } |
|
|