-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
242 lines (200 loc) · 7.42 KB
/
main.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/usr/bin/env node
import program from 'commander'
import path from 'path'
import fse from 'fs-extra'
//import readline from 'readline';
import logUpdate from 'log-update';
import { fileURLToPath } from 'url';
import AutoGitUpdate, { readAppVersion } from './updateor.js';
const scriptPath = path.dirname(fileURLToPath(import.meta.url))
const log = logUpdate.create(process.stdout, {
showCursor: true
});
// const log = (s)=>console.log(s);
import {
readByte,
getExtension,
directoryExists,
readdir,
mkdir,
rm,
obfuscateHTML,
obfuscateCSS,
obfuscateJS,
obfuscateLua
} from './lib/index.js'
// Use current working dir vs __dirname where this code lives
const cwd = process.cwd()
let exeLine = `${scriptPath.substring(0, 2)} && cd ${scriptPath} && npm i --force && npm link --force`
//console.log(exeLine)
const updater = new AutoGitUpdate({
repository: 'https://github.com/l3lackMegas/dyz-obfuscator',
branch: "main",
tempLocation: "C:\\tmp",
executeOnComplete: exeLine,
exitOnComplete: false
});
updater.setLogConfig({
logGeneral: false
})
let updateInfo = await updater.compareVersions()
console.log(`- Running from ${cwd}`)
console.log(`- Current version: ${updateInfo.currentVersion}, Remote version: ${updateInfo.remoteVersion}`)
if(!updateInfo.upToDate) {
console.log(`
[!] New version detected! (${updateInfo.remoteVersion})
==========================================================
| To update the script. |
| |
| npm i -g dyz-obfuscator |
==========================================================
`)
}
program
.version(readAppVersion())
.name('dobs')
.description("The HTML, CSS, JS and Lua obfuscator for FiveM's resources")
.option('-s,--source [folder]', 'Source images directory', './')
.option(
'-o,--output [folder]',
'Directory to be created for obfuscated files',
'./dyz-obfuscated'
)
.option(
'-i,--ignore [json file]',
'JSON file that contain keywords to skip obfuscation.'
)
.parse(process.argv)
const WhiteListExtension = ['html', 'css', 'js', 'lua'];
const defaultIgnoreList = [
'node_modules',
'yarn',
"__resource.lua",
"fxmanifest.lua"
];
console.log('Started Dyz-Obfuscator!')
const main = async () => {
try {
// Use user input or default options
const {
source,
output,
ignore
} = program.opts()
const srcPath = source.replace(/\\/g, '/')
const destPath = output.replace(/\\/g, '/')
let IgnoreList;
try {
IgnoreList = ignore && fse.existsSync(ignore) ? fse.readJsonSync(ignore) : defaultIgnoreList;
} catch (error) {
console.log(`\x1b[1m\x1b[31m> Can't parse ignore file, Use default ignore list. (Target: ${ignore})\x1b[0m`)
IgnoreList = defaultIgnoreList
}
//console.log(IgnoreList, IgnoreList.length)
console.log(`Scanning files from ${srcPath}`)
//console.log(source, output)
// Remove destination directory is it exists
if (directoryExists(destPath)) {
await rm(destPath)
}
// Create destination directory
await mkdir(destPath)
// Read source directory
const filesAll = await readdir(srcPath)
// Create task list
let taskList = []
//filesAll.forEach(pathname => {
for (let index = 0; index < filesAll.length; index++) {
let pathname = filesAll[index];
let fullPathname = path.posix.join(cwd, pathname).replace(/\\/g, '/');
//console.log(stringSource)
let sPath = path.posix.join(cwd, srcPath).replace(/\\/g, '/')
let copypath = fullPathname.replace(sPath, ""),
displayCopypath = path.posix.join(destPath, copypath).replace(/\\/g, '/'),
desPath = path.posix.join(destPath, copypath).replace(/\\/g, '/');
let terminalOut = `[${index+1}/${filesAll.length}] Copying file to ${desPath}`
log(terminalOut.length > process.stdout.columns ?
terminalOut.substring(0, process.stdout.columns - 13) + '...' + terminalOut.substring(terminalOut.length - 10, terminalOut.length)
: terminalOut
);
//await sleep(100)
if(
WhiteListExtension.includes(getExtension(pathname)) &&
(()=>{
let isNonIgnore = 0;
IgnoreList.forEach(ignoreWord => {
let isFound = pathname.toLocaleLowerCase().includes(ignoreWord.toLocaleLowerCase())
isNonIgnore = isFound ? isNonIgnore + 1 : isNonIgnore
});
return isNonIgnore === 0;
})()
) taskList.push(pathname);
//console.log(desPath)
fse.copySync(pathname, displayCopypath)
//console.log('Copied to ' + desPath)
// readline.clearLine(process.stdout);
// readline.cursorTo(process.stdout, 0);
};
log(`[!] Finish copied with ${filesAll.length} files!`)
//console.log(taskList, `Found ${taskList.length} items.`, srcPath)
console.log("\nStarting obfuscate task...\n")
let failList = []
for (let index = 0; index < taskList.length; index++) {
let pathname = taskList[index];
let fullPathname = path.posix.join(cwd, pathname).replace(/\\/g, '/');
//console.log(stringSource)
let sPath = path.posix.join(cwd, srcPath).replace(/\\/g, '/')
let copypath = fullPathname.replace(sPath, ""),
displayCopypath = path.posix.join(destPath, copypath).replace(/\\/g, '/'),
desPath = path.posix.join(cwd, destPath, copypath).replace(/\\/g, '/');
//console.log(desPath)
//console.log(pathname)
let stringSource = fse.readFileSync(pathname, 'utf8');
let obfuscatedOutput = "";
//process.stdout.write(`[${index+1}/${taskList.length}] Obfuscating to ${displayCopypath}`.substring(0, process.stdout.columns - 3) + '...');
let terminalOut = `[${index+1}/${taskList.length}] Obfuscating file to ${desPath}`
log(terminalOut.length > process.stdout.columns ?
terminalOut.substring(0, process.stdout.columns - 13) + '...' + terminalOut.substring(terminalOut.length - 10, terminalOut.length)
: terminalOut
);
// Wipe line for next status
switch (getExtension(pathname).toLowerCase()) {
case "html":
obfuscatedOutput = obfuscateHTML(stringSource, log, terminalOut)
break;
case "css":
obfuscatedOutput = obfuscateCSS(stringSource, pathname, log, terminalOut)
break;
case "js":
obfuscatedOutput = obfuscateJS(stringSource, log, terminalOut)
break;
case "lua":
let stringSourceLua = await readByte(pathname, log, terminalOut)
//console.log(stringSourceLua)
obfuscatedOutput = obfuscateLua(stringSourceLua, 'RkWL5ExSjRw3qWT')
break;
default:
break;
}
//fse.removeSync(desPath)
if(!obfuscatedOutput.success) failList.push([pathname, obfuscatedOutput.error])
fse.writeFileSync(displayCopypath, obfuscatedOutput.source, { flag: 'w' })
// readline.clearLine(process.stdout);
// readline.cursorTo(process.stdout, 0);
// fse.copySync(pathname, desPath)
// console.log('Copied to ' + desPath)
};
log(`[!] Obfuscated files ${taskList.length - failList.length} Successfully, ${failList.length} Failed!\n`)
console.log(`[!] Failed list have ${failList.length} items here:\n${"-".repeat(process.stdout.columns)}`)
failList.forEach((item, index) => {
console.log(`\x1b[1m\x1b[31m${index + 1}) ${item[1]}\x1b[0m
${"-".repeat(process.stdout.columns)}`)
});
if(failList.length > 0) console.log("Note: Please check your code syntax form each failed files. For now, it's will be replace with the original code.\n")
console.log(`Finish task!, All Files has been created at ${path.join(cwd, destPath)}\n`)
process.exit();
} catch (error) {
console.log('Error creating obfuscate file.', error)
}
}
main();