forked from automation-stack/node-machine-id
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
87 lines (77 loc) · 2.86 KB
/
index.ts
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
import {exec, execSync} from 'child_process';
import {createHash} from 'crypto';
const { platform } = process;
const reg = platform === "win32" ? require("native-reg") : null;
const guid = {
darwin: 'ioreg -rd1 -c IOPlatformExpertDevice',
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 hash(guid: string): string {
return createHash('sha256').update(guid).digest('hex');
}
function expose(result: string): string {
switch (platform) {
case 'darwin':
return result
.split('IOPlatformUUID')[1]
.split('\n')[0].replace(/\=|\s+|\"/ig, '')
.toLowerCase();
case 'win32':
return result
.toString()
.split('REG_SZ')[1]
.replace(/\r+|\n+|\s+/ig, '')
.toLowerCase();
case 'linux':
return result
.toString()
.replace(/\r+|\n+|\s+/ig, '')
.toLowerCase();
case 'freebsd':
return result
.toString()
.replace(/\r+|\n+|\s+/ig, '')
.toLowerCase();
default:
throw new Error(`Unsupported platform: ${process.platform}`);
}
}
function windowsMachineId(): string {
return reg.getValue(reg.HKEY.LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Cryptography", "MachineGuid").toString();
}
/**
* This function gets the OS native UUID/GUID synchronously, hashed by default.
* @param {boolean} [original=false] If true return original value of machine id, otherwise return hashed value (sha - 256)
*/
export function machineIdSync(original: boolean = false): string {
const id = platform === "win32"
? windowsMachineId()
: expose(execSync(guid[platform]).toString());
return original ? id : hash(id);
}
/**
* This function gets the OS native UUID/GUID asynchronously (recommended), hashed by default.
*
* Note: on windows this is still synchronous
* @param {boolean} [original=false] If true return original value of machine id, otherwise return hashed value (sha - 256)
*
*/
export function machineId(original: boolean = false): Promise<string> {
return new Promise((resolve: Function, reject: Function): Object => {
if (platform === "win32") {
try {
return resolve(windowsMachineId());
} catch (error) {
return reject(error);
}
}
return exec(guid[platform], {}, (err: any, stdout: any, stderr: any) => {
if (err) {
return reject(new Error(`Error while obtaining machine id: ${err.stack}`));
}
const id = expose(stdout.toString());
return resolve(original ? id : hash(id));
});
});
}