-
Notifications
You must be signed in to change notification settings - Fork 2
/
plist_service.py
148 lines (125 loc) · 4.17 KB
/
plist_service.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
#!/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 plistlib
import ssl
import struct
import logging
import codecs
from re import sub
from six import PY3
from pymobiledevice2.usbmux import usbmux
if PY3:
plistlib.readPlistFromString = plistlib.loads
plistlib.writePlistToString = plistlib.dumps
class PlistService(object):
def __init__(self, port, udid=None, logger=None):
self.logger = logger or logging.getLogger(__name__)
self.port = port
self.connect(udid)
def connect(self, udid=None):
mux = usbmux.USBMux()
mux.process(1.0)
dev = None
while not dev and mux.devices :
mux.process(1.0)
if udid:
for d in mux.devices:
serial = sub("-", '', d.serial)
udid = sub("-", '', udid)
if serial == udid:
dev = d
else:
dev = mux.devices[0]
self.logger.info("Connecting to device: " + dev.serial)
try:
self.s = mux.connect(dev, self.port)
except:
raise Exception("Connexion to device port %d failed" % self.port)
return dev.serial
def close(self):
self.s.close()
def recv(self, length=4096):
return self.s.recv(length)
def send(self, data):
try:
self.s.send(data)
except:
self.logger.error("Sending data to device failled")
return -1
return 0
def sendRequest(self, data):
res = None
if self.sendPlist(data) >= 0:
res = self.recvPlist()
return res
def recv_exact(self, l):
data = ""
if PY3:
data = b""
while l > 0:
d = self.recv(l)
if not d or len(d) == 0:
break
data += d
l -= len(d)
return data
def recv_raw(self):
l = self.recv_exact(4)
if not l or len(l) != 4:
return
l = struct.unpack(">L", l)[0]
return self.recv_exact(l)
def send_raw(self, data):
if PY3 and isinstance(data, str):
data = codecs.encode(data)
hdr = struct.pack(">L", len(data))
msg = b"".join([hdr,data])
return self.send(msg)
def recvPlist(self):
payload = self.recv_raw()
if not payload:
return
bplist_header = "bplist00"
xml_header = "<?xml"
if PY3:
bplist_header = b"bplist00"
xml_header = b"<?xml"
if payload.startswith(bplist_header):
if PY3:
return plistlib.readPlistFromString(payload)
else:
from pymobiledevice2.util.bplist import BPlistReader
return BPlistReader(payload).parse()
elif payload.startswith(xml_header):
#HAX lockdown HardwarePlatform with null bytes
payload = sub('[^\w<>\/ \-_0-9\"\'\\=\.\?\!\+]+','', payload.decode('utf-8')).encode('utf-8')
return plistlib.readPlistFromString(payload)
else:
raise Exception("recvPlist invalid data : %s" % payload[:100].encode("hex"))
def sendPlist(self, d):
payload = plistlib.writePlistToString(d)
l = struct.pack(">L", len(payload))
return self.send(l + payload)
def ssl_start(self, keyfile, certfile):
self.s = ssl.wrap_socket(self.s, keyfile, certfile, ssl_version=ssl.PROTOCOL_TLSv1)