From d8964da8d4516ef14dd19ffcc773ecaa47bdb544 Mon Sep 17 00:00:00 2001 From: sanex3339 Date: Fri, 1 Jul 2016 13:14:08 +0300 Subject: [PATCH] Refactoring --- dist/index.js | 91 ++++++++++++-------- src/Obfuscator.ts | 57 ++---------- src/node-groups/ConsoleOutputNodesGroup.ts | 22 +++++ src/node-groups/DebugProtectionNodesGroup.ts | 4 + src/node-groups/SelfDefendingNodesGroup.ts | 4 + src/node-groups/UnicodeArrayNodesGroup.ts | 4 + 6 files changed, 96 insertions(+), 86 deletions(-) create mode 100644 src/node-groups/ConsoleOutputNodesGroup.ts diff --git a/dist/index.js b/dist/index.js index 5eafd18d..c3c777e6 100644 --- a/dist/index.js +++ b/dist/index.js @@ -842,13 +842,15 @@ module.exports = JavaScriptObfuscator_1.JavaScriptObfuscator; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); +function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } + function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var estraverse = __webpack_require__(3); var AppendState_1 = __webpack_require__(6); var NodeType_1 = __webpack_require__(2); var CatchClauseObfuscator_1 = __webpack_require__(33); -var ConsoleOutputDisableExpressionNode_1 = __webpack_require__(21); +var ConsoleOutputNodesGroup_1 = __webpack_require__(47); var DebugProtectionNodesGroup_1 = __webpack_require__(30); var FunctionDeclarationObfuscator_1 = __webpack_require__(34); var FunctionObfuscator_1 = __webpack_require__(35); @@ -865,36 +867,20 @@ var Obfuscator = function () { function Obfuscator(options) { _classCallCheck(this, Obfuscator); - 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; + this.nodes = new Map([].concat(_toConsumableArray(new SelfDefendingNodesGroup_1.SelfDefendingNodesGroup(this.options).getNodes()), _toConsumableArray(new ConsoleOutputNodesGroup_1.ConsoleOutputNodesGroup(this.options).getNodes()), _toConsumableArray(new DebugProtectionNodesGroup_1.DebugProtectionNodesGroup(this.options).getNodes()), _toConsumableArray(new UnicodeArrayNodesGroup_1.UnicodeArrayNodesGroup(this.options).getNodes()))); } _createClass(Obfuscator, [{ key: 'obfuscateNode', value: function obfuscateNode(node) { - this.setNewNodes(); NodeUtils_1.NodeUtils.parentize(node); this.beforeObfuscation(node); this.obfuscate(node); this.afterObfuscation(node); return node; } - }, { - key: 'setNode', - value: function setNode(nodeName, node) { - this.nodes.set(nodeName, node); - } - }, { - key: 'setNodesGroup', - value: function setNodesGroup(nodesGroup) { - var _this = this; - - var nodes = nodesGroup.getNodes(); - nodes.forEach(function (node, key) { - _this.nodes.set(key, node); - }); - } }, { key: 'afterObfuscation', value: function afterObfuscation(astTree) { @@ -916,42 +902,26 @@ var Obfuscator = function () { }, { key: 'initializeNodeObfuscators', value: function initializeNodeObfuscators(node, parentNode) { - var _this2 = this; + var _this = this; if (!this.nodeObfuscators.has(node.type)) { return; } this.nodeObfuscators.get(node.type).forEach(function (obfuscator) { - new obfuscator(_this2.nodes, _this2.options).obfuscateNode(node, parentNode); + new obfuscator(_this.nodes, _this.options).obfuscateNode(node, parentNode); }); } }, { key: 'obfuscate', value: function obfuscate(node) { - var _this3 = this; + var _this2 = this; estraverse.replace(node, { leave: function leave(node, parentNode) { - _this3.initializeNodeObfuscators(node, parentNode); + _this2.initializeNodeObfuscators(node, parentNode); } }); } - }, { - key: 'setNewNodes', - value: function setNewNodes() { - if (this.options.get('selfDefending')) { - 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')) { - this.setNodesGroup(new DebugProtectionNodesGroup_1.DebugProtectionNodesGroup(this.options)); - } - if (this.options.get('unicodeArray')) { - this.setNodesGroup(new UnicodeArrayNodesGroup_1.UnicodeArrayNodesGroup(this.options)); - } - } }]); return Obfuscator; @@ -1884,6 +1854,9 @@ var DebugProtectionNodesGroup = function (_NodesGroup_1$NodesGr) { var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(DebugProtectionNodesGroup).call(this, options)); _this.debugProtectionFunctionIdentifier = Utils_1.Utils.getRandomVariableName(); + if (!_this.options.get('debugProtection')) { + return _possibleConstructorReturn(_this); + } _this.nodes.set('debugProtectionFunctionNode', new DebugProtectionFunctionNode_1.DebugProtectionFunctionNode(_this.debugProtectionFunctionIdentifier, _this.options)); _this.nodes.set('debugProtectionFunctionCallNode', new DebugProtectionFunctionCallNode_1.DebugProtectionFunctionCallNode(_this.debugProtectionFunctionIdentifier, _this.options)); if (_this.options.get('debugProtectionInterval')) { @@ -1921,6 +1894,9 @@ var SelfDefendingNodesGroup = function (_NodesGroup_1$NodesGr) { var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(SelfDefendingNodesGroup).call(this, options)); + if (!_this.options.get('selfDefending')) { + return _possibleConstructorReturn(_this); + } _this.nodes.set('selfDefendingUnicodeNode', new SelfDefendingUnicodeNode_1.SelfDefendingUnicodeNode(_this.options)); return _this; } @@ -1960,6 +1936,9 @@ var UnicodeArrayNodesGroup = function (_NodesGroup_1$NodesGr) { _this.unicodeArrayName = Utils_1.Utils.getRandomVariableName(UnicodeArrayNode_1.UnicodeArrayNode.UNICODE_ARRAY_RANDOM_LENGTH); _this.unicodeArrayTranslatorName = Utils_1.Utils.getRandomVariableName(UnicodeArrayNode_1.UnicodeArrayNode.UNICODE_ARRAY_RANDOM_LENGTH); + if (!_this.options.get('unicodeArray')) { + return _possibleConstructorReturn(_this); + } if (_this.options.get('rotateUnicodeArray')) { _this.unicodeArrayRotateValue = Utils_1.Utils.getRandomGenerator().integer({ min: 100, @@ -2641,6 +2620,42 @@ module.exports = require("mkdirp"); module.exports = require("path"); +/***/ }, +/* 47 */ +/***/ function(module, exports, __webpack_require__) { + +"use strict"; +"use strict"; + +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } + +function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } + +function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } + +var ConsoleOutputDisableExpressionNode_1 = __webpack_require__(21); +var NodesGroup_1 = __webpack_require__(12); + +var ConsoleOutputNodesGroup = function (_NodesGroup_1$NodesGr) { + _inherits(ConsoleOutputNodesGroup, _NodesGroup_1$NodesGr); + + function ConsoleOutputNodesGroup(options) { + _classCallCheck(this, ConsoleOutputNodesGroup); + + var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(ConsoleOutputNodesGroup).call(this, options)); + + if (!_this.options.get('disableConsoleOutput')) { + return _possibleConstructorReturn(_this); + } + _this.nodes.set('consoleOutputDisableExpressionNode', new ConsoleOutputDisableExpressionNode_1.ConsoleOutputDisableExpressionNode(_this.options)); + return _this; + } + + return ConsoleOutputNodesGroup; +}(NodesGroup_1.NodesGroup); + +exports.ConsoleOutputNodesGroup = ConsoleOutputNodesGroup; + /***/ } /******/ ]); //# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/src/Obfuscator.ts b/src/Obfuscator.ts index 85481f66..5938d572 100644 --- a/src/Obfuscator.ts +++ b/src/Obfuscator.ts @@ -1,7 +1,6 @@ import * as estraverse from 'estraverse'; import { ICustomNode } from './interfaces/ICustomNode'; -import { INodesGroup } from './interfaces/INodesGroup'; import { INode } from './interfaces/nodes/INode'; import { IObfuscator } from "./interfaces/IObfuscator"; import { IOptions } from "./interfaces/IOptions"; @@ -12,7 +11,7 @@ import { AppendState } from './enums/AppendState'; import { NodeType } from './enums/NodeType'; import { CatchClauseObfuscator } from './node-obfuscators/CatchClauseObfuscator'; -import { ConsoleOutputDisableExpressionNode } from './custom-nodes/console-output-nodes/ConsoleOutputDisableExpressionNode'; +import { ConsoleOutputNodesGroup } from "./node-groups/ConsoleOutputNodesGroup"; import { DebugProtectionNodesGroup } from './node-groups/DebugProtectionNodesGroup'; import { FunctionDeclarationObfuscator } from './node-obfuscators/FunctionDeclarationObfuscator'; import { FunctionObfuscator } from './node-obfuscators/FunctionObfuscator'; @@ -29,7 +28,7 @@ export class Obfuscator implements IObfuscator { /** * @type {Map} */ - private nodes: Map = new Map (); + private nodes: Map ; /** * @type {Map} @@ -60,6 +59,13 @@ export class Obfuscator implements IObfuscator { */ constructor (options: IOptions) { this.options = options; + + this.nodes = new Map ([ + ...new SelfDefendingNodesGroup(this.options).getNodes(), + ...new ConsoleOutputNodesGroup(this.options).getNodes(), + ...new DebugProtectionNodesGroup(this.options).getNodes(), + ...new UnicodeArrayNodesGroup(this.options).getNodes() + ]); } /** @@ -67,8 +73,6 @@ export class Obfuscator implements IObfuscator { * @returns {INode} */ public obfuscateNode (node: INode): INode { - this.setNewNodes(); - NodeUtils.parentize(node); this.beforeObfuscation(node); @@ -78,25 +82,6 @@ export class Obfuscator implements IObfuscator { return node; } - /** - * @param nodeName - * @param node - */ - private setNode (nodeName: string, node: ICustomNode): void { - this.nodes.set(nodeName, node); - } - - /** - * @param nodesGroup - */ - private setNodesGroup (nodesGroup: INodesGroup): void { - let nodes: Map = nodesGroup.getNodes(); - - nodes.forEach((node: ICustomNode, key: string) => { - this.nodes.set(key, node); - }); - } - /** * @param astTree */ @@ -144,28 +129,4 @@ export class Obfuscator implements IObfuscator { } }); } - - private setNewNodes (): void { - if (this.options.get('selfDefending')) { - this.setNodesGroup(new SelfDefendingNodesGroup(this.options)); - } - - if (this.options.get('disableConsoleOutput')) { - this.setNode( - 'consoleOutputDisableExpressionNode', - new ConsoleOutputDisableExpressionNode(this.options) - ); - } - - if (this.options.get('debugProtection')) { - this.setNodesGroup(new DebugProtectionNodesGroup(this.options)); - } - - if (this.options.get('unicodeArray')) { - /** - * Important to set this nodes latest to prevent runtime errors caused by `rotateUnicodeArray` option - */ - this.setNodesGroup(new UnicodeArrayNodesGroup(this.options)); - } - } } diff --git a/src/node-groups/ConsoleOutputNodesGroup.ts b/src/node-groups/ConsoleOutputNodesGroup.ts new file mode 100644 index 00000000..8d9da490 --- /dev/null +++ b/src/node-groups/ConsoleOutputNodesGroup.ts @@ -0,0 +1,22 @@ +import { IOptions } from "../interfaces/IOptions"; + +import { ConsoleOutputDisableExpressionNode } from "../custom-nodes/console-output-nodes/ConsoleOutputDisableExpressionNode"; +import { NodesGroup } from './NodesGroup'; + +export class ConsoleOutputNodesGroup extends NodesGroup { + /** + * @param options + */ + constructor (options: IOptions) { + super(options); + + if (!this.options.get('disableConsoleOutput')) { + return; + } + + this.nodes.set( + 'consoleOutputDisableExpressionNode', + new ConsoleOutputDisableExpressionNode(this.options) + ); + } +} diff --git a/src/node-groups/DebugProtectionNodesGroup.ts b/src/node-groups/DebugProtectionNodesGroup.ts index 8c623d5f..d84e5973 100644 --- a/src/node-groups/DebugProtectionNodesGroup.ts +++ b/src/node-groups/DebugProtectionNodesGroup.ts @@ -19,6 +19,10 @@ export class DebugProtectionNodesGroup extends NodesGroup { constructor (options: IOptions) { super(options); + if (!this.options.get('debugProtection')) { + return; + } + this.nodes.set( 'debugProtectionFunctionNode', new DebugProtectionFunctionNode(this.debugProtectionFunctionIdentifier, this.options) diff --git a/src/node-groups/SelfDefendingNodesGroup.ts b/src/node-groups/SelfDefendingNodesGroup.ts index 5ff8557e..21788ab2 100644 --- a/src/node-groups/SelfDefendingNodesGroup.ts +++ b/src/node-groups/SelfDefendingNodesGroup.ts @@ -10,6 +10,10 @@ export class SelfDefendingNodesGroup extends NodesGroup { constructor (options: IOptions) { super(options); + if (!this.options.get('selfDefending')) { + return; + } + this.nodes.set( 'selfDefendingUnicodeNode', new SelfDefendingUnicodeNode(this.options) diff --git a/src/node-groups/UnicodeArrayNodesGroup.ts b/src/node-groups/UnicodeArrayNodesGroup.ts index 863df5c5..172c1ed2 100644 --- a/src/node-groups/UnicodeArrayNodesGroup.ts +++ b/src/node-groups/UnicodeArrayNodesGroup.ts @@ -29,6 +29,10 @@ export class UnicodeArrayNodesGroup extends NodesGroup { constructor (options: IOptions) { super(options); + if (!this.options.get('unicodeArray')) { + return; + } + if (this.options.get('rotateUnicodeArray')) { this.unicodeArrayRotateValue = Utils.getRandomGenerator().integer({ min: 100,