Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { exec, execSync } from "child_process";
  2. import { createHash } from "crypto";
  3.  
  4. type MixedOrNative = "mixed" | "native" | "";
  5.  
  6. const win32RegBinPath: Record<MixedOrNative, string> = {
  7.   native: "%windir%\\System32",
  8.   mixed: "%windir%\\sysnative\\cmd.exe /c %windir%\\System32",
  9.   "": ""
  10. };
  11.  
  12. const guid: Partial<Record<NodeJS.Platform, string>> = {
  13.   darwin: "ioreg -rd1 -c IOPlatformExpertDevice",
  14.   win32:
  15.     `${
  16.       win32RegBinPath[isWindowsProcessMixedOrNativeArchitecture()]
  17.     }\\REG.exe ` +
  18.     "QUERY HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography " +
  19.     "/v MachineGuid",
  20.   linux:
  21.     "( cat /var/lib/dbus/machine-id /etc/machine-id 2> /dev/null || hostname ) | head -n 1 || :",
  22.   freebsd: "kenv -q smbios.system.uuid || sysctl -n kern.hostuuid"
  23. };
  24.  
  25. function isWindowsProcessMixedOrNativeArchitecture(): MixedOrNative | "" {
  26.   // detect if the node binary is the same arch as the Windows OS.
  27.   // or if this is 32 bit node on 64 bit windows.
  28.   if (process.platform !== "win32") {
  29.     return "";
  30.   }
  31.   if (
  32.     process.arch === "ia32" &&
  33.     process.env.hasOwnProperty("PROCESSOR_ARCHITEW6432")
  34.   ) {
  35.     return "mixed";
  36.   }
  37.   return "native";
  38. }
  39.  
  40. function hash(guid: string): string {
  41.   return createHash("sha256")
  42.     .update(guid)
  43.     .digest("hex");
  44. }
  45.  
  46. function expose(result: string): string {
  47.   switch (process.platform) {
  48.     case "darwin":
  49.       return result
  50.         .split("IOPlatformUUID")[1]
  51.         .split("\n")[0]
  52.         .replace(/\=|\s+|\"/gi, "")
  53.         .toLowerCase();
  54.     case "win32":
  55.       return result
  56.         .toString()
  57.         .split("REG_SZ")[1]
  58.         .replace(/\r+|\n+|\s+/gi, "")
  59.         .toLowerCase();
  60.     case "linux":
  61.       return result
  62.         .toString()
  63.         .replace(/\r+|\n+|\s+/gi, "")
  64.         .toLowerCase();
  65.     case "freebsd":
  66.       return result
  67.         .toString()
  68.         .replace(/\r+|\n+|\s+/gi, "")
  69.         .toLowerCase();
  70.     default:
  71.       throw new Error(`Unsupported platform: ${process.platform}`);
  72.   }
  73. }
  74.  
  75. export function machineIdSync(original: boolean): string {
  76.   const id: string = expose(execSync(guid[process.platform]!).toString());
  77.   return original ? id : hash(id);
  78. }
  79.  
  80. export function machineId(original: boolean): Promise<string> {
  81.   return new Promise((resolve, reject) => {
  82.     const g = guid[process.platform];
  83.     if (!g) {
  84.       return reject(new Error(`Error while obtaining machine id`));
  85.     }
  86.     return exec(g, {}, (err, stdout) => {
  87.       if (err) {
  88.         return reject(
  89.           new Error(`Error while obtaining machine id: ${err.stack}`)
  90.         );
  91.       }
  92.       const id: string = expose(stdout.toString());
  93.       return resolve(original ? id : hash(id));
  94.     });
  95.   });
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement