refactoring

This commit is contained in:
sanex3339
2016-11-28 13:25:53 +03:00
parent 954c00d232
commit 501b914be6
4 changed files with 34 additions and 43 deletions
+13 -13
View File
@@ -88,7 +88,7 @@ module.exports =
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 105);
/******/ return __webpack_require__(__webpack_require__.s = 106);
/******/ })
/************************************************************************/
/******/ ([
@@ -1576,6 +1576,7 @@ var esprima = __webpack_require__(27);
var escodegen = __webpack_require__(11);
var chance_1 = __webpack_require__(26);
var CustomNodesStorage_1 = __webpack_require__(78);
var NodeUtils_1 = __webpack_require__(8);
var ObfuscationEventEmitter_1 = __webpack_require__(53);
var ObfuscationResult_1 = __webpack_require__(20);
var Obfuscator_1 = __webpack_require__(31);
@@ -1617,7 +1618,10 @@ var JavaScriptObfuscatorInternal = function () {
Utils_1.Utils.setRandomGenerator(new chance_1.Chance(this.options.seed));
}
var astTree = esprima.parse(sourceCode, JavaScriptObfuscatorInternal.esprimaParams);
var obfuscatedAstTree = new Obfuscator_1.Obfuscator(new ObfuscationEventEmitter_1.ObfuscationEventEmitter(), new StackTraceAnalyzer_1.StackTraceAnalyzer(), new CustomNodesStorage_1.CustomNodesStorage(this.options), this.options).obfuscateAstTree(astTree);
NodeUtils_1.NodeUtils.parentize(astTree);
var customNodesStorage = new CustomNodesStorage_1.CustomNodesStorage(this.options);
customNodesStorage.initialize(new StackTraceAnalyzer_1.StackTraceAnalyzer().analyze(astTree.body));
var obfuscatedAstTree = new Obfuscator_1.Obfuscator(new ObfuscationEventEmitter_1.ObfuscationEventEmitter(), this.options).obfuscateAstTree(astTree, customNodesStorage);
var generatorOutput = this.generateCode(sourceCode, obfuscatedAstTree);
return this.getObfuscationResult(generatorOutput);
}
@@ -1654,36 +1658,31 @@ var VisitorDirection_1 = __webpack_require__(52);
var NodeControlFlowTransformersFactory_1 = __webpack_require__(57);
var NodeObfuscatorsFactory_1 = __webpack_require__(67);
var Node_1 = __webpack_require__(2);
var NodeUtils_1 = __webpack_require__(8);
var Obfuscator = function () {
function Obfuscator(obfuscationEventEmitter, stackTraceAnalyzer, customNodesStorage, options) {
function Obfuscator(obfuscationEventEmitter, options) {
_classCallCheck(this, Obfuscator);
this.obfuscationEventEmitter = obfuscationEventEmitter;
this.stackTraceAnalyzer = stackTraceAnalyzer;
this.customNodesStorage = customNodesStorage;
this.options = options;
}
_createClass(Obfuscator, [{
key: 'obfuscateAstTree',
value: function obfuscateAstTree(astTree) {
value: function obfuscateAstTree(astTree, customNodesStorage) {
var _this = this;
if (Node_1.Node.isProgramNode(astTree) && !astTree.body.length) {
return astTree;
}
NodeUtils_1.NodeUtils.parentize(astTree);
this.customNodesStorage.initialize(this.stackTraceAnalyzer.analyze(astTree.body));
this.customNodesStorage.getStorage().forEach(function (customNode) {
customNodesStorage.getStorage().forEach(function (customNode) {
_this.obfuscationEventEmitter.once(customNode.getAppendEvent(), customNode.appendNode.bind(customNode));
});
this.obfuscationEventEmitter.emit(ObfuscationEvents_1.ObfuscationEvents.BeforeObfuscation, astTree);
if (this.options.controlFlowFlattening) {
this.transformAstTree(astTree, VisitorDirection_1.VisitorDirection.leave, new NodeControlFlowTransformersFactory_1.NodeControlFlowTransformersFactory(this.customNodesStorage, this.options));
this.transformAstTree(astTree, VisitorDirection_1.VisitorDirection.leave, new NodeControlFlowTransformersFactory_1.NodeControlFlowTransformersFactory(customNodesStorage, this.options));
}
this.transformAstTree(astTree, VisitorDirection_1.VisitorDirection.enter, new NodeObfuscatorsFactory_1.NodeObfuscatorsFactory(this.customNodesStorage, this.options));
this.transformAstTree(astTree, VisitorDirection_1.VisitorDirection.enter, new NodeObfuscatorsFactory_1.NodeObfuscatorsFactory(customNodesStorage, this.options));
this.obfuscationEventEmitter.emit(ObfuscationEvents_1.ObfuscationEvents.AfterObfuscation, astTree);
return astTree;
}
@@ -5161,7 +5160,8 @@ module.exports = require("is-equal");
module.exports = require("mkdirp");
/***/ },
/* 105 */
/* 105 */,
/* 106 */
/***/ function(module, exports, __webpack_require__) {
"use strict";
+12 -3
View File
@@ -4,11 +4,14 @@ import * as ESTree from 'estree';
import { Chance } from 'chance';
import { ICustomNode } from './interfaces/custom-nodes/ICustomNode';
import { IGeneratorOutput } from './interfaces/IGeneratorOutput';
import { IObfuscationResult } from './interfaces/IObfuscationResult';
import { IOptions } from './interfaces/IOptions';
import { IStorage } from './interfaces/IStorage';
import { CustomNodesStorage } from './storages/custom-nodes/CustomNodesStorage';
import { NodeUtils } from './node/NodeUtils';
import { ObfuscationEventEmitter } from './event-emitters/ObfuscationEventEmitter';
import { ObfuscationResult } from './ObfuscationResult';
import { Obfuscator } from './Obfuscator';
@@ -97,13 +100,19 @@ export class JavaScriptObfuscatorInternal {
// parse AST tree
const astTree: ESTree.Program = esprima.parse(sourceCode, JavaScriptObfuscatorInternal.esprimaParams);
NodeUtils.parentize(astTree);
// obfuscate AST tree
const customNodesStorage: IStorage<ICustomNode> = new CustomNodesStorage(this.options);
customNodesStorage.initialize(
new StackTraceAnalyzer().analyze(astTree.body)
);
const obfuscatedAstTree: ESTree.Program = new Obfuscator(
new ObfuscationEventEmitter(),
new StackTraceAnalyzer(),
new CustomNodesStorage(this.options),
this.options
).obfuscateAstTree(astTree);
).obfuscateAstTree(astTree, customNodesStorage);
// generate code
const generatorOutput: IGeneratorOutput = this.generateCode(sourceCode, obfuscatedAstTree);
+5 -26
View File
@@ -9,7 +9,6 @@ import { IObfuscator } from './interfaces/IObfuscator';
import { IOptions } from './interfaces/IOptions';
import { INodeTransformer } from './interfaces/INodeTransformer';
import { INodeTransformersFactory } from './interfaces/INodeTransformersFactory';
import { IStackTraceAnalyzer } from './interfaces/stack-trace-analyzer/IStackTraceAnalyzer';
import { IStorage } from './interfaces/IStorage';
import { ObfuscationEvents } from './enums/ObfuscationEvents';
@@ -19,14 +18,8 @@ import { NodeControlFlowTransformersFactory } from './node-transformers/node-con
import { NodeObfuscatorsFactory } from './node-transformers/node-obfuscators/factory/NodeObfuscatorsFactory';
import { Node } from './node/Node';
import { NodeUtils } from './node/NodeUtils';
export class Obfuscator implements IObfuscator {
/**
* @type {IStorage<ICustomNode>}
*/
private readonly customNodesStorage: IStorage<ICustomNode>;
/**
* @type {IObfuscationEventEmitter}
*/
@@ -37,44 +30,30 @@ export class Obfuscator implements IObfuscator {
*/
private readonly options: IOptions;
/**
* @type {IStackTraceAnalyzer}
*/
private readonly stackTraceAnalyzer: IStackTraceAnalyzer;
/**
* @param obfuscationEventEmitter
* @param stackTraceAnalyzer
* @param customNodesStorage
* @param options
*/
constructor (
obfuscationEventEmitter: IObfuscationEventEmitter,
stackTraceAnalyzer: IStackTraceAnalyzer,
customNodesStorage: IStorage<ICustomNode>,
options: IOptions
) {
this.obfuscationEventEmitter = obfuscationEventEmitter;
this.stackTraceAnalyzer = stackTraceAnalyzer;
this.customNodesStorage = customNodesStorage;
this.options = options;
}
/**
* @param astTree
* @param customNodesStorage
* @returns {ESTree.Program}
*/
public obfuscateAstTree (astTree: ESTree.Program): ESTree.Program {
public obfuscateAstTree (astTree: ESTree.Program, customNodesStorage: IStorage<ICustomNode>): ESTree.Program {
if (Node.isProgramNode(astTree) && !astTree.body.length) {
return astTree;
}
// zero pass: parentize all nodes
NodeUtils.parentize(astTree);
// prepare custom nodes
this.customNodesStorage.initialize(this.stackTraceAnalyzer.analyze(astTree.body));
this.customNodesStorage
customNodesStorage
.getStorage()
.forEach((customNode: ICustomNode) => {
this.obfuscationEventEmitter.once(customNode.getAppendEvent(), customNode.appendNode.bind(customNode));
@@ -85,14 +64,14 @@ export class Obfuscator implements IObfuscator {
// first pass: control flow flattening
if (this.options.controlFlowFlattening) {
this.transformAstTree(astTree, VisitorDirection.leave, new NodeControlFlowTransformersFactory(
this.customNodesStorage,
customNodesStorage,
this.options
));
}
// second pass: nodes obfuscation
this.transformAstTree(astTree, VisitorDirection.enter, new NodeObfuscatorsFactory(
this.customNodesStorage,
customNodesStorage,
this.options
));
+4 -1
View File
@@ -1,5 +1,8 @@
import * as ESTree from 'estree';
import { ICustomNode } from './custom-nodes/ICustomNode';
import { IStorage } from './IStorage';
export interface IObfuscator {
obfuscateAstTree (astTree: ESTree.Program): ESTree.Program;
obfuscateAstTree (astTree: ESTree.Program, customNodesStorage: IStorage<ICustomNode>): ESTree.Program;
}