123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- import { injectable, inject } from 'inversify';
- import { ServiceIdentifiers } from '../../../container/ServiceIdentifiers';
- import { TCustomNodeFactory } from '../../../types/container/custom-nodes/TCustomNodeFactory';
- import { TNodeWithBlockStatement } from '../../../types/node/TNodeWithBlockStatement';
- import { TObfuscationEvent } from '../../../types/event-emitters/TObfuscationEvent';
- import { ICustomNode } from '../../../interfaces/custom-nodes/ICustomNode';
- import { IObfuscationEventEmitter } from '../../../interfaces/event-emitters/IObfuscationEventEmitter';
- import { IOptions } from '../../../interfaces/options/IOptions';
- import { IStackTraceData } from '../../../interfaces/stack-trace-analyzer/IStackTraceData';
- import { IStorage } from '../../../interfaces/storages/IStorage';
- import { initializable } from '../../../decorators/Initializable';
- import { CustomNode } from '../../../enums/container/custom-nodes/CustomNode';
- import { ObfuscationEvent } from '../../../enums/event-emitters/ObfuscationEvent';
- import { AbstractCustomNodeGroup } from '../../AbstractCustomNodeGroup';
- import { NodeAppender } from '../../../node/NodeAppender';
- import { RandomGeneratorUtils } from '../../../utils/RandomGeneratorUtils';
- import { Utils } from '../../../utils/Utils';
- @injectable()
- export class StringArrayCustomNodeGroup extends AbstractCustomNodeGroup {
- /**
- * @type {TObfuscationEvent}
- */
- protected appendEvent: TObfuscationEvent = ObfuscationEvent.AfterObfuscation;
- /**
- * @type {Map<CustomNode, ICustomNode>}
- */
- @initializable()
- protected customNodes: Map <CustomNode, ICustomNode>;
- /**
- * @type {TCustomNodeFactory}
- */
- private readonly customNodeFactory: TCustomNodeFactory;
- /**
- * @type {IObfuscationEventEmitter}
- */
- private readonly obfuscationEventEmitter: IObfuscationEventEmitter;
- /**
- * @type {IStorage <string>}
- */
- @initializable()
- private stringArrayStorage: IStorage <string>;
- /**
- * @param customNodeFactory
- * @param obfuscationEventEmitter
- * @param stringArrayStorage
- * @param options
- */
- constructor (
- @inject(ServiceIdentifiers.Factory__ICustomNode) customNodeFactory: TCustomNodeFactory,
- @inject(ServiceIdentifiers.IObfuscationEventEmitter) obfuscationEventEmitter: IObfuscationEventEmitter,
- @inject(ServiceIdentifiers.TStringArrayStorage) stringArrayStorage: IStorage<string>,
- @inject(ServiceIdentifiers.IOptions) options: IOptions
- ) {
- super(options);
- this.customNodeFactory = customNodeFactory;
- this.obfuscationEventEmitter = obfuscationEventEmitter;
- this.stringArrayStorage = stringArrayStorage;
- }
- /**
- * @param blockScopeNode
- * @param stackTraceData
- */
- public appendCustomNodes (blockScopeNode: TNodeWithBlockStatement, stackTraceData: IStackTraceData[]): void {
- if (!this.stringArrayStorage.getLength()) {
- return;
- }
- // stringArrayNode append
- this.appendCustomNodeIfExist(CustomNode.StringArrayNode, (customNode: ICustomNode) => {
- NodeAppender.prependNode(blockScopeNode, customNode.getNode());
- });
- // stringArrayCallsWrapper append
- this.appendCustomNodeIfExist(CustomNode.StringArrayCallsWrapper, (customNode: ICustomNode) => {
- NodeAppender.insertNodeAtIndex(blockScopeNode, customNode.getNode(), 1);
- });
- // stringArrayRotateFunctionNode append
- this.appendCustomNodeIfExist(CustomNode.StringArrayRotateFunctionNode, (customNode: ICustomNode) => {
- NodeAppender.insertNodeAtIndex(blockScopeNode, customNode.getNode(), 1);
- });
- }
- public initialize (): void {
- this.customNodes = new Map <CustomNode, ICustomNode> ();
- if (!this.options.stringArray) {
- return;
- }
- const stringArrayNode: ICustomNode = this.customNodeFactory(CustomNode.StringArrayNode);
- const stringArrayCallsWrapper: ICustomNode = this.customNodeFactory(CustomNode.StringArrayCallsWrapper);
- const stringArrayRotateFunctionNode: ICustomNode = this.customNodeFactory(CustomNode.StringArrayRotateFunctionNode);
- const stringArrayStorageId: string = this.stringArrayStorage.getStorageId();
- const stringArrayName: string = `_${Utils.hexadecimalPrefix}${stringArrayStorageId}`;
- const stringArrayCallsWrapperName: string = `_${Utils.hexadecimalPrefix}${Utils.stringRotate(stringArrayStorageId, 1)}`;
- let stringArrayRotateValue: number;
- if (this.options.rotateStringArray) {
- stringArrayRotateValue = RandomGeneratorUtils.getRandomInteger(100, 500);
- } else {
- stringArrayRotateValue = 0;
- }
- stringArrayNode.initialize(this.stringArrayStorage, stringArrayName, stringArrayRotateValue);
- stringArrayCallsWrapper.initialize(this.stringArrayStorage, stringArrayName, stringArrayCallsWrapperName);
- stringArrayRotateFunctionNode.initialize(this.stringArrayStorage, stringArrayName, stringArrayRotateValue);
- this.customNodes.set(CustomNode.StringArrayNode, stringArrayNode);
- this.customNodes.set(CustomNode.StringArrayCallsWrapper, stringArrayCallsWrapper);
- if (this.options.rotateStringArray) {
- this.customNodes.set(CustomNode.StringArrayRotateFunctionNode, stringArrayRotateFunctionNode);
- }
- }
- }
|