diff --git a/app/factory.py b/app/factory.py index c4eab00..163aa32 100644 --- a/app/factory.py +++ b/app/factory.py @@ -271,11 +271,11 @@ def create_app(settings_path=None): socketio = SocketIO(app, cors_allowed_origins=[], async_mode='threading') - # Initialize rate limiter + # Initialize rate limiter (disabled) limiter = Limiter( app=app, key_func=get_remote_address, - default_limits=["200 per day", "50 per hour"], + default_limits=[], storage_uri="memory://", ) app.limiter = limiter @@ -383,6 +383,69 @@ def create_app(settings_path=None): except Exception as e: logging.debug(f"Could not initialize SSL cert monitor: {e}") + # Start connection monitor for auto-reconnection on server restart/VM reboot + try: + def _connection_monitor(): + """Background thread that monitors and auto-reconnects services.""" + import time + check_interval = 60 # Check every 60 seconds + consecutive_failures = 0 + max_consecutive_failures = 5 + + while True: + time.sleep(check_interval) + try: + ssh_svc = services.get('ssh') + db_svc = services.get('db') + + # Check SSH connection + ssh_ok = False + if ssh_svc: + try: + ssh_ok = ssh_svc.check_connection() + except Exception: + pass + + # Check database connection + db_ok = False + if db_svc: + try: + db_ok = db_svc.check_health() + except Exception: + pass + + if ssh_ok and db_ok: + consecutive_failures = 0 + logging.debug("Connection monitor: All services healthy") + else: + consecutive_failures += 1 + logging.warning(f"Connection monitor: Service issue detected (failures={consecutive_failures})") + + # Force SSH reconnection if SSH check failed + if not ssh_ok and ssh_svc: + try: + ssh_svc.close() + logging.info("Connection monitor: SSH connection reset, will reconnect on next request") + except Exception: + pass + + # Reset database pool if multiple consecutive failures + if not db_ok and db_svc and consecutive_failures >= 3: + try: + db_svc.close_all() + logging.info("Connection monitor: Database pool reset due to connection issues") + except Exception: + pass + + except Exception as e: + logging.debug(f"Connection monitor: Background check failed: {e}") + + conn_monitor_thread = threading.Thread(target=_connection_monitor, daemon=True) + conn_monitor_thread.start() + logging.info("Connection monitor: Background service health monitor started") + except Exception as e: + logging.debug(f"Could not initialize connection monitor: {e}") + app.dune_settings = settings app.dune_services = services diff --git a/app/routes/api.py b/app/routes/api.py index 2b66e0c..a867239 100644 --- a/app/routes/api.py +++ b/app/routes/api.py @@ -45,7 +45,7 @@ def register_api_routes(app, services, settings): limiter = Limiter( app=app, key_func=get_remote_address, - default_limits=["2000 per hour"], # High limit for single admin access + default_limits=[], # Disabled storage_uri="memory://", ) else: diff --git a/app/services/admin.py b/app/services/admin.py index c74a60b..5a2fd29 100644 --- a/app/services/admin.py +++ b/app/services/admin.py @@ -109,7 +109,7 @@ class AdminService: cur.execute("SELECT ps.account_id FROM dune.player_state ps WHERE ps.player_controller_id = %s", [player_id]) account_row = cur.fetchone() - account_id = account_row[0] if account_row else None + account_id = account_row[0] if account_row and isinstance(account_row, (tuple, list)) else (account_row.get('account_id') if account_row else None) ips_to_unblock = [] if account_id: @@ -302,8 +302,8 @@ class AdminService: """, [name, name]) row = cur.fetchone() if row: - pid = row[0] - account_id = row[1] + pid = row[0] if isinstance(row, (tuple, list)) else row.get('funcom_id') + account_id = row[1] if isinstance(row, (tuple, list)) else row.get('account_id') cur.execute(""" INSERT INTO dune.player_ips (player_id, ip_address, updated_at) VALUES (%s, %s, NOW()) @@ -561,7 +561,7 @@ class AdminService: balance = EXCLUDED.balance RETURNING balance """, [player_controller_id, currency_id, new_balance]) - row = cur.fetchone() + cur.fetchone() conn.commit() audit_logger.info(f"EDIT_CURRENCY: player_controller_id={player_controller_id} currency={currency_id} new_balance={new_balance}") return True, {"currency_id": currency_id, "new_balance": new_balance} @@ -603,8 +603,13 @@ class AdminService: conn.rollback() return False, "Item not found" conn.commit() + if isinstance(row, dict): + value_result = row.get(field) + else: + field_index = {'id': 0, 'stack_size': 1, 'quality_level': 2, 'is_new': 3}.get(field, 0) + value_result = row[field_index] audit_logger.info(f"EDIT_ITEM: item_id={item_id} field={field} value={value}") - return True, {"item_id": item_id, "field": field, "value": row[field]} + return True, {"item_id": item_id, "field": field, "value": value_result} except Exception as e: logger.error(f"Failed to edit item {item_id}: {e}") if conn: @@ -630,7 +635,7 @@ class AdminService: if not row: conn.rollback() return False, "Item not found" - template_name = row[0] + template_name = row[0] if isinstance(row, (tuple, list)) else row.get('template_id') cur.execute("DELETE FROM dune.items WHERE id = %s", [item_id]) conn.commit() audit_logger.info(f"DELETE_ITEM: item_id={item_id} template={template_name}") @@ -664,7 +669,8 @@ class AdminService: try: cur = conn.cursor() cur.execute("SELECT id FROM dune.inventories WHERE id = %s", [inventory_id]) - if not cur.fetchone(): + row = cur.fetchone() + if not row or (isinstance(row, dict) and not row.get('id')): conn.rollback() return False, "Inventory not found" @@ -675,7 +681,10 @@ class AdminService: """, [inventory_id, template_id, stack_size, quality_level, inventory_id]) row = cur.fetchone() conn.commit() - new_item_id = row[0] if row else None + if row: + new_item_id = row[0] if isinstance(row, (tuple, list)) else row.get('id') + else: + new_item_id = None audit_logger.info(f"ADD_ITEM: inventory_id={inventory_id} template={template_id} stack={stack_size} quality={quality_level} new_item_id={new_item_id}") return True, {"item_id": new_item_id, "template_id": template_id} except Exception as e: