diff --git a/bin/javascript-obfuscator.js b/bin/javascript-obfuscator.js index bb298729..b39c4e35 100755 --- a/bin/javascript-obfuscator.js +++ b/bin/javascript-obfuscator.js @@ -4,86 +4,121 @@ var commands = require('commander'), fs = require('fs'), path = require('path'), JavaScriptObfuscator = require('../dist/index'), - packageConfig = fs.readFileSync(path.join(path.dirname(fs.realpathSync(process.argv[1])), '../package.json')), - buildVersion = JSON.parse(packageConfig).version, - isWindows = process.platform == 'win32'; + data = '', + defaultOptions = { + compact: true, + debugProtection: false, + debugProtectionInterval: false, + disableConsoleOutput: true, + encodeUnicodeLiterals: false, + reservedNames: [], + rotateUnicodeArray: true, + selfDefending: true, + unicodeArray: true, + unicodeArrayThreshold: 0.8, + wrapUnicodeArrayCalls: true + }; -// Specify commander options to parse command line params correctly -commands - .version(buildVersion, '-v, --version') - .usage('[options] STDIN STDOUT') - .option('--skip-compact', 'Disable one line output code compacting.') - .option('--debugProtection', 'Disable browser Debug panel (can cause DevTools enabled browser freeze).') - .option('--debugProtectionInterval', 'Disable browser Debug panel even after page was loaded (can cause DevTools enabled browser freeze).') - .option('--skip-disableConsoleOutput', 'Allow console.log, console.info, console.error and console.warn messages output into browser console.') - .option('--encodeUnicodeLiterals', 'All literals in Unicode array become encoded in Base64 (this option can slightly slow down your code speed).') - .option('--reservedNames ', 'Disable obfuscation of variable names, function names and names of function parameters that match the passed RegExp patterns (comma separated).', (val) => val.split(',')) - .option('--skip-rotateUnicodeArray', 'Disable rotation of unicode array values during obfuscation.') - .option('--skip-selfDefending', 'Disables self-defending for obfuscated code.') - .option('--skip-unicodeArray', 'Disables gathering of all literal strings into an array and replacing every literal string with an array call.') - .option('--unicodeArrayThreshold ', 'The probability that the literal string will be inserted into unicodeArray (Default: 0.8, Min: 0, Max: 1).', parseFloat) - .option('--skip-wrapUnicodeArrayCalls', 'Disables usage of special access function instead of direct array call.') -; - -commands.on('--help', function () { - console.log(' Examples:\n'); - console.log(' %> javascript-obfuscator < in.js > out.js'); - - if (isWindows) { - console.log(' %> type in1.js in2.js | javascript-obfuscator > out.js'); - } else { - console.log(' %> cat in1.js in2.js | javascript-obfuscator > out.js'); - } - - console.log(''); - - process.exit(); -}); +configureProcess(); +configureCommands(); commands.parse(process.argv); -// If no sensible data passed in just print help and exit -var fromStdin = !process.env.__DIRECT__ && !process.stdin.isTTY; +if (!isDataExist()) { + commands.outputHelp(); -if (!fromStdin) { - commands.outputHelp(); - - return 0; + return 0; } -var encoding = 'utf-8', - data = ''; +function buildOptions () { + var options = {}, + availableOptions = Object.keys(defaultOptions); -process.stdin.setEncoding(encoding); - -process.stdin.on('readable', function() { - var chunk; + for (var option in commands) { + if (availableOptions.indexOf(option) === -1) { + continue; + } - while (chunk = process.stdin.read()) { - data += chunk; + options[option] = commands[option]; } -}); - -process.stdin.on('end', function () { - processData(); -}); - -function processData () { - var options = { - compact : commands.skipCompact ? false : true, - debugProtection : commands.debugProtection, - debugProtectionInterval : commands.debugProtectionInterval, - disableConsoleOutput : commands.skipDisableConsoleOutput ? false : true, - encodeUnicodeLiterals: commands.encodeUnicodeLiterals, - reservedNames: commands.reservedNames ? commands.reservedNames : [], - rotateUnicodeArray : commands.skipRotateUnicodeArray ? false : true, - selfDefending: commands.skipSelfDefending ? false : true, - unicodeArray: commands.skipUnicodeArray ? false : true, - unicodeArrayThreshold: commands.unicodeArrayThreshold ? commands.unicodeArrayThreshold : 0.8, - wrapUnicodeArrayCalls: commands.skipWrapUnicodeArrayCalls ? false : true, - }; - var obfuscatedCode = JavaScriptObfuscator.obfuscate(data, options); + return Object.assign({}, defaultOptions, options); +} - process.stdout.write(obfuscatedCode); +function configureCommands () { + commands + .version(getBuildVersion(), '-v, --version') + .usage('[options] STDIN STDOUT') + .option('--compact ', 'Disable one line output code compacting.', parseBoolean) + .option('--debugProtection ', 'Disable browser Debug panel (can cause DevTools enabled browser freeze).', parseBoolean) + .option('--debugProtectionInterval ', 'Disable browser Debug panel even after page was loaded (can cause DevTools enabled browser freeze).', parseBoolean) + .option('--disableConsoleOutput ', 'Allow console.log, console.info, console.error and console.warn messages output into browser console.', parseBoolean) + .option('--encodeUnicodeLiterals ', 'All literals in Unicode array become encoded in Base64 (this option can slightly slow down your code speed).', parseBoolean) + .option('--reservedNames ', 'Disable obfuscation of variable names, function names and names of function parameters that match the passed RegExp patterns (comma separated).', (val) => val.split(',')) + .option('--rotateUnicodeArray ', 'Disable rotation of unicode array values during obfuscation.', parseBoolean) + .option('--selfDefending ', 'Disables self-defending for obfuscated code.', parseBoolean) + .option('--unicodeArray ', 'Disables gathering of all literal strings into an array and replacing every literal string with an array call.', parseBoolean) + .option('--unicodeArrayThreshold ', 'The probability that the literal string will be inserted into unicodeArray (Default: 0.8, Min: 0, Max: 1).', parseFloat) + .option('--wrapUnicodeArrayCalls ', 'Disables usage of special access function instead of direct array call.', parseBoolean) + ; + + commands.on('--help', function () { + var isWindows = process.platform == 'win32'; + + console.log(' Examples:\n'); + console.log(' %> javascript-obfuscator < in.js > out.js'); + + if (isWindows) { + console.log(' %> type in1.js in2.js | javascript-obfuscator > out.js'); + } else { + console.log(' %> cat in1.js in2.js | javascript-obfuscator > out.js'); + } + + console.log(''); + + process.exit(); + }); +} + +function configureProcess () { + process.stdin.setEncoding('utf-8'); + + process.stdin.on('readable', function () { + var chunk; + + while (chunk = process.stdin.read()) { + data += chunk; + } + }); + + process.stdin.on('end', function () { + processData(); + }); +} + +function getBuildVersion () { + var packageConfig = fs.readFileSync( + path.join( + path.dirname( + fs.realpathSync(process.argv[1]) + ), + '../package.json' + ) + ); + + return JSON.parse(packageConfig).version; +} + +function isDataExist () { + return !process.env.__DIRECT__ && !process.stdin.isTTY; +} + +function parseBoolean (value) { + return value === 'true' || value === '1'; +} + +function processData() { + process.stdout.write( + JavaScriptObfuscator.obfuscate(data, buildOptions()) + ); } diff --git a/readme.md b/readme.md index 3bed4ce3..4940c3e6 100644 --- a/readme.md +++ b/readme.md @@ -42,7 +42,7 @@ var obfuscatedCode = JavaScriptObfuscator.obfuscate( console.log(obfuscatedCode); /* var _0xabf1 = [ - '\x61\x62\x63', + '\x61\x62\x63', '\x6c\x6f\x67' ]; (function() { @@ -59,6 +59,11 @@ This is the main function that runs the Obfuscator. It takes two parameters, `so * `sourceCode` (`string`, default: `null`) – any valid source code, passed as a string variable. JS Obfuscator will parse this string and apply a set of modificating functions to it, generating a string with the new (obfuscated) code and printing it to the console; * `options` (`Object`, default: `null`) – an object literal. +### CLI +Usage: `javascript-obfuscator [options] < in.js > out.js` + +See CLI options below. + ## JavaScript Obfuscator Options At this moment of time, there are following options (`name: default`) available for the JS Obfuscator: