main.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. let Fs = require('fs');
  2. let Path = require('path');
  3. let JavascriptObfuscator = require('javascript-obfuscator');
  4. let defaultConfig = {
  5. auto: false,
  6. files: ['/src/project.js'],
  7. useAbsPath: false,
  8. preset: 'lower',
  9. options: {}
  10. };
  11. let presetFileUrl = 'packages://ccc-obfuscated-code/preset.json';
  12. let presets = null;
  13. /**
  14. * 保存配置
  15. * @param {*} config
  16. */
  17. function saveConfig(config) {
  18. let projectPath = Editor.Project.path || Editor.projectPath;
  19. let configDirPath = Path.join(projectPath, '/local/');
  20. if (!Fs.existsSync(configDirPath)) Fs.mkdirSync(configDirPath);
  21. let configFilePath = Path.join(configDirPath, 'ccc-obfuscated-code.json');
  22. let object = {};
  23. // 读取本地配置
  24. if (Fs.existsSync(configFilePath)) {
  25. object = JSON.parse(Fs.readFileSync(configFilePath, 'utf8'));
  26. }
  27. // 写入配置
  28. for (let key in config) {
  29. object[key] = config[key];
  30. }
  31. let string = JSON.stringify(object, null, 2);
  32. Fs.writeFileSync(configFilePath, string);
  33. Editor.log('[CC] 配置文件路径', configFilePath);
  34. }
  35. /**
  36. * 读取配置
  37. */
  38. function getConfig() {
  39. let projectPath = Editor.Project.path || Editor.projectPath;
  40. let configFilePath = Path.join(projectPath, '/local/ccc-obfuscated-code.json');
  41. let config = null;
  42. if (Fs.existsSync(configFilePath)) {
  43. config = JSON.parse(Fs.readFileSync(configFilePath, 'utf8'));
  44. let projectName = Editor.Project.name || projectPath.slice(projectPath.lastIndexOf('\\') + 1);
  45. if (config[projectName]) {
  46. Fs.unlinkSync(configFilePath);
  47. config = null;
  48. }
  49. }
  50. if (!config) {
  51. config = defaultConfig;
  52. config.options = getPreset('off');
  53. if (config.preset !== 'off') {
  54. let preset = getPreset(config.preset);
  55. for (let key in preset) {
  56. config.options[key] = preset[key];
  57. }
  58. }
  59. }
  60. return config;
  61. };
  62. /**
  63. * 读取预设参数
  64. */
  65. function getPreset(type) {
  66. if (presets) {
  67. return presets[type];
  68. } else {
  69. let presetFilePath = Editor.url(presetFileUrl);
  70. if (Fs.existsSync(presetFilePath)) {
  71. presets = JSON.parse(Fs.readFileSync(presetFilePath, 'utf8'));
  72. return presets[type];
  73. } else {
  74. return null;
  75. }
  76. }
  77. };
  78. /**
  79. * 混淆
  80. * @param {*} path 文件路径
  81. * @param {*} options 混淆参数
  82. */
  83. function obfuscate(path, options) {
  84. let sourceCode = Fs.readFileSync(path, 'utf8');
  85. let obfuscationResult = JavascriptObfuscator.obfuscate(sourceCode, options);
  86. let obfuscatedCode = obfuscationResult.getObfuscatedCode();
  87. Fs.writeFileSync(path, obfuscatedCode);
  88. }
  89. module.exports = {
  90. load() {
  91. Editor.Builder.on('build-start', this.onBuildStart);
  92. Editor.Builder.on('build-finished', this.onBuildFinished);
  93. },
  94. unload() {
  95. Editor.Builder.removeListener('build-start', this.onBuildStart);
  96. Editor.Builder.removeListener('build-finished', this.onBuildFinished);
  97. },
  98. messages: {
  99. 'open-panel'() {
  100. Editor.log('[CC] 代码混淆工具/构建后自动混淆');
  101. Editor.Panel.open('ccc-obfuscated-code');
  102. },
  103. // TODO
  104. // 'open-panel'() {
  105. // Editor.log('[CC] 代码混淆工具/主动混淆');
  106. // Editor.Panel.open('ccc-obfuscated-code-do');
  107. // },
  108. 'save-config'(event, config) {
  109. Editor.log('[CC] 保存配置');
  110. saveConfig(config);
  111. event.reply(null, true);
  112. },
  113. 'read-config'(event) {
  114. Editor.log('[CC] 读取配置');
  115. let config = getConfig();
  116. event.reply(null, config);
  117. },
  118. 'get-preset'(event, name) {
  119. Editor.log('[CC] 读取预设', name);
  120. let preset = getPreset(name);
  121. if (preset) {
  122. event.reply(null, preset);
  123. } else {
  124. Editor.log('[CC] 预设文件已丢失');
  125. Editor.log('[CC] 文件下载地址 https://gitee.com/ifaswind/ccc-obfuscated-code/blob/master/preset.json');
  126. event.reply(null, {});
  127. }
  128. }
  129. },
  130. onBuildStart(options, callback) {
  131. let config = getConfig();
  132. if (config.auto) Editor.log('[CC] 将在项目构建完成后自动混淆代码');
  133. callback();
  134. },
  135. onBuildFinished(options, callback) {
  136. let config = getConfig();
  137. if (config.auto) {
  138. Editor.log('[CC] 正在混淆代码');
  139. for (let i = 0; i < config.files.length; i++) {
  140. if (config.files[i] === '') continue;
  141. let path = config.useAbsPath ? config.files[i] : Path.join(options.dest, config.files[i]);
  142. if (Fs.existsSync(path)) {
  143. Editor.log('[CC] 混淆文件', path);
  144. obfuscate(path, config.options);
  145. } else {
  146. Editor.warn('[CC] 文件不存在', path);
  147. }
  148. }
  149. Editor.log('[CC] 混淆已结束');
  150. }
  151. callback();
  152. },
  153. }