forked from doronz88/pymobiledevice3
/
amfi.py
66 lines (52 loc) · 2.53 KB
/
amfi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
import logging
import construct
from pymobiledevice3.exceptions import AmfiError, BadDevError, ConnectionFailedError, DeveloperModeError, \
DeviceHasPasscodeSetError, NoDeviceConnectedError, PyMobileDevice3Exception
from pymobiledevice3.lockdown import LockdownClient, create_using_usbmux
from pymobiledevice3.services.heartbeat import HeartbeatService
class AmfiService:
SERVICE_NAME = 'com.apple.amfi.lockdown'
def __init__(self, lockdown: LockdownClient):
self._lockdown = lockdown
self._logger = logging.getLogger(self.__module__)
def create_amfi_show_override_path_file(self):
""" create an empty file at AMFIShowOverridePath """
service = self._lockdown.start_lockdown_service(self.SERVICE_NAME)
resp = service.send_recv_plist({'action': 0})
if not resp.get('success'):
raise PyMobileDevice3Exception(f'create_AMFIShowOverridePath() failed with: {resp}')
def enable_developer_mode(self, enable_post_restart=True):
"""
enable developer-mode
if enable_post_restart is True, then wait for device restart to answer the final prompt
with "yes"
"""
service = self._lockdown.start_lockdown_service(self.SERVICE_NAME)
resp = service.send_recv_plist({'action': 1})
error = resp.get('Error')
if error is not None:
if error == 'Device has a passcode set':
raise DeviceHasPasscodeSetError()
raise AmfiError(error)
if not resp.get('success'):
raise DeveloperModeError(f'enable_developer_mode(): {resp}')
if not enable_post_restart:
return
try:
HeartbeatService(self._lockdown).start()
except ConnectionAbortedError:
self._logger.debug('device disconnected, awaiting reconnect')
while True:
try:
self._lockdown = create_using_usbmux(self._lockdown.udid)
break
except (NoDeviceConnectedError, ConnectionFailedError, BadDevError, OSError, construct.core.StreamError):
pass
self.enable_developer_mode_post_restart()
def enable_developer_mode_post_restart(self):
""" answer the prompt that appears after the restart with "yes" """
service = self._lockdown.start_lockdown_service(self.SERVICE_NAME)
resp = service.send_recv_plist({'action': 2})
if not resp.get('success'):
raise DeveloperModeError(f'enable_developer_mode_post_restart() failed: {resp}')