-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
90 lines (75 loc) · 2.18 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
const util = require('util')
const proc = require('child_process')
const exec = util.promisify(proc.exec)
const spawn = proc.spawn
module.exports = function getSeedGenerationCommand() {
switch(process.platform) {
case 'darwin':
return createOsxSeed()
case 'win32':
return createWindowsSeed()
default:
return createLinuxSeed()
}
}
function createLinuxSeed() {
// 'cat /dev/urandom |tr -dc A-Z9|head -c${1:-81}'
return new Promise((resolve, reject) => {
const seed = spawn('cat /dev/urandom |tr -dc A-Z9|head -c${1:-81}', {
shell: true
})
seed.stdout.on('data', (data) => {
resolve(data.toString())
})
seed.stderr.on('data', err => {
reject(err.toString())
})
seed.on('error', err => {
reject(err)
})
seed.on('close', (code) => {
seed.kill()
})
})
}
function createOsxSeed() {
// 'cat /dev/urandom |LC_ALL=C tr -dc "A-Z9" |fold -w 81 |head -n 1'
return new Promise((resolve, reject) => {
const seed = spawn('head', ['-n', '1'])
const lines = spawn('fold', ['-w', '81'])
const alpha = spawn('tr', ['-dc', 'A-Z9'], { env: Object.assign({}, process.env, { LC_ALL: 'C' })})
const rand = spawn('cat', ['/dev/urandom'])
seed.stdout.on('data', (data) => {
resolve(data.toString().replace('\n', ''))
})
seed.stderr.on('data', reject)
seed.on('error', reject)
seed.on('close', (code) => {
seed.kill()
lines.kill()
alpha.kill()
rand.kill()
})
rand.stdout.pipe(alpha.stdin)
alpha.stdout.pipe(lines.stdin)
lines.stdout.pipe(seed.stdin)
})
}
function createWindowsSeed() {
// '-join ([char[]](65..90+57..57)*100 | Get-Random -Count 81)'
return new Promise(async (resolve, reject) => {
try {
const result = await exec('powershell.exe -Command "-join ([char[]](65..90+57..57)*100 | Get-Random -Count 81)"')
if(result.stdout) {
const textContainingSeed = result.stdout.replace('\r\n', '')
const textParts = textContainingSeed.split('\n')
resolve(textParts.pop())
} else {
reject(result.stderr)
}
}
catch (err) {
reject(err)
}
})
}