123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528 |
- /* eslint-disable max-lines */
- import * as commander from 'commander';
- import * as path from 'path';
- import { TInputCLIOptions } from '../types/options/TInputCLIOptions';
- import { TInputOptions } from '../types/options/TInputOptions';
- import { IFileData } from '../interfaces/cli/IFileData';
- import { IInitializable } from '../interfaces/IInitializable';
- import { IObfuscatedCode } from '../interfaces/source-code/IObfuscatedCode';
- import { initializable } from '../decorators/Initializable';
- import { IdentifierNamesGenerator } from '../enums/generators/identifier-names-generators/IdentifierNamesGenerator';
- import { LoggingPrefix } from '../enums/logger/LoggingPrefix';
- import { ObfuscationTarget } from '../enums/ObfuscationTarget';
- import { OptionsPreset } from '../enums/options/presets/OptionsPreset';
- import { RenamePropertiesMode } from '../enums/node-transformers/rename-properties-transformers/RenamePropertiesMode';
- import { SourceMapMode } from '../enums/source-map/SourceMapMode';
- import { StringArrayEncoding } from '../enums/node-transformers/string-array-transformers/StringArrayEncoding';
- import { StringArrayIndexesType } from '../enums/node-transformers/string-array-transformers/StringArrayIndexesType';
- import { StringArrayWrappersType } from '../enums/node-transformers/string-array-transformers/StringArrayWrappersType';
- import { DEFAULT_PRESET } from '../options/presets/Default';
- import { ArraySanitizer } from './sanitizers/ArraySanitizer';
- import { BooleanSanitizer } from './sanitizers/BooleanSanitizer';
- import { CLIUtils } from './utils/CLIUtils';
- import { JavaScriptObfuscator } from '../JavaScriptObfuscatorFacade';
- import { Logger } from '../logger/Logger';
- import { ObfuscatedCodeWriter } from './utils/ObfuscatedCodeWriter';
- import { SourceCodeReader } from './utils/SourceCodeReader';
- import { Utils } from '../utils/Utils';
- export class JavaScriptObfuscatorCLI implements IInitializable {
- /**
- * @type {string[]}
- */
- public static readonly availableInputExtensions: string[] = [
- '.js'
- ];
- /**
- * @type {BufferEncoding}
- */
- public static readonly encoding: BufferEncoding = 'utf8';
- /**
- * @type {string}
- */
- public static readonly obfuscatedFilePrefix: string = '-obfuscated';
- /**
- * @type {commander.CommanderStatic}
- */
- @initializable()
- private commands!: commander.CommanderStatic;
- /**
- * @type {TInputCLIOptions}
- */
- @initializable()
- private inputCLIOptions!: TInputCLIOptions;
- /**
- * @type {string}
- */
- @initializable()
- private inputPath!: string;
- /**
- * @type {SourceCodeReader}
- */
- @initializable()
- private sourceCodeReader!: SourceCodeReader;
- /**
- * @type {ObfuscatedCodeWriter}
- */
- @initializable()
- private obfuscatedCodeWriter!: ObfuscatedCodeWriter;
- /**
- * @type {string[]}
- */
- private readonly arguments: string[];
- /**
- * @type {string[]}
- */
- private readonly rawArguments: string[];
- /**
- * @param {string[]} argv
- */
- public constructor (argv: string[]) {
- this.rawArguments = argv;
- this.arguments = argv.slice(2);
- }
- /**
- * @param {TInputCLIOptions} inputOptions
- * @returns {TInputOptions}
- */
- private static buildOptions (inputOptions: TInputCLIOptions): TInputOptions {
- const inputCLIOptions: TInputOptions = JavaScriptObfuscatorCLI.filterOptions(inputOptions);
- const configFilePath: string | undefined = inputOptions.config;
- const configFileLocation: string = configFilePath ? path.resolve(configFilePath, '.') : '';
- const configFileOptions: TInputOptions = configFileLocation ? CLIUtils.getUserConfig(configFileLocation) : {};
- return {
- ...DEFAULT_PRESET,
- ...configFileOptions,
- ...inputCLIOptions
- };
- }
- /**
- * @param {TObject} options
- * @returns {TInputOptions}
- */
- private static filterOptions (options: TInputCLIOptions): TInputOptions {
- const filteredOptions: TInputOptions = {};
- Object
- .keys(options)
- .forEach((option: keyof TInputCLIOptions) => {
- if (options[option] === undefined) {
- return;
- }
- filteredOptions[option] = options[option];
- });
- return filteredOptions;
- }
- public initialize (): void {
- this.commands = <commander.CommanderStatic>(new commander.Command());
- this.configureCommands();
- this.configureHelp();
- this.inputPath = path.normalize(this.commands.args[0] || '');
- this.inputCLIOptions = JavaScriptObfuscatorCLI.buildOptions(this.commands.opts());
- this.sourceCodeReader = new SourceCodeReader(
- this.inputPath,
- this.inputCLIOptions
- );
- this.obfuscatedCodeWriter = new ObfuscatedCodeWriter(
- this.inputPath,
- this.inputCLIOptions
- );
- }
- public run (): void {
- const canShowHelp: boolean = !this.arguments.length || this.arguments.includes('--help');
- if (canShowHelp) {
- this.commands.outputHelp();
- return;
- }
- const sourceCodeData: IFileData[] = this.sourceCodeReader.readSourceCode();
- this.processSourceCodeData(sourceCodeData);
- }
- private configureCommands (): void {
- this.commands
- .usage('<inputPath> [options]')
- .version(
- Utils.buildVersionMessage(process.env.VERSION, process.env.BUILD_TIMESTAMP),
- '-v, --version'
- )
- .option(
- '-o, --output <path>',
- 'Output path for obfuscated code'
- )
- .option(
- '--compact <boolean>',
- 'Disable one line output code compacting',
- BooleanSanitizer
- )
- .option(
- '--config <boolean>',
- 'Name of js / json config file'
- )
- .option(
- '--control-flow-flattening <boolean>',
- 'Enables control flow flattening',
- BooleanSanitizer
- )
- .option(
- '--control-flow-flattening-threshold <number>',
- 'The probability that the control flow flattening transformation will be applied to the node',
- parseFloat
- )
- .option(
- '--dead-code-injection <boolean>',
- 'Enables dead code injection',
- BooleanSanitizer
- )
- .option(
- '--dead-code-injection-threshold <number>',
- 'The probability that the dead code injection transformation will be applied to the node',
- parseFloat
- )
- .option(
- '--debug-protection <boolean>',
- 'Disable browser Debug panel (can cause DevTools enabled browser freeze)',
- BooleanSanitizer
- )
- .option(
- '--debug-protection-interval <boolean>',
- 'Disable browser Debug panel even after page was loaded (can cause DevTools enabled browser freeze)',
- BooleanSanitizer
- )
- .option(
- '--disable-console-output <boolean>',
- 'Allow console.log, console.info, console.error and console.warn messages output into browser console',
- BooleanSanitizer
- )
- .option(
- '--domain-lock <list> (comma separated, without whitespaces)',
- 'Allows to run the obfuscated source code only on specific domains and/or sub-domains (comma separated)',
- ArraySanitizer
- )
- .option(
- '--exclude <list> (comma separated, without whitespaces)',
- 'A filename or glob which indicates files to exclude from obfuscation',
- ArraySanitizer
- )
- .option(
- '--force-transform-strings <list> (comma separated, without whitespaces)',
- 'Enables force transformation of string literals, which being matched by passed RegExp patterns (comma separated)',
- ArraySanitizer
- )
- .option(
- '--identifier-names-generator <string>',
- 'Sets identifier names generator. ' +
- `Values: ${CLIUtils.stringifyOptionAvailableValues(IdentifierNamesGenerator)}. ` +
- `Default: ${IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator}`
- )
- .option(
- '--identifiers-prefix <string>',
- 'Sets prefix for all global identifiers'
- )
- .option(
- '--identifiers-dictionary <list> (comma separated, without whitespaces)',
- 'Identifiers dictionary (comma separated) for `--identifier-names-generator dictionary` option',
- ArraySanitizer
- )
- .option(
- '--ignore-require-imports <boolean>', 'Prevents obfuscation of `require` imports',
- BooleanSanitizer
- )
- .option(
- '--log <boolean>', 'Enables logging of the information to the console',
- BooleanSanitizer
- )
- .option(
- '--numbers-to-expressions <boolean>', 'Enables numbers conversion to expressions',
- BooleanSanitizer
- )
- .option(
- '--options-preset <string>',
- 'Allows to set options preset. ' +
- `Values: ${CLIUtils.stringifyOptionAvailableValues(OptionsPreset)}. ` +
- `Default: ${OptionsPreset.Default}`
- )
- .option(
- '--reserved-names <list> (comma separated, without whitespaces)',
- 'Disables obfuscation and generation of identifiers, which being matched by passed RegExp patterns (comma separated)',
- ArraySanitizer
- )
- .option(
- '--reserved-strings <list> (comma separated, without whitespaces)',
- 'Disables transformation of string literals, which being matched by passed RegExp patterns (comma separated)',
- ArraySanitizer
- )
- .option(
- '--rename-globals <boolean>', 'Allows to enable obfuscation of global variable and function names with declaration',
- BooleanSanitizer
- )
- .option(
- '--rename-properties <boolean>', 'UNSAFE: Enables renaming of property names. This probably MAY break your code',
- BooleanSanitizer
- )
- .option(
- '--rename-properties-mode <boolean>',
- 'Specify `--rename-properties` option mode. ' +
- `Values: ${CLIUtils.stringifyOptionAvailableValues(RenamePropertiesMode)}. ` +
- `Default: ${RenamePropertiesMode.Safe}`
- )
- .option(
- '--rotate-string-array <boolean>', 'Enable rotation of string array values during obfuscation',
- BooleanSanitizer
- )
- .option(
- '--seed <string|number>',
- 'Sets seed for random generator. This is useful for creating repeatable results.',
- parseFloat
- )
- .option(
- '--self-defending <boolean>',
- 'Disables self-defending for obfuscated code',
- BooleanSanitizer
- )
- .option(
- '--shuffle-string-array <boolean>', 'Randomly shuffles string array items',
- BooleanSanitizer
- )
- .option(
- '--simplify <boolean>', 'Enables additional code obfuscation through simplification',
- BooleanSanitizer
- )
- .option(
- '--source-map <boolean>',
- 'Enables source map generation',
- BooleanSanitizer
- )
- .option(
- '--source-map-base-url <string>',
- 'Sets base url to the source map import url when `--source-map-mode=separate`'
- )
- .option(
- '--source-map-file-name <string>',
- 'Sets file name for output source map when `--source-map-mode=separate`'
- )
- .option(
- '--source-map-mode <string>',
- 'Specify source map output mode. ' +
- `Values: ${CLIUtils.stringifyOptionAvailableValues(SourceMapMode)}. ` +
- `Default: ${SourceMapMode.Separate}`
- )
- .option(
- '--split-strings <boolean>',
- 'Splits literal strings into chunks with length of `splitStringsChunkLength` option value',
- BooleanSanitizer
- )
- .option(
- '--split-strings-chunk-length <number>',
- 'Sets chunk length of `splitStrings` option',
- parseFloat
- )
- .option(
- '--string-array <boolean>',
- 'Disables gathering of all literal strings into an array and replacing every literal string with an array call',
- BooleanSanitizer
- )
- .option(
- '--string-array-encoding <list> (comma separated, without whitespaces)',
- 'Encodes each string in strings array using base64 or rc4 (this option can slow down your code speed). ' +
- `Values: ${CLIUtils.stringifyOptionAvailableValues(StringArrayEncoding)}. ` +
- `Default: ${StringArrayEncoding.None}`,
- ArraySanitizer
- )
- .option(
- '--string-array-indexes-type <list> (comma separated, without whitespaces)',
- 'Encodes each string in strings array using base64 or rc4 (this option can slow down your code speed). ' +
- `Values: ${CLIUtils.stringifyOptionAvailableValues(StringArrayIndexesType)}. ` +
- `Default: ${StringArrayIndexesType.HexadecimalNumber}`,
- ArraySanitizer
- )
- .option(
- '--string-array-index-shift <boolean>',
- 'Enables additional index shift for all string array calls',
- BooleanSanitizer
- )
- .option(
- '--string-array-wrappers-count <number>',
- 'Sets the count of wrappers for the string array inside each root or function scope',
- parseInt
- )
- .option(
- '--string-array-wrappers-chained-calls <boolean>',
- 'Enables the chained calls between string array wrappers',
- BooleanSanitizer
- )
- .option(
- '--string-array-wrappers-parameters-max-count <number>',
- 'Allows to control the maximum number of string array wrappers parameters',
- parseInt
- )
- .option(
- '--string-array-wrappers-type <string>',
- 'Allows to select a type of the wrappers that are appending by the `--string-array-wrappers-count` option. ' +
- `Values: ${CLIUtils.stringifyOptionAvailableValues(StringArrayWrappersType)}. ` +
- `Default: ${StringArrayWrappersType.Variable}`
- )
- .option(
- '--string-array-threshold <number>',
- 'The probability that the literal string will be inserted into stringArray (Default: 0.8, Min: 0, Max: 1)',
- parseFloat
- )
- .option(
- '--target <string>',
- 'Allows to set target environment for obfuscated code. ' +
- `Values: ${CLIUtils.stringifyOptionAvailableValues(ObfuscationTarget)}. ` +
- `Default: ${ObfuscationTarget.Browser}`
- )
- .option(
- '--transform-object-keys <boolean>',
- 'Enables transformation of object keys',
- BooleanSanitizer
- )
- .option(
- '--unicode-escape-sequence <boolean>',
- 'Allows to enable/disable string conversion to unicode escape sequence',
- BooleanSanitizer
- )
- .parse(this.rawArguments);
- }
- private configureHelp (): void {
- this.commands.on('--help', () => {
- console.log(' Examples:\n');
- console.log(' %> javascript-obfuscator input_file_name.js --compact true --self-defending false');
- console.log(' %> javascript-obfuscator input_file_name.js --output output_file_name.js --compact true --self-defending false');
- console.log(' %> javascript-obfuscator input_directory_name --compact true --self-defending false');
- console.log('');
- });
- }
- /**
- * @param {IFileData[]} sourceCodeData
- */
- private processSourceCodeData (sourceCodeData: IFileData[]): void {
- sourceCodeData.forEach(({ filePath, content }: IFileData, index: number) => {
- const outputCodePath: string = this.obfuscatedCodeWriter.getOutputCodePath(filePath);
- try {
- Logger.log(
- Logger.colorInfo,
- LoggingPrefix.CLI,
- `Obfuscating file: ${filePath}...`
- );
- this.processSourceCode(content, filePath, outputCodePath, index);
- } catch (error) {
- Logger.log(
- Logger.colorInfo,
- LoggingPrefix.CLI,
- `Error in file: ${filePath}...`
- );
- throw error;
- }
- });
- }
- /**
- * @param {string} sourceCode
- * @param {string} inputCodePath
- * @param {string} outputCodePath
- * @param {number | null} sourceCodeIndex
- */
- private processSourceCode (
- sourceCode: string,
- inputCodePath: string,
- outputCodePath: string,
- sourceCodeIndex: number | null
- ): void {
- const options: TInputOptions = {
- ...this.inputCLIOptions,
- inputFileName: path.basename(inputCodePath),
- ...sourceCodeIndex !== null && {
- identifiersPrefix: Utils.getIdentifiersPrefixForMultipleSources(
- this.inputCLIOptions.identifiersPrefix,
- sourceCodeIndex
- )
- }
- };
- if (options.sourceMap) {
- this.processSourceCodeWithSourceMap(sourceCode, outputCodePath, options);
- } else {
- this.processSourceCodeWithoutSourceMap(sourceCode, outputCodePath, options);
- }
- }
- /**
- * @param {string} sourceCode
- * @param {string} outputCodePath
- * @param {TInputOptions} options
- */
- private processSourceCodeWithoutSourceMap (
- sourceCode: string,
- outputCodePath: string,
- options: TInputOptions
- ): void {
- const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(sourceCode, options).getObfuscatedCode();
- this.obfuscatedCodeWriter.writeFile(outputCodePath, obfuscatedCode);
- }
- /**
- * @param {string} sourceCode
- * @param {string} outputCodePath
- * @param {TInputOptions} options
- */
- private processSourceCodeWithSourceMap (
- sourceCode: string,
- outputCodePath: string,
- options: TInputOptions
- ): void {
- const outputSourceMapPath: string = this.obfuscatedCodeWriter.getOutputSourceMapPath(
- outputCodePath,
- options.sourceMapFileName ?? ''
- );
- options = {
- ...options,
- sourceMapFileName: path.basename(outputSourceMapPath)
- };
- const obfuscatedCode: IObfuscatedCode = JavaScriptObfuscator.obfuscate(sourceCode, options);
- this.obfuscatedCodeWriter.writeFile(outputCodePath, obfuscatedCode.getObfuscatedCode());
- if (options.sourceMapMode === SourceMapMode.Separate && obfuscatedCode.getSourceMap()) {
- this.obfuscatedCodeWriter.writeFile(outputSourceMapPath, obfuscatedCode.getSourceMap());
- }
- }
- }
|