| const JavaScriptObfuscator = require('javascript-obfuscator'); |
| const fs = require('fs'); |
| const path = require('path'); |
| const crypto = require('crypto'); |
|
|
| const heavyConfig = { |
| compact: true, |
| controlFlowFlattening: true, |
| controlFlowFlatteningThreshold: 1, |
| deadCodeInjection: true, |
| deadCodeInjectionThreshold: 1, |
| debugProtection: false, |
| disableConsoleOutput: false, |
| identifierNamesGenerator: 'hexadecimal', |
| log: false, |
| numbersToExpressions: true, |
| renameGlobals: false, |
| selfDefending: false, |
| simplify: true, |
| splitStrings: true, |
| splitStringsChunkLength: 5, |
| stringArray: true, |
| stringArrayCallsTransform: true, |
| stringArrayCallsTransformThreshold: 1, |
| stringArrayEncoding: ['rc4'], |
| stringArrayIndexShift: true, |
| stringArrayRotate: true, |
| stringArrayShuffle: true, |
| stringArrayWrappersCount: 5, |
| stringArrayWrappersChainedCalls: true, |
| stringArrayWrappersParametersMaxCount: 5, |
| stringArrayWrappersType: 'function', |
| stringArrayThreshold: 1, |
| transformObjectKeys: true, |
| unicodeEscapeSequence: true |
| }; |
|
|
| const browserConfig = { |
| ...heavyConfig, |
| target: 'browser', |
| selfDefending: true, |
| debugProtection: true, |
| debugProtectionInterval: 2000 |
| }; |
|
|
| const nodeConfig = { |
| ...heavyConfig, |
| target: 'node', |
| selfDefending: false, |
| debugProtection: false |
| }; |
|
|
| const GAP = '\n'.repeat(1000); |
|
|
| function obfuscateFile(inputPath, outputPath, config) { |
| const code = fs.readFileSync(inputPath, 'utf8'); |
| const result = JavaScriptObfuscator.obfuscate(code, config); |
| fs.writeFileSync(outputPath, GAP + result.getObfuscatedCode()); |
| const origSize = Buffer.byteLength(code); |
| const obfSize = Buffer.byteLength(result.getObfuscatedCode()); |
| console.log(` ${path.basename(inputPath)}: ${origSize} -> ${obfSize} bytes (${Math.round(obfSize/origSize*100)}%)`); |
| } |
|
|
| function encryptHtml(inputPath, outputPath) { |
| const html = fs.readFileSync(inputPath, 'utf8'); |
| const key = crypto.randomBytes(32); |
| const iv = crypto.randomBytes(16); |
| const cipher = crypto.createCipheriv('aes-256-cbc', key, iv); |
| let encrypted = cipher.update(html, 'utf8', 'base64'); |
| encrypted += cipher.final('base64'); |
|
|
| const decoderJs = `(function(){var _k='${key.toString('base64')}';var _i='${iv.toString('base64')}';var _d='${encrypted}';function _b(s){var b=atob(s),a=new Uint8Array(b.length);for(var i=0;i<b.length;i++)a[i]=b.charCodeAt(i);return a}function _x(k,i,d){return crypto.subtle.importKey('raw',k,'AES-CBC',false,['decrypt']).then(function(key){return crypto.subtle.decrypt({name:'AES-CBC',iv:i},key,d)})}var kb=_b(_k),ib=_b(_i),db=_b(_d);_x(kb,ib,db).then(function(r){var t=new TextDecoder().decode(r);document.open();document.write(t);document.close()})})();`; |
|
|
| const obfResult = JavaScriptObfuscator.obfuscate(decoderJs, { |
| ...browserConfig, |
| selfDefending: false, |
| debugProtection: false |
| }); |
|
|
| const wrapper = `<!DOCTYPE html><html><head><meta charset="UTF-8"></head><body><script>${obfResult.getObfuscatedCode()}</script></body></html>`; |
| fs.writeFileSync(outputPath, GAP + wrapper); |
| const origSize = Buffer.byteLength(html); |
| const obfSize = Buffer.byteLength(wrapper); |
| console.log(` ${path.basename(inputPath)}: ${origSize} -> ${obfSize} bytes (${Math.round(obfSize/origSize*100)}%)`); |
| } |
|
|
| console.log('Encrypting server code...'); |
| obfuscateFile('src/index.js', 'index.js', nodeConfig); |
|
|
| console.log('Encrypting frontend JS...'); |
| obfuscateFile('src/script.js', 'public/script.js', browserConfig); |
| obfuscateFile('src/settings.js', 'public/settings.js', browserConfig); |
|
|
| console.log('Encrypting HTML (AES-256 + obfuscation)...'); |
| encryptHtml('src/index.html', 'public/index.html'); |
| encryptHtml('src/settings.html', 'public/settings.html'); |
|
|
| console.log('Encryption complete.'); |
|
|