JavaScriptObfuscatorCLI.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. import * as commander from 'commander';
  2. import * as packageJson from 'pjson';
  3. import * as path from 'path';
  4. import { TInputCLIOptions } from '../types/options/TInputCLIOptions';
  5. import { TInputOptions } from '../types/options/TInputOptions';
  6. import { TObject } from '../types/TObject';
  7. import { TSourceCodeData } from '../types/cli/TSourceCodeData';
  8. import { IFileData } from '../interfaces/cli/IFileData';
  9. import { IInitializable } from '../interfaces/IInitializable';
  10. import { IObfuscationResult } from '../interfaces/IObfuscationResult';
  11. import { initializable } from '../decorators/Initializable';
  12. import { DEFAULT_PRESET } from '../options/presets/Default';
  13. import { ArraySanitizer } from './sanitizers/ArraySanitizer';
  14. import { BooleanSanitizer } from './sanitizers/BooleanSanitizer';
  15. import { IdentifierNamesGeneratorSanitizer } from './sanitizers/IdentifierNamesGeneratorSanitizer';
  16. import { ObfuscationTargetSanitizer } from './sanitizers/ObfuscatingTargetSanitizer';
  17. import { SourceMapModeSanitizer } from './sanitizers/SourceMapModeSanitizer';
  18. import { StringArrayEncodingSanitizer } from './sanitizers/StringArrayEncodingSanitizer';
  19. import { CLIUtils } from './utils/CLIUtils';
  20. import { JavaScriptObfuscator } from '../JavaScriptObfuscatorFacade';
  21. import { SourceCodeReader } from './utils/SourceCodeReader';
  22. export class JavaScriptObfuscatorCLI implements IInitializable {
  23. /**
  24. * @type {string[]}
  25. */
  26. public static readonly availableInputExtensions: string[] = [
  27. '.js'
  28. ];
  29. /**
  30. * @type {BufferEncoding}
  31. */
  32. public static readonly encoding: BufferEncoding = 'utf8';
  33. /**
  34. * @type {string}
  35. */
  36. public static obfuscatedFilePrefix: string = '-obfuscated';
  37. /**
  38. * @type {string[]}
  39. */
  40. private readonly arguments: string[];
  41. /**
  42. * @type {string[]}
  43. */
  44. private readonly rawArguments: string[];
  45. /**
  46. * @type {commander.CommanderStatic}
  47. */
  48. @initializable()
  49. private commands: commander.CommanderStatic;
  50. /**
  51. * @type {TInputCLIOptions}
  52. */
  53. @initializable()
  54. private inputCLIOptions: TInputCLIOptions;
  55. /**
  56. * @type {string}
  57. */
  58. @initializable()
  59. private inputPath: string;
  60. /**
  61. * @param {string[]} argv
  62. */
  63. constructor (argv: string[]) {
  64. this.rawArguments = argv;
  65. this.arguments = argv.slice(2);
  66. }
  67. /**
  68. * @param {TObject} options
  69. * @returns {TInputOptions}
  70. */
  71. private static filterOptions (options: TObject): TInputOptions {
  72. const filteredOptions: TInputOptions = {};
  73. for (const option in options) {
  74. if (!options.hasOwnProperty(option) || options[option] === undefined) {
  75. continue;
  76. }
  77. filteredOptions[option] = options[option];
  78. }
  79. return filteredOptions;
  80. }
  81. /**
  82. * @param {string} sourceCode
  83. * @param {string} outputCodePath
  84. * @param {TInputOptions} options
  85. */
  86. private static processSourceCodeWithoutSourceMap (
  87. sourceCode: string,
  88. outputCodePath: string,
  89. options: TInputOptions
  90. ): void {
  91. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(sourceCode, options).getObfuscatedCode();
  92. CLIUtils.writeFile(outputCodePath, obfuscatedCode);
  93. }
  94. /**
  95. * @param {string} sourceCode
  96. * @param {string} outputCodePath
  97. * @param {TInputOptions} options
  98. */
  99. private static processSourceCodeWithSourceMap (
  100. sourceCode: string,
  101. outputCodePath: string,
  102. options: TInputOptions
  103. ): void {
  104. const outputSourceMapPath: string = CLIUtils.getOutputSourceMapPath(
  105. outputCodePath,
  106. options.sourceMapFileName || ''
  107. );
  108. options = {
  109. ...options,
  110. sourceMapFileName: path.basename(outputSourceMapPath)
  111. };
  112. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(sourceCode, options);
  113. CLIUtils.writeFile(outputCodePath, obfuscationResult.getObfuscatedCode());
  114. if (options.sourceMapMode === 'separate' && obfuscationResult.getSourceMap()) {
  115. CLIUtils.writeFile(outputSourceMapPath, obfuscationResult.getSourceMap());
  116. }
  117. }
  118. public initialize (): void {
  119. this.inputPath = this.arguments[0] || '';
  120. this.commands = <commander.CommanderStatic>(new commander.Command());
  121. this.configureCommands();
  122. this.configureHelp();
  123. this.inputCLIOptions = this.commands.opts();
  124. }
  125. public run (): void {
  126. const canShowHelp: boolean = !this.arguments.length || this.arguments.includes('--help');
  127. if (canShowHelp) {
  128. return this.commands.outputHelp();
  129. }
  130. const sourceCodeData: TSourceCodeData = SourceCodeReader.readSourceCode(this.inputPath);
  131. this.processSourceCodeData(sourceCodeData);
  132. }
  133. /**
  134. * @returns {TInputOptions}
  135. */
  136. private buildOptions (): TInputOptions {
  137. const inputCLIOptions: TInputOptions = JavaScriptObfuscatorCLI.filterOptions(this.inputCLIOptions);
  138. const configFilePath: string | undefined = this.inputCLIOptions.config;
  139. const configFileLocation: string = configFilePath ? path.resolve(configFilePath, '.') : '';
  140. const configFileOptions: TInputOptions = configFileLocation ? CLIUtils.getUserConfig(configFileLocation) : {};
  141. return {
  142. ...DEFAULT_PRESET,
  143. ...configFileOptions,
  144. ...inputCLIOptions
  145. };
  146. }
  147. private configureCommands (): void {
  148. this.commands
  149. .usage('<inputPath> [options]')
  150. .version(
  151. packageJson.version,
  152. '-v, --version'
  153. )
  154. .option(
  155. '-o, --output <path>',
  156. 'Output path for obfuscated code'
  157. )
  158. .option(
  159. '--compact <boolean>',
  160. 'Disable one line output code compacting',
  161. BooleanSanitizer
  162. )
  163. .option(
  164. '--config <boolean>',
  165. 'Name of js / json config file'
  166. )
  167. .option(
  168. '--control-flow-flattening <boolean>',
  169. 'Enables control flow flattening',
  170. BooleanSanitizer
  171. )
  172. .option(
  173. '--control-flow-flattening-threshold <number>',
  174. 'The probability that the control flow flattening transformation will be applied to the node',
  175. parseFloat
  176. )
  177. .option(
  178. '--dead-code-injection <boolean>',
  179. 'Enables dead code injection',
  180. BooleanSanitizer
  181. )
  182. .option(
  183. '--dead-code-injection-threshold <number>',
  184. 'The probability that the dead code injection transformation will be applied to the node',
  185. parseFloat
  186. )
  187. .option(
  188. '--debug-protection <boolean>',
  189. 'Disable browser Debug panel (can cause DevTools enabled browser freeze)',
  190. BooleanSanitizer
  191. )
  192. .option(
  193. '--debug-protection-interval <boolean>',
  194. 'Disable browser Debug panel even after page was loaded (can cause DevTools enabled browser freeze)',
  195. BooleanSanitizer
  196. )
  197. .option(
  198. '--disable-console-output <boolean>',
  199. 'Allow console.log, console.info, console.error and console.warn messages output into browser console',
  200. BooleanSanitizer
  201. )
  202. .option(
  203. '--domain-lock <list> (comma separated, without whitespaces)',
  204. 'Blocks the execution of the code in domains that do not match the passed RegExp patterns (comma separated)',
  205. ArraySanitizer
  206. )
  207. .option(
  208. '--identifier-names-generator <string> [hexadecimal, mangled]', 'Sets identifier names generator (Default: hexadecimal)',
  209. IdentifierNamesGeneratorSanitizer
  210. )
  211. .option(
  212. '--log <boolean>', 'Enables logging of the information to the console',
  213. BooleanSanitizer
  214. )
  215. .option(
  216. '--reserved-names <list> (comma separated, without whitespaces)',
  217. 'Disable obfuscation of variable names, function names and names of function parameters that match the passed RegExp patterns (comma separated)',
  218. ArraySanitizer
  219. )
  220. .option(
  221. '--rename-globals <boolean>', 'Allows to enable obfuscation of global variable and function names with declaration.',
  222. BooleanSanitizer
  223. )
  224. .option(
  225. '--rotate-string-array <boolean>', 'Disable rotation of unicode array values during obfuscation',
  226. BooleanSanitizer
  227. )
  228. .option(
  229. '--seed <number>',
  230. 'Sets seed for random generator. This is useful for creating repeatable results.',
  231. parseFloat
  232. )
  233. .option(
  234. '--self-defending <boolean>',
  235. 'Disables self-defending for obfuscated code',
  236. BooleanSanitizer
  237. )
  238. .option(
  239. '--source-map <boolean>',
  240. 'Enables source map generation',
  241. BooleanSanitizer
  242. )
  243. .option(
  244. '--source-map-base-url <string>',
  245. 'Sets base url to the source map import url when `--source-map-mode=separate`'
  246. )
  247. .option(
  248. '--source-map-file-name <string>',
  249. 'Sets file name for output source map when `--source-map-mode=separate`'
  250. )
  251. .option(
  252. '--source-map-mode <string> [inline, separate]',
  253. 'Specify source map output mode',
  254. SourceMapModeSanitizer
  255. )
  256. .option(
  257. '--string-array <boolean>',
  258. 'Disables gathering of all literal strings into an array and replacing every literal string with an array call',
  259. BooleanSanitizer
  260. )
  261. .option(
  262. '--string-array-encoding <boolean|string> [true, false, base64, rc4]',
  263. 'Encodes all strings in strings array using base64 or rc4 (this option can slow down your code speed',
  264. StringArrayEncodingSanitizer
  265. )
  266. .option(
  267. '--string-array-threshold <number>',
  268. 'The probability that the literal string will be inserted into stringArray (Default: 0.8, Min: 0, Max: 1)',
  269. parseFloat
  270. )
  271. .option(
  272. '--target <string>',
  273. 'Allows to set target environment for obfuscated code.',
  274. ObfuscationTargetSanitizer
  275. )
  276. .option(
  277. '--unicode-escape-sequence <boolean>',
  278. 'Allows to enable/disable string conversion to unicode escape sequence',
  279. BooleanSanitizer
  280. )
  281. .parse(this.rawArguments);
  282. }
  283. private configureHelp (): void {
  284. this.commands.on('--help', () => {
  285. console.log(' Examples:\n');
  286. console.log(' %> javascript-obfuscator input_file_name.js --compact true --self-defending false');
  287. console.log(' %> javascript-obfuscator input_file_name.js --output output_file_name.js --compact true --self-defending false');
  288. console.log(' %> javascript-obfuscator input_directory_name --compact true --self-defending false');
  289. console.log('');
  290. });
  291. }
  292. /**
  293. * @param {TSourceCodeData} sourceCodeData
  294. */
  295. private processSourceCodeData (sourceCodeData: TSourceCodeData): void {
  296. if (!Array.isArray(sourceCodeData)) {
  297. const outputCodePath: string = this.inputCLIOptions.output || CLIUtils.getOutputCodePath(this.inputPath);
  298. this.processSourceCode(sourceCodeData, outputCodePath);
  299. } else {
  300. sourceCodeData.forEach(({ filePath, content }: IFileData) => {
  301. const outputCodePath: string = CLIUtils.getOutputCodePath(filePath);
  302. this.processSourceCode(content, outputCodePath);
  303. });
  304. }
  305. }
  306. /**
  307. * @param {string} sourceCode
  308. * @param {string} outputCodePath
  309. */
  310. private processSourceCode (sourceCode: string, outputCodePath: string): void {
  311. const options: TInputOptions = this.buildOptions();
  312. if (options.sourceMap) {
  313. JavaScriptObfuscatorCLI.processSourceCodeWithSourceMap(sourceCode, outputCodePath, options);
  314. } else {
  315. JavaScriptObfuscatorCLI.processSourceCodeWithoutSourceMap(sourceCode, outputCodePath, options);
  316. }
  317. }
  318. }