JavaScriptObfuscatorInternal.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { injectable, inject } from 'inversify';
  2. import { ServiceIdentifiers } from './container/ServiceIdentifiers';
  3. import * as esprima from 'esprima';
  4. import * as escodegen from 'escodegen-wallaby';
  5. import * as esmangle from 'esmangle';
  6. import * as ESTree from 'estree';
  7. import { IGeneratorOutput } from './interfaces/IGeneratorOutput';
  8. import { IJavaScriptObfuscator } from './interfaces/IJavaScriptObfsucator';
  9. import { ILogger } from './interfaces/logger/ILogger';
  10. import { IObfuscationResult } from './interfaces/IObfuscationResult';
  11. import { IObfuscator } from './interfaces/IObfuscator';
  12. import { IOptions } from './interfaces/options/IOptions';
  13. import { IRandomGenerator } from './interfaces/utils/IRandomGenerator';
  14. import { ISourceMapCorrector } from './interfaces/ISourceMapCorrector';
  15. import { LoggingMessage } from './enums/logger/LoggingMessage';
  16. @injectable()
  17. export class JavaScriptObfuscatorInternal implements IJavaScriptObfuscator {
  18. /**
  19. * @type {GenerateOptions}
  20. */
  21. private static readonly escodegenParams: escodegen.GenerateOptions = {
  22. verbatim: 'x-verbatim-property',
  23. sourceMapWithCode: true
  24. };
  25. /**
  26. * @type {ILogger}
  27. */
  28. private readonly logger: ILogger;
  29. /**
  30. * @type {IObfuscator}
  31. */
  32. private readonly obfuscator: IObfuscator;
  33. /**
  34. * @type {IOptions}
  35. */
  36. private readonly options: IOptions;
  37. /**
  38. * @type {IRandomGenerator}
  39. */
  40. private readonly randomGenerator: IRandomGenerator;
  41. /**
  42. * @type {ISourceMapCorrector}
  43. */
  44. private readonly sourceMapCorrector: ISourceMapCorrector;
  45. /**
  46. * @param {IObfuscator} obfuscator
  47. * @param {ISourceMapCorrector} sourceMapCorrector
  48. * @param {IRandomGenerator} randomGenerator
  49. * @param {ILogger} logger
  50. * @param {IOptions} options
  51. */
  52. constructor (
  53. @inject(ServiceIdentifiers.IObfuscator) obfuscator: IObfuscator,
  54. @inject(ServiceIdentifiers.ISourceMapCorrector) sourceMapCorrector: ISourceMapCorrector,
  55. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  56. @inject(ServiceIdentifiers.ILogger) logger: ILogger,
  57. @inject(ServiceIdentifiers.IOptions) options: IOptions
  58. ) {
  59. this.obfuscator = obfuscator;
  60. this.sourceMapCorrector = sourceMapCorrector;
  61. this.randomGenerator = randomGenerator;
  62. this.logger = logger;
  63. this.options = options;
  64. }
  65. /**
  66. * @param {string} sourceCode
  67. * @returns {IObfuscationResult}
  68. */
  69. public obfuscate (sourceCode: string): IObfuscationResult {
  70. const timeStart: number = Date.now();
  71. this.logger.info(LoggingMessage.ObfuscationStarted);
  72. this.logger.info(LoggingMessage.RandomGeneratorSeed, this.randomGenerator.getSeed());
  73. // parse AST tree
  74. const astTree: ESTree.Program = this.parseCode(sourceCode);
  75. // obfuscate AST tree
  76. const obfuscatedAstTree: ESTree.Program = this.obfuscator.obfuscateAstTree(astTree);
  77. // generate code
  78. const generatorOutput: IGeneratorOutput = this.generateCode(sourceCode, obfuscatedAstTree);
  79. const obfuscationTime: number = (Date.now() - timeStart) / 1000;
  80. this.logger.success(LoggingMessage.ObfuscationCompleted, obfuscationTime);
  81. return this.getObfuscationResult(generatorOutput);
  82. }
  83. /**
  84. * @param {string} sourceCode
  85. * @returns {Program}
  86. */
  87. private parseCode (sourceCode: string): ESTree.Program {
  88. return esprima.parseScript(sourceCode, {
  89. loc: this.options.sourceMap
  90. });
  91. }
  92. /**
  93. * @param {string} sourceCode
  94. * @param {Program} astTree
  95. * @returns {IGeneratorOutput}
  96. */
  97. private generateCode (sourceCode: string, astTree: ESTree.Program): IGeneratorOutput {
  98. const escodegenParams: escodegen.GenerateOptions = {
  99. ...JavaScriptObfuscatorInternal.escodegenParams
  100. };
  101. if (this.options.sourceMap) {
  102. escodegenParams.sourceMap = 'sourceMap';
  103. escodegenParams.sourceContent = sourceCode;
  104. }
  105. if (this.options.mangle) {
  106. astTree = esmangle.mangle(astTree);
  107. }
  108. const generatorOutput: IGeneratorOutput = escodegen.generate(astTree, {
  109. ...escodegenParams,
  110. format: {
  111. compact: this.options.compact
  112. }
  113. });
  114. generatorOutput.map = generatorOutput.map ? generatorOutput.map.toString() : '';
  115. return generatorOutput;
  116. }
  117. /**
  118. * @param {IGeneratorOutput} generatorOutput
  119. * @returns {IObfuscationResult}
  120. */
  121. private getObfuscationResult (generatorOutput: IGeneratorOutput): IObfuscationResult {
  122. return this.sourceMapCorrector.correct(
  123. generatorOutput.code,
  124. generatorOutput.map
  125. );
  126. }
  127. }