UNPKG

can-algorithm

Version:

Cortex Algorithm Numeral - Intelligent development automation tool

175 lines (143 loc) 4.84 kB
const express = require('express'); const bodyParser = require('body-parser'); const crypto = require('crypto'); const fs = require('fs').promises; const path = require('path'); const app = express(); app.use(bodyParser.json()); const PORT = process.env.LICENSE_PORT || 3003; const licensesDB = new Map(); const activationsDB = new Map(); const initializeDB = async () => { try { const dbPath = path.join(__dirname, 'licenses.json'); const data = await fs.readFile(dbPath, 'utf8'); const licenses = JSON.parse(data); licenses.forEach(license => { licensesDB.set(license.key, license); }); } catch { const defaultLicenses = [ { key: 'CAN-TEST-TEST-TEST', user: 'Test User', email: 'test@imators.systems', type: 'development', expiration: new Date(Date.now() + 365 * 24 * 60 * 60 * 1000), max_activations: 3 } ]; defaultLicenses.forEach(license => { licensesDB.set(license.key, license); }); } }; app.post('/can/license/validate', (req, res) => { const { license_key, machine_id } = req.body; const license = licensesDB.get(license_key); if (!license) { return res.json({ valid: false, reason: 'License not found' }); } const activation = activationsDB.get(`${license_key}:${machine_id}`); if (!activation) { return res.json({ valid: false, reason: 'License not activated for this machine' }); } if (new Date(license.expiration) < new Date()) { return res.json({ valid: false, reason: 'License expired' }); } res.json({ valid: true, user: license.user, expiration: license.expiration, permissions: { commands: ['scan', 'ask', 'proj', 'crypt', 'custom'], max_requests_per_day: license.type === 'enterprise' ? -1 : 1000, enterprise_features: license.type === 'enterprise' } }); }); app.post('/can/license/activate', (req, res) => { const { license_key, password, machine_id } = req.body; const license = licensesDB.get(license_key); if (!license) { return res.status(400).json({ success: false, error: 'Invalid license key' }); } const existingActivations = Array.from(activationsDB.keys()) .filter(key => key.startsWith(license_key)) .length; if (existingActivations >= license.max_activations) { return res.status(400).json({ success: false, error: 'Maximum activations reached' }); } const activation = { license_key: license_key, machine_id: machine_id, password_hash: crypto.createHash('sha256').update(password).digest('hex'), activated_at: new Date(), last_seen: new Date() }; activationsDB.set(`${license_key}:${machine_id}`, activation); res.json({ success: true, user_id: license.user, message: 'License activated successfully' }); }); app.post('/can/license/deactivate', (req, res) => { const { license_key, machine_id } = req.body; activationsDB.delete(`${license_key}:${machine_id}`); res.json({ success: true, message: 'License deactivated' }); }); app.post('/can/license/generate', (req, res) => { const { email, user, type = 'standard' } = req.body; const segments = []; for (let i = 0; i < 3; i++) { const segment = crypto.randomBytes(2).toString('hex').toUpperCase(); segments.push(segment); } const key = `CAN-${segments.join('-')}`; const license = { key: key, user: user, email: email, type: type, expiration: new Date(Date.now() + 365 * 24 * 60 * 60 * 1000), max_activations: type === 'enterprise' ? 10 : 3, created_at: new Date() }; licensesDB.set(key, license); res.json({ success: true, license_key: key, expiration: license.expiration }); }); app.get('/can/license/health', (req, res) => { res.json({ status: 'healthy', version: '1.0.0', licenses_count: licensesDB.size, activations_count: activationsDB.size }); }); app.listen(PORT, async () => { await initializeDB(); console.log(`License Service running on port ${PORT}`); });