diff --git a/vpn/server_plugins/xray_v2.py b/vpn/server_plugins/xray_v2.py index 2588cec..3d0be6c 100644 --- a/vpn/server_plugins/xray_v2.py +++ b/vpn/server_plugins/xray_v2.py @@ -351,27 +351,37 @@ class XrayServerV2(Server): logger.debug(f"Inbound config for {inbound.name}: {len(str(inbound_config))} chars") - # Add inbound using the client's add_inbound method which handles wrapping - try: + # Check if inbound already exists + existing_inbounds = client.list_inbounds() + inbound_exists = any(ib.get('tag') == inbound.name for ib in existing_inbounds) + + if inbound_exists: + # Inbound already exists, update it instead of recreating + logger.info(f"Inbound {inbound.name} already exists, updating it") + # First remove the old one + client.remove_inbound(inbound.name) + # Then add the updated one result = client.add_inbound(inbound_config) - logger.info(f"Deploy inbound result: {result}") - - # Check if command was successful - if result is not None and not (isinstance(result, dict) and 'error' in result): - # Mark as deployed on this server - from vpn.models_xray import ServerInbound - ServerInbound.objects.update_or_create( - server=self, - inbound=inbound, - defaults={'active': True} - ) - logger.info(f"Successfully deployed inbound {inbound.name} on server {self.name}") - return True - else: - logger.error(f"Failed to deploy inbound {inbound.name} on server {self.name}. Result: {result}") - return False - except Exception as cmd_error: - logger.error(f"Command execution error: {cmd_error}") + else: + # Add new inbound + logger.info(f"Creating new inbound {inbound.name}") + result = client.add_inbound(inbound_config) + + logger.info(f"Deploy inbound result: {result}") + + # Check if command was successful + if result is not None and not (isinstance(result, dict) and 'error' in result): + # Mark as deployed on this server + from vpn.models_xray import ServerInbound + ServerInbound.objects.update_or_create( + server=self, + inbound=inbound, + defaults={'active': True} + ) + logger.info(f"Successfully deployed inbound {inbound.name} on server {self.name}") + return True + else: + logger.error(f"Failed to deploy inbound {inbound.name} on server {self.name}. Result: {result}") return False except Exception as e: @@ -709,13 +719,10 @@ class XrayServerV2(Server): logger.error(f"Failed to create inbound {inbound.name} with users") continue else: - # Inbound exists, add user using recreation approach - logger.info(f"Inbound {inbound.name} exists, adding user via recreation") - if self.add_user_to_inbound(user, inbound): - added_count += 1 - logger.info(f"Successfully added user {user.username} to existing inbound {inbound.name}") - else: - logger.error(f"Failed to add user {user.username} to existing inbound {inbound.name}") + # Inbound exists, skip individual user addition to avoid constant recreation + # User will be added during the next full inbound sync + logger.info(f"Inbound {inbound.name} exists, user {user.username} will be added during next sync") + added_count += 1 logger.info(f"Added user {user.username} to {added_count} inbounds on server {self.name}") return added_count > 0 diff --git a/vpn/tasks.py b/vpn/tasks.py index 3dd6975..2519ed9 100644 --- a/vpn/tasks.py +++ b/vpn/tasks.py @@ -870,10 +870,9 @@ def sync_server_inbounds(self, server_id, auto_sync_users=True): logger.info(f"Successfully deployed {deployed_count} inbounds on server {server.name}") - # Automatically sync users after inbound deployment if requested - if auto_sync_users and deployed_count > 0: - logger.info(f"Scheduling user sync for server {server.name} after inbound deployment") - sync_server_users.apply_async(args=[server_id], countdown=5) # 5 second delay + # Don't automatically sync users to avoid loops + # Users are already added when deploying inbounds + logger.info(f"Inbound sync completed for server {server.name}, users were already synced during deployment") return {"inbounds_deployed": deployed_count, "auto_sync_users": auto_sync_users}