DomainLockCustomNodeGroup.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { injectable, inject } from 'inversify';
  2. import { ServiceIdentifiers } from '../../../container/ServiceIdentifiers';
  3. import { TCustomNodeFactory } from '../../../types/container/custom-nodes/TCustomNodeFactory';
  4. import { TNodeWithBlockStatement } from '../../../types/node/TNodeWithBlockStatement';
  5. import { ICustomNode } from '../../../interfaces/custom-nodes/ICustomNode';
  6. import { IObfuscationEventEmitter } from '../../../interfaces/event-emitters/IObfuscationEventEmitter';
  7. import { IOptions } from '../../../interfaces/options/IOptions';
  8. import { IRandomGenerator } from '../../../interfaces/utils/IRandomGenerator';
  9. import { IStackTraceData } from '../../../interfaces/analyzers/stack-trace-analyzer/IStackTraceData';
  10. import { initializable } from '../../../decorators/Initializable';
  11. import { CustomNode } from '../../../enums/container/custom-nodes/CustomNode';
  12. import { ObfuscationEvent } from '../../../enums/event-emitters/ObfuscationEvent';
  13. import { AbstractCustomNodeGroup } from '../../AbstractCustomNodeGroup';
  14. import { NodeAppender } from '../../../node/NodeAppender';
  15. @injectable()
  16. export class DomainLockCustomNodeGroup extends AbstractCustomNodeGroup {
  17. /**
  18. * @type {ObfuscationEvent}
  19. */
  20. protected readonly appendEvent: ObfuscationEvent = ObfuscationEvent.BeforeObfuscation;
  21. /**
  22. * @type {Map<CustomNode, ICustomNode>}
  23. */
  24. @initializable()
  25. protected customNodes: Map <CustomNode, ICustomNode>;
  26. /**
  27. * @type {TCustomNodeFactory}
  28. */
  29. private readonly customNodeFactory: TCustomNodeFactory;
  30. /**
  31. * @type {IObfuscationEventEmitter}
  32. */
  33. private readonly obfuscationEventEmitter: IObfuscationEventEmitter;
  34. /**
  35. * @param {TCustomNodeFactory} customNodeFactory
  36. * @param {IObfuscationEventEmitter} obfuscationEventEmitter
  37. * @param {IRandomGenerator} randomGenerator
  38. * @param {IOptions} options
  39. */
  40. constructor (
  41. @inject(ServiceIdentifiers.Factory__ICustomNode) customNodeFactory: TCustomNodeFactory,
  42. @inject(ServiceIdentifiers.IObfuscationEventEmitter) obfuscationEventEmitter: IObfuscationEventEmitter,
  43. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  44. @inject(ServiceIdentifiers.IOptions) options: IOptions
  45. ) {
  46. super(randomGenerator, options);
  47. this.customNodeFactory = customNodeFactory;
  48. this.obfuscationEventEmitter = obfuscationEventEmitter;
  49. }
  50. /**
  51. * @param {TNodeWithBlockStatement} blockScopeNode
  52. * @param {IStackTraceData[]} stackTraceData
  53. */
  54. public appendCustomNodes (blockScopeNode: TNodeWithBlockStatement, stackTraceData: IStackTraceData[]): void {
  55. const randomStackTraceIndex: number = this.getRandomStackTraceIndex(stackTraceData.length);
  56. // domainLockNode append
  57. this.appendCustomNodeIfExist(CustomNode.DomainLockNode, (customNode: ICustomNode) => {
  58. NodeAppender.appendNodeToOptimalBlockScope(
  59. stackTraceData,
  60. blockScopeNode,
  61. customNode.getNode(),
  62. randomStackTraceIndex
  63. );
  64. });
  65. // nodeCallsControllerFunctionNode append
  66. this.appendCustomNodeIfExist(CustomNode.NodeCallsControllerFunctionNode, (customNode: ICustomNode) => {
  67. let targetBlockScope: TNodeWithBlockStatement;
  68. if (stackTraceData.length) {
  69. targetBlockScope = NodeAppender.getOptimalBlockScope(stackTraceData, randomStackTraceIndex, 1);
  70. } else {
  71. targetBlockScope = blockScopeNode;
  72. }
  73. NodeAppender.prependNode(targetBlockScope, customNode.getNode());
  74. });
  75. }
  76. public initialize (): void {
  77. this.customNodes = new Map <CustomNode, ICustomNode>();
  78. if (!this.options.domainLock.length) {
  79. return;
  80. }
  81. const callsControllerFunctionName: string = this.randomGenerator.getRandomVariableName(6);
  82. const domainLockNode: ICustomNode = this.customNodeFactory(CustomNode.DomainLockNode);
  83. const nodeCallsControllerFunctionNode: ICustomNode = this.customNodeFactory(CustomNode.NodeCallsControllerFunctionNode);
  84. domainLockNode.initialize(callsControllerFunctionName);
  85. nodeCallsControllerFunctionNode.initialize(this.appendEvent, callsControllerFunctionName);
  86. this.customNodes.set(CustomNode.DomainLockNode, domainLockNode);
  87. this.customNodes.set(CustomNode.NodeCallsControllerFunctionNode, nodeCallsControllerFunctionNode);
  88. }
  89. }