File size: 6,055 Bytes
6111b2b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | import { execFileSync, execSync } from "child_process";
import { existsSync, readFileSync } from "fs";
/**
* Get raw machine ID using OS-specific methods.
*
* We use try/catch waterfall: try each OS method and fall through
* to the next on failure. Platform checks are INSIDE try blocks so they
* run at RUNTIME (not build time), avoiding Next.js SWC dead-code elimination.
*
* On Linux: skips Windows (REG.exe) and macOS (ioreg) strategies entirely.
*/
function getMachineIdRaw(): string {
// Strategy 1: Windows β REG.exe query for MachineGuid
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(/* turbopackIgnore: true */ 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 {
// Not Windows or REG.exe failed β continue
}
// Strategy 2: macOS β ioreg IOPlatformUUID
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 {
// Not macOS or ioreg not available β continue
}
// Strategy 3: Linux β read machine-id files directly (no `head` or pipe)
try {
for (const filePath of ["/etc/machine-id", "/var/lib/dbus/machine-id"]) {
try {
const content = readFileSync(/* turbopackIgnore: true */ filePath, "utf8")
.trim()
.toLowerCase();
if (content.length > 8) return content;
} catch {
// Try the next candidate file
}
}
} catch {
// Files not readable β continue
}
// Strategy 4: Hostname fallback (works on all platforms)
try {
const hostname = execSync("hostname", { encoding: "utf8", timeout: 5000 });
const id = hostname.trim().toLowerCase();
if (id) return id;
} catch {
// hostname failed β continue
}
// Strategy 5: Node.js os.hostname() (no exec needed)
try {
const os = require("os");
return os.hostname().toLowerCase();
} catch {
// Final fallback
}
return "unknown-machine";
}
/**
* 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<string>} Machine ID (16-character base32)
*/
export async function getConsistentMachineId(salt = null) {
const saltValue = salt || process.env.MACHINE_ID_SALT || "endpoint-proxy-salt";
try {
const rawMachineId = getMachineIdRaw();
// Create consistent ID using salt
const crypto = await import("crypto");
const hashedMachineId = crypto
.createHash("sha256")
.update(rawMachineId + saltValue)
.digest("hex");
// Return only first 16 characters for brevity
return hashedMachineId.substring(0, 16);
} catch (error) {
console.log("Error getting machine ID:", error);
// Fallback to random ID if node-machine-id fails
try {
const cryptoFallback = await import("crypto");
return cryptoFallback.randomUUID();
} catch {
if (typeof globalThis !== "undefined" && globalThis.crypto && globalThis.crypto.randomUUID) {
return globalThis.crypto.randomUUID();
}
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
let r = 0;
if (
typeof globalThis !== "undefined" &&
globalThis.crypto &&
globalThis.crypto.getRandomValues
) {
const arr = new Uint8Array(1);
globalThis.crypto.getRandomValues(arr);
r = arr[0] % 16;
} else {
r = (Date.now() % 16) | 0;
}
const v = c === "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}
}
}
/**
* Get raw machine ID without hashing (for debugging purposes)
* @returns {Promise<string>} Raw machine ID
*/
export async function getRawMachineId() {
try {
return getMachineIdRaw();
} catch (error) {
console.log("Error getting raw machine ID:", error);
// Fallback to random ID if node-machine-id fails
try {
const cryptoFallback = await import("crypto");
return cryptoFallback.randomUUID();
} catch {
if (typeof globalThis !== "undefined" && globalThis.crypto && globalThis.crypto.randomUUID) {
return globalThis.crypto.randomUUID();
}
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
let r = 0;
if (
typeof globalThis !== "undefined" &&
globalThis.crypto &&
globalThis.crypto.getRandomValues
) {
const arr = new Uint8Array(1);
globalThis.crypto.getRandomValues(arr);
r = arr[0] % 16;
} else {
r = (Date.now() % 16) | 0;
}
const v = c === "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}
}
}
/**
* Check if we're running in browser or server environment
* @returns {boolean} True if in browser, false if in server
*/
export function isBrowser() {
return typeof window !== "undefined";
}
|