salt.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. const JavaScriptObfuscator = require('javascript-obfuscator');
  2. const path = require('path')
  3. const fs = require('fs')
  4. // 要混淆的文件夹路径
  5. const folderPath = './constant11';
  6. // 新文件夹名字
  7. const outputFolderName = 'constant11-salt';
  8. // 创建新文件夹
  9. const outputFolderPath = path.join('./', outputFolderName);
  10. if (!fs.existsSync(outputFolderPath)) {
  11. fs.mkdirSync(outputFolderPath);
  12. }
  13. const obfuscationOptions = {
  14. compact: true, // 缩短混淆后的代码
  15. controlFlowFlattening: true, // 控制流扁平化
  16. deadCodeInjection: true, // 注入死代码
  17. debugProtection: true, // 调试保护
  18. debugProtectionInterval: 0, // 调试保护间隔
  19. disableConsoleOutput: true, // 禁用控制台输出
  20. selfDefending: true, // 自我保护
  21. stringArray: true, // 字符串数组混淆
  22. stringArrayEncoding: ['base64'], // 字符串数组编码
  23. rotateStringArray: true, // 旋转字符串数组
  24. transformObjectKeys: true, // 转换对象键
  25. unicodeEscapeSequence: true // Unicode 转义序列
  26. };
  27. // 递归遍历文件夹下的所有 JavaScript 文件,并混淆后写入到新文件中
  28. function obfuscateFiles(folderPath, outputFolderPath) {
  29. const files = fs.readdirSync(folderPath);
  30. files.forEach(function(file) {
  31. const filePath = path.join(folderPath, file);
  32. if (fs.statSync(filePath).isDirectory()) {
  33. // 如果是文件夹,则递归处理子文件夹
  34. const subfolderName = file;
  35. const subfolderOutputPath = path.join(outputFolderPath, subfolderName);
  36. fs.mkdirSync(subfolderOutputPath);
  37. obfuscateFiles(filePath, subfolderOutputPath);
  38. } else if (path.extname(filePath) === '.js') {
  39. // 如果是 JavaScript 文件,则进行混淆
  40. const code = fs.readFileSync(filePath, 'utf8');
  41. const obfuscatedCode = JavaScriptObfuscator.obfuscate(code, obfuscationOptions).getObfuscatedCode();
  42. const obfuscatedFileName = file.replace('.js', '.js');
  43. const obfuscatedFilePath = path.join(outputFolderPath, obfuscatedFileName);
  44. fs.writeFileSync(obfuscatedFilePath, obfuscatedCode);
  45. console.log(`File ${filePath} has been obfuscated and written to ${obfuscatedFilePath}.`);
  46. }
  47. });
  48. }
  49. obfuscateFiles(folderPath, outputFolderPath);