Refactoring of CustomNode methods

This commit is contained in:
sanex3339
2020-01-10 13:06:33 +03:00
parent 63fd4a82d9
commit 77122b7374
23 changed files with 82 additions and 53 deletions
+13 -2
View File
@@ -79,7 +79,11 @@ export abstract class AbstractCustomNode <
*/
public getNode (): TStatement[] {
if (!this.cachedNode) {
this.cachedNode = this.customNodeFormatter.formatStructure(this.getNodeStructure());
const nodeTemplate: string = this.getNodeTemplate();
this.cachedNode = this.customNodeFormatter.formatStructure(
this.getNodeStructure(nodeTemplate)
);
}
return this.cachedNode;
@@ -97,5 +101,12 @@ export abstract class AbstractCustomNode <
/**
* @returns {TStatement[]}
*/
protected abstract getNodeStructure (): TStatement[];
protected abstract getNodeStructure (nodeTemplate: string): TStatement[];
/**
* @returns {string}
*/
protected getNodeTemplate (): string {
return '';
}
}
+9 -9
View File
@@ -10,20 +10,21 @@ import { TStatement } from '../types/node/TStatement';
import { ICustomNodeFormatter } from '../interfaces/custom-nodes/ICustomNodeFormatter';
import { IPrevailingKindOfVariablesAnalyzer } from '../interfaces/analyzers/calls-graph-analyzer/IPrevailingKindOfVariablesAnalyzer';
import { NodeGuards } from '../node/NodeGuards';
@injectable()
export class CustomNodeFormatter implements ICustomNodeFormatter {
/**
* @type {IPrevailingKindOfVariablesAnalyzer}
* @type {ESTree.VariableDeclaration['kind']}
*/
private readonly prevailingKindOfVariablesAnalyzer: IPrevailingKindOfVariablesAnalyzer;
private readonly prevailingKindOfVariables: ESTree.VariableDeclaration['kind'];
constructor (
@inject(ServiceIdentifiers.IPrevailingKindOfVariablesAnalyzer)
prevailingKindOfVariablesAnalyzer: IPrevailingKindOfVariablesAnalyzer
) {
this.prevailingKindOfVariablesAnalyzer = prevailingKindOfVariablesAnalyzer;
this.prevailingKindOfVariables = prevailingKindOfVariablesAnalyzer.getPrevailingKind();
}
/**
@@ -43,19 +44,18 @@ export class CustomNodeFormatter implements ICustomNodeFormatter {
* @returns {TStatement[]}
*/
public formatStructure (statements: TStatement[]): TStatement[] {
const prevailingKindOfVariables: ESTree.VariableDeclaration['kind'] =
this.prevailingKindOfVariablesAnalyzer.getPrevailingKind();
for (const statement of statements) {
estraverse.traverse(statement, {
enter: (node: ESTree.Node): estraverse.VisitorOption | void => {
estraverse.replace(statement, {
enter: (node: ESTree.Node): ESTree.Node | void => {
if (!NodeGuards.isVariableDeclarationNode(node)) {
return;
}
if (prevailingKindOfVariables === 'var') {
if (this.prevailingKindOfVariables === 'var') {
node.kind = 'var';
}
return node;
}
});
}
@@ -50,16 +50,17 @@ export class ConsoleOutputDisableExpressionNode extends AbstractCustomNode {
}
/**
* @param {string} nodeTemplate
* @returns {TStatement[]}
*/
protected getNodeStructure (): TStatement[] {
return NodeUtils.convertCodeToStructure(this.getTemplate());
protected getNodeStructure (nodeTemplate: string): TStatement[] {
return NodeUtils.convertCodeToStructure(nodeTemplate);
}
/**
* @returns {string}
*/
protected getTemplate (): string {
protected getNodeTemplate (): string {
const globalVariableTemplate: string = this.options.target !== ObfuscationTarget.BrowserNoEval
? this.getGlobalVariableTemplate()
: GlobalVariableNoEvalTemplate();
@@ -48,9 +48,10 @@ export class BinaryExpressionFunctionNode extends AbstractCustomNode {
}
/**
* @param {string} nodeTemplate
* @returns {TStatement[]}
*/
protected getNodeStructure (): TStatement[] {
protected getNodeStructure (nodeTemplate: string): TStatement[] {
const structure: TStatement = NodeFactory.expressionStatementNode(
NodeFactory.functionExpressionNode(
[
@@ -69,9 +69,10 @@ export class BlockStatementControlFlowFlatteningNode extends AbstractCustomNode
}
/**
* @param {string} nodeTemplate
* @returns {TStatement[]}
*/
protected getNodeStructure (): TStatement[] {
protected getNodeStructure (nodeTemplate: string): TStatement[] {
const controllerIdentifierName: string = this.randomGenerator.getRandomString(6);
const indexIdentifierName: string = this.randomGenerator.getRandomString(6);
const structure: ESTree.BlockStatement = NodeFactory.blockStatementNode([
@@ -48,9 +48,10 @@ export class CallExpressionFunctionNode extends AbstractCustomNode {
}
/**
* @param {string} nodeTemplate
* @returns {TStatement[]}
*/
protected getNodeStructure (): TStatement[] {
protected getNodeStructure (nodeTemplate: string): TStatement[] {
const calleeIdentifier: ESTree.Identifier = NodeFactory.identifierNode('callee');
const params: ESTree.Identifier[] = [];
const argumentsLength: number = this.expressionArguments.length;
@@ -48,9 +48,10 @@ export class LogicalExpressionFunctionNode extends AbstractCustomNode {
}
/**
* @param {string} nodeTemplate
* @returns {TStatement[]}
*/
protected getNodeStructure (): TStatement[] {
protected getNodeStructure (nodeTemplate: string): TStatement[] {
const structure: TStatement = NodeFactory.expressionStatementNode(
NodeFactory.functionExpressionNode(
[
@@ -45,9 +45,10 @@ export class StringLiteralNode extends AbstractCustomNode {
}
/**
* @param {string} nodeTemplate
* @returns {TStatement[]}
*/
protected getNodeStructure (): TStatement[] {
protected getNodeStructure (nodeTemplate: string): TStatement[] {
const structure: TStatement = NodeFactory.expressionStatementNode(
NodeFactory.literalNode(this.literalValue)
);
@@ -51,9 +51,10 @@ export class ControlFlowStorageNode extends AbstractCustomNode {
}
/**
* @param {string} nodeTemplate
* @returns {TStatement[]}
*/
protected getNodeStructure (): TStatement[] {
protected getNodeStructure (nodeTemplate: string): TStatement[] {
const propertyNodes: ESTree.Property[] = Array
.from<[string, ICustomNode]>(this.controlFlowStorage.getStorage())
.map(([key, value]: [string, ICustomNode]) => {
@@ -59,9 +59,10 @@ export class BlockStatementDeadCodeInjectionNode extends AbstractCustomNode {
}
/**
* @param {string} nodeTemplate
* @returns {TStatement[]}
*/
protected getNodeStructure (): TStatement[] {
protected getNodeStructure (nodeTemplate: string): TStatement[] {
const random1: boolean = this.randomGenerator.getMathRandom() > 0.5;
const random2: boolean = this.randomGenerator.getMathRandom() > 0.5;
@@ -55,16 +55,17 @@ export class DebugProtectionFunctionCallNode extends AbstractCustomNode {
}
/**
* @param {string} nodeTemplate
* @returns {TStatement[]}
*/
protected getNodeStructure (): TStatement[] {
return NodeUtils.convertCodeToStructure(this.getTemplate());
protected getNodeStructure (nodeTemplate: string): TStatement[] {
return NodeUtils.convertCodeToStructure(nodeTemplate);
}
/**
* @returns {string}
*/
protected getTemplate (): string {
protected getNodeTemplate (): string {
return this.customNodeFormatter.formatTemplate(DebugProtectionFunctionCallTemplate(), {
debugProtectionFunctionName: this.debugProtectionFunctionName,
singleNodeCallControllerFunctionName: this.callsControllerFunctionName
@@ -47,16 +47,17 @@ export class DebugProtectionFunctionIntervalNode extends AbstractCustomNode {
}
/**
* @param {string} nodeTemplate
* @returns {TStatement[]}
*/
protected getNodeStructure (): TStatement[] {
return NodeUtils.convertCodeToStructure(this.getTemplate());
protected getNodeStructure (nodeTemplate: string): TStatement[] {
return NodeUtils.convertCodeToStructure(nodeTemplate);
}
/**
* @returns {string}
*/
protected getTemplate (): string {
protected getNodeTemplate (): string {
return this.customNodeFormatter.formatTemplate(DebugProtectionFunctionIntervalTemplate(), {
debugProtectionFunctionName: this.debugProtectionFunctionName
});
@@ -51,16 +51,17 @@ export class DebugProtectionFunctionNode extends AbstractCustomNode {
}
/**
* @param {string} nodeTemplate
* @returns {TStatement[]}
*/
protected getNodeStructure (): TStatement[] {
return NodeUtils.convertCodeToStructure(this.getTemplate());
protected getNodeStructure (nodeTemplate: string): TStatement[] {
return NodeUtils.convertCodeToStructure(nodeTemplate);
}
/**
* @returns {string}
*/
protected getTemplate (): string {
protected getNodeTemplate (): string {
const debuggerTemplate: string = this.options.target !== ObfuscationTarget.BrowserNoEval
? DebuggerTemplate()
: DebuggerTemplateNoEval();
@@ -60,16 +60,17 @@ export class DomainLockNode extends AbstractCustomNode {
}
/**
* @param {string} nodeTemplate
* @returns {TStatement[]}
*/
protected getNodeStructure (): TStatement[] {
return NodeUtils.convertCodeToStructure(this.getTemplate());
protected getNodeStructure (nodeTemplate: string): TStatement[] {
return NodeUtils.convertCodeToStructure(nodeTemplate);
}
/**
* @returns {string}
*/
protected getTemplate (): string {
protected getNodeTemplate (): string {
const domainsString: string = this.options.domainLock.join(';');
const [hiddenDomainsString, diff]: string[] = this.cryptUtils.hideString(
domainsString,
@@ -60,16 +60,17 @@ export class NodeCallsControllerFunctionNode extends AbstractCustomNode {
}
/**
* @param {string} nodeTemplate
* @returns {TStatement[]}
*/
protected getNodeStructure (): TStatement[] {
return NodeUtils.convertCodeToStructure(this.getTemplate());
protected getNodeStructure (nodeTemplate: string): TStatement[] {
return NodeUtils.convertCodeToStructure(nodeTemplate);
}
/**
* @returns {string}
*/
protected getTemplate (): string {
protected getNodeTemplate (): string {
if (this.appendEvent === ObfuscationEvent.AfterObfuscation) {
return JavaScriptObfuscator.obfuscate(
this.customNodeFormatter.formatTemplate(SingleNodeCallControllerTemplate(), {
@@ -32,9 +32,10 @@ export class BasePropertiesExtractorObjectExpressionHostNode extends AbstractCus
public initialize (): void {}
/**
* @param {string} nodeTemplate
* @returns {TStatement[]}
*/
protected getNodeStructure (): TStatement[] {
protected getNodeStructure (nodeTemplate: string): TStatement[] {
const structure: TStatement = NodeFactory.variableDeclarationNode(
[
NodeFactory.variableDeclaratorNode(
@@ -60,16 +60,17 @@ export class SelfDefendingUnicodeNode extends AbstractCustomNode {
}
/**
* @param {string} nodeTemplate
* @returns {TStatement[]}
*/
protected getNodeStructure (): TStatement[] {
return NodeUtils.convertCodeToStructure(this.getTemplate());
protected getNodeStructure (nodeTemplate: string): TStatement[] {
return NodeUtils.convertCodeToStructure(nodeTemplate);
}
/**
* @returns {string}
*/
protected getTemplate (): string {
protected getNodeTemplate (): string {
return JavaScriptObfuscator.obfuscate(
this.customNodeFormatter.formatTemplate(SelfDefendingTemplate(this.escapeSequenceEncoder), {
selfDefendingFunctionName: this.identifierNamesGenerator.generate(),
@@ -80,16 +80,17 @@ export class StringArrayCallsWrapper extends AbstractCustomNode {
}
/**
* @param {string} nodeTemplate
* @returns {TStatement[]}
*/
protected getNodeStructure (): TStatement[] {
return NodeUtils.convertCodeToStructure(this.getTemplate());
protected getNodeStructure (nodeTemplate: string): TStatement[] {
return NodeUtils.convertCodeToStructure(nodeTemplate);
}
/**
* @returns {string}
*/
protected getTemplate (): string {
protected getNodeTemplate (): string {
const decodeNodeTemplate: string = this.getDecodeStringArrayTemplate();
return JavaScriptObfuscator.obfuscate(
@@ -78,16 +78,17 @@ export class StringArrayNode extends AbstractCustomNode {
}
/**
* @param {string} nodeTemplate
* @returns {TStatement[]}
*/
protected getNodeStructure (): TStatement[] {
return NodeUtils.convertCodeToStructure(this.getTemplate());
protected getNodeStructure (nodeTemplate: string): TStatement[] {
return NodeUtils.convertCodeToStructure(nodeTemplate);
}
/**
* @returns {string}
*/
protected getTemplate (): string {
protected getNodeTemplate (): string {
return this.customNodeFormatter.formatTemplate(StringArrayTemplate(), {
stringArrayName: this.stringArrayName,
stringArray: this.stringArrayStorage.toString()
@@ -73,16 +73,17 @@ export class StringArrayRotateFunctionNode extends AbstractCustomNode {
}
/**
* @param {string} nodeTemplate
* @returns {TStatement[]}
*/
protected getNodeStructure (): TStatement[] {
return NodeUtils.convertCodeToStructure(this.getTemplate());
protected getNodeStructure (nodeTemplate: string): TStatement[] {
return NodeUtils.convertCodeToStructure(nodeTemplate);
}
/**
* @returns {string}
*/
protected getTemplate (): string {
protected getNodeTemplate (): string {
const timesName: string = this.identifierNamesGenerator.generate();
const whileFunctionName: string = this.identifierNamesGenerator.generate();