diff --git a/flask_backend/production_app.py b/flask_backend/production_app.py index 6ea218807..e95ef423d 100644 --- a/flask_backend/production_app.py +++ b/flask_backend/production_app.py @@ -10,21 +10,116 @@ import threading from pymobiledevice3.lockdown import LockdownClient -# Import our compatibility module instead of the missing module -try: - # Try to import the original module first - from pymobiledevice3.services.debugserver import DebugServerService - logging.info("Using original pymobiledevice3.services.debugserver module") -except ImportError: - # If it fails, use our compatibility module - from flask_backend.pymobiledevice3_compat.debugserver import DebugServerService - logging.info("Using compatibility pymobiledevice3_compat.debugserver module") - # Configure logging logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') logger = logging.getLogger(__name__) +# Try to import the original module first +try: + from pymobiledevice3.services.debugserver import DebugServerService + logger.info("Using original pymobiledevice3.services.debugserver module") +except ImportError: + # If it fails, implement the compatibility class inline + logger.info("Original debugserver module not found, using inline compatibility implementation") + + # Import the necessary dependencies + from pymobiledevice3.services.dvt.dvt_secure_socket_proxy import DvtSecureSocketProxyService + + # Implement the DebugServerService class inline + class DebugServerService: + """ + Compatibility implementation of DebugServerService for pymobiledevice3 + + This class provides the same interface as the original DebugServerService + but uses the DvtSecureSocketProxyService to implement the functionality. + """ + + def __init__(self, lockdown: LockdownClient): + """ + Initialize the DebugServerService + + Args: + lockdown: The LockdownClient instance to use for communication with the device + """ + self.lockdown = lockdown + self.dvt = DvtSecureSocketProxyService(lockdown=lockdown) + logger.info("Initialized DebugServerService compatibility layer") + + def enable_jit(self, bundle_id: str) -> bool: + """ + Enable JIT for the specified application + + Args: + bundle_id: The bundle ID of the application to enable JIT for + + Returns: + True if JIT was enabled successfully, False otherwise + """ + try: + logger.info(f"Enabling JIT for application: {bundle_id}") + + # Use the process_control service to enable JIT + from pymobiledevice3.services.dvt.instruments.process_control import ProcessControl + + process_control = ProcessControl(self.dvt) + + # Get the list of running applications + apps = process_control.list_running_processes() + + # Find the target application + target_app = None + for app in apps: + if app.get('bundle_id') == bundle_id: + target_app = app + break + + if not target_app: + logger.error(f"Application with bundle ID {bundle_id} not found in running processes") + return False + + # Get the process ID + pid = target_app.get('pid') + if not pid: + logger.error(f"Could not get process ID for application {bundle_id}") + return False + + logger.info(f"Found application {bundle_id} with PID {pid}") + + # Enable JIT for the process + # This is a simplified implementation - in reality, enabling JIT might require + # more complex interactions with the device + try: + # Try to use the debugserver command if available + self.dvt.channel.send_command("debugserver", {"enable_jit": True, "pid": pid}) + logger.info(f"Successfully enabled JIT for {bundle_id} using debugserver command") + return True + except Exception as e: + logger.warning(f"Could not enable JIT using debugserver command: {str(e)}") + + # Alternative approach: use the process_control to kill and restart the app + # This might trigger JIT enablement on some iOS versions + try: + logger.info(f"Trying alternative approach to enable JIT for {bundle_id}") + process_control.kill(pid) + logger.info(f"Killed process {pid}, waiting for app to restart") + # The app should restart automatically + return True + except Exception as e2: + logger.error(f"Failed to enable JIT using alternative approach: {str(e2)}") + return False + + except Exception as e: + logger.error(f"Error enabling JIT for {bundle_id}: {str(e)}") + return False + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + if hasattr(self, 'dvt'): + self.dvt.__exit__(exc_type, exc_val, exc_tb) + app = Flask(__name__) CORS(app)