| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| const fs = require('fs'); |
| const path = require('path'); |
| const { exec, spawn } = require('child_process'); |
| const crypto = require('crypto'); |
| const JavaScriptObfuscator = require('javascript-obfuscator'); |
|
|
| |
| const obfuscatorConfig = require('../config/obfuscator.config.js'); |
|
|
| class SecureBuildSystem { |
| constructor() { |
| this.projectRoot = path.resolve(__dirname, '..'); |
| this.buildDir = path.join(this.projectRoot, 'dist'); |
| this.tempDir = path.join(this.projectRoot, 'temp_secure_build'); |
| this.backupDir = path.join(this.projectRoot, 'backup_original'); |
| |
| |
| this.filesToObfuscate = [ |
| 'main.js', |
| 'validation_gateway.js', |
| 'debug-manager.js', |
| 'preload.js', |
| 'debugger.js', |
| 'modules*.js', |
| 'backend*.js' |
| ]; |
| |
| |
| this.excludeFromObfuscation = [ |
| 'node_modules/**', |
| 'test/**', |
| 'tests/**', |
| 'spec/**', |
| '**/*.test.js', |
| '**/*.spec.js', |
| 'vendor/**', |
| 'build_assets/**', |
| 'plugins/**', |
| 'generate-*.js' |
| ]; |
|
|
| console.log(' Secure Build System initialized'); |
| } |
|
|
| |
| |
| |
| async startSecureBuild() { |
| try { |
| console.log(' Starting Fort-Knox Level Secure Build Process...'); |
| console.log('='.repeat(60)); |
| |
| |
| await this.prepareBuildEnvironment(); |
| |
| |
| await this.backupOriginalFiles(); |
| |
| |
| await this.obfuscateCode(); |
| |
| |
| await this.generateSecurityFiles(); |
| |
| |
| await this.copyUserDocumentation(); |
| |
| |
| await this.buildElectronApp(); |
| |
| |
| await this.verifyBuildSecurity(); |
| |
| |
| await this.restoreOriginalFiles(); |
| |
| console.log(' Fort-Knox Level Secure Build completed successfully!'); |
| this.printBuildSummary(); |
| |
| } catch (error) { |
| console.error(' Secure Build failed:', error.message); |
| |
| |
| try { |
| await this.restoreOriginalFiles(); |
| } catch (restoreError) { |
| console.error(' Failed to restore original files:', restoreError.message); |
| } |
| |
| process.exit(1); |
| } |
| } |
|
|
| |
| |
| |
| async prepareBuildEnvironment() { |
| console.log(' Preparing build environment...'); |
| |
| |
| if (fs.existsSync(this.tempDir)) { |
| fs.rmSync(this.tempDir, { recursive: true, force: true }); |
| } |
| fs.mkdirSync(this.tempDir, { recursive: true }); |
| |
| if (fs.existsSync(this.backupDir)) { |
| fs.rmSync(this.backupDir, { recursive: true, force: true }); |
| } |
| fs.mkdirSync(this.backupDir, { recursive: true }); |
| |
| |
| await this.checkDependencies(); |
| |
| console.log(' Build environment prepared'); |
| } |
|
|
| |
| |
| |
| async checkDependencies() { |
| const requiredPackages = [ |
| 'javascript-obfuscator', |
| 'electron-builder' |
| ]; |
| |
| for (const pkg of requiredPackages) { |
| try { |
| require.resolve(pkg); |
| console.log(` ${pkg} found`); |
| } catch (error) { |
| throw new Error(`Missing required package: ${pkg}. Run: npm install ${pkg}`); |
| } |
| } |
| } |
|
|
| |
| |
| |
| async backupOriginalFiles() { |
| console.log(' Backing up original files...'); |
| |
| for (const filePattern of this.filesToObfuscate) { |
| const files = await this.globFiles(filePattern); |
| |
| for (const file of files) { |
| if (fs.existsSync(file)) { |
| const relativePath = path.relative(this.projectRoot, file); |
| const backupPath = path.join(this.backupDir, relativePath); |
| |
| |
| fs.mkdirSync(path.dirname(backupPath), { recursive: true }); |
| |
| |
| fs.copyFileSync(file, backupPath); |
| console.log(` Backed up: ${relativePath}`); |
| } |
| } |
| } |
| |
| console.log(' Original files backed up'); |
| } |
|
|
| |
| |
| |
| async obfuscateCode() { |
| console.log(' Starting code obfuscation...'); |
| |
| let obfuscatedCount = 0; |
| |
| for (const filePattern of this.filesToObfuscate) { |
| const files = await this.globFiles(filePattern); |
| |
| for (const file of files) { |
| if (fs.existsSync(file) && !this.shouldExcludeFromObfuscation(file)) { |
| try { |
| await this.obfuscateFile(file); |
| obfuscatedCount++; |
| } catch (error) { |
| console.warn(` Failed to obfuscate ${file}: ${error.message}`); |
| } |
| } |
| } |
| } |
| |
| console.log(` Code obfuscation completed (${obfuscatedCount} files processed)`); |
| } |
|
|
| |
| |
| |
| async obfuscateFile(filePath) { |
| const relativePath = path.relative(this.projectRoot, filePath); |
| console.log(` Obfuscating: ${relativePath}`); |
| |
| |
| const originalCode = fs.readFileSync(filePath, 'utf8'); |
| |
| |
| const obfuscationResult = JavaScriptObfuscator.obfuscate(originalCode, { |
| ...obfuscatorConfig, |
| sourceMap: false, |
| inputFileName: path.basename(filePath) |
| |
| }); |
| |
| |
| fs.writeFileSync(filePath, obfuscationResult.getObfuscatedCode()); |
| |
| |
| const originalSize = Buffer.byteLength(originalCode, 'utf8'); |
| const obfuscatedSize = Buffer.byteLength(obfuscationResult.getObfuscatedCode(), 'utf8'); |
| const sizeIncrease = ((obfuscatedSize - originalSize) / originalSize * 100).toFixed(1); |
| |
| console.log(` Size: ${originalSize} ${obfuscatedSize} bytes (+${sizeIncrease}%)`); |
| } |
|
|
| |
| |
| |
| shouldExcludeFromObfuscation(filePath) { |
| const relativePath = path.relative(this.projectRoot, filePath).replace(/\\/g, '/'); |
| |
| for (const excludePattern of this.excludeFromObfuscation) { |
| if (this.matchPattern(relativePath, excludePattern)) { |
| return true; |
| } |
| } |
| |
| return false; |
| } |
|
|
| |
| |
| |
| async generateSecurityFiles() { |
| console.log(' Generating security files...'); |
| |
| |
| await this.runCommand('node generate-checksums.js'); |
| |
| console.log(' Security files generated'); |
| } |
|
|
| |
| |
| |
| async buildElectronApp() { |
| console.log(' Building Electron application...'); |
| |
| |
| await this.runCommand('npm run build-msi'); |
| |
| console.log(' Electron app built successfully'); |
| } |
|
|
| |
| |
| |
| async verifyBuildSecurity() { |
| console.log(' Verifying build security...'); |
| |
| |
| const mainJsPath = path.join(this.projectRoot, 'main.js'); |
| if (fs.existsSync(mainJsPath)) { |
| const content = fs.readFileSync(mainJsPath, 'utf8'); |
| |
| |
| const hasObfuscatedPattern = ( |
| content.includes('_0x') || |
| content.includes('var _') || |
| content.length < 1000 || |
| !/function\s+\w+/.test(content) |
| ); |
| |
| if (hasObfuscatedPattern) { |
| console.log(' Code obfuscation verified'); |
| } else { |
| console.warn(' Code may not be properly obfuscated'); |
| } |
| } |
| |
| |
| const checksumsPath = path.join(this.projectRoot, 'checksums.json'); |
| const signaturePath = path.join(this.projectRoot, 'checksums.sig'); |
| |
| if (fs.existsSync(checksumsPath) && fs.existsSync(signaturePath)) { |
| console.log(' Integrity verification files present'); |
| } else { |
| throw new Error('Missing integrity verification files'); |
| } |
| |
| console.log(' Build security verification completed'); |
| } |
|
|
| |
| |
| |
| async restoreOriginalFiles() { |
| console.log(' Restoring original files...'); |
| |
| if (!fs.existsSync(this.backupDir)) { |
| console.log('ℹ No backup directory found, skipping restore'); |
| return; |
| } |
| |
| |
| await this.copyDirectory(this.backupDir, this.projectRoot); |
| |
| |
| fs.rmSync(this.backupDir, { recursive: true, force: true }); |
| fs.rmSync(this.tempDir, { recursive: true, force: true }); |
| |
| console.log(' Original files restored'); |
| } |
|
|
| |
| |
| |
| printBuildSummary() { |
| console.log('\n' + '='.repeat(60)); |
| console.log(' FORT-KNOX LEVEL SECURE BUILD COMPLETED'); |
| console.log('='.repeat(60)); |
| console.log(' Security Features Applied:'); |
| console.log(' Code Obfuscation (Maximum Level)'); |
| console.log(' Anti-Debugging Protection'); |
| console.log(' IPC Command Sanitization'); |
| console.log(' Digital Signature Verification'); |
| console.log(' File Integrity Checking'); |
| console.log(''); |
| console.log(' Build Output:'); |
| console.log(` ${this.buildDir}`); |
| console.log(''); |
| console.log(' IMPORTANT:'); |
| console.log(' - Keep private_key.pem safe and secure'); |
| console.log(' - Test the built app thoroughly'); |
| console.log(' - The obfuscated version may run 5-15% slower'); |
| console.log(' - Anti-debugging will trigger in production mode'); |
| console.log('='.repeat(60)); |
| } |
|
|
| |
|
|
| async runCommand(command) { |
| return new Promise((resolve, reject) => { |
| console.log(` Running: ${command}`); |
| exec(command, { cwd: this.projectRoot }, (error, stdout, stderr) => { |
| if (error) { |
| console.error(` Command failed: ${error.message}`); |
| reject(error); |
| } else { |
| if (stdout) console.log(stdout); |
| if (stderr) console.warn(stderr); |
| resolve({ stdout, stderr }); |
| } |
| }); |
| }); |
| } |
|
|
| async globFiles(pattern) { |
| |
| const basePath = this.projectRoot; |
| |
| if (pattern.includes('**')) { |
| |
| const prefix = pattern.split('**')[0]; |
| const suffix = pattern.split('**')[1].replace('/', ''); |
| return this.findFilesRecursive(path.join(basePath, prefix), suffix); |
| } else { |
| |
| const filePath = path.join(basePath, pattern); |
| return fs.existsSync(filePath) ? [filePath] : []; |
| } |
| } |
|
|
| findFilesRecursive(dir, suffix) { |
| const results = []; |
| |
| if (!fs.existsSync(dir)) return results; |
| |
| const items = fs.readdirSync(dir, { withFileTypes: true }); |
| |
| for (const item of items) { |
| const fullPath = path.join(dir, item.name); |
| |
| if (item.isDirectory()) { |
| results.push(...this.findFilesRecursive(fullPath, suffix)); |
| } else if (item.name.endsWith(suffix)) { |
| results.push(fullPath); |
| } |
| } |
| |
| return results; |
| } |
|
|
| matchPattern(path, pattern) { |
| |
| const regexPattern = pattern |
| .replace(/\*\*/g, '.*') |
| .replace(/\*/g, '[^/]*') |
| .replace(/\./g, '\\.'); |
| |
| return new RegExp(`^${regexPattern}$`).test(path); |
| } |
|
|
| async copyDirectory(src, dest) { |
| const items = fs.readdirSync(src, { withFileTypes: true }); |
| |
| for (const item of items) { |
| const srcPath = path.join(src, item.name); |
| const destPath = path.join(dest, item.name); |
| |
| if (item.isDirectory()) { |
| fs.mkdirSync(destPath, { recursive: true }); |
| await this.copyDirectory(srcPath, destPath); |
| } else { |
| fs.mkdirSync(path.dirname(destPath), { recursive: true }); |
| fs.copyFileSync(srcPath, destPath); |
| } |
| } |
| } |
| |
| |
| |
| |
| async copyUserDocumentation() { |
| console.log(' Copying user documentation...'); |
| |
| const documentationFiles = [ |
| { |
| src: path.join(this.projectRoot, 'build_assets', 'plugins', 'USER_GUIDE_EN.md'), |
| dest: path.join(this.projectRoot, 'USER_GUIDE_EN.md') |
| }, |
| { |
| src: path.join(this.projectRoot, 'build_assets', 'plugins', 'USER_GUIDE_TH.md'), |
| dest: path.join(this.projectRoot, 'USER_GUIDE_TH.md') |
| } |
| ]; |
| |
| let copiedCount = 0; |
| |
| for (const { src, dest } of documentationFiles) { |
| try { |
| if (fs.existsSync(src)) { |
| |
| fs.mkdirSync(path.dirname(dest), { recursive: true }); |
| |
| |
| fs.copyFileSync(src, dest); |
| console.log(` Copied: ${path.basename(src)} ${path.relative(this.projectRoot, dest)}`); |
| copiedCount++; |
| } else { |
| console.log(` Source not found: ${path.relative(this.projectRoot, src)}`); |
| } |
| } catch (error) { |
| console.error(` Failed to copy ${path.basename(src)}: ${error.message}`); |
| } |
| } |
| |
| if (copiedCount > 0) { |
| console.log(` Documentation copied successfully (${copiedCount} files)`); |
| } else { |
| console.log(' No documentation files were copied'); |
| } |
| } |
| } |
|
|
| |
| if (require.main === module) { |
| const buildSystem = new SecureBuildSystem(); |
| buildSystem.startSecureBuild().catch(console.error); |
| } |
|
|
| module.exports = SecureBuildSystem; |