JavaScriptObfuscatorInternal.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { injectable, inject } from 'inversify';
  2. import { ServiceIdentifiers } from './container/ServiceIdentifiers';
  3. import * as esprima from 'esprima';
  4. import * as escodegen from 'escodegen';
  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 { IObfuscationResult } from './interfaces/IObfuscationResult';
  10. import { IObfuscator } from './interfaces/IObfuscator';
  11. import { IOptions } from './interfaces/options/IOptions';
  12. import { ISourceMapCorrector } from './interfaces/ISourceMapCorrector';
  13. @injectable()
  14. export class JavaScriptObfuscatorInternal implements IJavaScriptObfuscator {
  15. /**
  16. * @type {GenerateOptions}
  17. */
  18. private static readonly escodegenParams: escodegen.GenerateOptions = {
  19. verbatim: 'x-verbatim-property',
  20. sourceMapWithCode: true
  21. };
  22. /**
  23. * @type {IObfuscator}
  24. */
  25. private readonly obfuscator: IObfuscator;
  26. /**
  27. * @type {IOptions}
  28. */
  29. private readonly options: IOptions;
  30. /**
  31. * @type {ISourceMapCorrector}
  32. */
  33. private readonly sourceMapCorrector: ISourceMapCorrector;
  34. /**
  35. * @param obfuscator
  36. * @param sourceMapCorrector
  37. * @param options
  38. */
  39. constructor (
  40. @inject(ServiceIdentifiers.IObfuscator) obfuscator: IObfuscator,
  41. @inject(ServiceIdentifiers.ISourceMapCorrector) sourceMapCorrector: ISourceMapCorrector,
  42. @inject(ServiceIdentifiers.IOptions) options: IOptions
  43. ) {
  44. this.obfuscator = obfuscator;
  45. this.sourceMapCorrector = sourceMapCorrector;
  46. this.options = options;
  47. }
  48. /**
  49. * @param sourceCode
  50. * @returns {IObfuscationResult}
  51. */
  52. public obfuscate (sourceCode: string): IObfuscationResult {
  53. // parse AST tree
  54. const astTree: ESTree.Program = esprima.parse(sourceCode, { loc: this.options.sourceMap });
  55. // obfuscate AST tree
  56. const obfuscatedAstTree: ESTree.Program = this.obfuscator.obfuscateAstTree(astTree);
  57. // generate code
  58. const generatorOutput: IGeneratorOutput = this.generateCode(sourceCode, obfuscatedAstTree);
  59. return this.getObfuscationResult(generatorOutput);
  60. }
  61. /**
  62. * @param sourceCode
  63. * @param astTree
  64. */
  65. private generateCode (sourceCode: string, astTree: ESTree.Program): IGeneratorOutput {
  66. const escodegenParams: escodegen.GenerateOptions = {
  67. ...JavaScriptObfuscatorInternal.escodegenParams
  68. };
  69. if (this.options.sourceMap) {
  70. escodegenParams.sourceMap = 'sourceMap';
  71. escodegenParams.sourceContent = sourceCode;
  72. }
  73. if (this.options.mangle) {
  74. astTree = esmangle.mangle(astTree);
  75. }
  76. const generatorOutput: IGeneratorOutput = escodegen.generate(astTree, {
  77. ...escodegenParams,
  78. format: {
  79. compact: this.options.compact
  80. }
  81. });
  82. generatorOutput.map = generatorOutput.map ? generatorOutput.map.toString() : '';
  83. return generatorOutput;
  84. }
  85. /**
  86. * @param generatorOutput
  87. * @returns {IObfuscationResult}
  88. */
  89. private getObfuscationResult (generatorOutput: IGeneratorOutput): IObfuscationResult {
  90. return this.sourceMapCorrector.correct(
  91. generatorOutput.code,
  92. generatorOutput.map
  93. );
  94. }
  95. }