mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
100 lines
3.3 KiB
TypeScript
100 lines
3.3 KiB
TypeScript
import { injectable, inject } from 'inversify';
|
|
import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
|
|
|
|
import * as estraverse from 'estraverse';
|
|
import * as ESTree from 'estree';
|
|
|
|
import { IOptions } from '../../interfaces/options/IOptions';
|
|
import { IObfuscationReplacer } from '../../interfaces/node-transformers/IObfuscationReplacer';
|
|
import { IObfuscationReplacerWithStorage } from '../../interfaces/node-transformers/IObfuscationReplacerWithStorage';
|
|
|
|
import { NodeObfuscatorsReplacers } from '../../enums/container/NodeObfuscationReplacers';
|
|
|
|
import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
|
|
import { Node } from '../../node/Node';
|
|
|
|
/**
|
|
* replaces:
|
|
* try {} catch (e) { console.log(e); };
|
|
*
|
|
* on:
|
|
* try {} catch (_0x12d45f) { console.log(_0x12d45f); };
|
|
*
|
|
*/
|
|
@injectable()
|
|
export class CatchClauseTransformer extends AbstractNodeTransformer {
|
|
/**
|
|
* @type {IObfuscationReplacerWithStorage}
|
|
*/
|
|
private readonly identifierReplacer: IObfuscationReplacerWithStorage;
|
|
|
|
/**
|
|
* @param replacersFactory
|
|
* @param options
|
|
*/
|
|
constructor (
|
|
@inject(ServiceIdentifiers.Factory__IObfuscatorReplacer) replacersFactory: (replacer: NodeObfuscatorsReplacers) => IObfuscationReplacer,
|
|
@inject(ServiceIdentifiers.IOptions) options: IOptions
|
|
) {
|
|
super(options);
|
|
|
|
this.identifierReplacer = <IObfuscationReplacerWithStorage>replacersFactory(NodeObfuscatorsReplacers.IdentifierReplacer);
|
|
}
|
|
|
|
/**
|
|
* @return {estraverse.Visitor}
|
|
*/
|
|
public getVisitor (): estraverse.Visitor {
|
|
return {
|
|
enter: (node: ESTree.Node, parentNode: ESTree.Node) => {
|
|
if (Node.isCatchClauseNode(node)) {
|
|
return this.transformNode(node, parentNode);
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
/**
|
|
* @param catchClauseNode
|
|
* @param parentNode
|
|
* @returns {ESTree.Node}
|
|
*/
|
|
public transformNode (catchClauseNode: ESTree.CatchClause, parentNode: ESTree.Node): ESTree.Node {
|
|
const nodeIdentifier: number = this.nodeIdentifier++;
|
|
|
|
this.storeCatchClauseParam(catchClauseNode, nodeIdentifier);
|
|
this.replaceCatchClauseParam(catchClauseNode, nodeIdentifier);
|
|
|
|
return catchClauseNode;
|
|
}
|
|
|
|
/**
|
|
* @param catchClauseNode
|
|
* @param nodeIdentifier
|
|
*/
|
|
private storeCatchClauseParam (catchClauseNode: ESTree.CatchClause, nodeIdentifier: number): void {
|
|
if (Node.isIdentifierNode(catchClauseNode.param)) {
|
|
this.identifierReplacer.storeNames(catchClauseNode.param.name, nodeIdentifier);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param catchClauseNode
|
|
* @param nodeIdentifier
|
|
*/
|
|
private replaceCatchClauseParam (catchClauseNode: ESTree.CatchClause, nodeIdentifier: number): void {
|
|
estraverse.replace(catchClauseNode, {
|
|
enter: (node: ESTree.Node, parentNode: ESTree.Node): any => {
|
|
if (Node.isReplaceableIdentifierNode(node, parentNode)) {
|
|
const newNodeName: string = this.identifierReplacer.replace(node.name, nodeIdentifier);
|
|
|
|
if (node.name !== newNodeName) {
|
|
node.name = newNodeName;
|
|
node.obfuscatedNode = true;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|