Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { exec, execSync } from "child_process";
- import { createHash } from "crypto";
- type MixedOrNative = "mixed" | "native" | "";
- const win32RegBinPath: Record<MixedOrNative, string> = {
- native: "%windir%\\System32",
- mixed: "%windir%\\sysnative\\cmd.exe /c %windir%\\System32",
- "": ""
- };
- const guid: Partial<Record<NodeJS.Platform, string>> = {
- darwin: "ioreg -rd1 -c IOPlatformExpertDevice",
- win32:
- `${
- win32RegBinPath[isWindowsProcessMixedOrNativeArchitecture()]
- }\\REG.exe ` +
- "QUERY HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography " +
- "/v MachineGuid",
- linux:
- "( cat /var/lib/dbus/machine-id /etc/machine-id 2> /dev/null || hostname ) | head -n 1 || :",
- freebsd: "kenv -q smbios.system.uuid || sysctl -n kern.hostuuid"
- };
- function isWindowsProcessMixedOrNativeArchitecture(): MixedOrNative | "" {
- // detect if the node binary is the same arch as the Windows OS.
- // or if this is 32 bit node on 64 bit windows.
- if (process.platform !== "win32") {
- return "";
- }
- if (
- process.arch === "ia32" &&
- process.env.hasOwnProperty("PROCESSOR_ARCHITEW6432")
- ) {
- return "mixed";
- }
- return "native";
- }
- function hash(guid: string): string {
- return createHash("sha256")
- .update(guid)
- .digest("hex");
- }
- function expose(result: string): string {
- switch (process.platform) {
- case "darwin":
- return result
- .split("IOPlatformUUID")[1]
- .split("\n")[0]
- .replace(/\=|\s+|\"/gi, "")
- .toLowerCase();
- case "win32":
- return result
- .toString()
- .split("REG_SZ")[1]
- .replace(/\r+|\n+|\s+/gi, "")
- .toLowerCase();
- case "linux":
- return result
- .toString()
- .replace(/\r+|\n+|\s+/gi, "")
- .toLowerCase();
- case "freebsd":
- return result
- .toString()
- .replace(/\r+|\n+|\s+/gi, "")
- .toLowerCase();
- default:
- throw new Error(`Unsupported platform: ${process.platform}`);
- }
- }
- export function machineIdSync(original: boolean): string {
- const id: string = expose(execSync(guid[process.platform]!).toString());
- return original ? id : hash(id);
- }
- export function machineId(original: boolean): Promise<string> {
- return new Promise((resolve, reject) => {
- const g = guid[process.platform];
- if (!g) {
- return reject(new Error(`Error while obtaining machine id`));
- }
- return exec(g, {}, (err, stdout) => {
- if (err) {
- return reject(
- new Error(`Error while obtaining machine id: ${err.stack}`)
- );
- }
- const id: string = expose(stdout.toString());
- return resolve(original ? id : hash(id));
- });
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement