forked from mountainstorm/MobileDevice
-
Notifications
You must be signed in to change notification settings - Fork 1
/
webkitinspector.py
220 lines (188 loc) · 6.09 KB
/
webkitinspector.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/python
# coding: utf-8
# Copyright (c) 2013 Mountainstorm
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from MobileDeviceCore import *
from amdevice import *
from wirservice import *
import uuid
import json
# Talk to safari using:
# https://developers.google.com/chrome-developer-tools/docs/protocol/1.0/index
class WebKitInspector(WIRService):
def __init__(self, amdevice):
WIRService.__init__(self, amdevice, [AMSVC_WEBINSPECTOR])
def uniqueid(self):
retval = str(uuid.uuid1())
if isinstance(retval, str):
# get round a bug in safari's implementation of 'forwardSocketSetup'
# they don't check the type/convert WIRConnectionIdentifierKey
# Thus if we send a string, it converts to a CFData and Safari goes
# pop when it tries to call compare on the CFData
retval = retval.decode(u'utf-8')
return retval
# register a connection_uuid
def reportIdentifier(self, connection_uuid):
self._sendmsg(
u'_rpc_reportIdentifier:',
{
u'WIRConnectionIdentifierKey': connection_uuid,
}
)
retval = self._recvmsg()
retval = self._recvmsg() # discard the getConnectedApplications response
return retval[1]
# list avaliable applications
def getConnectedApplications(self, connection_uuid):
self._sendmsg(
u'_rpc_getConnectedApplications:',
{
u'WIRConnectionIdentifierKey': connection_uuid
}
)
retval = self._recvmsg()
return retval[1]
# get info about an applications e.g. pages in mobilesafari
def forwardGetListing(self, connection_uuid, appid):
self._sendmsg(
u'_rpc_forwardGetListing:',
{
u'WIRConnectionIdentifierKey': connection_uuid,
u'WIRApplicationIdentifierKey': appid
}
)
retval = self._recvmsg()
while retval[0] != u'_rpc_applicationSentListing:':
retval = self._recvmsg()
return retval[1]
# highlight a webview page
def forwardIndicateWebView(self, connection_uuid, appid, pageid, enable):
self.sendmsg(
u'_rpc_forwardIndicateWebView:',
{
u'WIRConnectionIdentifierKey': connection_uuid,
u'WIRApplicationIdentifierKey': appid,
u'WIRPageIdentifierKey': pageid,
u'WIRIndicateEnabledKey': enable
}
)
print self._recvmsg()
# setup a webkit protocol connection
def forwardSocketSetup(self, connection_uuid, appid, pageid, sender_uuid):
self._sendmsg(
u'_rpc_forwardSocketSetup:',
{
u'WIRConnectionIdentifierKey': connection_uuid,
u'WIRApplicationIdentifierKey': appid,
u'WIRPageIdentifierKey': pageid,
u'WIRSenderKey': sender_uuid
}
)
self._recvmsg() # throw away the forwardGetListing response
# send webkit protocol message
def forwardSocketData(self, connection_uuid, appid, pageid, sender_uuid, jsonmsg):
self._sendmsg(
u'_rpc_forwardSocketData:',
{
u'WIRConnectionIdentifierKey': connection_uuid,
u'WIRApplicationIdentifierKey': appid,
u'WIRPageIdentifierKey': pageid,
u'WIRSenderKey': sender_uuid,
u'WIRSocketDataKey': jsonmsg.encode(u'utf-8')
}
)
return self._recvmsg()[1]
def forwardDidClose(self, connection_uuid, appid, pageid, sender_uuid):
self.sendmsg(
u'_rpc_forwardDidClose:',
{
u'WIRConnectionIdentifierKey': connection_uuid,
u'WIRApplicationIdentifierKey': appid,
u'WIRPageIdentifierKey': pageid,
u'WIRSenderKey': sender_uuid
}
)
print self._recvmsg()
def navigate(self, url):
conn = self.uniqueid()
app = u'com.apple.mobilesafari'
self.reportIdentifier(conn)
self.getConnectedApplications(conn)
listing = self.forwardGetListing(conn, app)
pages = listing[u'WIRListingKey']
ident = int(pages.values()[0][u'WIRPageIdentifierKey'])
session = self.uniqueid()
self.forwardSocketSetup(conn, app, ident, session)
data = {
u'id': ident,
u'method': u'Page.navigate',
u'params': {
u'url': (
url
)
}
}
print self.forwardSocketData(conn, app, ident, session, json.dumps(data))
def register_argparse_webinspector(cmdargs):
import argparse
import sys
import mimetypes
import base64
def cmd_load(args, dev):
mimetype = mimetypes.guess_type(args.path)
f = open(args.path, u'rb')
content = f.read()
f.close()
# wakeup device
# switch to safari?
wi = WebKitInspector(dev)
wi.navigate(u'data:' + mimetype[0] + u';base64,' + base64.b64encode(content))
wi.disconnect()
def cmd_navigate(args, dev):
# wakeup device
# switch to safari?
wi = WebKitInspector(dev)
wi.navigate(args.url.decode(u'utf-8'))
wi.disconnect()
webparser = cmdargs.add_parser(
u'web',
help=u'webinspector actions; requires settings->safari->advanced->web-inspector be enabled'
)
webcmd = webparser.add_subparsers()
# load command
loadcmd = webcmd.add_parser(
u'load',
help=u'loads a single safari window with the specified file'
)
loadcmd.add_argument(
u'path',
help=u'the file to load (as a data: url) into the first page of mobilesafari'
)
loadcmd.set_defaults(func=cmd_load)
# navigate command
navigatecmd = webcmd.add_parser(
u'navigate',
help=u'navigate a single safari window with the specified url'
)
navigatecmd.add_argument(
u'url',
help=u'the url to navigate the first page of mobilesafari to'
)
navigatecmd.set_defaults(func=cmd_navigate)