can-algorithm
Version:
Cortex Algorithm Numeral - Intelligent development automation tool
62 lines (50 loc) • 1.82 kB
JavaScript
const fs = require('fs').promises;
const path = require('path');
const crypto = require('crypto');
const PROTECTION_HASH = crypto.createHash('sha256').update('can-algorithm-protection').digest('hex');
const PROTECTION_PATH = path.join(process.cwd(), '.can', '.protection');
const LOG_PATH = path.join(process.cwd(), 'logs', 'protection.log');
async function checkAndRestore() {
try {
const content = await fs.readFile(PROTECTION_PATH, 'utf8');
if (content !== PROTECTION_HASH) {
await logEvent('Protection file corrupted, restoring...');
await restore();
}
} catch (error) {
await logEvent('Protection file missing, restoring...');
await restore();
}
await checkCriticalFiles();
}
async function restore() {
await fs.mkdir(path.dirname(PROTECTION_PATH), { recursive: true, mode: 0o700 });
await fs.writeFile(PROTECTION_PATH, PROTECTION_HASH, { mode: 0o600 });
await logEvent('Protection restored successfully');
}
async function checkCriticalFiles() {
const criticalFiles = [
'can.js',
'core/can-core.js',
'core/license.js',
'core/protection.js'
];
for (const file of criticalFiles) {
try {
await fs.access(file);
} catch {
await logEvent(`Critical file missing: ${file}`);
}
}
}
async function logEvent(message) {
const timestamp = new Date().toISOString();
const logEntry = `[${timestamp}] ${message}\n`;
await fs.mkdir(path.dirname(LOG_PATH), { recursive: true });
await fs.appendFile(LOG_PATH, logEntry);
}
if (require.main === module) {
checkAndRestore();
}
module.exports = { checkAndRestore };