All files / src Obfuscator.js

96.3% Statements 52/54
86.67% Branches 13/15
100% Functions 6/6
96.3% Lines 52/54
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101  1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   5x 5x 5x                             5x     5x 5x 5x 5x 5x 5x     1x   3x 3x 3x 5x       5x 6x 5x         5x 6x 1x         262x 262x 184x   78x 80x     5x 5x   262x         5x     5x 1x   5x     5x 3x       1x  
"use strict";
const estraverse = require('estraverse');
const AppendState_1 = require('./enums/AppendState');
const NodeType_1 = require('./enums/NodeType');
const CatchClauseObfuscator_1 = require('./node-obfuscators/CatchClauseObfuscator');
const ConsoleOutputDisableExpressionNode_1 = require('./custom-nodes/console-output-nodes/ConsoleOutputDisableExpressionNode');
const DebugProtectionNodesGroup_1 = require('./node-groups/DebugProtectionNodesGroup');
const FunctionDeclarationObfuscator_1 = require('./node-obfuscators/FunctionDeclarationObfuscator');
const FunctionObfuscator_1 = require('./node-obfuscators/FunctionObfuscator');
const LiteralObfuscator_1 = require('./node-obfuscators/LiteralObfuscator');
const MemberExpressionObfuscator_1 = require('./node-obfuscators/MemberExpressionObfuscator');
const MethodDefinitionObfuscator_1 = require('./node-obfuscators/MethodDefinitionObfuscator');
const NodeUtils_1 = require("./NodeUtils");
const ObjectExpressionObfuscator_1 = require('./node-obfuscators/ObjectExpressionObfuscator');
const SelfDefendingNodesGroup_1 = require("./node-groups/SelfDefendingNodesGroup");
const UnicodeArrayNodesGroup_1 = require('./node-groups/UnicodeArrayNodesGroup');
const VariableDeclarationObfuscator_1 = require('./node-obfuscators/VariableDeclarationObfuscator');
class Obfuscator {
    constructor(options) {
        this.nodes = new Map();
        this.nodeObfuscators = new Map([
            [NodeType_1.NodeType.ArrowFunctionExpression, [FunctionObfuscator_1.FunctionObfuscator]],
            [NodeType_1.NodeType.ClassDeclaration, [FunctionDeclarationObfuscator_1.FunctionDeclarationObfuscator]],
            [NodeType_1.NodeType.CatchClause, [CatchClauseObfuscator_1.CatchClauseObfuscator]],
            [NodeType_1.NodeType.FunctionDeclaration, [
                    FunctionDeclarationObfuscator_1.FunctionDeclarationObfuscator,
                    FunctionObfuscator_1.FunctionObfuscator
                ]],
            [NodeType_1.NodeType.FunctionExpression, [FunctionObfuscator_1.FunctionObfuscator]],
            [NodeType_1.NodeType.MemberExpression, [MemberExpressionObfuscator_1.MemberExpressionObfuscator]],
            [NodeType_1.NodeType.MethodDefinition, [MethodDefinitionObfuscator_1.MethodDefinitionObfuscator]],
            [NodeType_1.NodeType.ObjectExpression, [ObjectExpressionObfuscator_1.ObjectExpressionObfuscator]],
            [NodeType_1.NodeType.VariableDeclaration, [VariableDeclarationObfuscator_1.VariableDeclarationObfuscator]],
            [NodeType_1.NodeType.Literal, [LiteralObfuscator_1.LiteralObfuscator]]
        ]);
        this.options = options;
    }
    obfuscateNode(node) {
        this.setNewNodes();
        NodeUtils_1.NodeUtils.parentize(node);
        this.beforeObfuscation(node);
        this.obfuscate(node);
        this.afterObfuscation(node);
        return node;
    }
    setNode(nodeName, node) {
        this.nodes.set(nodeName, node);
    }
    setNodesGroup(nodesGroup) {
        let nodes = nodesGroup.getNodes();
        nodes.forEach((node, key) => {
            this.nodes.set(key, node);
        });
    }
    afterObfuscation(astTree) {
        this.nodes.forEach((node) => {
            if (node.getAppendState() === AppendState_1.AppendState.AfterObfuscation) {
                node.appendNode(astTree);
            }
        });
    }
    beforeObfuscation(astTree) {
        this.nodes.forEach((node) => {
            if (node.getAppendState() === AppendState_1.AppendState.BeforeObfuscation) {
                node.appendNode(astTree);
            }
        });
    }
    ;
    initializeNodeObfuscators(node, parentNode) {
        if (!this.nodeObfuscators.has(node.type)) {
            return;
        }
        this.nodeObfuscators.get(node.type).forEach((obfuscator) => {
            new obfuscator(this.nodes, this.options).obfuscateNode(node, parentNode);
        });
    }
    obfuscate(node) {
        estraverse.replace(node, {
            leave: (node, parentNode) => {
                this.initializeNodeObfuscators(node, parentNode);
            }
        });
    }
    setNewNodes() {
        if (this.options.get('selfDefending')) I{
            this.setNodesGroup(new SelfDefendingNodesGroup_1.SelfDefendingNodesGroup(this.options));
        }
        if (this.options.get('disableConsoleOutput')) {
            this.setNode('consoleOutputDisableExpressionNode', new ConsoleOutputDisableExpressionNode_1.ConsoleOutputDisableExpressionNode(this.options));
        }
        if (this.options.get('debugProtection')) I{
            this.setNodesGroup(new DebugProtectionNodesGroup_1.DebugProtectionNodesGroup(this.options));
        }
        if (this.options.get('unicodeArray')) {
            this.setNodesGroup(new UnicodeArrayNodesGroup_1.UnicodeArrayNodesGroup(this.options));
        }
    }
}
exports.Obfuscator = Obfuscator;
//# sourceMappingURL=Obfuscator.js.map