12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- const JavaScriptObfuscator = require('javascript-obfuscator');
- const path = require('path')
- const fs = require('fs')
- // 要混淆的文件夹路径
- const folderPath = './constant11';
- // 新文件夹名字
- const outputFolderName = 'constant11-salt';
- // 创建新文件夹
- const outputFolderPath = path.join('./', outputFolderName);
- if (!fs.existsSync(outputFolderPath)) {
- fs.mkdirSync(outputFolderPath);
- }
- const obfuscationOptions = {
- compact: true, // 缩短混淆后的代码
- controlFlowFlattening: true, // 控制流扁平化
- deadCodeInjection: true, // 注入死代码
- debugProtection: true, // 调试保护
- debugProtectionInterval: 0, // 调试保护间隔
- disableConsoleOutput: true, // 禁用控制台输出
- selfDefending: true, // 自我保护
- stringArray: true, // 字符串数组混淆
- stringArrayEncoding: ['base64'], // 字符串数组编码
- rotateStringArray: true, // 旋转字符串数组
- transformObjectKeys: true, // 转换对象键
- unicodeEscapeSequence: true // Unicode 转义序列
- };
- // 递归遍历文件夹下的所有 JavaScript 文件,并混淆后写入到新文件中
- function obfuscateFiles(folderPath, outputFolderPath) {
- const files = fs.readdirSync(folderPath);
- files.forEach(function(file) {
- const filePath = path.join(folderPath, file);
- if (fs.statSync(filePath).isDirectory()) {
- // 如果是文件夹,则递归处理子文件夹
- const subfolderName = file;
- const subfolderOutputPath = path.join(outputFolderPath, subfolderName);
- fs.mkdirSync(subfolderOutputPath);
- obfuscateFiles(filePath, subfolderOutputPath);
- } else if (path.extname(filePath) === '.js') {
- // 如果是 JavaScript 文件,则进行混淆
- const code = fs.readFileSync(filePath, 'utf8');
- const obfuscatedCode = JavaScriptObfuscator.obfuscate(code, obfuscationOptions).getObfuscatedCode();
- const obfuscatedFileName = file.replace('.js', '.js');
- const obfuscatedFilePath = path.join(outputFolderPath, obfuscatedFileName);
- fs.writeFileSync(obfuscatedFilePath, obfuscatedCode);
- console.log(`File ${filePath} has been obfuscated and written to ${obfuscatedFilePath}.`);
- }
- });
- }
- obfuscateFiles(folderPath, outputFolderPath);
|