From 1887ac643254e2813f9b8a0496321a53b2ebb436 Mon Sep 17 00:00:00 2001 From: COOL Date: Sat, 28 Dec 2024 18:09:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=89=93=E5=8C=85=E6=B7=B7?= =?UTF-8?q?=E6=B7=86=E5=8A=A0=E5=AF=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cool/obfuscate.js | 70 +++++++++++++++++++++++++++++++++++++++++++++++ package.json | 2 ++ 2 files changed, 72 insertions(+) create mode 100644 cool/obfuscate.js diff --git a/cool/obfuscate.js b/cool/obfuscate.js new file mode 100644 index 0000000..b7d5530 --- /dev/null +++ b/cool/obfuscate.js @@ -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 目录不存在!'); +} diff --git a/package.json b/package.json index c9905f7..fa77243 100644 --- a/package.json +++ b/package.json @@ -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" },