-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.js
203 lines (155 loc) · 4.21 KB
/
core.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
const { execSync } = require('child_process')
const timestamp = () => {
const lastDate = new Date()
const date = Math.floor(lastDate.getTime() / 1000)
const time = lastDate.getTime() * 1000000
return { date, time }
}
const taskkill = processName => {
try {
return execSync(`taskkill /F /IM ${processName}`, { encoding: 'utf-8' })
}
catch(error) {
return { error }
}
}
const flusshDNS = () => {
try {
return execSync(`ipconfig /release&ipconfig /flushdns&ipconfig /renew&ipconfig /flushdns`, { encoding: 'utf-8' })
}
catch(error) {
return { error }
}
}
const rand = (length) => {
let hex = '0123456789abcdef'
let result = ''
for(let i = 0; i < length; i++) {
result = result + hex.charAt(Math.floor(Math.random() * hex.length))
}
return result
}
class Reg {
#lastKey = ''
#createList = []
#readList = []
#writeList = []
#deleteList = []
#error = false
#readingResult = ''
get readingResult() {
return this.#readingResult
}
#writingResult = ''
get writingResult() {
return this.#writingResult
}
prepare(key) {
if (this.#error) return this
this.#lastKey = key
return this
}
clearCreationList() {
this.#createList = []
return this
}
clearReadingList() {
this.#readList = []
return this
}
clearWritingList() {
this.#writeList = []
return this
}
clearDeletionList() {
this.#deleteList = []
return this
}
addToCreationList() {
if (this.#error) return this
this.#createList.push(`reg add "${this.#lastKey}" /F`)
return this
}
addToReadingList(name) {
if (this.#error) return this
this.#readList.push(`reg query "${this.#lastKey}" /v ${name}`)
return this
}
addToWritingList(name, type, value) {
if (this.#error) return this
this.#writeList.push(`reg add "${this.#lastKey}" /v ${name} /t ${type} /d "${value}" /F`)
return this
}
addToDeleteList(name) {
if (this.#error) return this
this.#deleteList.push(name ? `reg delete "${this.#lastKey}" /v ${name} /F` : `reg delete "${this.#lastKey}" /F`)
return this
}
#addResultToList(typeList, result) {
if (this.#error) return this
result.trim().split('\n\n\n').forEach(info => {
const [ key, values ] = info.split('\n')
if (!values) {
if (typeList == 'create') this.#createList.push(`reg add "${key}" /F`)
if (typeList == 'delete') this.#createList.push(`reg delete "${key}" /F`)
return
}
const [name, type, value] = values.trim().split(/\s{1,8}/)
if (typeList == 'read') this.#readList.push(`reg query "${key}" /v ${name}`)
if (typeList == 'write') this.#writeList.push(`reg add "${key}" /v ${name} /t ${type} /d "${value}" /F`)
if (typeList == 'delete') this.#createList.push(`reg delete "${key}" /v ${name} /F`)
})
}
addResultToCreationList(result) {
this.#addResultToList('create', result)
return this
}
addResultToReadingList(result) {
this.#addResultToList('read', result)
return this
}
addResultToWritingList(result) {
this.#addResultToList('write', result)
return this
}
addResultToDeletionnList(result) {
this.#addResultToList('delete', result)
return this
}
#runCommand(command) {
if (this.#error) return this
try {
return execSync(command, { encoding: 'utf-8' })
}
catch(error) {
this.#error = error
}
}
runCreationList() {
this.#runCommand(this.#createList.join('&'))
this.clearCreationList()
return this
}
runReadingList() {
this.#readingResult = this.#runCommand(this.#readList.join('&'))
this.clearReadingList()
return this
}
runWritingList() {
this.#runCommand(this.#writeList.join('&'))
const replaceReg = write => write.replace(/reg add "(.*)" \/v (.*) \/t (REG_.*) \/d "(.*)" \/F/g, '\r\n$1\r\n $2 $3 $4\r\n')
this.#writingResult = this.#writeList.map(replaceReg).join('\r\n') + '\r\n'
this.clearWritingList()
return this
}
runDeletionList() {
this.#runCommand(this.#deleteList.join('&'))
this.clearDeletionList()
return this
}
trackErros(callback) {
if (this.#error) callback(this.#error)
return this
}
}
module.exports = { Reg, rand, timestamp, taskkill, flusshDNS }