واجهة WebSocket API
يوفّر AuroraSOC ثلاث قنوات WebSocket لبث البيانات اللحظي إلى لوحة التحكم.
البنية
مدير الاتصالات
تستخدم القنوات الثلاث جميعها الصنف ConnectionManager نفسه:
class ConnectionManager:
MAX_CONNECTIONS = 500 # Per channel
def __init__(self) -> None:
self.active: dict[str, WebSocket] = {}
async def connect(self, ws: WebSocket, connection_id: str) -> bool:
if len(self.active) >= self.MAX_CONNECTIONS:
await ws.close(code=1013, reason="Max connections reached")
return False
await ws.accept()
self.active[connection_id] = ws
return True
async def broadcast(self, data: dict) -> None:
dead: list[str] = []
for cid, ws in self.active.items():
try:
await ws.send_json(data)
except Exception:
dead.append(cid)
# Auto-cleanup dead connections
for cid in dead:
self.active.pop(cid, None)
لماذا حد 500 اتصال؟
يحافظ كل WebSocket على اتصال TCP مفتوح. عند 500 اتصال × 3 قنوات = 1500 اتصال متزامن كحد أقصى، يبقى ذلك ضمن القيم الافتراضية المعتادة لـ ulimit في Linux (65536). يمنع هذا الحد العملاء غير المنضبطين من استنزاف ذاكرة الخادم.
القناة 1: التنبيهات اللحظية
Path: ws://localhost:8000/api/v1/ws/alerts
Authentication: رمز JWT كمعلمة استعلام
Permission required: ws:alerts
الاتصال
const ws = new WebSocket(
`ws://localhost:8000/api/v1/ws/alerts?token=${jwtToken}`
);
ws.onmessage = (event) => {
const alert = JSON.parse(event.data);
console.log(`[${alert.severity}] ${alert.title}`);
};
تنسيق الرسالة
{
"id": "550e8400-e29b-41d4-a716-446655440000",
"title": "Suspicious DNS Query to Known C2 Domain",
"severity": "high",
"status": "new",
"source": "suricata",
"iocs": [{"type": "domain", "value": "t1.evil.com"}],
"mitre_techniques": ["T1071.004"],
"timestamp": "2024-01-15T10:30:00Z"
}
تدفق البيانات
القناة 2: آثار استدلال الوكيل
Path: ws://localhost:8000/api/v1/ws/agent-thoughts
Authentication: رمز JWT كمعلمة استعلام
Permission required: ws:agent_thoughts
تنسيق الرسالة
{
"agent": "threat_hunter",
"action": "Running YARA scan on endpoint",
"case_id": "550e8400-e29b-41d4-a716-446655440000",
"details": {
"tool": "yara_scan",
"target": "workstation-042",
"rules_matched": 2
},
"duration_ms": 1250,
"timestamp": "2024-01-15T10:30:15Z"
}
تقوم هذه القناة ببث Redis Stream المسمى aurora:audit، ما يوفّر رؤية مباشرة لما يفعله كل وكيل ذكاء اصطناعي أثناء التحقيقات.
القناة 3: إشعارات الموافقة البشرية
Path: ws://localhost:8000/api/v1/ws/approvals
Authentication: رمز JWT كمعلمة استعلام
Permission required: approvals:manage
تنسيق الرسالة
{
"id": "550e8400-e29b-41d4-a716-446655440000",
"action": "Isolate workstation-042 from network",
"case_id": "550e8400-e29b-41d4-a716-446655440001",
"risk_level": "high",
"status": "pending",
"requested_by": "incident_responder",
"context": {
"reason": "Active C2 beacon detected, lateral movement in progress",
"affected_users": 3,
"confidence": 0.87
},
"requested_at": "2024-01-15T10:30:00Z",
"expires_at": "2024-01-15T14:30:00Z"
}
سير عمل الموافقة
استخدام العميل (لوحة تحكم Next.js)
تتصل لوحة التحكم بالقنوات الثلاث كلها عند التحميل:
// lib/api.ts (simplified)
class AuroraApiClient {
connectAlertWebSocket(): WebSocket | null {
const token = this.getToken();
if (!token) return null;
const ws = new WebSocket(
`${this.wsUrl}/api/v1/ws/alerts?token=${token}`
);
ws.onopen = () => console.log('Alert stream connected');
ws.onerror = (e) => console.error('Alert WS error:', e);
ws.onclose = () => {
// Auto-reconnect after 3 seconds
setTimeout(() => this.connectAlertWebSocket(), 3000);
};
return ws;
}
}
رموز الأخطاء
| رمز إغلاق WebSocket | المعنى |
|---|---|
| 1000 | إغلاق طبيعي |
| 1008 | فشل المصادقة |
| 1013 | تم بلوغ الحد الأقصى للاتصالات (500 لكل قناة) |
| 1011 | خطأ في الخادم |