Update main.py

This commit is contained in:
ilyastar9999
2025-12-03 18:15:14 +03:00
parent abc891ae43
commit 34662c2a11

View File

@@ -243,39 +243,36 @@ async def socketio_listener():
async def process_flag_stolen(event_data): async def process_flag_stolen(event_data):
"""Process flag_stolen event""" """Process flag_stolen event"""
try: try:
print(f"[DEBUG] process_flag_stolen called with event_data: {event_data}")
attacker_id = event_data.get('attacker_id') attacker_id = event_data.get('attacker_id')
victim_id = event_data.get('victim_id') victim_id = event_data.get('victim_id')
task_id = event_data.get('task_id') task_id = event_data.get('task_id')
attacker_delta = event_data.get('attacker_delta', 0) attacker_delta = event_data.get('attacker_delta', 0)
print(f"[DEBUG] attacker_id={attacker_id}, victim_id={victim_id}, task_id={task_id}, attacker_delta={attacker_delta}")
if attacker_id is None or victim_id is None: if attacker_id is None or victim_id is None:
print("[DEBUG] attacker_id or victim_id is None, skipping event")
return return
service_name = task_names.get(task_id, f"task_{task_id}") service_name = task_names.get(task_id, f"task_{task_id}")
timestamp = datetime.utcnow() timestamp = datetime.utcnow()
is_our_attack = attacker_id == OUR_TEAM_ID is_our_attack = attacker_id == OUR_TEAM_ID
is_attack_to_us = victim_id == OUR_TEAM_ID is_attack_to_us = victim_id == OUR_TEAM_ID
print(f"[DEBUG] is_our_attack={is_our_attack}, is_attack_to_us={is_attack_to_us}, ALERT_THRESHOLD_POINTS={ALERT_THRESHOLD_POINTS}")
if is_our_attack or is_attack_to_us: if is_our_attack or is_attack_to_us:
conn = await db_pool.acquire() conn = await db_pool.acquire()
try: try:
attack_id = f"flag_{attacker_id}_{victim_id}_{task_id}_{int(timestamp.timestamp())}" attack_id = f"flag_{attacker_id}_{victim_id}_{task_id}_{int(timestamp.timestamp())}"
await conn.execute(""" await conn.execute("""
INSERT INTO attacks (attack_id, attacker_team_id, victim_team_id, service_name, timestamp, points, is_our_attack, is_attack_to_us) INSERT INTO attacks (attack_id, attacker_team_id, victim_team_id, service_name, timestamp, points, is_our_attack, is_attack_to_us)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
ON CONFLICT (attack_id) DO NOTHING ON CONFLICT (attack_id) DO NOTHING
""", attack_id, attacker_id, victim_id, service_name, timestamp, float(attacker_delta), is_our_attack, is_attack_to_us) """, attack_id, attacker_id, victim_id, service_name, timestamp, float(attacker_delta), is_our_attack, is_attack_to_us)
if is_our_attack: if is_our_attack:
print(f" ✅ We stole flag from Team {victim_id} on {service_name} (+{attacker_delta:.2f} FP)") print(f" ✅ We stole flag from Team {victim_id} on {service_name} (+{attacker_delta:.2f} FP)")
elif is_attack_to_us: elif is_attack_to_us:
print(f" ⚠️ Team {attacker_id} stole flag from us on {service_name} (-{attacker_delta:.2f} FP)") print(f" ⚠️ Team {attacker_id} stole flag from us on {service_name} (-{attacker_delta:.2f} FP)")
if attacker_delta >= ALERT_THRESHOLD_POINTS: if attacker_delta >= ALERT_THRESHOLD_POINTS:
# Create and send alert print(f"[DEBUG] Sending Telegram alert: attacker_delta={attacker_delta} >= ALERT_THRESHOLD_POINTS={ALERT_THRESHOLD_POINTS}")
alert_message = f"🚨 ATTACK DETECTED!\nTeam {attacker_id} stole flag from {service_name}\nPoints lost: {attacker_delta:.2f} FP" alert_message = f"🚨 ATTACK DETECTED!\nTeam {attacker_id} stole flag from {service_name}\nPoints lost: {attacker_delta:.2f} FP"
alert_id = await conn.fetchval(""" alert_id = await conn.fetchval("""
INSERT INTO attack_alerts (attack_id, alert_type, severity, message) INSERT INTO attack_alerts (attack_id, alert_type, severity, message)
VALUES ( VALUES (
@@ -286,10 +283,11 @@ async def socketio_listener():
) )
RETURNING id RETURNING id
""", attack_id, alert_message) """, attack_id, alert_message)
await send_telegram_alert(alert_message) await send_telegram_alert(alert_message)
await conn.execute("UPDATE attack_alerts SET notified = true WHERE id = $1", alert_id) await conn.execute("UPDATE attack_alerts SET notified = true WHERE id = $1", alert_id)
print(f" 📱 Alert sent to Telegram") print(f" 📱 Alert sent to Telegram")
else:
print(f"[DEBUG] No alert sent: attacker_delta={attacker_delta} < ALERT_THRESHOLD_POINTS={ALERT_THRESHOLD_POINTS}")
finally: finally:
await db_pool.release(conn) await db_pool.release(conn)
except Exception as e: except Exception as e: