refactoring

This commit is contained in:
sanex3339
2016-12-10 13:58:39 +03:00
parent ed0273f066
commit b1db0d9b0c
30 changed files with 301 additions and 232 deletions
+74 -90
View File
@@ -111,7 +111,7 @@ exports.ServiceIdentifiers = {
'Factory<ICustomNodeGroup>': Symbol('Factory<ICustomNodeGroup>'),
'Factory<INodeTransformer[]>': Symbol('Factory<INodeTransformer[]>'),
'Factory<IObfuscationResult>': Symbol('Factory<IObfuscationResult>'),
'Factory<IReplacer>': Symbol('Factory<IReplacer>'),
'Factory<IObfuscatorReplacer>': Symbol('Factory<IObfuscatorReplacer>'),
'Factory<IStorage<ICustomNode>>': Symbol('Factory<IStorage<ICustomNode>>'),
ICalleeDataExtractor: Symbol('ICalleeDataExtractor'),
ICustomNode: Symbol('ICustomNode'),
@@ -123,7 +123,7 @@ exports.ServiceIdentifiers = {
IObfuscationResult: Symbol('IObfuscationResult'),
IObfuscator: Symbol('IObfuscator'),
IOptions: Symbol('IOptions'),
IReplacer: Symbol('IReplacer'),
IObfuscatorReplacer: Symbol('IObfuscatorReplacer'),
ISourceMapCorrector: Symbol('ISourceMapCorrector'),
IStackTraceAnalyzer: Symbol('IStackTraceAnalyzer'),
'IStorage<ICustomNode>': Symbol('IStorage<ICustomNode>'),
@@ -150,7 +150,7 @@ var RandomGeneratorUtils = function () {
}
_createClass(RandomGeneratorUtils, null, [{
key: 'getRandomFloat',
key: "getRandomFloat",
value: function getRandomFloat(min, max) {
return RandomGeneratorUtils.randomGenerator.floating({
min: min,
@@ -159,16 +159,16 @@ var RandomGeneratorUtils = function () {
});
}
}, {
key: 'getRandomGenerator',
key: "getRandomGenerator",
value: function getRandomGenerator() {
var randomGenerator = RandomGeneratorUtils.randomGenerator;
if (!randomGenerator) {
throw new Error('`randomGenerator` static property is undefined');
throw new Error("`randomGenerator` static property is undefined");
}
return RandomGeneratorUtils.randomGenerator;
}
}, {
key: 'getRandomInteger',
key: "getRandomInteger",
value: function getRandomInteger(min, max) {
return RandomGeneratorUtils.randomGenerator.integer({
min: min,
@@ -176,16 +176,23 @@ var RandomGeneratorUtils = function () {
});
}
}, {
key: 'getRandomVariableName',
key: "getRandomString",
value: function getRandomString(length) {
var pool = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : RandomGeneratorUtils.randomGeneratorPool;
return RandomGeneratorUtils.getRandomGenerator().string({ length: length, pool: pool });
}
}, {
key: "getRandomVariableName",
value: function getRandomVariableName() {
var length = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 6;
var rangeMinInteger = 10000,
rangeMaxInteger = 99999999;
return '_' + Utils_1.Utils.hexadecimalPrefix + Utils_1.Utils.decToHex(RandomGeneratorUtils.getRandomInteger(rangeMinInteger, rangeMaxInteger)).substr(0, length);
return "_" + Utils_1.Utils.hexadecimalPrefix + Utils_1.Utils.decToHex(RandomGeneratorUtils.getRandomInteger(rangeMinInteger, rangeMaxInteger)).substr(0, length);
}
}, {
key: 'setRandomGeneratorSeed',
key: "setRandomGeneratorSeed",
value: function setRandomGeneratorSeed(randomGeneratorSeed) {
RandomGeneratorUtils.randomGenerator = new chance_1.Chance(randomGeneratorSeed);
}
@@ -195,7 +202,7 @@ var RandomGeneratorUtils = function () {
}();
RandomGeneratorUtils.randomGeneratorPool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
RandomGeneratorUtils.randomGeneratorPoolWithNumbers = RandomGeneratorUtils.randomGeneratorPool + '0123456789';
RandomGeneratorUtils.randomGeneratorPoolWithNumbers = RandomGeneratorUtils.randomGeneratorPool + "0123456789";
RandomGeneratorUtils.randomGenerator = new chance_1.Chance();
exports.RandomGeneratorUtils = RandomGeneratorUtils;
@@ -1164,7 +1171,7 @@ var AbstractReplacer = function AbstractReplacer(options) {
this.options = options;
};
AbstractReplacer = __decorate([inversify_1.injectable(), __param(0, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers.IOptions)), __metadata('design:paramtypes', [Object])], AbstractReplacer);
AbstractReplacer = __decorate([inversify_1.injectable(), __param(0, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers.IOptions)), __metadata("design:paramtypes", [Object])], AbstractReplacer);
exports.AbstractReplacer = AbstractReplacer;
/***/ },
@@ -2414,17 +2421,17 @@ var IdentifierReplacer_1 = __webpack_require__(80);
var NumberLiteralReplacer_1 = __webpack_require__(81);
var StringLiteralReplacer_1 = __webpack_require__(82);
exports.nodeObfuscatorsModule = new inversify_1.ContainerModule(function (bind) {
bind(ServiceIdentifiers_1.ServiceIdentifiers.IReplacer).to(BooleanLiteralReplacer_1.BooleanLiteralReplacer).whenTargetNamed(NodeObfuscatorsReplacers_1.NodeObfuscatorsReplacers.BooleanReplacer);
bind(ServiceIdentifiers_1.ServiceIdentifiers.IReplacer).to(IdentifierReplacer_1.IdentifierReplacer).whenTargetNamed(NodeObfuscatorsReplacers_1.NodeObfuscatorsReplacers.IdentifierReplacer);
bind(ServiceIdentifiers_1.ServiceIdentifiers.IReplacer).to(NumberLiteralReplacer_1.NumberLiteralReplacer).whenTargetNamed(NodeObfuscatorsReplacers_1.NodeObfuscatorsReplacers.NumberLiteralReplacer);
bind(ServiceIdentifiers_1.ServiceIdentifiers.IReplacer).to(StringLiteralReplacer_1.StringLiteralReplacer).whenTargetNamed(NodeObfuscatorsReplacers_1.NodeObfuscatorsReplacers.StringLiteralReplacer);
bind(ServiceIdentifiers_1.ServiceIdentifiers['Factory<IReplacer>']).toFactory(function (context) {
bind(ServiceIdentifiers_1.ServiceIdentifiers.IObfuscatorReplacer).to(BooleanLiteralReplacer_1.BooleanLiteralReplacer).whenTargetNamed(NodeObfuscatorsReplacers_1.NodeObfuscatorsReplacers.BooleanReplacer);
bind(ServiceIdentifiers_1.ServiceIdentifiers.IObfuscatorReplacer).to(IdentifierReplacer_1.IdentifierReplacer).whenTargetNamed(NodeObfuscatorsReplacers_1.NodeObfuscatorsReplacers.IdentifierReplacer);
bind(ServiceIdentifiers_1.ServiceIdentifiers.IObfuscatorReplacer).to(NumberLiteralReplacer_1.NumberLiteralReplacer).whenTargetNamed(NodeObfuscatorsReplacers_1.NodeObfuscatorsReplacers.NumberLiteralReplacer);
bind(ServiceIdentifiers_1.ServiceIdentifiers.IObfuscatorReplacer).to(StringLiteralReplacer_1.StringLiteralReplacer).whenTargetNamed(NodeObfuscatorsReplacers_1.NodeObfuscatorsReplacers.StringLiteralReplacer);
bind(ServiceIdentifiers_1.ServiceIdentifiers['Factory<IObfuscatorReplacer>']).toFactory(function (context) {
var cache = new Map();
return function (replacerName) {
if (cache.has(replacerName)) {
return cache.get(replacerName);
}
var obfuscationReplacer = context.container.getNamed(ServiceIdentifiers_1.ServiceIdentifiers.IReplacer, replacerName);
var obfuscationReplacer = context.container.getNamed(ServiceIdentifiers_1.ServiceIdentifiers.IObfuscatorReplacer, replacerName);
cache.set(replacerName, obfuscationReplacer);
return obfuscationReplacer;
};
@@ -4346,33 +4353,30 @@ var CatchClauseObfuscator = function (_AbstractNodeTransfor) {
_createClass(CatchClauseObfuscator, [{
key: "transformNode",
value: function transformNode(catchClauseNode) {
this.identifierReplacer.setPrefix(RandomGeneratorUtils_1.RandomGeneratorUtils.getRandomGenerator().string({
length: 5,
pool: RandomGeneratorUtils_1.RandomGeneratorUtils.randomGeneratorPool
}));
this.storeCatchClauseParam(catchClauseNode);
this.replaceCatchClauseParam(catchClauseNode);
var nodeIdentifier = RandomGeneratorUtils_1.RandomGeneratorUtils.getRandomString(7);
this.storeCatchClauseParam(catchClauseNode, nodeIdentifier);
this.replaceCatchClauseParam(catchClauseNode, nodeIdentifier);
}
}, {
key: "storeCatchClauseParam",
value: function storeCatchClauseParam(catchClauseNode) {
value: function storeCatchClauseParam(catchClauseNode, nodeIdentifier) {
var _this2 = this;
NodeUtils_1.NodeUtils.typedTraverse(catchClauseNode.param, NodeType_1.NodeType.Identifier, {
enter: function enter(node) {
return _this2.identifierReplacer.storeNames(node.name);
return _this2.identifierReplacer.storeNames(node.name, nodeIdentifier);
}
});
}
}, {
key: "replaceCatchClauseParam",
value: function replaceCatchClauseParam(catchClauseNode) {
value: function replaceCatchClauseParam(catchClauseNode, nodeIdentifier) {
var _this3 = this;
estraverse.replace(catchClauseNode, {
enter: function enter(node, parentNode) {
if (Node_1.Node.isReplaceableIdentifierNode(node, parentNode)) {
node.name = _this3.identifierReplacer.replace(node.name);
node.name = _this3.identifierReplacer.replace(node.name, nodeIdentifier);
}
}
});
@@ -4381,7 +4385,7 @@ var CatchClauseObfuscator = function (_AbstractNodeTransfor) {
return CatchClauseObfuscator;
}(AbstractNodeTransformer_1.AbstractNodeTransformer);
CatchClauseObfuscator = __decorate([inversify_1.injectable(), __param(0, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers['Factory<IReplacer>'])), __param(1, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers.IOptions)), __metadata('design:paramtypes', [Function, Object])], CatchClauseObfuscator);
CatchClauseObfuscator = __decorate([inversify_1.injectable(), __param(0, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers['Factory<IObfuscatorReplacer>'])), __param(1, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers.IOptions)), __metadata("design:paramtypes", [Function, Object])], CatchClauseObfuscator);
exports.CatchClauseObfuscator = CatchClauseObfuscator;
/***/ },
@@ -4441,37 +4445,34 @@ var FunctionDeclarationObfuscator = function (_AbstractNodeTransfor) {
_createClass(FunctionDeclarationObfuscator, [{
key: "transformNode",
value: function transformNode(functionDeclarationNode, parentNode) {
this.identifierReplacer.setPrefix(RandomGeneratorUtils_1.RandomGeneratorUtils.getRandomGenerator().string({
length: 5,
pool: RandomGeneratorUtils_1.RandomGeneratorUtils.randomGeneratorPool
}));
var nodeIdentifier = RandomGeneratorUtils_1.RandomGeneratorUtils.getRandomString(7);
var blockScopeOfFunctionDeclarationNode = NodeUtils_1.NodeUtils.getBlockScopeOfNode(functionDeclarationNode);
if (blockScopeOfFunctionDeclarationNode.type === NodeType_1.NodeType.Program) {
return;
}
this.storeFunctionName(functionDeclarationNode);
this.replaceFunctionName(blockScopeOfFunctionDeclarationNode);
this.storeFunctionName(functionDeclarationNode, nodeIdentifier);
this.replaceFunctionName(blockScopeOfFunctionDeclarationNode, nodeIdentifier);
}
}, {
key: "storeFunctionName",
value: function storeFunctionName(functionDeclarationNode) {
value: function storeFunctionName(functionDeclarationNode, nodeIdentifier) {
var _this2 = this;
NodeUtils_1.NodeUtils.typedTraverse(functionDeclarationNode.id, NodeType_1.NodeType.Identifier, {
enter: function enter(node) {
return _this2.identifierReplacer.storeNames(node.name);
return _this2.identifierReplacer.storeNames(node.name, nodeIdentifier);
}
});
}
}, {
key: "replaceFunctionName",
value: function replaceFunctionName(scopeNode) {
value: function replaceFunctionName(scopeNode, nodeIdentifier) {
var _this3 = this;
estraverse.replace(scopeNode, {
enter: function enter(node, parentNode) {
if (Node_1.Node.isReplaceableIdentifierNode(node, parentNode)) {
node.name = _this3.identifierReplacer.replace(node.name);
node.name = _this3.identifierReplacer.replace(node.name, nodeIdentifier);
}
}
});
@@ -4480,7 +4481,7 @@ var FunctionDeclarationObfuscator = function (_AbstractNodeTransfor) {
return FunctionDeclarationObfuscator;
}(AbstractNodeTransformer_1.AbstractNodeTransformer);
FunctionDeclarationObfuscator = __decorate([inversify_1.injectable(), __param(0, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers['Factory<IReplacer>'])), __param(1, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers.IOptions)), __metadata('design:paramtypes', [Function, Object])], FunctionDeclarationObfuscator);
FunctionDeclarationObfuscator = __decorate([inversify_1.injectable(), __param(0, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers['Factory<IObfuscatorReplacer>'])), __param(1, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers.IOptions)), __metadata("design:paramtypes", [Function, Object])], FunctionDeclarationObfuscator);
exports.FunctionDeclarationObfuscator = FunctionDeclarationObfuscator;
/***/ },
@@ -4540,35 +4541,32 @@ var FunctionObfuscator = function (_AbstractNodeTransfor) {
_createClass(FunctionObfuscator, [{
key: "transformNode",
value: function transformNode(functionNode) {
this.identifierReplacer.setPrefix(RandomGeneratorUtils_1.RandomGeneratorUtils.getRandomGenerator().string({
length: 5,
pool: RandomGeneratorUtils_1.RandomGeneratorUtils.randomGeneratorPool
}));
this.storeFunctionParams(functionNode);
this.replaceFunctionParams(functionNode);
var nodeIdentifier = RandomGeneratorUtils_1.RandomGeneratorUtils.getRandomString(7);
this.storeFunctionParams(functionNode, nodeIdentifier);
this.replaceFunctionParams(functionNode, nodeIdentifier);
}
}, {
key: "storeFunctionParams",
value: function storeFunctionParams(functionNode) {
value: function storeFunctionParams(functionNode, nodeIdentifier) {
var _this2 = this;
functionNode.params.forEach(function (paramsNode) {
NodeUtils_1.NodeUtils.typedTraverse(paramsNode, NodeType_1.NodeType.Identifier, {
enter: function enter(node) {
return _this2.identifierReplacer.storeNames(node.name);
return _this2.identifierReplacer.storeNames(node.name, nodeIdentifier);
}
});
});
}
}, {
key: "replaceFunctionParams",
value: function replaceFunctionParams(functionNode) {
value: function replaceFunctionParams(functionNode, nodeIdentifier) {
var _this3 = this;
var traverseVisitor = {
enter: function enter(node, parentNode) {
if (Node_1.Node.isReplaceableIdentifierNode(node, parentNode)) {
var newNodeName = _this3.identifierReplacer.replace(node.name);
var newNodeName = _this3.identifierReplacer.replace(node.name, nodeIdentifier);
if (node.name !== newNodeName) {
node.name = newNodeName;
node.obfuscated = true;
@@ -4585,7 +4583,7 @@ var FunctionObfuscator = function (_AbstractNodeTransfor) {
return FunctionObfuscator;
}(AbstractNodeTransformer_1.AbstractNodeTransformer);
FunctionObfuscator = __decorate([inversify_1.injectable(), __param(0, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers['Factory<IReplacer>'])), __param(1, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers.IOptions)), __metadata('design:paramtypes', [Function, Object])], FunctionObfuscator);
FunctionObfuscator = __decorate([inversify_1.injectable(), __param(0, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers['Factory<IObfuscatorReplacer>'])), __param(1, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers.IOptions)), __metadata("design:paramtypes", [Function, Object])], FunctionObfuscator);
exports.FunctionObfuscator = FunctionObfuscator;
/***/ },
@@ -4645,33 +4643,30 @@ var LabeledStatementObfuscator = function (_AbstractNodeTransfor) {
_createClass(LabeledStatementObfuscator, [{
key: "transformNode",
value: function transformNode(labeledStatementNode) {
this.identifierReplacer.setPrefix(RandomGeneratorUtils_1.RandomGeneratorUtils.getRandomGenerator().string({
length: 5,
pool: RandomGeneratorUtils_1.RandomGeneratorUtils.randomGeneratorPool
}));
this.storeLabeledStatementName(labeledStatementNode);
this.replaceLabeledStatementName(labeledStatementNode);
var nodeIdentifier = RandomGeneratorUtils_1.RandomGeneratorUtils.getRandomString(7);
this.storeLabeledStatementName(labeledStatementNode, nodeIdentifier);
this.replaceLabeledStatementName(labeledStatementNode, nodeIdentifier);
}
}, {
key: "storeLabeledStatementName",
value: function storeLabeledStatementName(labeledStatementNode) {
value: function storeLabeledStatementName(labeledStatementNode, nodeIdentifier) {
var _this2 = this;
NodeUtils_1.NodeUtils.typedTraverse(labeledStatementNode.label, NodeType_1.NodeType.Identifier, {
enter: function enter(node) {
return _this2.identifierReplacer.storeNames(node.name);
return _this2.identifierReplacer.storeNames(node.name, nodeIdentifier);
}
});
}
}, {
key: "replaceLabeledStatementName",
value: function replaceLabeledStatementName(labeledStatementNode) {
value: function replaceLabeledStatementName(labeledStatementNode, nodeIdentifier) {
var _this3 = this;
estraverse.replace(labeledStatementNode, {
enter: function enter(node, parentNode) {
if (Node_1.Node.isLabelIdentifierNode(node, parentNode)) {
node.name = _this3.identifierReplacer.replace(node.name);
node.name = _this3.identifierReplacer.replace(node.name, nodeIdentifier);
}
}
});
@@ -4680,7 +4675,7 @@ var LabeledStatementObfuscator = function (_AbstractNodeTransfor) {
return LabeledStatementObfuscator;
}(AbstractNodeTransformer_1.AbstractNodeTransformer);
LabeledStatementObfuscator = __decorate([inversify_1.injectable(), __param(0, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers['Factory<IReplacer>'])), __param(1, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers.IOptions)), __metadata('design:paramtypes', [Function, Object])], LabeledStatementObfuscator);
LabeledStatementObfuscator = __decorate([inversify_1.injectable(), __param(0, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers['Factory<IObfuscatorReplacer>'])), __param(1, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers.IOptions)), __metadata("design:paramtypes", [Function, Object])], LabeledStatementObfuscator);
exports.LabeledStatementObfuscator = LabeledStatementObfuscator;
/***/ },
@@ -4763,7 +4758,7 @@ var LiteralObfuscator = function (_AbstractNodeTransfor) {
return LiteralObfuscator;
}(AbstractNodeTransformer_1.AbstractNodeTransformer);
LiteralObfuscator = __decorate([inversify_1.injectable(), __param(0, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers['Factory<IReplacer>'])), __param(1, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers.IOptions)), __metadata('design:paramtypes', [Function, Object])], LiteralObfuscator);
LiteralObfuscator = __decorate([inversify_1.injectable(), __param(0, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers['Factory<IObfuscatorReplacer>'])), __param(1, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers.IOptions)), __metadata("design:paramtypes", [Function, Object])], LiteralObfuscator);
exports.LiteralObfuscator = LiteralObfuscator;
/***/ },
@@ -4870,7 +4865,7 @@ var MemberExpressionObfuscator = function (_AbstractNodeTransfor) {
return MemberExpressionObfuscator;
}(AbstractNodeTransformer_1.AbstractNodeTransformer);
MemberExpressionObfuscator = __decorate([inversify_1.injectable(), __param(0, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers['Factory<IReplacer>'])), __param(1, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers.IOptions)), __metadata('design:paramtypes', [Function, Object])], MemberExpressionObfuscator);
MemberExpressionObfuscator = __decorate([inversify_1.injectable(), __param(0, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers['Factory<IObfuscatorReplacer>'])), __param(1, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers.IOptions)), __metadata("design:paramtypes", [Function, Object])], MemberExpressionObfuscator);
exports.MemberExpressionObfuscator = MemberExpressionObfuscator;
/***/ },
@@ -4913,7 +4908,7 @@ var NodeObfuscatorsReplacers_1 = __webpack_require__(13);
var AbstractNodeTransformer_1 = __webpack_require__(11);
var Node_1 = __webpack_require__(5);
var Utils_1 = __webpack_require__(4);
var MethodDefinitionObfuscator_1 = function (_AbstractNodeTransfor) {
var MethodDefinitionObfuscator = MethodDefinitionObfuscator_1 = function (_AbstractNodeTransfor) {
_inherits(MethodDefinitionObfuscator, _AbstractNodeTransfor);
function MethodDefinitionObfuscator(replacersFactory, options) {
@@ -4950,10 +4945,10 @@ var MethodDefinitionObfuscator_1 = function (_AbstractNodeTransfor) {
return MethodDefinitionObfuscator;
}(AbstractNodeTransformer_1.AbstractNodeTransformer);
var MethodDefinitionObfuscator = MethodDefinitionObfuscator_1;
MethodDefinitionObfuscator.ignoredNames = ['constructor'];
MethodDefinitionObfuscator = MethodDefinitionObfuscator_1 = __decorate([inversify_1.injectable(), __param(0, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers['Factory<IReplacer>'])), __param(1, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers.IOptions)), __metadata('design:paramtypes', [Function, Object])], MethodDefinitionObfuscator);
MethodDefinitionObfuscator = MethodDefinitionObfuscator_1 = __decorate([inversify_1.injectable(), __param(0, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers['Factory<IObfuscatorReplacer>'])), __param(1, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers.IOptions)), __metadata("design:paramtypes", [Function, Object])], MethodDefinitionObfuscator);
exports.MethodDefinitionObfuscator = MethodDefinitionObfuscator;
var MethodDefinitionObfuscator_1;
/***/ },
/* 77 */
@@ -5117,40 +5112,37 @@ var VariableDeclarationObfuscator = function (_AbstractNodeTransfor) {
_createClass(VariableDeclarationObfuscator, [{
key: "transformNode",
value: function transformNode(variableDeclarationNode, parentNode) {
this.identifierReplacer.setPrefix(RandomGeneratorUtils_1.RandomGeneratorUtils.getRandomGenerator().string({
length: 5,
pool: RandomGeneratorUtils_1.RandomGeneratorUtils.randomGeneratorPool
}));
var blockScopeOfVariableDeclarationNode = NodeUtils_1.NodeUtils.getBlockScopeOfNode(variableDeclarationNode);
if (blockScopeOfVariableDeclarationNode.type === NodeType_1.NodeType.Program) {
return;
}
var nodeIdentifier = RandomGeneratorUtils_1.RandomGeneratorUtils.getRandomString(7);
var scopeNode = variableDeclarationNode.kind === 'var' ? blockScopeOfVariableDeclarationNode : parentNode;
this.storeVariableNames(variableDeclarationNode);
this.replaceVariableNames(scopeNode);
this.storeVariableNames(variableDeclarationNode, nodeIdentifier);
this.replaceVariableNames(scopeNode, nodeIdentifier);
}
}, {
key: "storeVariableNames",
value: function storeVariableNames(variableDeclarationNode) {
value: function storeVariableNames(variableDeclarationNode, nodeIdentifier) {
var _this2 = this;
variableDeclarationNode.declarations.forEach(function (declarationNode) {
NodeUtils_1.NodeUtils.typedTraverse(declarationNode.id, NodeType_1.NodeType.Identifier, {
enter: function enter(node) {
return _this2.identifierReplacer.storeNames(node.name);
return _this2.identifierReplacer.storeNames(node.name, nodeIdentifier);
}
});
});
}
}, {
key: "replaceVariableNames",
value: function replaceVariableNames(scopeNode) {
value: function replaceVariableNames(scopeNode, nodeIdentifier) {
var _this3 = this;
estraverse.replace(scopeNode, {
enter: function enter(node, parentNode) {
if (!node.obfuscated && Node_1.Node.isReplaceableIdentifierNode(node, parentNode)) {
node.name = _this3.identifierReplacer.replace(node.name);
node.name = _this3.identifierReplacer.replace(node.name, nodeIdentifier);
}
}
});
@@ -5159,7 +5151,7 @@ var VariableDeclarationObfuscator = function (_AbstractNodeTransfor) {
return VariableDeclarationObfuscator;
}(AbstractNodeTransformer_1.AbstractNodeTransformer);
VariableDeclarationObfuscator = __decorate([inversify_1.injectable(), __param(0, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers['Factory<IReplacer>'])), __param(1, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers.IOptions)), __metadata('design:paramtypes', [Function, Object])], VariableDeclarationObfuscator);
VariableDeclarationObfuscator = __decorate([inversify_1.injectable(), __param(0, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers['Factory<IObfuscatorReplacer>'])), __param(1, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers.IOptions)), __metadata("design:paramtypes", [Function, Object])], VariableDeclarationObfuscator);
exports.VariableDeclarationObfuscator = VariableDeclarationObfuscator;
/***/ },
@@ -5271,26 +5263,18 @@ var IdentifierReplacer = function (_AbstractReplacer_1$A) {
_createClass(IdentifierReplacer, [{
key: "replace",
value: function replace(nodeValue) {
var obfuscatedIdentifierName = this.namesMap.get(nodeValue + "-" + this.uniquePrefix);
value: function replace(nodeValue, nodeIdentifier) {
var obfuscatedIdentifierName = this.namesMap.get(nodeValue + "-" + nodeIdentifier);
if (!obfuscatedIdentifierName) {
return nodeValue;
}
return obfuscatedIdentifierName;
}
}, {
key: "setPrefix",
value: function setPrefix(uniquePrefix) {
this.uniquePrefix = uniquePrefix;
}
}, {
key: "storeNames",
value: function storeNames(nodeName) {
if (!this.uniquePrefix) {
throw new Error('`uniquePrefix` is `undefined`. Set it before `storeNames`');
}
value: function storeNames(nodeName, nodeIdentifier) {
if (!this.isReservedName(nodeName)) {
this.namesMap.set(nodeName + "-" + this.uniquePrefix, RandomGeneratorUtils_1.RandomGeneratorUtils.getRandomVariableName());
this.namesMap.set(nodeName + "-" + nodeIdentifier, RandomGeneratorUtils_1.RandomGeneratorUtils.getRandomVariableName());
}
}
}, {
@@ -5304,7 +5288,7 @@ var IdentifierReplacer = function (_AbstractReplacer_1$A) {
return IdentifierReplacer;
}(AbstractReplacer_1.AbstractReplacer);
IdentifierReplacer = __decorate([inversify_1.injectable(), __param(0, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers.IOptions)), __metadata('design:paramtypes', [Object])], IdentifierReplacer);
IdentifierReplacer = __decorate([inversify_1.injectable(), __param(0, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers.IOptions)), __metadata("design:paramtypes", [Object])], IdentifierReplacer);
exports.IdentifierReplacer = IdentifierReplacer;
/***/ },
+3 -3
View File
@@ -27,7 +27,7 @@
"escodegen": "1.8.1",
"esprima": "3.1.2",
"estraverse": "4.2.0",
"inversify": "^3.0.0-rc.1",
"inversify": "^3.0.0-rc.2",
"is-equal": "^1.5.3",
"mkdirp": "0.5.1",
"reflect-metadata": "^0.1.8",
@@ -48,7 +48,7 @@
"@types/node": "6.0.51",
"@types/sinon": "1.16.32",
"@types/string-template": "^1.0.2",
"awesome-typescript-loader": "^3.0.0-beta.9",
"awesome-typescript-loader": "^3.0.0-beta.10",
"babel-cli": "6.18.0",
"babel-loader": "6.2.9",
"babel-preset-es2015": "6.18.0",
@@ -57,7 +57,7 @@
"istanbul": "1.1.0-alpha.1",
"mocha": "3.2.0",
"sinon": "2.0.0-pre.3",
"ts-node": "1.7.0",
"ts-node": "1.7.2",
"tslint": "4.0.2",
"typescript": "2.1.4",
"webpack": "2.1.0-beta.27",
+2 -2
View File
@@ -5,7 +5,7 @@ export const ServiceIdentifiers: any = {
'Factory<ICustomNodeGroup>': Symbol('Factory<ICustomNodeGroup>'),
'Factory<INodeTransformer[]>': Symbol('Factory<INodeTransformer[]>'),
'Factory<IObfuscationResult>': Symbol('Factory<IObfuscationResult>'),
'Factory<IReplacer>': Symbol('Factory<IReplacer>'),
'Factory<IObfuscatorReplacer>': Symbol('Factory<IObfuscatorReplacer>'),
'Factory<IStorage<ICustomNode>>': Symbol('Factory<IStorage<ICustomNode>>'),
ICalleeDataExtractor: Symbol('ICalleeDataExtractor'),
ICustomNode: Symbol('ICustomNode'),
@@ -17,7 +17,7 @@ export const ServiceIdentifiers: any = {
IObfuscationResult: Symbol('IObfuscationResult'),
IObfuscator: Symbol('IObfuscator'),
IOptions: Symbol('IOptions'),
IReplacer: Symbol('IReplacer'),
IObfuscatorReplacer: Symbol('IObfuscatorReplacer'),
ISourceMapCorrector: Symbol('ISourceMapCorrector'),
IStackTraceAnalyzer: Symbol('IStackTraceAnalyzer'),
'IStorage<ICustomNode>': Symbol('IStorage<ICustomNode>'),
@@ -1,7 +1,7 @@
import { ContainerModule, interfaces } from 'inversify';
import { ServiceIdentifiers } from '../../ServiceIdentifiers';
import { IReplacer } from '../../../interfaces/node-transformers/IReplacer';
import { IObfuscatorReplacer } from '../../../interfaces/node-transformers/IObfuscatorReplacer';
import { NodeObfuscatorsReplacers } from '../../../enums/container/NodeObfuscatorsReplacers';
@@ -11,33 +11,33 @@ import { NumberLiteralReplacer } from '../../../node-transformers/node-obfuscato
import { StringLiteralReplacer } from '../../../node-transformers/node-obfuscators/replacers/StringLiteralReplacer';
export const nodeObfuscatorsModule: interfaces.ContainerModule = new ContainerModule((bind: interfaces.Bind) => {
bind<IReplacer>(ServiceIdentifiers.IReplacer)
bind<IObfuscatorReplacer>(ServiceIdentifiers.IObfuscatorReplacer)
.to(BooleanLiteralReplacer)
.whenTargetNamed(NodeObfuscatorsReplacers.BooleanReplacer);
bind<IReplacer>(ServiceIdentifiers.IReplacer)
bind<IObfuscatorReplacer>(ServiceIdentifiers.IObfuscatorReplacer)
.to(IdentifierReplacer)
.whenTargetNamed(NodeObfuscatorsReplacers.IdentifierReplacer);
bind<IReplacer>(ServiceIdentifiers.IReplacer)
bind<IObfuscatorReplacer>(ServiceIdentifiers.IObfuscatorReplacer)
.to(NumberLiteralReplacer)
.whenTargetNamed(NodeObfuscatorsReplacers.NumberLiteralReplacer);
bind<IReplacer>(ServiceIdentifiers.IReplacer)
bind<IObfuscatorReplacer>(ServiceIdentifiers.IObfuscatorReplacer)
.to(StringLiteralReplacer)
.whenTargetNamed(NodeObfuscatorsReplacers.StringLiteralReplacer);
bind<IReplacer>(ServiceIdentifiers['Factory<IReplacer>'])
.toFactory<IReplacer>((context: interfaces.Context) => {
const cache: Map <NodeObfuscatorsReplacers, IReplacer> = new Map <NodeObfuscatorsReplacers, IReplacer> ();
bind<IObfuscatorReplacer>(ServiceIdentifiers['Factory<IObfuscatorReplacer>'])
.toFactory<IObfuscatorReplacer>((context: interfaces.Context) => {
const cache: Map <NodeObfuscatorsReplacers, IObfuscatorReplacer> = new Map <NodeObfuscatorsReplacers, IObfuscatorReplacer> ();
return (replacerName: NodeObfuscatorsReplacers) => {
if (cache.has(replacerName)) {
return <IReplacer>cache.get(replacerName);
return <IObfuscatorReplacer>cache.get(replacerName);
}
const obfuscationReplacer: IReplacer = context.container.getNamed<IReplacer>(
ServiceIdentifiers.IReplacer,
const obfuscationReplacer: IObfuscatorReplacer = context.container.getNamed<IObfuscatorReplacer>(
ServiceIdentifiers.IObfuscatorReplacer,
replacerName
);
+10
View File
@@ -0,0 +1,10 @@
import { IGeneratorOutput } from '../interfaces/IGeneratorOutput';
declare module 'escodegen' {
/**
* @param ast
* @param options
* @returns IGeneratorOutput
*/
export function generate(ast: any, options?: GenerateOptions): IGeneratorOutput;
}
-5
View File
@@ -1,5 +0,0 @@
import { IGeneratorOutput } from './IGeneratorOutput';
declare module 'escodegen' {
export function generate(ast: any, options?: GenerateOptions): IGeneratorOutput;
}
+4
View File
@@ -1,5 +1,9 @@
import { IObfuscationResult } from './IObfuscationResult';
export interface IJavaScriptObfuscator {
/**
* @param sourceCode
* @returns IObfuscationResult
*/
obfuscate (sourceCode: string): IObfuscationResult;
}
+4
View File
@@ -1,5 +1,9 @@
import * as ESTree from 'estree';
export interface IObfuscator {
/**
* @param astTree
* @returns ESTree.Program
*/
obfuscateAstTree (astTree: ESTree.Program): ESTree.Program;
}
+5
View File
@@ -1,5 +1,10 @@
import { IObfuscationResult } from './IObfuscationResult';
export interface ISourceMapCorrector {
/**
* @param obfuscatedCode
* @param sourceMap
* @returns IObfuscationResult
*/
correct (obfuscatedCode: string, sourceMap: string): IObfuscationResult;
}
@@ -1,5 +1,8 @@
import { ICustomNode } from './ICustomNode';
export interface ICustomNodeWithIdentifier extends ICustomNode {
/**
* @returns string
*/
getNodeIdentifier (): string;
}
@@ -3,6 +3,17 @@ import Events = NodeJS.Events;
import { TObfuscationEvent } from '../../types/event-emitters/TObfuscationEvent';
export interface IObfuscationEventEmitter extends Events {
/**
* @param event
* @param listener
* @returns this
*/
on(event: TObfuscationEvent, listener: Function): this;
/**
* @param event
* @param listener
* @returns this
*/
once(event: TObfuscationEvent, listener: Function): this;
}
@@ -4,6 +4,13 @@ import { ICustomNode } from '../custom-nodes/ICustomNode';
import { IStorage } from '../storages/IStorage';
export interface IControlFlowReplacer {
/**
* @param node
* @param parentNode
* @param controlFlowStorage
* @param controlFlowStorageCustomNodeName
* @returns ICustomNode | undefined
*/
replace (
node: ESTree.Node,
parentNode: ESTree.Node,
@@ -1,5 +1,9 @@
import * as ESTree from 'estree';
export interface INodeTransformer {
/**
* @param node
* @param parentNode
*/
transformNode (node: ESTree.Node, parentNode?: ESTree.Node): void;
}
@@ -0,0 +1,7 @@
export interface IObfuscatorReplacer {
/**
* @param nodeValue
* @param nodeIdentifier
*/
replace (nodeValue: any, nodeIdentifier?: string): string;
}
@@ -0,0 +1,9 @@
import { IObfuscatorReplacer } from './IObfuscatorReplacer';
export interface IObfuscatorReplacerWithStorage extends IObfuscatorReplacer {
/**
* @param nodeValue
* @param nodeIdentifier
*/
storeNames (nodeValue: any, nodeIdentifier: string): void;
}
-3
View File
@@ -1,3 +0,0 @@
export interface IReplacer {
replace (nodeValue: any): string;
}
@@ -3,5 +3,10 @@ import * as ESTree from 'estree';
import { ICalleeData } from './ICalleeData';
export interface ICalleeDataExtractor {
/**
* @param blockScopeBody
* @param callee
* @returns ICalleeData|null
*/
extract (blockScopeBody: ESTree.Node[], callee: ESTree.Node): ICalleeData|null;
}
@@ -3,5 +3,9 @@ import * as ESTree from 'estree';
import { IStackTraceData } from './IStackTraceData';
export interface IStackTraceAnalyzer {
/**
* @param blockScopeBody
* @returns IStackTraceData[]
*/
analyze (blockScopeBody: ESTree.Node[]): IStackTraceData[];
}
+30
View File
@@ -1,11 +1,41 @@
import { IInitializable } from '../IInitializable';
export interface IStorage <T> extends IInitializable {
/**
* @param key
* @returns T
*/
get (key: string | number): T;
/**
* @param value
* @returns string | number | null
*/
getKeyOf (value: T): string | number | null;
/**
* @returns number
*/
getLength (): number;
/**
* @returns any
*/
getStorage (): any;
/**
* @param args
*/
initialize (...args: any[]): void;
/**
* @param key
* @param value
*/
set (key: string | number | null, value: T): void;
/**
* @returns string
*/
toString (): string;
}
@@ -5,13 +5,13 @@ import * as estraverse from 'estraverse';
import * as ESTree from 'estree';
import { IOptions } from '../../interfaces/options/IOptions';
import { IReplacer } from '../../interfaces/node-transformers/IReplacer';
import { IObfuscatorReplacer } from '../../interfaces/node-transformers/IObfuscatorReplacer';
import { IObfuscatorReplacerWithStorage } from '../../interfaces/node-transformers/IObfuscatorReplacerWithStorage';
import { NodeObfuscatorsReplacers } from '../../enums/container/NodeObfuscatorsReplacers';
import { NodeType } from '../../enums/NodeType';
import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
import { IdentifierReplacer } from './replacers/IdentifierReplacer';
import { Node } from '../../node/Node';
import { NodeUtils } from '../../node/NodeUtils';
import { RandomGeneratorUtils } from '../../utils/RandomGeneratorUtils';
@@ -27,53 +27,52 @@ import { RandomGeneratorUtils } from '../../utils/RandomGeneratorUtils';
@injectable()
export class CatchClauseObfuscator extends AbstractNodeTransformer {
/**
* @type {IdentifierReplacer}
* @type {IObfuscatorReplacerWithStorage}
*/
private readonly identifierReplacer: IReplacer & IdentifierReplacer;
private readonly identifierReplacer: IObfuscatorReplacerWithStorage;
/**
* @param replacersFactory
* @param options
*/
constructor(
@inject(ServiceIdentifiers['Factory<IReplacer>']) replacersFactory: (replacer: NodeObfuscatorsReplacers) => IReplacer,
@inject(ServiceIdentifiers['Factory<IObfuscatorReplacer>']) replacersFactory: (replacer: NodeObfuscatorsReplacers) => IObfuscatorReplacer,
@inject(ServiceIdentifiers.IOptions) options: IOptions
) {
super(options);
this.identifierReplacer = <IdentifierReplacer>replacersFactory(NodeObfuscatorsReplacers.IdentifierReplacer);
this.identifierReplacer = <IObfuscatorReplacerWithStorage>replacersFactory(NodeObfuscatorsReplacers.IdentifierReplacer);
}
/**
* @param catchClauseNode
*/
public transformNode (catchClauseNode: ESTree.CatchClause): void {
this.identifierReplacer.setPrefix(RandomGeneratorUtils.getRandomGenerator().string({
length: 5,
pool: RandomGeneratorUtils.randomGeneratorPool
}));
const nodeIdentifier: string = RandomGeneratorUtils.getRandomString(7);
this.storeCatchClauseParam(catchClauseNode);
this.replaceCatchClauseParam(catchClauseNode);
this.storeCatchClauseParam(catchClauseNode, nodeIdentifier);
this.replaceCatchClauseParam(catchClauseNode, nodeIdentifier);
}
/**
* @param catchClauseNode
* @param nodeIdentifier
*/
private storeCatchClauseParam (catchClauseNode: ESTree.CatchClause): void {
private storeCatchClauseParam (catchClauseNode: ESTree.CatchClause, nodeIdentifier: string): void {
NodeUtils.typedTraverse(catchClauseNode.param, NodeType.Identifier, {
enter: (node: ESTree.Identifier) => this.identifierReplacer.storeNames(node.name)
enter: (node: ESTree.Identifier) => this.identifierReplacer.storeNames(node.name, nodeIdentifier)
});
}
/**
* @param catchClauseNode
* @param nodeIdentifier
*/
private replaceCatchClauseParam (catchClauseNode: ESTree.CatchClause): void {
private replaceCatchClauseParam (catchClauseNode: ESTree.CatchClause, nodeIdentifier: string): void {
estraverse.replace(catchClauseNode, {
enter: (node: ESTree.Node, parentNode: ESTree.Node): any => {
if (Node.isReplaceableIdentifierNode(node, parentNode)) {
node.name = this.identifierReplacer.replace(node.name);
node.name = this.identifierReplacer.replace(node.name, nodeIdentifier);
}
}
});
@@ -7,13 +7,13 @@ import * as ESTree from 'estree';
import { TNodeWithBlockStatement } from '../../types/node/TNodeWithBlockStatement';
import { IOptions } from '../../interfaces/options/IOptions';
import { IReplacer } from '../../interfaces/node-transformers/IReplacer';
import { IObfuscatorReplacer } from '../../interfaces/node-transformers/IObfuscatorReplacer';
import { IObfuscatorReplacerWithStorage } from '../../interfaces/node-transformers/IObfuscatorReplacerWithStorage';
import { NodeObfuscatorsReplacers } from '../../enums/container/NodeObfuscatorsReplacers';
import { NodeType } from '../../enums/NodeType';
import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
import { IdentifierReplacer } from './replacers/IdentifierReplacer';
import { Node } from '../../node/Node';
import { NodeUtils } from '../../node/NodeUtils';
import { RandomGeneratorUtils } from '../../utils/RandomGeneratorUtils';
@@ -30,21 +30,21 @@ import { RandomGeneratorUtils } from '../../utils/RandomGeneratorUtils';
@injectable()
export class FunctionDeclarationObfuscator extends AbstractNodeTransformer {
/**
* @type {IdentifierReplacer}
* @type {IObfuscatorReplacerWithStorage}
*/
private readonly identifierReplacer: IdentifierReplacer;
private readonly identifierReplacer: IObfuscatorReplacerWithStorage;
/**
* @param nodeObfuscatorsReplacersFactory
* @param options
*/
constructor(
@inject(ServiceIdentifiers['Factory<IReplacer>']) nodeObfuscatorsReplacersFactory: (replacer: NodeObfuscatorsReplacers) => IReplacer,
@inject(ServiceIdentifiers['Factory<IObfuscatorReplacer>']) nodeObfuscatorsReplacersFactory: (replacer: NodeObfuscatorsReplacers) => IObfuscatorReplacer,
@inject(ServiceIdentifiers.IOptions) options: IOptions
) {
super(options);
this.identifierReplacer = <IdentifierReplacer>nodeObfuscatorsReplacersFactory(NodeObfuscatorsReplacers.IdentifierReplacer);
this.identifierReplacer = <IObfuscatorReplacerWithStorage>nodeObfuscatorsReplacersFactory(NodeObfuscatorsReplacers.IdentifierReplacer);
}
/**
@@ -52,11 +52,7 @@ export class FunctionDeclarationObfuscator extends AbstractNodeTransformer {
* @param parentNode
*/
public transformNode (functionDeclarationNode: ESTree.FunctionDeclaration, parentNode: ESTree.Node): void {
this.identifierReplacer.setPrefix(RandomGeneratorUtils.getRandomGenerator().string({
length: 5,
pool: RandomGeneratorUtils.randomGeneratorPool
}));
const nodeIdentifier: string = RandomGeneratorUtils.getRandomString(7);
const blockScopeOfFunctionDeclarationNode: TNodeWithBlockStatement = NodeUtils
.getBlockScopeOfNode(functionDeclarationNode);
@@ -64,27 +60,29 @@ export class FunctionDeclarationObfuscator extends AbstractNodeTransformer {
return;
}
this.storeFunctionName(functionDeclarationNode);
this.replaceFunctionName(blockScopeOfFunctionDeclarationNode);
this.storeFunctionName(functionDeclarationNode, nodeIdentifier);
this.replaceFunctionName(blockScopeOfFunctionDeclarationNode, nodeIdentifier);
}
/**
* @param functionDeclarationNode
* @param nodeIdentifier
*/
private storeFunctionName (functionDeclarationNode: ESTree.FunctionDeclaration): void {
private storeFunctionName (functionDeclarationNode: ESTree.FunctionDeclaration, nodeIdentifier: string): void {
NodeUtils.typedTraverse(functionDeclarationNode.id, NodeType.Identifier, {
enter: (node: ESTree.Identifier) => this.identifierReplacer.storeNames(node.name)
enter: (node: ESTree.Identifier) => this.identifierReplacer.storeNames(node.name, nodeIdentifier)
});
}
/**
* @param scopeNode
* @param nodeIdentifier
*/
private replaceFunctionName (scopeNode: ESTree.Node): void {
private replaceFunctionName (scopeNode: ESTree.Node, nodeIdentifier: string): void {
estraverse.replace(scopeNode, {
enter: (node: ESTree.Node, parentNode: ESTree.Node): any => {
if (Node.isReplaceableIdentifierNode(node, parentNode)) {
node.name = this.identifierReplacer.replace(node.name);
node.name = this.identifierReplacer.replace(node.name, nodeIdentifier);
}
}
});
@@ -5,13 +5,13 @@ import * as estraverse from 'estraverse';
import * as ESTree from 'estree';
import { IOptions } from '../../interfaces/options/IOptions';
import { IReplacer } from '../../interfaces/node-transformers/IReplacer';
import { IObfuscatorReplacer } from '../../interfaces/node-transformers/IObfuscatorReplacer';
import { IObfuscatorReplacerWithStorage } from '../../interfaces/node-transformers/IObfuscatorReplacerWithStorage';
import { NodeObfuscatorsReplacers } from '../../enums/container/NodeObfuscatorsReplacers';
import { NodeType } from '../../enums/NodeType';
import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
import { IdentifierReplacer } from './replacers/IdentifierReplacer';
import { Node } from '../../node/Node';
import { NodeUtils } from '../../node/NodeUtils';
import { RandomGeneratorUtils } from '../../utils/RandomGeneratorUtils';
@@ -27,56 +27,55 @@ import { RandomGeneratorUtils } from '../../utils/RandomGeneratorUtils';
@injectable()
export class FunctionObfuscator extends AbstractNodeTransformer {
/**
* @type {IdentifierReplacer}
* @type {IObfuscatorReplacerWithStorage}
*/
private readonly identifierReplacer: IdentifierReplacer;
private readonly identifierReplacer: IObfuscatorReplacerWithStorage;
/**
* @param nodeObfuscatorsReplacersFactory
* @param options
*/
constructor(
@inject(ServiceIdentifiers['Factory<IReplacer>']) nodeObfuscatorsReplacersFactory: (replacer: NodeObfuscatorsReplacers) => IReplacer,
@inject(ServiceIdentifiers['Factory<IObfuscatorReplacer>']) nodeObfuscatorsReplacersFactory: (replacer: NodeObfuscatorsReplacers) => IObfuscatorReplacer,
@inject(ServiceIdentifiers.IOptions) options: IOptions
) {
super(options);
this.identifierReplacer = <IdentifierReplacer>nodeObfuscatorsReplacersFactory(NodeObfuscatorsReplacers.IdentifierReplacer);
this.identifierReplacer = <IObfuscatorReplacerWithStorage>nodeObfuscatorsReplacersFactory(NodeObfuscatorsReplacers.IdentifierReplacer);
}
/**
* @param functionNode
*/
public transformNode (functionNode: ESTree.Function): void {
this.identifierReplacer.setPrefix(RandomGeneratorUtils.getRandomGenerator().string({
length: 5,
pool: RandomGeneratorUtils.randomGeneratorPool
}));
const nodeIdentifier: string = RandomGeneratorUtils.getRandomString(7);
this.storeFunctionParams(functionNode);
this.replaceFunctionParams(functionNode);
this.storeFunctionParams(functionNode, nodeIdentifier);
this.replaceFunctionParams(functionNode, nodeIdentifier);
}
/**
* @param functionNode
* @param nodeIdentifier
*/
private storeFunctionParams (functionNode: ESTree.Function): void {
private storeFunctionParams (functionNode: ESTree.Function, nodeIdentifier: string): void {
functionNode.params
.forEach((paramsNode: ESTree.Node) => {
NodeUtils.typedTraverse(paramsNode, NodeType.Identifier, {
enter: (node: ESTree.Identifier) => this.identifierReplacer.storeNames(node.name)
enter: (node: ESTree.Identifier) => this.identifierReplacer.storeNames(node.name, nodeIdentifier)
});
});
}
/**
* @param functionNode
* @param nodeIdentifier
*/
private replaceFunctionParams (functionNode: ESTree.Function): void {
private replaceFunctionParams (functionNode: ESTree.Function, nodeIdentifier: string): void {
let traverseVisitor: estraverse.Visitor = {
enter: (node: ESTree.Node, parentNode: ESTree.Node): any => {
if (Node.isReplaceableIdentifierNode(node, parentNode)) {
const newNodeName: string = this.identifierReplacer.replace(node.name);
const newNodeName: string = this.identifierReplacer.replace(node.name, nodeIdentifier);
if (node.name !== newNodeName) {
node.name = newNodeName;
@@ -5,13 +5,13 @@ import * as estraverse from 'estraverse';
import * as ESTree from 'estree';
import { IOptions } from '../../interfaces/options/IOptions';
import { IReplacer } from '../../interfaces/node-transformers/IReplacer';
import { IObfuscatorReplacer } from '../../interfaces/node-transformers/IObfuscatorReplacer';
import { IObfuscatorReplacerWithStorage } from '../../interfaces/node-transformers/IObfuscatorReplacerWithStorage';
import { NodeObfuscatorsReplacers } from '../../enums/container/NodeObfuscatorsReplacers';
import { NodeType } from '../../enums/NodeType';
import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
import { IdentifierReplacer } from './replacers/IdentifierReplacer';
import { Node } from '../../node/Node';
import { NodeUtils } from '../../node/NodeUtils';
import { RandomGeneratorUtils } from '../../utils/RandomGeneratorUtils';
@@ -35,53 +35,52 @@ import { RandomGeneratorUtils } from '../../utils/RandomGeneratorUtils';
@injectable()
export class LabeledStatementObfuscator extends AbstractNodeTransformer {
/**
* @type {IdentifierReplacer}
* @type {IObfuscatorReplacerWithStorage}
*/
private readonly identifierReplacer: IdentifierReplacer;
private readonly identifierReplacer: IObfuscatorReplacerWithStorage;
/**
* @param nodeObfuscatorsReplacersFactory
* @param options
*/
constructor(
@inject(ServiceIdentifiers['Factory<IReplacer>']) nodeObfuscatorsReplacersFactory: (replacer: NodeObfuscatorsReplacers) => IReplacer,
@inject(ServiceIdentifiers['Factory<IObfuscatorReplacer>']) nodeObfuscatorsReplacersFactory: (replacer: NodeObfuscatorsReplacers) => IObfuscatorReplacer,
@inject(ServiceIdentifiers.IOptions) options: IOptions
) {
super(options);
this.identifierReplacer = <IdentifierReplacer>nodeObfuscatorsReplacersFactory(NodeObfuscatorsReplacers.IdentifierReplacer);
this.identifierReplacer = <IObfuscatorReplacerWithStorage>nodeObfuscatorsReplacersFactory(NodeObfuscatorsReplacers.IdentifierReplacer);
}
/**
* @param labeledStatementNode
*/
public transformNode (labeledStatementNode: ESTree.LabeledStatement): void {
this.identifierReplacer.setPrefix(RandomGeneratorUtils.getRandomGenerator().string({
length: 5,
pool: RandomGeneratorUtils.randomGeneratorPool
}));
const nodeIdentifier: string = RandomGeneratorUtils.getRandomString(7);
this.storeLabeledStatementName(labeledStatementNode);
this.replaceLabeledStatementName(labeledStatementNode);
this.storeLabeledStatementName(labeledStatementNode, nodeIdentifier);
this.replaceLabeledStatementName(labeledStatementNode, nodeIdentifier);
}
/**
* @param labeledStatementNode
* @param nodeIdentifier
*/
private storeLabeledStatementName (labeledStatementNode: ESTree.LabeledStatement): void {
private storeLabeledStatementName (labeledStatementNode: ESTree.LabeledStatement, nodeIdentifier: string): void {
NodeUtils.typedTraverse(labeledStatementNode.label, NodeType.Identifier, {
enter: (node: ESTree.Identifier) => this.identifierReplacer.storeNames(node.name)
enter: (node: ESTree.Identifier) => this.identifierReplacer.storeNames(node.name, nodeIdentifier)
});
}
/**
* @param labeledStatementNode
* @param nodeIdentifier
*/
private replaceLabeledStatementName (labeledStatementNode: ESTree.LabeledStatement): void {
private replaceLabeledStatementName (labeledStatementNode: ESTree.LabeledStatement, nodeIdentifier: string): void {
estraverse.replace(labeledStatementNode, {
enter: (node: ESTree.Node, parentNode: ESTree.Node): any => {
if (Node.isLabelIdentifierNode(node, parentNode)) {
node.name = this.identifierReplacer.replace(node.name);
node.name = this.identifierReplacer.replace(node.name, nodeIdentifier);
}
}
});
@@ -5,7 +5,7 @@ import * as escodegen from 'escodegen';
import * as ESTree from 'estree';
import { IOptions } from '../../interfaces/options/IOptions';
import { IReplacer } from '../../interfaces/node-transformers/IReplacer';
import { IObfuscatorReplacer } from '../../interfaces/node-transformers/IObfuscatorReplacer';
import { NodeObfuscatorsReplacers } from '../../enums/container/NodeObfuscatorsReplacers';
@@ -15,16 +15,16 @@ import { Node } from '../../node/Node';
@injectable()
export class LiteralObfuscator extends AbstractNodeTransformer {
/**
* @type {(replacer: NodeObfuscatorsReplacers) => IReplacer}
* @type {(replacer: NodeObfuscatorsReplacers) => IObfuscatorReplacer}
*/
private readonly replacersFactory: (replacer: NodeObfuscatorsReplacers) => IReplacer;
private readonly replacersFactory: (replacer: NodeObfuscatorsReplacers) => IObfuscatorReplacer;
/**
* @param replacersFactory
* @param options
*/
constructor(
@inject(ServiceIdentifiers['Factory<IReplacer>']) replacersFactory: (replacer: NodeObfuscatorsReplacers) => IReplacer,
@inject(ServiceIdentifiers['Factory<IObfuscatorReplacer>']) replacersFactory: (replacer: NodeObfuscatorsReplacers) => IObfuscatorReplacer,
@inject(ServiceIdentifiers.IOptions) options: IOptions
) {
super(options);
@@ -6,7 +6,7 @@ import * as estraverse from 'estraverse';
import * as ESTree from 'estree';
import { IOptions } from '../../interfaces/options/IOptions';
import { IReplacer } from '../../interfaces/node-transformers/IReplacer';
import { IObfuscatorReplacer } from '../../interfaces/node-transformers/IObfuscatorReplacer';
import { NodeObfuscatorsReplacers } from '../../enums/container/NodeObfuscatorsReplacers';
import { NodeType } from '../../enums/NodeType';
@@ -17,16 +17,16 @@ import { Node } from '../../node/Node';
@injectable()
export class MemberExpressionObfuscator extends AbstractNodeTransformer {
/**
* @type {IReplacer}
* @type {IObfuscatorReplacer}
*/
private readonly stringLiteralReplacer: IReplacer;
private readonly stringLiteralReplacer: IObfuscatorReplacer;
/**
* @param replacersFactory
* @param options
*/
constructor(
@inject(ServiceIdentifiers['Factory<IReplacer>']) replacersFactory: (replacer: NodeObfuscatorsReplacers) => IReplacer,
@inject(ServiceIdentifiers['Factory<IObfuscatorReplacer>']) replacersFactory: (replacer: NodeObfuscatorsReplacers) => IObfuscatorReplacer,
@inject(ServiceIdentifiers.IOptions) options: IOptions
) {
super(options);
@@ -5,7 +5,7 @@ import * as estraverse from 'estraverse';
import * as ESTree from 'estree';
import { IOptions } from '../../interfaces/options/IOptions';
import { IReplacer } from '../../interfaces/node-transformers/IReplacer';
import { IObfuscatorReplacer } from '../../interfaces/node-transformers/IObfuscatorReplacer';
import { NodeObfuscatorsReplacers } from '../../enums/container/NodeObfuscatorsReplacers';
@@ -23,9 +23,9 @@ import { Utils } from '../../utils/Utils';
@injectable()
export class MethodDefinitionObfuscator extends AbstractNodeTransformer {
/**
* @type {IReplacer}
* @type {IObfuscatorReplacer}
*/
private readonly stringLiteralReplacer: IReplacer;
private readonly stringLiteralReplacer: IObfuscatorReplacer;
/**
* @type {string[]}
@@ -37,7 +37,7 @@ export class MethodDefinitionObfuscator extends AbstractNodeTransformer {
* @param options
*/
constructor(
@inject(ServiceIdentifiers['Factory<IReplacer>']) replacersFactory: (replacer: NodeObfuscatorsReplacers) => IReplacer,
@inject(ServiceIdentifiers['Factory<IObfuscatorReplacer>']) replacersFactory: (replacer: NodeObfuscatorsReplacers) => IObfuscatorReplacer,
@inject(ServiceIdentifiers.IOptions) options: IOptions
) {
super(options);
@@ -7,13 +7,13 @@ import * as ESTree from 'estree';
import { TNodeWithBlockStatement } from '../../types/node/TNodeWithBlockStatement';
import { IOptions } from '../../interfaces/options/IOptions';
import { IReplacer } from '../../interfaces/node-transformers/IReplacer';
import { IObfuscatorReplacer } from '../../interfaces/node-transformers/IObfuscatorReplacer';
import { IObfuscatorReplacerWithStorage } from '../../interfaces/node-transformers/IObfuscatorReplacerWithStorage';
import { NodeObfuscatorsReplacers } from '../../enums/container/NodeObfuscatorsReplacers';
import { NodeType } from '../../enums/NodeType';
import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
import { IdentifierReplacer } from './replacers/IdentifierReplacer';
import { Node } from '../../node/Node';
import { NodeUtils } from '../../node/NodeUtils';
import { RandomGeneratorUtils } from '../../utils/RandomGeneratorUtils';
@@ -31,21 +31,21 @@ import { RandomGeneratorUtils } from '../../utils/RandomGeneratorUtils';
@injectable()
export class VariableDeclarationObfuscator extends AbstractNodeTransformer {
/**
* @type {IdentifierReplacer}
* @type {IObfuscatorReplacerWithStorage}
*/
private readonly identifierReplacer: IReplacer & IdentifierReplacer;
private readonly identifierReplacer: IObfuscatorReplacerWithStorage;
/**
* @param replacersFactory
* @param options
*/
constructor(
@inject(ServiceIdentifiers['Factory<IReplacer>']) replacersFactory: (replacer: NodeObfuscatorsReplacers) => IReplacer,
@inject(ServiceIdentifiers['Factory<IObfuscatorReplacer>']) replacersFactory: (replacer: NodeObfuscatorsReplacers) => IObfuscatorReplacer,
@inject(ServiceIdentifiers.IOptions) options: IOptions
) {
super(options);
this.identifierReplacer = <IdentifierReplacer>replacersFactory(NodeObfuscatorsReplacers.IdentifierReplacer);
this.identifierReplacer = <IObfuscatorReplacerWithStorage>replacersFactory(NodeObfuscatorsReplacers.IdentifierReplacer);
}
/**
@@ -53,11 +53,6 @@ export class VariableDeclarationObfuscator extends AbstractNodeTransformer {
* @param parentNode
*/
public transformNode (variableDeclarationNode: ESTree.VariableDeclaration, parentNode: ESTree.Node): void {
this.identifierReplacer.setPrefix(RandomGeneratorUtils.getRandomGenerator().string({
length: 5,
pool: RandomGeneratorUtils.randomGeneratorPool
}));
const blockScopeOfVariableDeclarationNode: TNodeWithBlockStatement = NodeUtils
.getBlockScopeOfNode(variableDeclarationNode);
@@ -65,34 +60,37 @@ export class VariableDeclarationObfuscator extends AbstractNodeTransformer {
return;
}
const nodeIdentifier: string = RandomGeneratorUtils.getRandomString(7);
const scopeNode: ESTree.Node = variableDeclarationNode.kind === 'var'
? blockScopeOfVariableDeclarationNode
: parentNode;
this.storeVariableNames(variableDeclarationNode);
this.replaceVariableNames(scopeNode);
this.storeVariableNames(variableDeclarationNode, nodeIdentifier);
this.replaceVariableNames(scopeNode, nodeIdentifier);
}
/**
* @param variableDeclarationNode
* @param nodeIdentifier
*/
private storeVariableNames (variableDeclarationNode: ESTree.VariableDeclaration): void {
private storeVariableNames (variableDeclarationNode: ESTree.VariableDeclaration, nodeIdentifier: string): void {
variableDeclarationNode.declarations
.forEach((declarationNode: ESTree.VariableDeclarator) => {
NodeUtils.typedTraverse(declarationNode.id, NodeType.Identifier, {
enter: (node: ESTree.Identifier) => this.identifierReplacer.storeNames(node.name)
enter: (node: ESTree.Identifier) => this.identifierReplacer.storeNames(node.name, nodeIdentifier)
});
});
}
/**
* @param scopeNode
* @param nodeIdentifier
*/
private replaceVariableNames (scopeNode: ESTree.Node): void {
private replaceVariableNames (scopeNode: ESTree.Node, nodeIdentifier: string): void {
estraverse.replace(scopeNode, {
enter: (node: ESTree.Node, parentNode: ESTree.Node): any => {
if (!node.obfuscated && Node.isReplaceableIdentifierNode(node, parentNode)) {
node.name = this.identifierReplacer.replace(node.name);
node.name = this.identifierReplacer.replace(node.name, nodeIdentifier);
}
}
});
@@ -2,10 +2,10 @@ import { injectable, inject } from 'inversify';
import { ServiceIdentifiers } from '../../../container/ServiceIdentifiers';
import { IOptions } from '../../../interfaces/options/IOptions';
import { IReplacer } from '../../../interfaces/node-transformers/IReplacer';
import { IObfuscatorReplacer } from '../../../interfaces/node-transformers/IObfuscatorReplacer';
@injectable()
export abstract class AbstractReplacer implements IReplacer {
export abstract class AbstractReplacer implements IObfuscatorReplacer {
/**
* @type {IOptions}
*/
@@ -22,7 +22,8 @@ export abstract class AbstractReplacer implements IReplacer {
/**
* @param nodeValue
* @param nodeIdentifier
* @returns {string}
*/
public abstract replace (nodeValue: any): string;
public abstract replace (nodeValue: any, nodeIdentifier?: string): string;
}
@@ -1,23 +1,19 @@
import { injectable, inject } from 'inversify';
import { ServiceIdentifiers } from '../../../container/ServiceIdentifiers';
import { IObfuscatorReplacerWithStorage } from '../../../interfaces/node-transformers/IObfuscatorReplacerWithStorage';
import { IOptions } from '../../../interfaces/options/IOptions';
import { AbstractReplacer } from './AbstractReplacer';
import { RandomGeneratorUtils } from '../../../utils/RandomGeneratorUtils';
@injectable()
export class IdentifierReplacer extends AbstractReplacer {
export class IdentifierReplacer extends AbstractReplacer implements IObfuscatorReplacerWithStorage {
/**
* @type {Map<string, string>}
*/
private readonly namesMap: Map<string, string> = new Map<string, string>();
/**
* @type {string}
*/
private uniquePrefix: string;
/**
* @param options
*/
@@ -29,10 +25,11 @@ export class IdentifierReplacer extends AbstractReplacer {
/**
* @param nodeValue
* @param nodeIdentifier
* @returns {string}
*/
public replace (nodeValue: string): string {
const obfuscatedIdentifierName: string|undefined = this.namesMap.get(`${nodeValue}-${this.uniquePrefix}`);
public replace (nodeValue: string, nodeIdentifier: string): string {
const obfuscatedIdentifierName: string|undefined = this.namesMap.get(`${nodeValue}-${nodeIdentifier}`);
if (!obfuscatedIdentifierName) {
return nodeValue;
@@ -41,26 +38,16 @@ export class IdentifierReplacer extends AbstractReplacer {
return obfuscatedIdentifierName;
}
/**
* @param uniquePrefix
*/
public setPrefix (uniquePrefix: string): void {
this.uniquePrefix = uniquePrefix
}
/**
* Store all identifiers names as keys in given `namesMap` with random names as value.
* Reserved names will be ignored.
*
* @param nodeName
* @param nodeIdentifier
*/
public storeNames (nodeName: string): void {
if (!this.uniquePrefix) {
throw new Error('`uniquePrefix` is `undefined`. Set it before `storeNames`');
}
public storeNames (nodeName: string, nodeIdentifier: string): void {
if (!this.isReservedName(nodeName)) {
this.namesMap.set(`${nodeName}-${this.uniquePrefix}`, RandomGeneratorUtils.getRandomVariableName());
this.namesMap.set(`${nodeName}-${nodeIdentifier}`, RandomGeneratorUtils.getRandomVariableName());
}
}
+9
View File
@@ -56,6 +56,15 @@ export class RandomGeneratorUtils {
});
}
/**
* @param length
* @param pool
* @returns {string}
*/
public static getRandomString (length: number, pool: string = RandomGeneratorUtils.randomGeneratorPool): string {
return RandomGeneratorUtils.getRandomGenerator().string({ length, pool });
}
/**
* @param length
* @returns {string}