mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
WIP
This commit is contained in:
Vendored
+23
-18
@@ -718,15 +718,24 @@ var IdentifierReplacer = function (_AbstractReplacer_1$A) {
|
||||
_inherits(IdentifierReplacer, _AbstractReplacer_1$A);
|
||||
|
||||
function IdentifierReplacer() {
|
||||
var _Object$getPrototypeO;
|
||||
|
||||
_classCallCheck(this, IdentifierReplacer);
|
||||
|
||||
return _possibleConstructorReturn(this, Object.getPrototypeOf(IdentifierReplacer).apply(this, arguments));
|
||||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
|
||||
args[_key] = arguments[_key];
|
||||
}
|
||||
|
||||
var _this = _possibleConstructorReturn(this, (_Object$getPrototypeO = Object.getPrototypeOf(IdentifierReplacer)).call.apply(_Object$getPrototypeO, [this].concat(args)));
|
||||
|
||||
_this.namesMap = new Map();
|
||||
return _this;
|
||||
}
|
||||
|
||||
_createClass(IdentifierReplacer, [{
|
||||
key: "replace",
|
||||
value: function replace(nodeValue, namesMap) {
|
||||
var obfuscatedIdentifierName = namesMap.get(nodeValue);
|
||||
value: function replace(nodeValue) {
|
||||
var obfuscatedIdentifierName = this.namesMap.get(nodeValue);
|
||||
if (!obfuscatedIdentifierName) {
|
||||
return nodeValue;
|
||||
}
|
||||
@@ -734,9 +743,9 @@ var IdentifierReplacer = function (_AbstractReplacer_1$A) {
|
||||
}
|
||||
}, {
|
||||
key: "storeNames",
|
||||
value: function storeNames(nodeName, namesMap) {
|
||||
value: function storeNames(nodeName) {
|
||||
if (!this.isReservedName(nodeName)) {
|
||||
namesMap.set(nodeName, Utils_1.Utils.getRandomVariableName());
|
||||
this.namesMap.set(nodeName, Utils_1.Utils.getRandomVariableName());
|
||||
}
|
||||
}
|
||||
}, {
|
||||
@@ -2387,7 +2396,6 @@ var CatchClauseObfuscator = function (_AbstractNodeObfuscat) {
|
||||
|
||||
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(CatchClauseObfuscator).call(this, nodes, options));
|
||||
|
||||
_this.catchClauseParam = new Map();
|
||||
_this.identifierReplacer = new IdentifierReplacer_1.IdentifierReplacer(_this.nodes, _this.options);
|
||||
return _this;
|
||||
}
|
||||
@@ -2405,7 +2413,7 @@ var CatchClauseObfuscator = function (_AbstractNodeObfuscat) {
|
||||
|
||||
NodeUtils_1.NodeUtils.typedReplace(catchClauseNode.param, NodeType_1.NodeType.Identifier, {
|
||||
leave: function leave(node) {
|
||||
_this2.identifierReplacer.storeNames(node.name, _this2.catchClauseParam);
|
||||
_this2.identifierReplacer.storeNames(node.name);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -2417,7 +2425,7 @@ var CatchClauseObfuscator = function (_AbstractNodeObfuscat) {
|
||||
estraverse.replace(catchClauseNode, {
|
||||
leave: function leave(node, parentNode) {
|
||||
if (Nodes_1.Nodes.isReplaceableIdentifierNode(node, parentNode)) {
|
||||
node.name = _this3.identifierReplacer.replace(node.name, _this3.catchClauseParam);
|
||||
node.name = _this3.identifierReplacer.replace(node.name);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -2459,7 +2467,6 @@ var FunctionDeclarationObfuscator = function (_AbstractNodeObfuscat) {
|
||||
|
||||
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(FunctionDeclarationObfuscator).call(this, nodes, options));
|
||||
|
||||
_this.functionName = new Map();
|
||||
_this.identifierReplacer = new IdentifierReplacer_1.IdentifierReplacer(_this.nodes, _this.options);
|
||||
return _this;
|
||||
}
|
||||
@@ -2480,7 +2487,7 @@ var FunctionDeclarationObfuscator = function (_AbstractNodeObfuscat) {
|
||||
|
||||
NodeUtils_1.NodeUtils.typedReplace(functionDeclarationNode.id, NodeType_1.NodeType.Identifier, {
|
||||
leave: function leave(node) {
|
||||
_this2.identifierReplacer.storeNames(node.name, _this2.functionName);
|
||||
_this2.identifierReplacer.storeNames(node.name);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -2493,7 +2500,7 @@ var FunctionDeclarationObfuscator = function (_AbstractNodeObfuscat) {
|
||||
estraverse.replace(scopeNode, {
|
||||
enter: function enter(node, parentNode) {
|
||||
if (Nodes_1.Nodes.isReplaceableIdentifierNode(node, parentNode)) {
|
||||
node.name = _this3.identifierReplacer.replace(node.name, _this3.functionName);
|
||||
node.name = _this3.identifierReplacer.replace(node.name);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -2535,7 +2542,6 @@ var FunctionObfuscator = function (_AbstractNodeObfuscat) {
|
||||
|
||||
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(FunctionObfuscator).call(this, nodes, options));
|
||||
|
||||
_this.functionParams = new Map();
|
||||
_this.identifierReplacer = new IdentifierReplacer_1.IdentifierReplacer(_this.nodes, _this.options);
|
||||
return _this;
|
||||
}
|
||||
@@ -2554,7 +2560,7 @@ var FunctionObfuscator = function (_AbstractNodeObfuscat) {
|
||||
functionNode.params.forEach(function (paramsNode) {
|
||||
NodeUtils_1.NodeUtils.typedReplace(paramsNode, NodeType_1.NodeType.Identifier, {
|
||||
leave: function leave(node) {
|
||||
_this2.identifierReplacer.storeNames(node.name, _this2.functionParams);
|
||||
_this2.identifierReplacer.storeNames(node.name);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -2567,7 +2573,7 @@ var FunctionObfuscator = function (_AbstractNodeObfuscat) {
|
||||
var replaceVisitor = {
|
||||
leave: function leave(node, parentNode) {
|
||||
if (Nodes_1.Nodes.isReplaceableIdentifierNode(node, parentNode)) {
|
||||
node.name = _this3.identifierReplacer.replace(node.name, _this3.functionParams);
|
||||
node.name = _this3.identifierReplacer.replace(node.name);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -2916,7 +2922,6 @@ var VariableDeclarationObfuscator = function (_AbstractNodeObfuscat) {
|
||||
|
||||
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(VariableDeclarationObfuscator).call(this, nodes, options));
|
||||
|
||||
_this.variableNames = new Map();
|
||||
_this.identifierReplacer = new IdentifierReplacer_1.IdentifierReplacer(_this.nodes, _this.options);
|
||||
return _this;
|
||||
}
|
||||
@@ -2938,7 +2943,7 @@ var VariableDeclarationObfuscator = function (_AbstractNodeObfuscat) {
|
||||
variableDeclarationNode.declarations.forEach(function (declarationNode) {
|
||||
NodeUtils_1.NodeUtils.typedReplace(declarationNode.id, NodeType_1.NodeType.Identifier, {
|
||||
leave: function leave(node) {
|
||||
_this2.identifierReplacer.storeNames(node.name, _this2.variableNames);
|
||||
_this2.identifierReplacer.storeNames(node.name);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -2956,7 +2961,7 @@ var VariableDeclarationObfuscator = function (_AbstractNodeObfuscat) {
|
||||
estraverse.replace(node, {
|
||||
enter: function enter(node, parentNode) {
|
||||
if (Nodes_1.Nodes.isReplaceableIdentifierNode(node, parentNode)) {
|
||||
node.name = _this3.identifierReplacer.replace(node.name, _this3.variableNames);
|
||||
node.name = _this3.identifierReplacer.replace(node.name);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -2965,7 +2970,7 @@ var VariableDeclarationObfuscator = function (_AbstractNodeObfuscat) {
|
||||
isNodeAfterVariableDeclaratorFlag = true;
|
||||
}
|
||||
if (Nodes_1.Nodes.isReplaceableIdentifierNode(node, parentNode) && isNodeAfterVariableDeclaratorFlag) {
|
||||
node.name = _this3.identifierReplacer.replace(node.name, _this3.variableNames);
|
||||
node.name = _this3.identifierReplacer.replace(node.name);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -5,7 +5,6 @@ import { ICustomNode } from "../interfaces/custom-nodes/ICustomNode";
|
||||
import { IIdentifierNode } from "../interfaces/nodes/IIdentifierNode";
|
||||
import { INode } from '../interfaces/nodes/INode';
|
||||
import { IOptions } from "../interfaces/IOptions";
|
||||
import { IReplacer } from "../interfaces/IReplacer";
|
||||
|
||||
import { NodeType } from "../enums/NodeType";
|
||||
|
||||
@@ -24,14 +23,9 @@ import { NodeUtils } from "../NodeUtils";
|
||||
*/
|
||||
export class CatchClauseObfuscator extends AbstractNodeObfuscator {
|
||||
/**
|
||||
* @type {Map<string, string>}
|
||||
* @type {IdentifierReplacer}
|
||||
*/
|
||||
private catchClauseParam: Map <string, string> = new Map <string, string> ();
|
||||
|
||||
/**
|
||||
* @type {IReplacer&IdentifierReplacer}
|
||||
*/
|
||||
private identifierReplacer: IReplacer&IdentifierReplacer;
|
||||
private identifierReplacer: IdentifierReplacer;
|
||||
|
||||
/**
|
||||
* @param nodes
|
||||
@@ -57,7 +51,7 @@ export class CatchClauseObfuscator extends AbstractNodeObfuscator {
|
||||
private storeCatchClauseParam (catchClauseNode: ICatchClauseNode): void {
|
||||
NodeUtils.typedReplace(catchClauseNode.param, NodeType.Identifier, {
|
||||
leave: (node: IIdentifierNode) => {
|
||||
this.identifierReplacer.storeNames(node.name, this.catchClauseParam)
|
||||
this.identifierReplacer.storeNames(node.name)
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -69,7 +63,7 @@ export class CatchClauseObfuscator extends AbstractNodeObfuscator {
|
||||
estraverse.replace(catchClauseNode, {
|
||||
leave: (node: INode, parentNode: INode): any => {
|
||||
if (Nodes.isReplaceableIdentifierNode(node, parentNode)) {
|
||||
node.name = this.identifierReplacer.replace(node.name, this.catchClauseParam);
|
||||
node.name = this.identifierReplacer.replace(node.name);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -5,7 +5,6 @@ import { IFunctionDeclarationNode } from "../interfaces/nodes/IFunctionDeclarati
|
||||
import { IIdentifierNode } from "../interfaces/nodes/IIdentifierNode";
|
||||
import { INode } from "../interfaces/nodes/INode";
|
||||
import { IOptions } from "../interfaces/IOptions";
|
||||
import { IReplacer } from "../interfaces/IReplacer";
|
||||
|
||||
import { NodeType } from "../enums/NodeType";
|
||||
|
||||
@@ -25,14 +24,9 @@ import { NodeUtils } from "../NodeUtils";
|
||||
*/
|
||||
export class FunctionDeclarationObfuscator extends AbstractNodeObfuscator {
|
||||
/**
|
||||
* @type {Map<string, string>}
|
||||
* @type {IdentifierReplacer}
|
||||
*/
|
||||
private functionName: Map <string, string> = new Map <string, string> ();
|
||||
|
||||
/**
|
||||
* @type {IReplacer&IdentifierReplacer}
|
||||
*/
|
||||
private identifierReplacer: IReplacer&IdentifierReplacer;
|
||||
private identifierReplacer: IdentifierReplacer;
|
||||
|
||||
/**
|
||||
* @param nodes
|
||||
@@ -63,7 +57,7 @@ export class FunctionDeclarationObfuscator extends AbstractNodeObfuscator {
|
||||
private storeFunctionName (functionDeclarationNode: IFunctionDeclarationNode): void {
|
||||
NodeUtils.typedReplace(functionDeclarationNode.id, NodeType.Identifier, {
|
||||
leave: (node: IIdentifierNode) => {
|
||||
this.identifierReplacer.storeNames(node.name, this.functionName)
|
||||
this.identifierReplacer.storeNames(node.name)
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -79,7 +73,7 @@ export class FunctionDeclarationObfuscator extends AbstractNodeObfuscator {
|
||||
estraverse.replace(scopeNode, {
|
||||
enter: (node: INode, parentNode: INode): any => {
|
||||
if (Nodes.isReplaceableIdentifierNode(node, parentNode)) {
|
||||
node.name = this.identifierReplacer.replace(node.name, this.functionName);
|
||||
node.name = this.identifierReplacer.replace(node.name);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -5,7 +5,6 @@ import { IFunctionNode } from "../interfaces/nodes/IFunctionNode";
|
||||
import { IIdentifierNode } from "../interfaces/nodes/IIdentifierNode";
|
||||
import { INode } from "../interfaces/nodes/INode";
|
||||
import { IOptions } from "../interfaces/IOptions";
|
||||
import { IReplacer } from "../interfaces/IReplacer";
|
||||
|
||||
import { NodeType } from "../enums/NodeType";
|
||||
|
||||
@@ -24,14 +23,9 @@ import { NodeUtils } from "../NodeUtils";
|
||||
*/
|
||||
export class FunctionObfuscator extends AbstractNodeObfuscator {
|
||||
/**
|
||||
* @type {Map<string, string>}
|
||||
* @type {IdentifierReplacer}
|
||||
*/
|
||||
private functionParams: Map <string, string> = new Map <string, string> ();
|
||||
|
||||
/**
|
||||
* @type {IReplacer&IdentifierReplacer}
|
||||
*/
|
||||
private identifierReplacer: IReplacer&IdentifierReplacer;
|
||||
private identifierReplacer: IdentifierReplacer;
|
||||
|
||||
/**
|
||||
* @param nodes
|
||||
@@ -59,7 +53,7 @@ export class FunctionObfuscator extends AbstractNodeObfuscator {
|
||||
.forEach((paramsNode: INode) => {
|
||||
NodeUtils.typedReplace(paramsNode, NodeType.Identifier, {
|
||||
leave: (node: IIdentifierNode) => {
|
||||
this.identifierReplacer.storeNames(node.name, this.functionParams)
|
||||
this.identifierReplacer.storeNames(node.name)
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -72,7 +66,7 @@ export class FunctionObfuscator extends AbstractNodeObfuscator {
|
||||
let replaceVisitor: estraverse.Visitor = {
|
||||
leave: (node: INode, parentNode: INode): any => {
|
||||
if (Nodes.isReplaceableIdentifierNode(node, parentNode)) {
|
||||
node.name = this.identifierReplacer.replace(node.name, this.functionParams);
|
||||
node.name = this.identifierReplacer.replace(node.name);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -4,7 +4,6 @@ import { ICustomNode } from "../interfaces/custom-nodes/ICustomNode";
|
||||
import { IIdentifierNode } from "../interfaces/nodes/IIdentifierNode";
|
||||
import { INode } from "../interfaces/nodes/INode";
|
||||
import { IOptions } from "../interfaces/IOptions";
|
||||
import { IReplacer } from "../interfaces/IReplacer";
|
||||
import { IVariableDeclarationNode } from "../interfaces/nodes/IVariableDeclarationNode";
|
||||
import { IVariableDeclaratorNode } from "../interfaces/nodes/IVariableDeclaratorNode";
|
||||
|
||||
@@ -27,14 +26,9 @@ import { NodeUtils } from "../NodeUtils";
|
||||
*/
|
||||
export class VariableDeclarationObfuscator extends AbstractNodeObfuscator {
|
||||
/**
|
||||
* @type {IReplacer&IdentifierReplacer}
|
||||
* @type {IdentifierReplacer}
|
||||
*/
|
||||
private identifierReplacer: IReplacer&IdentifierReplacer;
|
||||
|
||||
/**
|
||||
* @type {Map<string, string>}
|
||||
*/
|
||||
private variableNames: Map <string, string> = new Map <string, string> ();
|
||||
private identifierReplacer: IdentifierReplacer;
|
||||
|
||||
/**
|
||||
* @param nodes
|
||||
@@ -67,7 +61,7 @@ export class VariableDeclarationObfuscator extends AbstractNodeObfuscator {
|
||||
.forEach((declarationNode: IVariableDeclaratorNode) => {
|
||||
NodeUtils.typedReplace(declarationNode.id, NodeType.Identifier, {
|
||||
leave: (node: IIdentifierNode) => {
|
||||
this.identifierReplacer.storeNames(node.name, this.variableNames)
|
||||
this.identifierReplacer.storeNames(node.name)
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -93,7 +87,7 @@ export class VariableDeclarationObfuscator extends AbstractNodeObfuscator {
|
||||
estraverse.replace(node, {
|
||||
enter: (node: INode, parentNode: INode): any => {
|
||||
if (Nodes.isReplaceableIdentifierNode(node, parentNode)) {
|
||||
node.name = this.identifierReplacer.replace(node.name, this.variableNames);
|
||||
node.name = this.identifierReplacer.replace(node.name);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -104,7 +98,7 @@ export class VariableDeclarationObfuscator extends AbstractNodeObfuscator {
|
||||
}
|
||||
|
||||
if (Nodes.isReplaceableIdentifierNode(node, parentNode) && isNodeAfterVariableDeclaratorFlag) {
|
||||
node.name = this.identifierReplacer.replace(node.name, this.variableNames);
|
||||
node.name = this.identifierReplacer.replace(node.name);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -2,13 +2,17 @@ import { AbstractReplacer } from "./AbstractReplacer";
|
||||
import { Utils } from "../../Utils";
|
||||
|
||||
export class IdentifierReplacer extends AbstractReplacer {
|
||||
/**
|
||||
* @type {Map<string, string>}
|
||||
*/
|
||||
private namesMap: Map<string, string> = new Map<string, string>();
|
||||
|
||||
/**
|
||||
* @param nodeValue
|
||||
* @param namesMap
|
||||
* @returns {string}
|
||||
*/
|
||||
public replace (nodeValue: string, namesMap: Map <string, string>): string {
|
||||
const obfuscatedIdentifierName: string|undefined = namesMap.get(nodeValue);
|
||||
public replace (nodeValue: string): string {
|
||||
const obfuscatedIdentifierName: string|undefined = this.namesMap.get(nodeValue);
|
||||
|
||||
if (!obfuscatedIdentifierName) {
|
||||
return nodeValue;
|
||||
@@ -22,11 +26,10 @@ export class IdentifierReplacer extends AbstractReplacer {
|
||||
* Reserved names will be ignored.
|
||||
*
|
||||
* @param nodeName
|
||||
* @param namesMap
|
||||
*/
|
||||
public storeNames (nodeName: string, namesMap: Map <string, string>): void {
|
||||
public storeNames (nodeName: string): void {
|
||||
if (!this.isReservedName(nodeName)) {
|
||||
namesMap.set(nodeName, Utils.getRandomVariableName());
|
||||
this.namesMap.set(nodeName, Utils.getRandomVariableName());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user