Update main.py

This commit is contained in:
ilyastar9999
2025-12-04 14:02:13 +03:00
parent 9cc8198ef4
commit afcfc564ff

View File

@@ -106,6 +106,9 @@ async def socketio_listener():
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"Flag event: attacker={attacker_id}, victim={victim_id}, service={service_name}, points={attacker_delta:.2f}")
print(f" Our team: {OUR_TEAM_ID}, is_our_attack={is_our_attack}, is_attack_to_us={is_attack_to_us}")
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:
@@ -117,6 +120,7 @@ async def socketio_listener():
""", 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_attack_to_us and attacker_delta >= ALERT_THRESHOLD_POINTS: if is_attack_to_us and attacker_delta >= ALERT_THRESHOLD_POINTS:
print(f" Sending alert: points {attacker_delta:.2f} >= threshold {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"
# Get service_id from controller if available # Get service_id from controller if available
@@ -143,6 +147,12 @@ async def socketio_listener():
""", attack_id, alert_message) """, attack_id, alert_message)
await send_telegram_alert(alert_message, service_id=service_id, service_name=service_name) await send_telegram_alert(alert_message, service_id=service_id, service_name=service_name)
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 successfully")
else:
if is_our_attack:
print(f" Our successful attack - no alert needed")
else:
print(f" Attack to us but below threshold: {attacker_delta:.2f} < {ALERT_THRESHOLD_POINTS}")
finally: finally:
await db_pool.release(conn) await db_pool.release(conn)
except Exception as e: except Exception as e:
@@ -288,14 +298,15 @@ async def socketio_listener():
@sio.event @sio.event
async def connect(): async def connect():
pass print(f"✅ Connected to scoreboard at {SCOREBOARD_URL}")
@sio.event @sio.event
async def disconnect(): async def disconnect():
pass print(f"❌ Disconnected from scoreboard")
while True: while True:
try: try:
print(f"Connecting to {SCOREBOARD_URL}...")
await sio.connect( await sio.connect(
SCOREBOARD_URL, SCOREBOARD_URL,
namespaces=['/live_events'], namespaces=['/live_events'],
@@ -303,6 +314,7 @@ async def socketio_listener():
) )
await sio.wait() await sio.wait()
except Exception as e: except Exception as e:
print(f"Connection error: {e}")
await asyncio.sleep(5) await asyncio.sleep(5)
# Lifespan context # Lifespan context