NodeCallsControllerFunctionNode.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { injectable, inject } from 'inversify';
  2. import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
  3. import * as format from 'string-template';
  4. import { TNodeWithBlockStatement } from '../../types/node/TNodeWithBlockStatement';
  5. import { TObfuscationEvent } from '../../types/event-emitters/TObfuscationEvent';
  6. import { IOptions } from '../../interfaces/options/IOptions';
  7. import { IStackTraceData } from '../../interfaces/stack-trace-analyzer/IStackTraceData';
  8. import { ObfuscationEvents } from '../../enums/ObfuscationEvents';
  9. import { initializable } from '../../decorators/Initializable';
  10. import { SingleNodeCallControllerTemplate } from '../../templates/custom-nodes/SingleNodeCallControllerTemplate';
  11. import { NO_CUSTOM_NODES_PRESET } from '../../preset-options/NoCustomNodesPreset';
  12. import { AbstractCustomNode } from '../AbstractCustomNode';
  13. import { JavaScriptObfuscator } from '../../JavaScriptObfuscator';
  14. import { NodeAppender } from '../../node/NodeAppender';
  15. @injectable()
  16. export class NodeCallsControllerFunctionNode extends AbstractCustomNode {
  17. /**
  18. * @type {TObfuscationEvent}
  19. */
  20. protected readonly appendEvent: TObfuscationEvent = ObfuscationEvents.BeforeObfuscation;
  21. /**
  22. * @type {string}
  23. */
  24. @initializable()
  25. protected callsControllerFunctionName: string;
  26. /**
  27. * @type {number}
  28. */
  29. @initializable()
  30. protected randomStackTraceIndex: number;
  31. /**
  32. * @param options
  33. */
  34. constructor (
  35. @inject(ServiceIdentifiers.IOptions) options: IOptions
  36. ) {
  37. super(options);
  38. }
  39. /**
  40. * @param callsControllerFunctionName
  41. * @param randomStackTraceIndex
  42. */
  43. public initialize (callsControllerFunctionName: string, randomStackTraceIndex: number): void {
  44. this.callsControllerFunctionName = callsControllerFunctionName;
  45. this.randomStackTraceIndex = randomStackTraceIndex;
  46. }
  47. /**
  48. * @param blockScopeNode
  49. * @param stackTraceData
  50. */
  51. public appendNode (blockScopeNode: TNodeWithBlockStatement, stackTraceData: IStackTraceData[]): void {
  52. let targetBlockScope: TNodeWithBlockStatement;
  53. if (stackTraceData.length) {
  54. targetBlockScope = NodeAppender
  55. .getOptimalBlockScope(stackTraceData, this.randomStackTraceIndex, 1);
  56. } else {
  57. targetBlockScope = blockScopeNode;
  58. }
  59. NodeAppender.prependNode(targetBlockScope, this.getNode());
  60. }
  61. /**
  62. * @returns {string}
  63. */
  64. public getCode (): string {
  65. if (this.appendEvent === ObfuscationEvents.AfterObfuscation) {
  66. return JavaScriptObfuscator.obfuscate(
  67. format(SingleNodeCallControllerTemplate(), {
  68. singleNodeCallControllerFunctionName: this.callsControllerFunctionName
  69. }),
  70. Object.assign({}, NO_CUSTOM_NODES_PRESET, {
  71. seed: this.options.seed
  72. })
  73. ).getObfuscatedCode();
  74. }
  75. return format(SingleNodeCallControllerTemplate(), {
  76. singleNodeCallControllerFunctionName: this.callsControllerFunctionName
  77. });
  78. }
  79. }