JavaScriptObfuscatorCLI.ts 6.9 KB

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