Skip to content

Commit ada708d

Browse files
committed
obfuscator: Exclude comments when generating code. #118
Signed-off-by: echo094 <20028238+echo094@users.noreply.github.com>
1 parent 66a32b6 commit ada708d

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

src/plugin/obfuscator.js

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ function virtualGlobalEval(jsStr) {
1717
return globalContext.evalSync(String(jsStr))
1818
}
1919

20+
const optGenMin = {
21+
comments: false,
22+
minified: true,
23+
jsescOption: { minimal: true },
24+
}
25+
2026
/**
2127
* Extract the literal value of an object, and remove an object if all
2228
* references to the object are replaced.
@@ -120,7 +126,7 @@ function stringArrayV2(ast) {
120126
// The string array can be found by its binding
121127
const bind = path.scope.getBinding(obj.stringArrayName)
122128
const def = t.variableDeclaration('var', [bind.path.node])
123-
obj.stringArrayCodes.push(generator(def, { minified: true }).code)
129+
obj.stringArrayCodes.push(generator(def, optGenMin).code)
124130
// The calls can be found by its references
125131
for (let ref of bind.referencePaths) {
126132
if (ref?.listKey === 'arguments') {
@@ -135,7 +141,7 @@ function stringArrayV2(ast) {
135141
if (up1.node.id) {
136142
// 2.12.0 <= v < 2.15.4
137143
// The `stringArrayCallsWrapperName` is included in the definition
138-
obj.stringArrayCodes.push(generator(up1.node, { minified: true }).code)
144+
obj.stringArrayCodes.push(generator(up1.node, optGenMin).code)
139145
up1.node.body = t.blockStatement([])
140146
obj.stringArrayCalls.push({ name: up1.node.id.name, path: up1 })
141147
continue
@@ -145,7 +151,7 @@ function stringArrayV2(ast) {
145151
// The `stringArrayCallsWrapperName` is defined by VariableDeclarator
146152
up1 = up1.parentPath
147153
const node = t.variableDeclaration('var', [up1.node])
148-
obj.stringArrayCodes.push(generator(node, { minified: true }).code)
154+
obj.stringArrayCodes.push(generator(node, optGenMin).code)
149155
up1.node.init = null
150156
obj.stringArrayCalls.push({ name: up1.node.id.name, path: up1 })
151157
continue
@@ -159,15 +165,15 @@ function stringArrayV2(ast) {
159165
console.warn('Unexpected reference!')
160166
continue
161167
}
162-
obj.stringArrayCodes.push(generator(up2.node, { minified: true }).code)
168+
obj.stringArrayCodes.push(generator(up2.node, optGenMin).code)
163169
up2.node.body = t.blockStatement([])
164170
obj.stringArrayCalls.push({ name: wrapper, path: up2 })
165171
}
166172
// Remove the string array
167173
bind.path.remove()
168174
// Add the rotate function
169175
const node = t.expressionStatement(path.node)
170-
obj.stringArrayCodes.push(generator(node, { minified: true }).code)
176+
obj.stringArrayCodes.push(generator(node, optGenMin).code)
171177
path.stop()
172178
if (path.parentPath.isUnaryExpression()) {
173179
path.parentPath.remove()
@@ -233,7 +239,7 @@ function stringArrayV3(ast) {
233239
}
234240
const nodes = [...body]
235241
nodes.shift()
236-
const code = generator(t.BlockStatement(nodes)).code
242+
const code = generator(t.BlockStatement(nodes), optGenMin).code
237243
const fp = `${name_func}=function(){return${string_var}}${name_func}()`
238244
if (!checkPattern(code, fp)) {
239245
return
@@ -275,7 +281,7 @@ function stringArrayV3(ast) {
275281
if (name_func == rm_path.node.id.name) {
276282
return
277283
}
278-
const code = generator(rm_path.node, { minified: true }).code
284+
const code = generator(rm_path.node, optGenMin).code
279285
rm_path.node.body = t.blockStatement([])
280286
nodes.push([code, 'func3', rm_path])
281287
} else {
@@ -287,7 +293,7 @@ function stringArrayV3(ast) {
287293
return
288294
}
289295
ob_string_func_name = name_func
290-
ob_func_str.push(generator(path.node, { minified: true }).code)
296+
ob_func_str.push(generator(path.node, optGenMin).code)
291297
nodes.map(function (item) {
292298
if (item[1] == 'func3') {
293299
ob_func_str.push(item[0])
@@ -298,7 +304,7 @@ function stringArrayV3(ast) {
298304
if (t.isCallExpression(node)) {
299305
node = t.expressionStatement(node)
300306
}
301-
ob_func_str.push(generator(node, { minified: true }).code)
307+
ob_func_str.push(generator(node, optGenMin).code)
302308
})
303309
path.stop()
304310
path.remove()
@@ -334,7 +340,7 @@ function decodeGlobal(ast) {
334340
Program(path) {
335341
let loc = path.scope.getBinding(lost).path
336342
let obj = t.variableDeclaration(loc.parent.kind, [loc.node])
337-
ob_func_str.unshift(generator(obj, { minified: true }).code)
343+
ob_func_str.unshift(generator(obj, optGenMin).code)
338344
loc.node.init = null
339345
ob_dec_call.push({ name: lost, path: loc })
340346
path.stop()
@@ -358,7 +364,7 @@ function decodeGlobal(ast) {
358364
// FunctionDeclaration
359365
// function A (...) { return function B (...) }
360366
root = func
361-
code = generator(root.node, { minified: true }).code
367+
code = generator(root.node, optGenMin).code
362368
} else {
363369
// FunctionExpression
364370
// var A = function (...) { return function B (...) }
@@ -975,22 +981,13 @@ module.exports = function (code) {
975981
console.log('清理死代码...')
976982
ast = cleanDeadCode(ast)
977983
// 刷新代码
978-
ast = parse(
979-
generator(ast, {
980-
comments: false,
981-
jsescOption: { minimal: true },
982-
}).code,
983-
{ errorRecovery: true }
984-
)
984+
ast = parse(generator(ast, optGenMin).code, { errorRecovery: true })
985985
console.log('提高代码可读性...')
986986
ast = purifyCode(ast)
987987
console.log('解除环境限制...')
988988
ast = unlockEnv(ast)
989989
console.log('净化完成')
990-
code = generator(ast, {
991-
comments: false,
992-
jsescOption: { minimal: true },
993-
}).code
990+
code = generator(ast, optGenMin).code
994991
if (global_eval) {
995992
code = PluginEval.pack(code)
996993
}

0 commit comments

Comments
 (0)