支持打包混淆加密代码

This commit is contained in:
COOL 2024-12-28 18:09:53 +08:00
parent 51826ca76b
commit 1887ac6432
2 changed files with 72 additions and 0 deletions

70
cool/obfuscate.js Normal file
View File

@ -0,0 +1,70 @@
const fs = require('fs');
const path = require('path');
const JavaScriptObfuscator = require('javascript-obfuscator');
// 混淆配置
const obfuscatorOptions = {
compact: true,
controlFlowFlattening: true,
controlFlowFlatteningThreshold: 0.7,
deadCodeInjection: true,
deadCodeInjectionThreshold: 0.4,
debugProtection: false,
debugProtectionInterval: 0,
disableConsoleOutput: true,
identifierNamesGenerator: 'hexadecimal',
log: false,
numbersToExpressions: true,
renameGlobals: false,
rotateStringArray: true,
selfDefending: true,
shuffleStringArray: true,
splitStrings: true,
splitStringsChunkLength: 10,
stringArray: true,
stringArrayEncoding: ['base64'],
stringArrayThreshold: 0.75,
transformObjectKeys: true,
unicodeEscapeSequence: false,
};
// 处理单个文件的函数
function obfuscateFile(filePath) {
try {
const code = fs.readFileSync(filePath, 'utf8');
const obfuscationResult = JavaScriptObfuscator.obfuscate(
code,
obfuscatorOptions
);
fs.writeFileSync(filePath, obfuscationResult.getObfuscatedCode());
console.log(`成功混淆文件: ${filePath}`);
} catch (error) {
console.error(`处理文件 ${filePath} 时出错:`, error);
}
}
// 递归处理目录的函数
function processDirectory(directory) {
const files = fs.readdirSync(directory);
files.forEach(file => {
const fullPath = path.join(directory, file);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
processDirectory(fullPath);
} else if (path.extname(file) === '.js') {
obfuscateFile(fullPath);
}
});
}
// 开始处理
const distPath = path.join(process.cwd(), 'dist');
if (fs.existsSync(distPath)) {
console.log('开始混淆 dist 目录下的 JS 文件...');
processDirectory(distPath);
console.log('混淆完成!');
} else {
console.error('错误: dist 目录不存在!');
}

View File

@ -44,6 +44,7 @@
"@types/koa": "^2.15.0",
"@types/node": "20",
"cross-env": "^7.0.3",
"javascript-obfuscator": "^4.1.1",
"jest": "^29.7.0",
"mwts": "^1.3.0",
"mwtsc": "^1.10.1",
@ -62,6 +63,7 @@
"lint:fix": "mwts fix",
"ci": "npm run cov",
"build": "mwtsc --cleanOutDir",
"build:obfuscate": "mwtsc --cleanOutDir && node cool/obfuscate.js",
"pm2:start": "pm2 start ./bootstrap.js -i max --name cool-admin",
"pm2:stop": "pm2 stop cool-admin & pm2 delete cool-admin"
},