Skip to content

Commit

Permalink
Obfuscator version update to 2.8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sanex3339 committed Nov 7, 2020
1 parent 53a0923 commit 77987c4
Show file tree
Hide file tree
Showing 3 changed files with 8,620 additions and 427 deletions.
110 changes: 7 additions & 103 deletions loader/index.ts
Original file line number Diff line number Diff line change
@@ -1,108 +1,7 @@
"use strict";

import JavaScriptObfuscator, {ObfuscatorOptions} from 'javascript-obfuscator';
import estraverse from 'estraverse';
import * as ESTree from 'estree';
import loaderUtils from 'loader-utils';
import * as acorn from 'acorn';

class WebpackObfuscatorLoaderHelper {
/**
* @type {acorn.Options['sourceType'][]}
*/
private static readonly sourceTypes: acorn.Options['sourceType'][] = [
'script',
'module'
];

/**
* @param {string} sourceCode
* @returns {string}
*/
public static getCommentedSource (sourceCode: string): string {
// Parses source code and collects require expression nodes
const entries: {
start: number;
end: number;
}[] = [];
const astTree: ESTree.Program = WebpackObfuscatorLoaderHelper.parseCode(sourceCode);

estraverse.traverse(astTree, {
enter: (node: ESTree.Node): void => {
if (WebpackObfuscatorLoaderHelper.isRequire(node) && node.start && node.end) {
entries.push({
start: node.start,
end: node.end,
});
}
}
});

// Wraps requires in conditional comments
let commentedSource: string = sourceCode.slice();

entries
.sort((a, b) => b.end - a.end)
.forEach((n) => {
const before = commentedSource.slice(0, n.start);
const mid = commentedSource.slice(n.start, n.end);
const after = commentedSource.slice(n.end);

commentedSource = `${before}/* javascript-obfuscator:disable */${mid}/* javascript-obfuscator:enable */${after}`;
});

return commentedSource;
}

/**
* @param {string} sourceCode
* @returns {ESTree.Program}
*/
private static parseCode (sourceCode: string): ESTree.Program {
const sourceTypeLength: number = WebpackObfuscatorLoaderHelper.sourceTypes.length;

for (let i: number = 0; i < sourceTypeLength; i++) {
try {
return WebpackObfuscatorLoaderHelper.parseType(sourceCode, WebpackObfuscatorLoaderHelper.sourceTypes[i]);
} catch (error) {
if (i < sourceTypeLength - 1) {
continue;
}

throw new Error(error);
}
}

throw new Error('Acorn parsing error');
}

/**
* @param {string} sourceCode
* @param {acorn.Options["sourceType"]} sourceType
* @returns {Program}
*/
private static parseType (
sourceCode: string,
sourceType: acorn.Options['sourceType']
): ESTree.Program {
const config: acorn.Options = {
sourceType,
ecmaVersion: 11
};

return <any>acorn.parse(sourceCode, config);
}

/**
* @param {ESTree.Node} node
* @returns {boolean}
*/
private static isRequire (node: ESTree.Node) {
return node.type === 'CallExpression'
&& node.callee.type === 'Identifier'
&& node.callee.name === 'require';
}
}

/**
* JavaScript Obfuscator loader based on `obfuscator-loader` package
Expand All @@ -111,8 +10,13 @@ function Loader (sourceCode: string) {
// Obfuscates commented source code
// @ts-ignore
const options = loaderUtils.getOptions<ObfuscatorOptions>(this) || {};
const commentedSourceCode: string = WebpackObfuscatorLoaderHelper.getCommentedSource(sourceCode);
const obfuscationResult = JavaScriptObfuscator.obfuscate(commentedSourceCode, options);
const obfuscationResult = JavaScriptObfuscator.obfuscate(
sourceCode,
{
...options,
ignoreRequireImports: true
}
);

return obfuscationResult.getObfuscatedCode();
}
Expand Down
Loading

0 comments on commit 77987c4

Please sign in to comment.