javascript-obfuscator.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/env node
  2. var commands = require('commander'),
  3. fs = require('fs'),
  4. path = require('path'),
  5. JavaScriptObfuscator = require('../dist/index'),
  6. data = '',
  7. defaultOptions = {
  8. compact: true,
  9. debugProtection: false,
  10. debugProtectionInterval: false,
  11. disableConsoleOutput: true,
  12. encodeUnicodeLiterals: false,
  13. reservedNames: [],
  14. rotateUnicodeArray: true,
  15. selfDefending: true,
  16. unicodeArray: true,
  17. unicodeArrayThreshold: 0.8,
  18. wrapUnicodeArrayCalls: true
  19. };
  20. configureProcess();
  21. configureCommands();
  22. if (!isDataExist()) {
  23. commands.outputHelp();
  24. return 0;
  25. }
  26. function buildOptions () {
  27. var options = {},
  28. availableOptions = Object.keys(defaultOptions);
  29. for (var option in commands) {
  30. if (availableOptions.indexOf(option) === -1) {
  31. continue;
  32. }
  33. options[option] = commands[option];
  34. }
  35. return Object.assign({}, defaultOptions, options);
  36. }
  37. function configureCommands () {
  38. commands
  39. .version(getBuildVersion(), '-v, --version')
  40. .usage('[options] STDIN STDOUT')
  41. .option('--compact <boolean>', 'Disable one line output code compacting', parseBoolean)
  42. .option('--debugProtection <boolean>', 'Disable browser Debug panel (can cause DevTools enabled browser freeze)', parseBoolean)
  43. .option('--debugProtectionInterval <boolean>', 'Disable browser Debug panel even after page was loaded (can cause DevTools enabled browser freeze)', parseBoolean)
  44. .option('--disableConsoleOutput <boolean>', 'Allow console.log, console.info, console.error and console.warn messages output into browser console', parseBoolean)
  45. .option('--encodeUnicodeLiterals <boolean>', 'All literals in Unicode array become encoded in Base64 (this option can slightly slow down your code speed)', parseBoolean)
  46. .option('--reservedNames <list>', 'Disable obfuscation of variable names, function names and names of function parameters that match the passed RegExp patterns (comma separated)', (val) => val.split(','))
  47. .option('--rotateUnicodeArray <boolean>', 'Disable rotation of unicode array values during obfuscation', parseBoolean)
  48. .option('--selfDefending <boolean>', 'Disables self-defending for obfuscated code', parseBoolean)
  49. .option('--unicodeArray <boolean>', 'Disables gathering of all literal strings into an array and replacing every literal string with an array call', parseBoolean)
  50. .option('--unicodeArrayThreshold <number>', 'The probability that the literal string will be inserted into unicodeArray (Default: 0.8, Min: 0, Max: 1)', parseFloat)
  51. .option('--wrapUnicodeArrayCalls <boolean>', 'Disables usage of special access function instead of direct array call', parseBoolean)
  52. .parse(process.argv);
  53. commands.on('--help', function () {
  54. var isWindows = process.platform == 'win32';
  55. console.log(' Examples:\n');
  56. console.log(' %> javascript-obfuscator < in.js > out.js');
  57. if (isWindows) {
  58. console.log(' %> type in1.js in2.js | javascript-obfuscator > out.js');
  59. } else {
  60. console.log(' %> cat in1.js in2.js | javascript-obfuscator > out.js');
  61. }
  62. console.log('');
  63. process.exit();
  64. });
  65. }
  66. function configureProcess () {
  67. process.stdin.setEncoding('utf-8');
  68. process.stdin.on('readable', function () {
  69. var chunk;
  70. while (chunk = process.stdin.read()) {
  71. data += chunk;
  72. }
  73. });
  74. process.stdin.on('end', processData);
  75. }
  76. function getBuildVersion () {
  77. var packageConfig = fs.readFileSync(
  78. path.join(
  79. path.dirname(
  80. fs.realpathSync(process.argv[1])
  81. ),
  82. '../package.json'
  83. )
  84. );
  85. return JSON.parse(packageConfig).version;
  86. }
  87. function isDataExist () {
  88. return !process.env.__DIRECT__ && !process.stdin.isTTY;
  89. }
  90. function parseBoolean (value) {
  91. return value === 'true' || value === '1';
  92. }
  93. function processData() {
  94. process.stdout.write(
  95. JavaScriptObfuscator.obfuscate(data, buildOptions())
  96. );
  97. }