StringArrayCustomNodeGroup.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { inject, injectable, } from 'inversify';
  2. import { ServiceIdentifiers } from '../../../container/ServiceIdentifiers';
  3. import { TCustomNodeFactory } from '../../../types/container/custom-nodes/TCustomNodeFactory';
  4. import { TIdentifierNamesGeneratorFactory } from '../../../types/container/generators/TIdentifierNamesGeneratorFactory';
  5. import { TNodeWithBlockScope } from '../../../types/node/TNodeWithBlockScope';
  6. import { TStringArrayStorage } from '../../../types/storages/TStringArrayStorage';
  7. import { ICustomNode } from '../../../interfaces/custom-nodes/ICustomNode';
  8. import { IOptions } from '../../../interfaces/options/IOptions';
  9. import { IRandomGenerator } from '../../../interfaces/utils/IRandomGenerator';
  10. import { IStackTraceData } from '../../../interfaces/analyzers/stack-trace-analyzer/IStackTraceData';
  11. import { initializable } from '../../../decorators/Initializable';
  12. import { CustomNode } from '../../../enums/custom-nodes/CustomNode';
  13. import { ObfuscationEvent } from '../../../enums/event-emitters/ObfuscationEvent';
  14. import { AbstractCustomNodeGroup } from '../../AbstractCustomNodeGroup';
  15. import { NodeAppender } from '../../../node/NodeAppender';
  16. @injectable()
  17. export class StringArrayCustomNodeGroup extends AbstractCustomNodeGroup {
  18. /**
  19. * @type {ObfuscationEvent}
  20. */
  21. protected appendEvent: ObfuscationEvent = ObfuscationEvent.AfterObfuscation;
  22. /**
  23. * @type {Map<CustomNode, ICustomNode>}
  24. */
  25. @initializable()
  26. protected customNodes!: Map <CustomNode, ICustomNode>;
  27. /**
  28. * @type {TCustomNodeFactory}
  29. */
  30. private readonly customNodeFactory: TCustomNodeFactory;
  31. /**
  32. * @type {TStringArrayStorage}
  33. */
  34. private readonly stringArrayStorage: TStringArrayStorage;
  35. /**
  36. * @param {TCustomNodeFactory} customNodeFactory
  37. * @param {TStringArrayStorage} stringArrayStorage
  38. * @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
  39. * @param {IRandomGenerator} randomGenerator
  40. * @param {IOptions} options
  41. */
  42. constructor (
  43. @inject(ServiceIdentifiers.Factory__ICustomNode) customNodeFactory: TCustomNodeFactory,
  44. @inject(ServiceIdentifiers.TStringArrayStorage) stringArrayStorage: TStringArrayStorage,
  45. @inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
  46. identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
  47. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  48. @inject(ServiceIdentifiers.IOptions) options: IOptions
  49. ) {
  50. super(identifierNamesGeneratorFactory, randomGenerator, options);
  51. this.customNodeFactory = customNodeFactory;
  52. this.stringArrayStorage = stringArrayStorage;
  53. }
  54. /**
  55. * @param {TNodeWithBlockScope} blockScopeNode
  56. * @param {IStackTraceData[]} stackTraceData
  57. */
  58. public appendCustomNodes (blockScopeNode: TNodeWithBlockScope, stackTraceData: IStackTraceData[]): void {
  59. if (!this.stringArrayStorage.getLength()) {
  60. return;
  61. }
  62. // stringArrayNode append
  63. this.appendCustomNodeIfExist(CustomNode.StringArrayNode, (customNode: ICustomNode) => {
  64. NodeAppender.prependNode(blockScopeNode, customNode.getNode());
  65. });
  66. // stringArrayCallsWrapper append
  67. this.appendCustomNodeIfExist(CustomNode.StringArrayCallsWrapper, (customNode: ICustomNode) => {
  68. NodeAppender.insertNodeAtIndex(blockScopeNode, customNode.getNode(), 1);
  69. });
  70. // stringArrayRotateFunctionNode append
  71. this.appendCustomNodeIfExist(CustomNode.StringArrayRotateFunctionNode, (customNode: ICustomNode) => {
  72. NodeAppender.insertNodeAtIndex(blockScopeNode, customNode.getNode(), 1);
  73. });
  74. }
  75. public initialize (): void {
  76. this.customNodes = new Map <CustomNode, ICustomNode>();
  77. if (!this.options.stringArray) {
  78. return;
  79. }
  80. const stringArrayNode: ICustomNode = this.customNodeFactory(CustomNode.StringArrayNode);
  81. const stringArrayCallsWrapper: ICustomNode = this.customNodeFactory(CustomNode.StringArrayCallsWrapper);
  82. const stringArrayRotateFunctionNode: ICustomNode = this.customNodeFactory(CustomNode.StringArrayRotateFunctionNode);
  83. const stringArrayStorageId: string = this.stringArrayStorage.getStorageId();
  84. const [stringArrayName, stringArrayCallsWrapperName]: string[] = stringArrayStorageId.split('|');
  85. let stringArrayRotateValue: number;
  86. if (this.options.rotateStringArray) {
  87. stringArrayRotateValue = this.randomGenerator.getRandomInteger(100, 500);
  88. } else {
  89. stringArrayRotateValue = 0;
  90. }
  91. stringArrayNode.initialize(this.stringArrayStorage, stringArrayName, stringArrayRotateValue);
  92. stringArrayCallsWrapper.initialize(stringArrayName, stringArrayCallsWrapperName);
  93. stringArrayRotateFunctionNode.initialize(stringArrayName, stringArrayRotateValue);
  94. this.customNodes.set(CustomNode.StringArrayNode, stringArrayNode);
  95. this.customNodes.set(CustomNode.StringArrayCallsWrapper, stringArrayCallsWrapper);
  96. if (this.options.rotateStringArray) {
  97. this.customNodes.set(CustomNode.StringArrayRotateFunctionNode, stringArrayRotateFunctionNode);
  98. }
  99. }
  100. }