javascript-obfuscator
Advanced tools
Comparing version
14
index.js
@@ -6,10 +6,11 @@ "use strict"; | ||
class JavaScriptObfuscator { | ||
static obfuscate(sourceCode, options = {}) { | ||
let astTree = esprima.parse(sourceCode), obfuscator = new Obfuscator_1.Obfuscator(options); | ||
static obfuscate(sourceCode, customOptions) { | ||
let astTree = esprima.parse(sourceCode), options = Object.assign(JavaScriptObfuscator.defaultOptions, customOptions), obfuscator = new Obfuscator_1.Obfuscator(options); | ||
obfuscator.obfuscateNode(astTree); | ||
return JavaScriptObfuscator.generateCode(astTree, options); | ||
} | ||
static generateCode(astTree, options = {}) { | ||
static generateCode(astTree, options) { | ||
let escodegenParams = Object.assign({}, JavaScriptObfuscator.escodegenParams); | ||
if (options.hasOwnProperty('compact')) { | ||
escodegenParams.format = {}; | ||
escodegenParams.format.compact = options.compact; | ||
@@ -20,6 +21,7 @@ } | ||
} | ||
JavaScriptObfuscator.defaultOptions = { | ||
compact: true, | ||
rotateUnicodeArray: true | ||
}; | ||
JavaScriptObfuscator.escodegenParams = { | ||
format: { | ||
compact: true | ||
}, | ||
verbatim: 'x-verbatim-property' | ||
@@ -26,0 +28,0 @@ }; |
16
index.ts
@@ -11,2 +11,7 @@ "use strict"; | ||
export class JavaScriptObfuscator { | ||
private static defaultOptions: any = { | ||
compact: true, | ||
rotateUnicodeArray: true | ||
}; | ||
/** | ||
@@ -16,5 +21,2 @@ * @type any | ||
private static escodegenParams: any = { | ||
format: { | ||
compact: true | ||
}, | ||
verbatim: 'x-verbatim-property' | ||
@@ -25,6 +27,7 @@ }; | ||
* @param sourceCode | ||
* @param options | ||
* @param customOptions | ||
*/ | ||
public static obfuscate (sourceCode: string, options: any = {}): string { | ||
public static obfuscate (sourceCode: string, customOptions: any): string { | ||
let astTree: IProgramNode = esprima.parse(sourceCode), | ||
options: any = Object.assign(JavaScriptObfuscator.defaultOptions, customOptions), | ||
obfuscator: Obfuscator = new Obfuscator(options); | ||
@@ -41,6 +44,7 @@ | ||
*/ | ||
private static generateCode (astTree: IProgramNode, options: any = {}): string { | ||
private static generateCode (astTree: IProgramNode, options: any): string { | ||
let escodegenParams: any = Object.assign({}, JavaScriptObfuscator.escodegenParams); | ||
if (options.hasOwnProperty('compact')) { | ||
escodegenParams.format = {}; | ||
escodegenParams.format.compact = options.compact; | ||
@@ -47,0 +51,0 @@ } |
{ | ||
"name": "javascript-obfuscator", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "JavaScript obfuscator", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -8,4 +8,2 @@ #JavaScript obfuscator for Node.js | ||
##Important: right now obfuscated code may not work after minification with UglifyJS (tested on Angular 2 bundle). I will try to fix that soon or later. | ||
###Installation | ||
@@ -66,2 +64,7 @@ | ||
###Available options | ||
####compact | ||
Type: `boolean` Default: `true` | ||
Compact code output in one line. | ||
####rotateUnicodeArray | ||
@@ -68,0 +71,0 @@ Type: `boolean` Default: `true` |
@@ -38,9 +38,4 @@ "use strict"; | ||
let scopeNode; | ||
if (variableDeclarationNode.kind === 'var') { | ||
scopeNode = NodeUtils_1.NodeUtils.getNodeScope(variableDeclarationNode); | ||
} | ||
else { | ||
scopeNode = variableParentNode; | ||
} | ||
let isNodeAfterVariableDeclaratorFlag = false, isNodeBeforeVariableDeclaratorFlag = true, functionParentScope, functionNextNode, functionIndex = -1; | ||
scopeNode = variableDeclarationNode.kind === 'var' ? NodeUtils_1.NodeUtils.getNodeScope(variableDeclarationNode) : variableParentNode; | ||
let isNodeAfterVariableDeclaratorFlag = false; | ||
estraverse.replace(scopeNode, { | ||
@@ -51,19 +46,10 @@ enter: (node, parentNode) => { | ||
node.type === 'ArrowFunctionExpression') { | ||
functionParentScope = NodeUtils_1.NodeUtils.getNodeScope(node); | ||
if (NodeUtils_1.NodeUtils.isBlockStatementNode(functionParentScope)) { | ||
functionIndex = functionParentScope.body.indexOf(node); | ||
if (functionIndex >= 0) { | ||
functionNextNode = functionParentScope.body[functionIndex + 1]; | ||
estraverse.replace(node, { | ||
enter: (node, parentNode) => { | ||
this.replaceNodeIdentifierByNewValue(node, parentNode, this.variableNames); | ||
} | ||
} | ||
isNodeAfterVariableDeclaratorFlag = true; | ||
}); | ||
} | ||
if (functionNextNode && isNodeBeforeVariableDeclaratorFlag && node === functionNextNode) { | ||
isNodeAfterVariableDeclaratorFlag = false; | ||
functionNextNode = undefined; | ||
functionIndex = -1; | ||
} | ||
if (node === variableDeclarationNode) { | ||
isNodeAfterVariableDeclaratorFlag = true; | ||
isNodeBeforeVariableDeclaratorFlag = false; | ||
} | ||
@@ -70,0 +56,0 @@ if (isNodeAfterVariableDeclaratorFlag) { |
@@ -71,17 +71,8 @@ import * as estraverse from 'estraverse'; | ||
if (variableDeclarationNode.kind === 'var') { | ||
scopeNode = NodeUtils.getNodeScope( | ||
variableDeclarationNode | ||
); | ||
} else { | ||
scopeNode = variableParentNode; | ||
} | ||
scopeNode = variableDeclarationNode.kind === 'var' ? NodeUtils.getNodeScope( | ||
variableDeclarationNode | ||
) : variableParentNode; | ||
let isNodeAfterVariableDeclaratorFlag: boolean = false, | ||
isNodeBeforeVariableDeclaratorFlag: boolean = true, | ||
functionParentScope: ITreeNode, | ||
functionNextNode: ITreeNode, | ||
functionIndex: number = -1; | ||
let isNodeAfterVariableDeclaratorFlag: boolean = false; | ||
//TODO: REFACTOR THIS | ||
estraverse.replace(scopeNode, { | ||
@@ -94,24 +85,11 @@ enter: (node: ITreeNode, parentNode: ITreeNode) => { | ||
) { | ||
functionParentScope = NodeUtils.getNodeScope(node); | ||
if (NodeUtils.isBlockStatementNode(functionParentScope)) { | ||
functionIndex = functionParentScope.body.indexOf(node); | ||
if (functionIndex >= 0) { | ||
functionNextNode = functionParentScope.body[functionIndex + 1]; | ||
estraverse.replace(node, { | ||
enter: (node: ITreeNode, parentNode: ITreeNode) => { | ||
this.replaceNodeIdentifierByNewValue(node, parentNode, this.variableNames); | ||
} | ||
} | ||
isNodeAfterVariableDeclaratorFlag = true; | ||
}); | ||
} | ||
if (functionNextNode && isNodeBeforeVariableDeclaratorFlag && node === functionNextNode) { | ||
isNodeAfterVariableDeclaratorFlag = false; | ||
functionNextNode = undefined; | ||
functionIndex = -1; | ||
} | ||
if (node === variableDeclarationNode) { | ||
isNodeAfterVariableDeclaratorFlag = true; | ||
isNodeBeforeVariableDeclaratorFlag = false; | ||
} | ||
@@ -125,2 +103,2 @@ | ||
} | ||
} | ||
} |
var JavaScriptObfuscator = require('../index.js'); | ||
var obfuscatedCode = JavaScriptObfuscator.obfuscate(` | ||
(function(){ | ||
(function(){ | ||
var result = 1, | ||
@@ -5,0 +5,0 @@ term1 = 0, |
@@ -5,3 +5,3 @@ var JavaScriptObfuscator = require('../index.js'); | ||
` | ||
(function(){ | ||
(function(){ | ||
var result = 1, | ||
@@ -8,0 +8,0 @@ term1 = 0, |
77
4.05%243556
-0.55%5885
-0.44%