瀏覽代碼

Refactoring of CustomNode methods

sanex3339 5 年之前
父節點
當前提交
77122b7374
共有 23 個文件被更改,包括 78 次插入49 次删除
  1. 0 0
      dist/index.browser.js
  2. 0 0
      dist/index.cli.js
  3. 0 0
      dist/index.js
  4. 13 2
      src/custom-nodes/AbstractCustomNode.ts
  5. 9 9
      src/custom-nodes/CustomNodeFormatter.ts
  6. 4 3
      src/custom-nodes/console-output-nodes/ConsoleOutputDisableExpressionNode.ts
  7. 2 1
      src/custom-nodes/control-flow-flattening-nodes/BinaryExpressionFunctionNode.ts
  8. 2 1
      src/custom-nodes/control-flow-flattening-nodes/BlockStatementControlFlowFlatteningNode.ts
  9. 2 1
      src/custom-nodes/control-flow-flattening-nodes/CallExpressionFunctionNode.ts
  10. 2 1
      src/custom-nodes/control-flow-flattening-nodes/LogicalExpressionFunctionNode.ts
  11. 2 1
      src/custom-nodes/control-flow-flattening-nodes/StringLiteralNode.ts
  12. 2 1
      src/custom-nodes/control-flow-flattening-nodes/control-flow-storage-nodes/ControlFlowStorageNode.ts
  13. 2 1
      src/custom-nodes/dead-code-injection-nodes/BlockStatementDeadCodeInjectionNode.ts
  14. 4 3
      src/custom-nodes/debug-protection-nodes/DebugProtectionFunctionCallNode.ts
  15. 4 3
      src/custom-nodes/debug-protection-nodes/DebugProtectionFunctionIntervalNode.ts
  16. 4 3
      src/custom-nodes/debug-protection-nodes/DebugProtectionFunctionNode.ts
  17. 4 3
      src/custom-nodes/domain-lock-nodes/DomainLockNode.ts
  18. 4 3
      src/custom-nodes/node-calls-controller-nodes/NodeCallsControllerFunctionNode.ts
  19. 2 1
      src/custom-nodes/object-expression-keys-transformer-nodes/BasePropertiesExtractorObjectExpressionHostNode.ts
  20. 4 3
      src/custom-nodes/self-defending-nodes/SelfDefendingUnicodeNode.ts
  21. 4 3
      src/custom-nodes/string-array-nodes/StringArrayCallsWrapper.ts
  22. 4 3
      src/custom-nodes/string-array-nodes/StringArrayNode.ts
  23. 4 3
      src/custom-nodes/string-array-nodes/StringArrayRotateFunctionNode.ts

File diff suppressed because it is too large
+ 0 - 0
dist/index.browser.js


File diff suppressed because it is too large
+ 0 - 0
dist/index.cli.js


File diff suppressed because it is too large
+ 0 - 0
dist/index.js


+ 13 - 2
src/custom-nodes/AbstractCustomNode.ts

@@ -79,7 +79,11 @@ export abstract class AbstractCustomNode <
      */
     public getNode (): TStatement[] {
         if (!this.cachedNode) {
-            this.cachedNode = this.customNodeFormatter.formatStructure(this.getNodeStructure());
+            const nodeTemplate: string = this.getNodeTemplate();
+
+            this.cachedNode = this.customNodeFormatter.formatStructure(
+                this.getNodeStructure(nodeTemplate)
+            );
         }
 
         return this.cachedNode;
@@ -97,5 +101,12 @@ export abstract class AbstractCustomNode <
     /**
      * @returns {TStatement[]}
      */
-    protected abstract getNodeStructure (): TStatement[];
+    protected abstract getNodeStructure (nodeTemplate: string): TStatement[];
+
+    /**
+     * @returns {string}
+     */
+    protected getNodeTemplate (): string {
+        return '';
+    }
 }

+ 9 - 9
src/custom-nodes/CustomNodeFormatter.ts

@@ -10,20 +10,21 @@ import { TStatement } from '../types/node/TStatement';
 
 import { ICustomNodeFormatter } from '../interfaces/custom-nodes/ICustomNodeFormatter';
 import { IPrevailingKindOfVariablesAnalyzer } from '../interfaces/analyzers/calls-graph-analyzer/IPrevailingKindOfVariablesAnalyzer';
+
 import { NodeGuards } from '../node/NodeGuards';
 
 @injectable()
 export class CustomNodeFormatter implements ICustomNodeFormatter {
     /**
-     * @type {IPrevailingKindOfVariablesAnalyzer}
+     * @type {ESTree.VariableDeclaration['kind']}
      */
-    private readonly prevailingKindOfVariablesAnalyzer: IPrevailingKindOfVariablesAnalyzer;
+    private readonly prevailingKindOfVariables: ESTree.VariableDeclaration['kind'];
 
     constructor (
         @inject(ServiceIdentifiers.IPrevailingKindOfVariablesAnalyzer)
             prevailingKindOfVariablesAnalyzer: IPrevailingKindOfVariablesAnalyzer
     ) {
-        this.prevailingKindOfVariablesAnalyzer = prevailingKindOfVariablesAnalyzer;
+        this.prevailingKindOfVariables = prevailingKindOfVariablesAnalyzer.getPrevailingKind();
     }
 
     /**
@@ -43,19 +44,18 @@ export class CustomNodeFormatter implements ICustomNodeFormatter {
      * @returns {TStatement[]}
      */
     public formatStructure (statements: TStatement[]): TStatement[] {
-        const prevailingKindOfVariables: ESTree.VariableDeclaration['kind'] =
-            this.prevailingKindOfVariablesAnalyzer.getPrevailingKind();
-
         for (const statement of statements) {
-            estraverse.traverse(statement, {
-                enter: (node: ESTree.Node): estraverse.VisitorOption | void => {
+            estraverse.replace(statement, {
+                enter: (node: ESTree.Node): ESTree.Node | void => {
                     if (!NodeGuards.isVariableDeclarationNode(node)) {
                         return;
                     }
 
-                    if (prevailingKindOfVariables === 'var') {
+                    if (this.prevailingKindOfVariables === 'var') {
                         node.kind = 'var';
                     }
+
+                    return node;
                 }
             });
         }

+ 4 - 3
src/custom-nodes/console-output-nodes/ConsoleOutputDisableExpressionNode.ts

@@ -50,16 +50,17 @@ export class ConsoleOutputDisableExpressionNode extends AbstractCustomNode {
     }
 
     /**
+     * @param {string} nodeTemplate
      * @returns {TStatement[]}
      */
-    protected getNodeStructure (): TStatement[] {
-        return NodeUtils.convertCodeToStructure(this.getTemplate());
+    protected getNodeStructure (nodeTemplate: string): TStatement[] {
+        return NodeUtils.convertCodeToStructure(nodeTemplate);
     }
 
     /**
      * @returns {string}
      */
-    protected getTemplate (): string {
+    protected getNodeTemplate (): string {
         const globalVariableTemplate: string = this.options.target !== ObfuscationTarget.BrowserNoEval
             ? this.getGlobalVariableTemplate()
             : GlobalVariableNoEvalTemplate();

+ 2 - 1
src/custom-nodes/control-flow-flattening-nodes/BinaryExpressionFunctionNode.ts

@@ -48,9 +48,10 @@ export class BinaryExpressionFunctionNode extends AbstractCustomNode {
     }
 
     /**
+     * @param {string} nodeTemplate
      * @returns {TStatement[]}
      */
-    protected getNodeStructure (): TStatement[] {
+    protected getNodeStructure (nodeTemplate: string): TStatement[] {
         const structure: TStatement = NodeFactory.expressionStatementNode(
             NodeFactory.functionExpressionNode(
                 [

+ 2 - 1
src/custom-nodes/control-flow-flattening-nodes/BlockStatementControlFlowFlatteningNode.ts

@@ -69,9 +69,10 @@ export class BlockStatementControlFlowFlatteningNode extends AbstractCustomNode
     }
 
     /**
+     * @param {string} nodeTemplate
      * @returns {TStatement[]}
      */
-    protected getNodeStructure (): TStatement[] {
+    protected getNodeStructure (nodeTemplate: string): TStatement[] {
         const controllerIdentifierName: string = this.randomGenerator.getRandomString(6);
         const indexIdentifierName: string = this.randomGenerator.getRandomString(6);
         const structure: ESTree.BlockStatement = NodeFactory.blockStatementNode([

+ 2 - 1
src/custom-nodes/control-flow-flattening-nodes/CallExpressionFunctionNode.ts

@@ -48,9 +48,10 @@ export class CallExpressionFunctionNode extends AbstractCustomNode {
     }
 
     /**
+     * @param {string} nodeTemplate
      * @returns {TStatement[]}
      */
-    protected getNodeStructure (): TStatement[] {
+    protected getNodeStructure (nodeTemplate: string): TStatement[] {
         const calleeIdentifier: ESTree.Identifier = NodeFactory.identifierNode('callee');
         const params: ESTree.Identifier[] = [];
         const argumentsLength: number = this.expressionArguments.length;

+ 2 - 1
src/custom-nodes/control-flow-flattening-nodes/LogicalExpressionFunctionNode.ts

@@ -48,9 +48,10 @@ export class LogicalExpressionFunctionNode extends AbstractCustomNode {
     }
 
     /**
+     * @param {string} nodeTemplate
      * @returns {TStatement[]}
      */
-    protected getNodeStructure (): TStatement[] {
+    protected getNodeStructure (nodeTemplate: string): TStatement[] {
         const structure: TStatement = NodeFactory.expressionStatementNode(
             NodeFactory.functionExpressionNode(
                 [

+ 2 - 1
src/custom-nodes/control-flow-flattening-nodes/StringLiteralNode.ts

@@ -45,9 +45,10 @@ export class StringLiteralNode extends AbstractCustomNode {
     }
 
     /**
+     * @param {string} nodeTemplate
      * @returns {TStatement[]}
      */
-    protected getNodeStructure (): TStatement[] {
+    protected getNodeStructure (nodeTemplate: string): TStatement[] {
         const structure: TStatement = NodeFactory.expressionStatementNode(
             NodeFactory.literalNode(this.literalValue)
         );

+ 2 - 1
src/custom-nodes/control-flow-flattening-nodes/control-flow-storage-nodes/ControlFlowStorageNode.ts

@@ -51,9 +51,10 @@ export class ControlFlowStorageNode extends AbstractCustomNode {
     }
 
     /**
+     * @param {string} nodeTemplate
      * @returns {TStatement[]}
      */
-    protected getNodeStructure (): TStatement[] {
+    protected getNodeStructure (nodeTemplate: string): TStatement[] {
         const propertyNodes: ESTree.Property[] = Array
             .from<[string, ICustomNode]>(this.controlFlowStorage.getStorage())
             .map(([key, value]: [string, ICustomNode]) => {

+ 2 - 1
src/custom-nodes/dead-code-injection-nodes/BlockStatementDeadCodeInjectionNode.ts

@@ -59,9 +59,10 @@ export class BlockStatementDeadCodeInjectionNode extends AbstractCustomNode {
     }
 
     /**
+     * @param {string} nodeTemplate
      * @returns {TStatement[]}
      */
-    protected getNodeStructure (): TStatement[] {
+    protected getNodeStructure (nodeTemplate: string): TStatement[] {
         const random1: boolean = this.randomGenerator.getMathRandom() > 0.5;
         const random2: boolean = this.randomGenerator.getMathRandom() > 0.5;
 

+ 4 - 3
src/custom-nodes/debug-protection-nodes/DebugProtectionFunctionCallNode.ts

@@ -55,16 +55,17 @@ export class DebugProtectionFunctionCallNode extends AbstractCustomNode {
     }
 
     /**
+     * @param {string} nodeTemplate
      * @returns {TStatement[]}
      */
-    protected getNodeStructure (): TStatement[] {
-        return NodeUtils.convertCodeToStructure(this.getTemplate());
+    protected getNodeStructure (nodeTemplate: string): TStatement[] {
+        return NodeUtils.convertCodeToStructure(nodeTemplate);
     }
 
     /**
      * @returns {string}
      */
-    protected getTemplate (): string {
+    protected getNodeTemplate (): string {
         return this.customNodeFormatter.formatTemplate(DebugProtectionFunctionCallTemplate(), {
             debugProtectionFunctionName: this.debugProtectionFunctionName,
             singleNodeCallControllerFunctionName: this.callsControllerFunctionName

+ 4 - 3
src/custom-nodes/debug-protection-nodes/DebugProtectionFunctionIntervalNode.ts

@@ -47,16 +47,17 @@ export class DebugProtectionFunctionIntervalNode extends AbstractCustomNode {
     }
 
     /**
+     * @param {string} nodeTemplate
      * @returns {TStatement[]}
      */
-    protected getNodeStructure (): TStatement[] {
-        return NodeUtils.convertCodeToStructure(this.getTemplate());
+    protected getNodeStructure (nodeTemplate: string): TStatement[] {
+        return NodeUtils.convertCodeToStructure(nodeTemplate);
     }
 
     /**
      * @returns {string}
      */
-    protected getTemplate (): string {
+    protected getNodeTemplate (): string {
         return this.customNodeFormatter.formatTemplate(DebugProtectionFunctionIntervalTemplate(), {
             debugProtectionFunctionName: this.debugProtectionFunctionName
         });

+ 4 - 3
src/custom-nodes/debug-protection-nodes/DebugProtectionFunctionNode.ts

@@ -51,16 +51,17 @@ export class DebugProtectionFunctionNode extends AbstractCustomNode {
     }
 
     /**
+     * @param {string} nodeTemplate
      * @returns {TStatement[]}
      */
-    protected getNodeStructure (): TStatement[] {
-        return NodeUtils.convertCodeToStructure(this.getTemplate());
+    protected getNodeStructure (nodeTemplate: string): TStatement[] {
+        return NodeUtils.convertCodeToStructure(nodeTemplate);
     }
 
     /**
      * @returns {string}
      */
-    protected getTemplate (): string {
+    protected getNodeTemplate (): string {
         const debuggerTemplate: string = this.options.target !== ObfuscationTarget.BrowserNoEval
             ? DebuggerTemplate()
             : DebuggerTemplateNoEval();

+ 4 - 3
src/custom-nodes/domain-lock-nodes/DomainLockNode.ts

@@ -60,16 +60,17 @@ export class DomainLockNode extends AbstractCustomNode {
     }
 
     /**
+     * @param {string} nodeTemplate
      * @returns {TStatement[]}
      */
-    protected getNodeStructure (): TStatement[] {
-        return NodeUtils.convertCodeToStructure(this.getTemplate());
+    protected getNodeStructure (nodeTemplate: string): TStatement[] {
+        return NodeUtils.convertCodeToStructure(nodeTemplate);
     }
 
     /**
      * @returns {string}
      */
-    protected getTemplate (): string {
+    protected getNodeTemplate (): string {
         const domainsString: string = this.options.domainLock.join(';');
         const [hiddenDomainsString, diff]: string[] = this.cryptUtils.hideString(
             domainsString,

+ 4 - 3
src/custom-nodes/node-calls-controller-nodes/NodeCallsControllerFunctionNode.ts

@@ -60,16 +60,17 @@ export class NodeCallsControllerFunctionNode extends AbstractCustomNode {
     }
 
     /**
+     * @param {string} nodeTemplate
      * @returns {TStatement[]}
      */
-    protected getNodeStructure (): TStatement[] {
-        return NodeUtils.convertCodeToStructure(this.getTemplate());
+    protected getNodeStructure (nodeTemplate: string): TStatement[] {
+        return NodeUtils.convertCodeToStructure(nodeTemplate);
     }
 
     /**
      * @returns {string}
      */
-    protected getTemplate (): string {
+    protected getNodeTemplate (): string {
         if (this.appendEvent === ObfuscationEvent.AfterObfuscation) {
             return JavaScriptObfuscator.obfuscate(
                 this.customNodeFormatter.formatTemplate(SingleNodeCallControllerTemplate(), {

+ 2 - 1
src/custom-nodes/object-expression-keys-transformer-nodes/BasePropertiesExtractorObjectExpressionHostNode.ts

@@ -32,9 +32,10 @@ export class BasePropertiesExtractorObjectExpressionHostNode extends AbstractCus
     public initialize (): void {}
 
     /**
+     * @param {string} nodeTemplate
      * @returns {TStatement[]}
      */
-    protected getNodeStructure (): TStatement[] {
+    protected getNodeStructure (nodeTemplate: string): TStatement[] {
         const structure: TStatement = NodeFactory.variableDeclarationNode(
             [
                 NodeFactory.variableDeclaratorNode(

+ 4 - 3
src/custom-nodes/self-defending-nodes/SelfDefendingUnicodeNode.ts

@@ -60,16 +60,17 @@ export class SelfDefendingUnicodeNode extends AbstractCustomNode {
     }
 
     /**
+     * @param {string} nodeTemplate
      * @returns {TStatement[]}
      */
-    protected getNodeStructure (): TStatement[] {
-        return NodeUtils.convertCodeToStructure(this.getTemplate());
+    protected getNodeStructure (nodeTemplate: string): TStatement[] {
+        return NodeUtils.convertCodeToStructure(nodeTemplate);
     }
 
     /**
      * @returns {string}
      */
-    protected getTemplate (): string {
+    protected getNodeTemplate (): string {
         return JavaScriptObfuscator.obfuscate(
             this.customNodeFormatter.formatTemplate(SelfDefendingTemplate(this.escapeSequenceEncoder), {
                 selfDefendingFunctionName: this.identifierNamesGenerator.generate(),

+ 4 - 3
src/custom-nodes/string-array-nodes/StringArrayCallsWrapper.ts

@@ -80,16 +80,17 @@ export class StringArrayCallsWrapper extends AbstractCustomNode {
     }
 
     /**
+     * @param {string} nodeTemplate
      * @returns {TStatement[]}
      */
-    protected getNodeStructure (): TStatement[] {
-        return NodeUtils.convertCodeToStructure(this.getTemplate());
+    protected getNodeStructure (nodeTemplate: string): TStatement[] {
+        return NodeUtils.convertCodeToStructure(nodeTemplate);
     }
 
     /**
      * @returns {string}
      */
-    protected getTemplate (): string {
+    protected getNodeTemplate (): string {
         const decodeNodeTemplate: string = this.getDecodeStringArrayTemplate();
 
         return JavaScriptObfuscator.obfuscate(

+ 4 - 3
src/custom-nodes/string-array-nodes/StringArrayNode.ts

@@ -78,16 +78,17 @@ export class StringArrayNode extends AbstractCustomNode {
     }
 
     /**
+     * @param {string} nodeTemplate
      * @returns {TStatement[]}
      */
-    protected getNodeStructure (): TStatement[] {
-        return NodeUtils.convertCodeToStructure(this.getTemplate());
+    protected getNodeStructure (nodeTemplate: string): TStatement[] {
+        return NodeUtils.convertCodeToStructure(nodeTemplate);
     }
 
     /**
      * @returns {string}
      */
-    protected getTemplate (): string {
+    protected getNodeTemplate (): string {
         return this.customNodeFormatter.formatTemplate(StringArrayTemplate(), {
             stringArrayName: this.stringArrayName,
             stringArray: this.stringArrayStorage.toString()

+ 4 - 3
src/custom-nodes/string-array-nodes/StringArrayRotateFunctionNode.ts

@@ -73,16 +73,17 @@ export class StringArrayRotateFunctionNode extends AbstractCustomNode {
     }
 
     /**
+     * @param {string} nodeTemplate
      * @returns {TStatement[]}
      */
-    protected getNodeStructure (): TStatement[] {
-        return NodeUtils.convertCodeToStructure(this.getTemplate());
+    protected getNodeStructure (nodeTemplate: string): TStatement[] {
+        return NodeUtils.convertCodeToStructure(nodeTemplate);
     }
 
     /**
      * @returns {string}
      */
-    protected getTemplate (): string {
+    protected getNodeTemplate (): string {
         const timesName: string = this.identifierNamesGenerator.generate();
         const whileFunctionName: string = this.identifierNamesGenerator.generate();
 

Some files were not shown because too many files changed in this diff