mirror of
https://github.com/smallfawn/decode_action.git
synced 2025-12-23 18:25:08 +08:00
55 lines
1.3 KiB
JavaScript
55 lines
1.3 KiB
JavaScript
import { parse } from '@babel/parser'
|
|
import _generate from '@babel/generator'
|
|
const generator = _generate.default
|
|
import _traverse from '@babel/traverse'
|
|
const traverse = _traverse.default
|
|
import * as t from '@babel/types'
|
|
|
|
function unpack(code) {
|
|
let ast = parse(code, { errorRecovery: true })
|
|
let lines = ast.program.body
|
|
let data = null
|
|
for (let line of lines) {
|
|
if (t.isEmptyStatement(line)) {
|
|
continue
|
|
}
|
|
if (data) {
|
|
return null
|
|
}
|
|
if (
|
|
t.isCallExpression(line?.expression) &&
|
|
line.expression.callee?.name === 'eval' &&
|
|
line.expression.arguments.length === 1 &&
|
|
t.isCallExpression(line.expression.arguments[0])
|
|
) {
|
|
data = t.expressionStatement(line.expression.arguments[0])
|
|
continue
|
|
}
|
|
return null
|
|
}
|
|
if (!data) {
|
|
return null
|
|
}
|
|
code = generator(data, { minified: true }).code
|
|
return eval(code)
|
|
}
|
|
|
|
function pack(code) {
|
|
let ast1 = parse('(function(){}())')
|
|
let ast2 = parse(code)
|
|
traverse(ast1, {
|
|
FunctionExpression(path) {
|
|
let body = t.blockStatement(ast2.program.body)
|
|
path.replaceWith(t.functionExpression(null, [], body))
|
|
path.stop()
|
|
},
|
|
})
|
|
code = generator(ast1, { minified: false }).code
|
|
return code
|
|
}
|
|
|
|
export default {
|
|
unpack,
|
|
pack,
|
|
}
|