mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
Refactoring
This commit is contained in:
+9
-48
@@ -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<string, Node>}
|
||||
*/
|
||||
private nodes: Map <string, ICustomNode> = new Map <string, ICustomNode> ();
|
||||
private nodes: Map <string, ICustomNode>;
|
||||
|
||||
/**
|
||||
* @type {Map<string, TNodeObfuscator[]>}
|
||||
@@ -60,6 +59,13 @@ export class Obfuscator implements IObfuscator {
|
||||
*/
|
||||
constructor (options: IOptions) {
|
||||
this.options = options;
|
||||
|
||||
this.nodes = new Map <string, ICustomNode> ([
|
||||
...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 <string, ICustomNode> = 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user