-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
80 lines (64 loc) · 2.01 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
const os = require("os")
const crypto = require('crypto');
const cmd = require('child_process').execSync
class Digest {
constructor () {
this.metas = {}
this._init()
this.secret = "|machine-digest|"
}
_init() {
this.metas.OS_TYPE = os.type()
this.metas.OS_PLATFORM = os.platform()
this.metas.OS_ARCH = os.arch()
// this.metas.OS_RELEASE = os.release()
this.metas.OS_CPU = os.cpus().map(k=>`${k.model}`).sort().join('.')
// _macs = []
// Object.values(os.networkInterfaces()).forEach(k=>k.forEach(i=>{if (!i.mac.startsWith("00:") && i.family == 'IPv4') _macs.push(i.mac)}))
// this.metas.OS_MAC = _macs.sort().join('.')
let _id
try {
if (this.metas.OS_PLATFORM == 'darwin') {
_id = /IOPlatformUUID"\s=\s"([^\s]+)"/.exec(cmd('ioreg -rd1 -c IOPlatformExpertDevice').toString('utf8'))
if (_id) _id = _id[1]
} else if (this.metas.OS_PLATFORM == 'linux') {
_id = cmd('cat /var/lib/dbus/machine-id').toString('utf8')
} else if (['win32', 'win64'].includes(this.metas.OS_PLATFORM)) {
_id = cmd('wmic CsProduct Get UUID').toString('utf8').replace('UUID', '')
}
} catch (e) {}
this.metas.id = _id == null? "machine unique id": _id.replace('\n', '')
}
put(options) {
if (typeof(options == "object")) Object.assign(this.metas, options)
}
_make() {
const ss = Object.keys(this.metas).sort().map(k=>`${k}-|-${this.metas[k]}`).join('-')
const _raw = `M${ss}D`
const raw = this._compose(_raw)
return this._encrypt(raw)
}
_encrypt(raw) {
return crypto.createHmac('sha256', this.secret).update(raw)
}
_compose (raw) {
return raw
}
get() {
const digest = this._make().digest('hex')
return {digest, machine: this.metas}
}
uuid() {
return this._make().digest('hex')
}
UUID() {
return this._encrypt(this.metas.id).digest('hex')
}
rawUUID() {
return this.metas.id
}
digest(enc) {
return this._make().digest(enc)
}
}
module.exports = new Digest