'use strict'; /** * ╔══════════════════════════════════════════════════════════════════════════╗ * Security — v19.0 | Security Layers for Bot + Dashboard ║ * ╠══════════════════════════════════════════════════════════════════════════╣ * ✅ Rate Limiting: Token bucket per IP/bot ║ * ✅ Input Validation: Whitelist commands + sanitize strings ║ * ✅ Auth: Simple password for dashboard (DASH_PASSWORD env) ║ * ✅ Command Injection Prevention: No eval, parameterized ║ * ✅ Anti-Bot Detection: Randomized usernames + human patterns ║ * ✅ Data Protection: Config sanitization ║ * ╚══════════════════════════════════════════════════════════════════════════╝ */ const crypto = require('crypto'); const tsNow = () => new Date().toISOString().replace('T', ' ').slice(0, 19); class RateLimiter { constructor(options = {}) { this.maxTokens = options.maxTokens || 10; this.refillRate = options.refillRate || 1; this.refillInterval = options.refillInterval || 1000; this.buckets = new Map(); this._cleanupTimer = setInterval(() => this._cleanup(), 60000); } _cleanup() { const now = Date.now(); for (const [key, bucket] of this.buckets) { if (now - bucket.lastRefill > 300000) { this.buckets.delete(key); } } } tryConsume(key, tokens = 1) { const now = Date.now(); let bucket = this.buckets.get(key); if (!bucket) { bucket = { tokens: this.maxTokens, lastRefill: now }; this.buckets.set(key, bucket); } const elapsed = now - bucket.lastRefill; const refill = Math.floor(elapsed / this.refillInterval) * this.refillRate; bucket.tokens = Math.min(this.maxTokens, bucket.tokens + refill); bucket.lastRefill = now; if (bucket.tokens >= tokens) { bucket.tokens -= tokens; return true; } return false; } getRemaining(key) { const bucket = this.buckets.get(key); return bucket ? bucket.tokens : this.maxTokens; } destroy() { clearInterval(this._cleanupTimer); this.buckets.clear(); } } class SecurityManager { constructor(config = {}) { this.dashPassword = config.dashPassword || process.env.DASH_PASSWORD || ''; this.authEnabled = !!this.dashPassword; this.sessions = new Map(); this.sessionTTL = config.sessionTTL || 3600000; this.rateLimiter = new RateLimiter({ maxTokens: config.rateMaxTokens || 30, refillRate: config.rateRefillRate || 1, refillInterval: config.rateRefillInterval || 1000 }); this.apiRateLimiter = new RateLimiter({ maxTokens: config.apiMaxTokens || 20, refillRate: config.apiRefillRate || 1, refillInterval: config.apiRefillInterval || 1000 }); this.allowedCommands = new Set([ 'stop', 'restart', 'plugins', 'optimize', 'come', 'follow', 'sleep', 'wake', 'chat' ]); this.blockedPatterns = [ /eval\s*\(/i, /exec\s*\(/i, /system\s*\(/i, /\.\.\//i, /\.\.\\/, /