Add debug logging to track_client_activity

- Log API token, rustdesk_id, IP, and user agent received
- Log client creation and updates with user agent changes
- Log database commit success
- Add rollback on exception for better error handling
parent 2130b77e
...@@ -809,6 +809,7 @@ def track_client_activity(api_token, rustdesk_id=None): ...@@ -809,6 +809,7 @@ def track_client_activity(api_token, rustdesk_id=None):
"""Track client activity for online status""" """Track client activity for online status"""
try: try:
if not api_token or not api_token.is_valid(): if not api_token or not api_token.is_valid():
logger.debug(f"track_client_activity: Invalid API token")
return return
from app.models import ClientActivity from app.models import ClientActivity
...@@ -818,6 +819,8 @@ def track_client_activity(api_token, rustdesk_id=None): ...@@ -818,6 +819,8 @@ def track_client_activity(api_token, rustdesk_id=None):
ip_address = request.headers.get('X-Forwarded-For', request.remote_addr) ip_address = request.headers.get('X-Forwarded-For', request.remote_addr)
user_agent = request.headers.get('User-Agent') user_agent = request.headers.get('User-Agent')
logger.info(f"track_client_activity: api_token_id={api_token.id}, rustdesk_id={rustdesk_id}, ip={ip_address}, user_agent='{user_agent}'")
if rustdesk_id: if rustdesk_id:
# If rustdesk_id is provided, use specific client # If rustdesk_id is provided, use specific client
client = ClientActivity.query.filter_by( client = ClientActivity.query.filter_by(
...@@ -827,9 +830,11 @@ def track_client_activity(api_token, rustdesk_id=None): ...@@ -827,9 +830,11 @@ def track_client_activity(api_token, rustdesk_id=None):
if client: if client:
# Update existing client # Update existing client
old_user_agent = client.user_agent
client.last_seen = datetime.utcnow() client.last_seen = datetime.utcnow()
client.ip_address = ip_address client.ip_address = ip_address
client.user_agent = user_agent client.user_agent = user_agent
logger.info(f"track_client_activity: Updated existing client {client.id}, user_agent changed from '{old_user_agent}' to '{user_agent}'")
else: else:
# Create new client # Create new client
client = ClientActivity( client = ClientActivity(
...@@ -839,6 +844,7 @@ def track_client_activity(api_token, rustdesk_id=None): ...@@ -839,6 +844,7 @@ def track_client_activity(api_token, rustdesk_id=None):
user_agent=user_agent user_agent=user_agent
) )
db.session.add(client) db.session.add(client)
logger.info(f"track_client_activity: Created new client with rustdesk_id {rustdesk_id}")
else: else:
# If no rustdesk_id provided, update the most recent client for this API token # If no rustdesk_id provided, update the most recent client for this API token
client = ClientActivity.query.filter_by( client = ClientActivity.query.filter_by(
...@@ -847,9 +853,11 @@ def track_client_activity(api_token, rustdesk_id=None): ...@@ -847,9 +853,11 @@ def track_client_activity(api_token, rustdesk_id=None):
if client: if client:
# Update most recent client # Update most recent client
old_user_agent = client.user_agent
client.last_seen = datetime.utcnow() client.last_seen = datetime.utcnow()
client.ip_address = ip_address client.ip_address = ip_address
client.user_agent = user_agent client.user_agent = user_agent
logger.info(f"track_client_activity: Updated most recent client {client.id} (rustdesk_id: {client.rustdesk_id}), user_agent changed from '{old_user_agent}' to '{user_agent}'")
else: else:
# Create new client with unknown rustdesk_id # Create new client with unknown rustdesk_id
client = ClientActivity( client = ClientActivity(
...@@ -859,11 +867,14 @@ def track_client_activity(api_token, rustdesk_id=None): ...@@ -859,11 +867,14 @@ def track_client_activity(api_token, rustdesk_id=None):
user_agent=user_agent user_agent=user_agent
) )
db.session.add(client) db.session.add(client)
logger.info(f"track_client_activity: Created new client with unknown rustdesk_id")
db.session.commit() db.session.commit()
logger.info(f"track_client_activity: Database commit successful")
except Exception as e: except Exception as e:
logger.error(f"Failed to track client activity: {str(e)}") logger.error(f"Failed to track client activity: {str(e)}")
db.session.rollback()
@bp.route('/track', methods=['POST']) @bp.route('/track', methods=['POST'])
@csrf.exempt @csrf.exempt
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment