-
Notifications
You must be signed in to change notification settings - Fork 2
/
notification_proxy.py
159 lines (127 loc) · 5.96 KB
/
notification_proxy.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python
# -*- coding: utf8 -*-
#
# $Id$
#
# Copyright (c) 2012-2014 "dark[-at-]gotohack.org"
#
# This file is part of pymobiledevice2
#
# pymobiledevice2 is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
import time
import plistlib
import time
import logging
from pymobiledevice2.lockdown import LockdownClient
from six.moves import _thread as thread
from pprint import pprint
# NP Client to device Notifications (post_notification)
NP_SYNC_WILL_START = "com.apple.itunes-mobdev.syncWillStart"
NP_SYNC_DID_START = "com.apple.itunes-mobdev.syncDidStart"
NP_SYNC_DID_FINISH = "com.apple.itunes-mobdev.syncDidFinish"
NP_SYNC_LOCK_REQUEST = "com.apple.itunes-mobdev.syncLockRequest"
# Device to NP Client Notifications (get_notification)
NP_SYNC_CANCEL_REQUEST = "com.apple.itunes-client.syncCancelRequest"
NP_SYNC_SUSPEND_REQUEST = "com.apple.itunes-client.syncSuspendRequest"
NP_SYNC_RESUME_REQUEST = "com.apple.itunes-client.syncResumeRequest"
NP_PHONE_NUMBER_CHANGED = "com.apple.mobile.lockdown.phone_number_changed"
NP_DEVICE_NAME_CHANGED = "com.apple.mobile.lockdown.device_name_changed"
NP_TIMEZONE_CHANGED = "com.apple.mobile.lockdown.timezone_changed"
NP_TRUSTED_HOST_ATTACHED = "com.apple.mobile.lockdown.trusted_host_attached"
NP_HOST_DETACHED = "com.apple.mobile.lockdown.host_detached"
NP_HOST_ATTACHED = "com.apple.mobile.lockdown.host_attached"
NP_REGISTRATION_FAILED = "com.apple.mobile.lockdown.registration_failed"
NP_ACTIVATION_STATE = "com.apple.mobile.lockdown.activation_state"
NP_BRICK_STATE = "com.apple.mobile.lockdown.brick_state"
NP_DISK_USAGE_CHANGED = "com.apple.mobile.lockdown.disk_usage_changed"
NP_DS_DOMAIN_CHANGED = "com.apple.mobile.data_sync.domain_changed"
NP_BACKUP_DOMAIN_CHANGED = "com.apple.mobile.backup.domain_changed"
NP_APP_INSTALLED = "com.apple.mobile.application_installed"
NP_APP_UNINSTALLED = "com.apple.mobile.application_uninstalled"
NP_DEV_IMAGE_MOUNTED = "com.apple.mobile.developer_image_mounted"
NP_ATTEMPTACTIVATION = "com.apple.springboard.attemptactivation"
NP_ITDBPREP_DID_END = "com.apple.itdbprep.notification.didEnd"
NP_LANGUAGE_CHANGED = "com.apple.language.changed"
NP_ADDRESS_BOOK_PREF_CHANGED = "com.apple.AddressBook.PreferenceChanged"
class NPClient(object):
def __init__(self, lockdown=None, serviceName="com.apple.mobile.notification_proxy", udid=None, logger=None):
self.logger = logger or logging.getLogger(__name__)
self.lockdown = lockdown if lockdown else LockdownClient(udid=udid)
self.service = self.lockdown.startService(serviceName)
def stop_session(self):
self.logger.info("Disconecting...")
self.service.close()
def post_notification(self, notification):
#Sends a notification to the device's notification_proxy.
self.service.sendPlist({"Command": "PostNotification",
"Name": notification})
self.service.sendPlist({"Command": "Shutdown"})
res = self.service.recvPlist()
#pprint(res)
if res:
if res.get("Command") == "ProxyDeath":
return res.get("Command")
else:
self.logger.error("Got unknown NotificationProxy command %s", res.get("Command"))
self.logger.debug(res)
return
def observe_notification(self, notification):
#Tells the device to send a notification on the specified event
self.logger.info("Observing %s", notification)
self.service.sendPlist({"Command": "ObserveNotification",
"Name": notification})
def get_notification(self, notification):
#Checks if a notification has been sent by the device
res = self.service.recvPlist()
if res:
if res.get("Command") == "RelayNotification":
if res.get("Name"):
return res.get("Name")
elif res.get("Command") == "ProxyDeath":
self.logger.error("NotificationProxy died!")
else:
self.logger.warn("Got unknown NotificationProxy command %s", res.get("Command"))
self.logger.debug(res)
return
def notifier(self, name, args=None):
if args == None:
return None
self.observe_notification(args.get("notification"))
while args.get("running") == True:
np_name = self.get_notification(args.get("notification"))
if np_name:
userdata = args.get("userdata")
try:
thread.start_new_thread( args.get("callback") , (np_name, userdata, ) )
except:
self.logger.error("Error: unable to start thread")
def subscribe(self, notification, cb, data=None):
np_data = {
"running": True,
"notification": notification,
"callback": cb,
"userdata": data,
}
thread.start_new_thread( self.notifier, ("NotificationProxyNotifier_"+notification, np_data, ) )
while(1):
time.sleep(1)
def cb_test(name,data=None):
print("Got Notification >> %s" % name)
print("Data:")
pprint(data)
if __name__ == "__main__":
np = NPClient()
np.subscribe(NP_DEVICE_NAME_CHANGED, cb_test, data=None)