forked from es128/serial-number
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
121 lines (99 loc) · 2.48 KB
/
index.js
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
'use strict';
var fs = require('fs');
var http = require('http');
var exec = require('child_process').exec;
var serialNumber = function (cb, cmdPrefix) {
var delimiter = ': ';
var uselessSerials = [
'To be filled by O.E.M.',
]
var fromCache = function (error, stdout) {
fs.readFile(__dirname + '/cache', function (fsErr, data) {
if (data) {data = data.toString().trim();}
if (fsErr || !data || data.length < 2) {
attemptEC2(function() {
stdoutHandler(error, stdout, true);
});
} else {
cb(null, data);
}
});
};
var stdoutHandler = function (error, stdout, bypassCache) {
if (error && !bypassCache) {
fromCache(error, stdout);
} else {
cb(error, parseResult(stdout));
}
};
var parseResult = function (input) {
var result = input.slice(input.indexOf(delimiter) + 2).trim();
var isResultUseless = uselessSerials.some(function(val) {
return val === result;
});
if (isResultUseless) {
return '';
}
return result;
};
var attemptEC2 = function (failCb) {
var data = '';
var failHandler = function () {
failCb();
failCb = function () {};
};
var request = http.get(
'http://169.254.169.254/latest/meta-data/instance-id',
function (res) {
res.on('data', function (chunk) {
data += chunk;
}).on('end', function () {
if (data.length > 2) {
cb(null, data.trim());
} else {
failHandler();
}
});
}
);
request.on('error', failHandler).setTimeout(1000, failHandler);
};
cmdPrefix = cmdPrefix || '';
var vals = ['Serial', 'UUID'];
var cmd;
switch (process.platform) {
case 'win32':
delimiter = '\r\n';
vals[0] = 'IdentifyingNumber';
cmd = 'wmic csproduct get ';
break;
case 'darwin':
cmd = 'system_profiler SPHardwareDataType | grep ';
break;
case 'linux':
if (process.arch === 'arm') {
vals[1] = 'Serial';
cmd = 'cat /proc/cpuinfo | grep ';
} else {
cmd = 'dmidecode -t system | grep ';
}
break;
case 'freebsd':
cmd = 'dmidecode -t system | grep ';
break;
}
if (!cmd) return cb(new Error('Cannot provide serial number for ' + process.platform));
if (serialNumber.preferUUID) vals.reverse();
exec(cmdPrefix + cmd + vals[0], function (error, stdout) {
if (error || parseResult(stdout).length > 1) {
stdoutHandler(error, stdout);
} else {
exec(cmdPrefix + cmd + vals[1], stdoutHandler);
}
});
};
serialNumber.preferUUID = false;
module.exports = exports = serialNumber;
exports.useSudo = function (cb) {
serialNumber(cb, 'sudo ');
};