JavaScriptObfuscatorCLI.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import * as commands from 'commander';
  2. import * as fs from 'fs';
  3. import * as mkdirp from 'mkdirp';
  4. import * as path from 'path';
  5. import { execSync } from "child_process";
  6. import { IOptionsPreset } from "../interfaces/IOptionsPreset";
  7. import { DEFAULT_PRESET } from "../preset-options/DefaultPreset";
  8. import { JavaScriptObfuscator } from "../JavaScriptObfuscator";
  9. export class JavaScriptObfuscatorCLI {
  10. /**
  11. * @type {string[]}
  12. */
  13. private static availableInputExtensions: string[] = [
  14. '.js'
  15. ];
  16. /**
  17. * @type {BufferEncoding}
  18. */
  19. private static encoding: BufferEncoding = 'utf8';
  20. /**
  21. * @type {string}
  22. */
  23. private static packageName: string = 'javascript-obfuscator';
  24. /**
  25. * @type {string[]}
  26. */
  27. private arguments: string[];
  28. /**
  29. * @type {string[]}
  30. */
  31. private rawArguments: string[];
  32. /**
  33. * @type {string}
  34. */
  35. private data: string = '';
  36. /**
  37. * @type {string}
  38. */
  39. private inputPath: string;
  40. /**
  41. * @param argv
  42. */
  43. constructor (argv: string[]) {
  44. this.rawArguments = argv;
  45. this.arguments = this.rawArguments.slice(2);
  46. }
  47. /**
  48. * @returns {IOptionsPreset}
  49. */
  50. private static buildOptions (): IOptionsPreset {
  51. let options: IOptionsPreset = {},
  52. availableOptions: string[] = Object.keys(DEFAULT_PRESET);
  53. for (let option in commands) {
  54. if (!commands.hasOwnProperty(option)) {
  55. continue;
  56. }
  57. if (availableOptions.indexOf(option) === -1) {
  58. continue;
  59. }
  60. options[option] = (<any>commands)[option];
  61. }
  62. return Object.assign({}, DEFAULT_PRESET, options);
  63. }
  64. /**
  65. * @returns {string}
  66. */
  67. private static getBuildVersion (): string {
  68. return execSync(`npm info ${JavaScriptObfuscatorCLI.packageName} version`, {
  69. encoding: JavaScriptObfuscatorCLI.encoding
  70. });
  71. }
  72. /**
  73. * @param filePath
  74. */
  75. private static isFilePath (filePath: string): boolean {
  76. try {
  77. return fs.statSync(filePath).isFile();
  78. } catch (e) {
  79. return false;
  80. }
  81. }
  82. /**
  83. * @param value
  84. * @returns {boolean}
  85. */
  86. private static parseBoolean (value: string): boolean {
  87. return value === 'true' || value === '1';
  88. }
  89. public run (): void {
  90. this.configureCommands();
  91. if (!this.arguments.length || this.arguments.indexOf('--help') >= 0) {
  92. commands.outputHelp();
  93. return;
  94. }
  95. this.inputPath = this.getInputPath();
  96. this.getData();
  97. this.processData();
  98. }
  99. private configureCommands (): void {
  100. commands
  101. .version(JavaScriptObfuscatorCLI.getBuildVersion(), '-v, --version')
  102. .usage('<inputPath> [options]')
  103. .option('-o, --output <path>', 'Output path for obfuscated code')
  104. .option('--compact <boolean>', 'Disable one line output code compacting', JavaScriptObfuscatorCLI.parseBoolean)
  105. .option('--debugProtection <boolean>', 'Disable browser Debug panel (can cause DevTools enabled browser freeze)', JavaScriptObfuscatorCLI.parseBoolean)
  106. .option('--debugProtectionInterval <boolean>', 'Disable browser Debug panel even after page was loaded (can cause DevTools enabled browser freeze)', JavaScriptObfuscatorCLI.parseBoolean)
  107. .option('--disableConsoleOutput <boolean>', 'Allow console.log, console.info, console.error and console.warn messages output into browser console', JavaScriptObfuscatorCLI.parseBoolean)
  108. .option('--encodeUnicodeLiterals <boolean>', 'All literals in Unicode array become encoded in Base64 (this option can slightly slow down your code speed)', JavaScriptObfuscatorCLI.parseBoolean)
  109. .option('--reservedNames <list>', 'Disable obfuscation of variable names, function names and names of function parameters that match the passed RegExp patterns (comma separated)', (val: string) => val.split(','))
  110. .option('--rotateUnicodeArray <boolean>', 'Disable rotation of unicode array values during obfuscation', JavaScriptObfuscatorCLI.parseBoolean)
  111. .option('--selfDefending <boolean>', 'Disables self-defending for obfuscated code', JavaScriptObfuscatorCLI.parseBoolean)
  112. .option('--unicodeArray <boolean>', 'Disables gathering of all literal strings into an array and replacing every literal string with an array call', JavaScriptObfuscatorCLI.parseBoolean)
  113. .option('--unicodeArrayThreshold <number>', 'The probability that the literal string will be inserted into unicodeArray (Default: 0.8, Min: 0, Max: 1)', parseFloat)
  114. .option('--wrapUnicodeArrayCalls <boolean>', 'Disables usage of special access function instead of direct array call', JavaScriptObfuscatorCLI.parseBoolean)
  115. .parse(this.rawArguments);
  116. commands.on('--help', () => {
  117. let isWindows: boolean = process.platform === 'win32',
  118. commandName: string = isWindows ? 'type' : 'cat';
  119. console.log(' Examples:\n');
  120. console.log(' %> javascript-obfuscator < in.js > out.js');
  121. console.log(` %> ${commandName} in1.js in2.js | javascript-obfuscator > out.js`);
  122. console.log('');
  123. process.exit();
  124. });
  125. }
  126. private getData (): void {
  127. this.data = fs.readFileSync(this.inputPath, JavaScriptObfuscatorCLI.encoding);
  128. }
  129. /**
  130. * @returns {string}
  131. */
  132. private getInputPath (): string {
  133. let inputPath: string = this.arguments[0];
  134. if (!JavaScriptObfuscatorCLI.isFilePath(inputPath)) {
  135. throw new ReferenceError(`First argument must be a valid file path`);
  136. }
  137. if (JavaScriptObfuscatorCLI.availableInputExtensions.indexOf(path.extname(inputPath)) === -1) {
  138. throw new ReferenceError(`Input file must have .js extension`);
  139. }
  140. return inputPath;
  141. }
  142. /**
  143. * @returns {string}
  144. */
  145. private getOutputPath (): string {
  146. let outputPath: string = (<any>commands).output;
  147. if (outputPath) {
  148. return outputPath;
  149. }
  150. return this.inputPath
  151. .split('.')
  152. .map<string>((value: string, index: number) => {
  153. return index === 0 ? `${value}-obfuscated` : value;
  154. })
  155. .join('.');
  156. }
  157. private processData (): void {
  158. let outputPath: string = this.getOutputPath(),
  159. dirName: string = path.dirname(outputPath);
  160. mkdirp.sync(dirName);
  161. fs.writeFileSync(
  162. this.getOutputPath(),
  163. JavaScriptObfuscator.obfuscate(this.data, JavaScriptObfuscatorCLI.buildOptions()),
  164. {
  165. encoding: JavaScriptObfuscatorCLI.encoding
  166. }
  167. );
  168. }
  169. }