123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- import { inject, injectable, } from 'inversify';
- import { ServiceIdentifiers } from '../../../container/ServiceIdentifiers';
- import { TCustomNodeFactory } from '../../../types/container/custom-nodes/TCustomNodeFactory';
- import { TIdentifierNameGeneratorFactory } from '../../../types/container/generators/TIdentifierNameGeneratorFactory';
- import { TNodeWithBlockStatement } from '../../../types/node/TNodeWithBlockStatement';
- import { ICustomNode } from '../../../interfaces/custom-nodes/ICustomNode';
- import { IOptions } from '../../../interfaces/options/IOptions';
- import { IRandomGenerator } from '../../../interfaces/utils/IRandomGenerator';
- import { IStackTraceData } from '../../../interfaces/analyzers/stack-trace-analyzer/IStackTraceData';
- import { initializable } from '../../../decorators/Initializable';
- import { CustomNode } from '../../../enums/custom-nodes/CustomNode';
- import { ObfuscationEvent } from '../../../enums/event-emitters/ObfuscationEvent';
- import { AbstractCustomNodeGroup } from '../../AbstractCustomNodeGroup';
- import { NodeAppender } from '../../../node/NodeAppender';
- @injectable()
- export class SelfDefendingCustomNodeGroup extends AbstractCustomNodeGroup {
- /**
- * @type {ObfuscationEvent}
- */
- protected appendEvent: ObfuscationEvent = ObfuscationEvent.AfterObfuscation;
- /**
- * @type {Map<CustomNode, ICustomNode>}
- */
- @initializable()
- protected customNodes: Map <CustomNode, ICustomNode>;
- /**
- * @type {TCustomNodeFactory}
- */
- private readonly customNodeFactory: TCustomNodeFactory;
- /**
- * @param {TCustomNodeFactory} customNodeFactory
- * @param {TIdentifierNameGeneratorFactory} identifierNameGeneratorFactory
- * @param {IRandomGenerator} randomGenerator
- * @param {IOptions} options
- */
- constructor (
- @inject(ServiceIdentifiers.Factory__ICustomNode) customNodeFactory: TCustomNodeFactory,
- @inject(ServiceIdentifiers.Factory__IIdentifierNameGenerator)
- identifierNameGeneratorFactory: TIdentifierNameGeneratorFactory,
- @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
- @inject(ServiceIdentifiers.IOptions) options: IOptions
- ) {
- super(identifierNameGeneratorFactory, randomGenerator, options);
- this.customNodeFactory = customNodeFactory;
- }
- /**
- * @param {TNodeWithBlockStatement} blockScopeNode
- * @param {IStackTraceData[]} stackTraceData
- */
- public appendCustomNodes (blockScopeNode: TNodeWithBlockStatement, stackTraceData: IStackTraceData[]): void {
- const randomStackTraceIndex: number = this.getRandomStackTraceIndex(stackTraceData.length);
- // selfDefendingUnicodeNode append
- this.appendCustomNodeIfExist(CustomNode.SelfDefendingUnicodeNode, (customNode: ICustomNode) => {
- NodeAppender.appendNodeToOptimalBlockScope(
- stackTraceData,
- blockScopeNode,
- customNode.getNode(),
- randomStackTraceIndex
- );
- });
- // nodeCallsControllerFunctionNode append
- this.appendCustomNodeIfExist(CustomNode.NodeCallsControllerFunctionNode, (customNode: ICustomNode) => {
- let targetBlockScope: TNodeWithBlockStatement;
- if (stackTraceData.length) {
- targetBlockScope = NodeAppender.getOptimalBlockScope(stackTraceData, randomStackTraceIndex, 1);
- } else {
- targetBlockScope = blockScopeNode;
- }
- NodeAppender.prependNode(targetBlockScope, customNode.getNode());
- });
- }
- public initialize (): void {
- this.customNodes = new Map <CustomNode, ICustomNode>();
- if (!this.options.selfDefending) {
- return;
- }
- const callsControllerFunctionName: string = this.identifierNameGenerator.generate(6);
- const selfDefendingUnicodeNode: ICustomNode = this.customNodeFactory(CustomNode.SelfDefendingUnicodeNode);
- const nodeCallsControllerFunctionNode: ICustomNode = this.customNodeFactory(CustomNode.NodeCallsControllerFunctionNode);
- selfDefendingUnicodeNode.initialize(callsControllerFunctionName);
- nodeCallsControllerFunctionNode.initialize(this.appendEvent, callsControllerFunctionName);
- this.customNodes.set(CustomNode.SelfDefendingUnicodeNode, selfDefendingUnicodeNode);
- this.customNodes.set(CustomNode.NodeCallsControllerFunctionNode, nodeCallsControllerFunctionNode);
- }
- }
|