From bff6a18a9b2b9b109870410a9f799df90c10dd1f Mon Sep 17 00:00:00 2001 From: Alexander Borg Date: Mon, 22 Sep 2025 08:43:21 +0200 Subject: [PATCH] Fix jwt-token --- server/services/alertService.js | 172 +++++++++++++++----------------- 1 file changed, 83 insertions(+), 89 deletions(-) diff --git a/server/services/alertService.js b/server/services/alertService.js index 095ff66..dfa581e 100644 --- a/server/services/alertService.js +++ b/server/services/alertService.js @@ -272,94 +272,10 @@ class AlertService { console.log(`✅ Rule "${rule.name}": No device filter (applies to all devices)`); } - // PRIORITY 2: Critical threats and Orlan drones (security overrides) - const isOrlanDrone = detection.drone_type === 1; - const isCriticalThreat = threatAssessment.level === 'critical'; - - if (isOrlanDrone) { - console.log(`🚨 ORLAN DRONE DETECTED - Rule "${rule.name}" will trigger (security override)`); - return true; - } - - if (isCriticalThreat) { - console.log(`🚨 CRITICAL THREAT DETECTED - Rule "${rule.name}" will trigger (security override)`); - return true; - } - - // PRIORITY 3: Threat level requirements - if (rule.min_threat_level) { - const threatLevels = { 'monitoring': 0, 'low': 1, 'medium': 2, 'high': 3, 'critical': 4 }; - const requiredLevel = threatLevels[rule.min_threat_level] || 0; - const currentLevel = threatLevels[threatAssessment.level] || 0; - - if (currentLevel < requiredLevel) { - console.log(`❌ Rule "${rule.name}": Threat level ${threatAssessment.level} below minimum ${rule.min_threat_level}`); - return false; - } else { - console.log(`✅ Rule "${rule.name}": Threat level ${threatAssessment.level} meets minimum ${rule.min_threat_level}`); - } - } - - // PRIORITY 4: Drone type filter - if (rule.drone_types && rule.drone_types.length > 0 && - !rule.drone_types.includes(detection.drone_type)) { - console.log(`❌ Rule "${rule.name}": Drone type ${detection.drone_type} not in allowed types [${rule.drone_types.join(', ')}]`); - return false; - } - - // PRIORITY 5: RSSI thresholds (distance-based) - if (rule.min_rssi && detection.rssi < rule.min_rssi) { - console.log(`❌ Rule "${rule.name}": RSSI ${detection.rssi} below minimum ${rule.min_rssi}`); - return false; - } - - if (rule.max_rssi && detection.rssi > rule.max_rssi) { - console.log(`❌ Rule "${rule.name}": RSSI ${detection.rssi} above maximum ${rule.max_rssi}`); - return false; - } - - // PRIORITY 6: Distance requirements - if (rule.max_distance && threatAssessment.estimatedDistance > rule.max_distance) { - console.log(`❌ Rule "${rule.name}": Distance ${threatAssessment.estimatedDistance}m exceeds maximum ${rule.max_distance}m`); - return false; - } - - // PRIORITY 7: Frequency ranges - if (rule.frequency_ranges && rule.frequency_ranges.length > 0) { - const inRange = rule.frequency_ranges.some(range => - detection.freq >= range.min && detection.freq <= range.max - ); - if (!inRange) { - console.log(`❌ Rule "${rule.name}": Frequency ${detection.freq}MHz not in allowed ranges`); - return false; - } - } - - // PRIORITY 8: Time window and minimum detections - if (rule.min_detections > 1) { - const timeWindowStart = new Date(Date.now() - rule.time_window * 1000); - const recentDetections = await DroneDetection.count({ - where: { - device_id: detection.device_id, - drone_id: detection.drone_id, - server_timestamp: { - [Op.gte]: timeWindowStart - } - } - }); - - if (recentDetections < rule.min_detections) { - console.log(`❌ Rule "${rule.name}": Only ${recentDetections} detections, need ${rule.min_detections}`); - return false; - } - } - - // PRIORITY 9: Cooldown period check + // PRIORITY 2: Cooldown period check (applies even to critical threats for the same device) if (rule.cooldown_period > 0) { const cooldownStart = new Date(Date.now() - rule.cooldown_period * 1000); - console.log(`🔍 Checking cooldown for rule "${rule.name}" (${rule.cooldown_period}s) - looking for alerts after ${cooldownStart.toISOString()}`); - // First, get all recent alert logs for this rule that were successfully sent const recentAlerts = await AlertLog.findAll({ where: { @@ -377,12 +293,8 @@ class AlertService { }] }); - console.log(`🔍 Found ${recentAlerts.length} recent alerts for rule "${rule.name}"`); - // Check if any recent alert was for the same device const deviceAlert = recentAlerts.find(alert => { - console.log(`🔍 Checking alert: detection device_id=${alert.detection?.device_id}, alert device_id=${alert.device_id}, target device_id=${detection.device_id}, sent_at=${alert.sent_at}`); - // For alerts with detection, check device_id match if (alert.detection && alert.detection.device_id === detection.device_id) { return true; @@ -402,6 +314,88 @@ class AlertService { } } + // PRIORITY 3: Critical threats and Orlan drones (security overrides) + const isOrlanDrone = detection.drone_type === 1; + const isCriticalThreat = threatAssessment.level === 'critical'; + + if (isOrlanDrone) { + console.log(`🚨 ORLAN DRONE DETECTED - Rule "${rule.name}" will trigger (security override)`); + return true; + } + + if (isCriticalThreat) { + console.log(`🚨 CRITICAL THREAT DETECTED - Rule "${rule.name}" will trigger (security override)`); + return true; + } + + // PRIORITY 4: Threat level requirements + if (rule.min_threat_level) { + const threatLevels = { 'monitoring': 0, 'low': 1, 'medium': 2, 'high': 3, 'critical': 4 }; + const requiredLevel = threatLevels[rule.min_threat_level] || 0; + const currentLevel = threatLevels[threatAssessment.level] || 0; + + if (currentLevel < requiredLevel) { + console.log(`❌ Rule "${rule.name}": Threat level ${threatAssessment.level} below minimum ${rule.min_threat_level}`); + return false; + } else { + console.log(`✅ Rule "${rule.name}": Threat level ${threatAssessment.level} meets minimum ${rule.min_threat_level}`); + } + } + + // PRIORITY 5: Drone type filter + if (rule.drone_types && rule.drone_types.length > 0 && + !rule.drone_types.includes(detection.drone_type)) { + console.log(`❌ Rule "${rule.name}": Drone type ${detection.drone_type} not in allowed types [${rule.drone_types.join(', ')}]`); + return false; + } + + // PRIORITY 6: RSSI thresholds (distance-based) + if (rule.min_rssi && detection.rssi < rule.min_rssi) { + console.log(`❌ Rule "${rule.name}": RSSI ${detection.rssi} below minimum ${rule.min_rssi}`); + return false; + } + + if (rule.max_rssi && detection.rssi > rule.max_rssi) { + console.log(`❌ Rule "${rule.name}": RSSI ${detection.rssi} above maximum ${rule.max_rssi}`); + return false; + } + + // PRIORITY 7: Distance requirements + if (rule.max_distance && threatAssessment.estimatedDistance > rule.max_distance) { + console.log(`❌ Rule "${rule.name}": Distance ${threatAssessment.estimatedDistance}m exceeds maximum ${rule.max_distance}m`); + return false; + } + + // PRIORITY 8: Frequency ranges + if (rule.frequency_ranges && rule.frequency_ranges.length > 0) { + const inRange = rule.frequency_ranges.some(range => + detection.freq >= range.min && detection.freq <= range.max + ); + if (!inRange) { + console.log(`❌ Rule "${rule.name}": Frequency ${detection.freq}MHz not in allowed ranges`); + return false; + } + } + + // PRIORITY 9: Time window and minimum detections + if (rule.min_detections > 1) { + const timeWindowStart = new Date(Date.now() - rule.time_window * 1000); + const recentDetections = await DroneDetection.count({ + where: { + device_id: detection.device_id, + drone_id: detection.drone_id, + server_timestamp: { + [Op.gte]: timeWindowStart + } + } + }); + + if (recentDetections < rule.min_detections) { + console.log(`❌ Rule "${rule.name}": Only ${recentDetections} detections, need ${rule.min_detections}`); + return false; + } + } + // PRIORITY 10: Active hours and days if (rule.active_hours || rule.active_days) { const now = new Date();