-
Notifications
You must be signed in to change notification settings - Fork 17
/
diags.py
179 lines (166 loc) · 7.29 KB
/
diags.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
from mitmproxy.models import flow
from mitmproxy.models import HTTPResponse
from netlib.http import Headers
import cgi
import re
from gzip import GzipFile
import StringIO
import time
XML_OK_RESPONSE = '''<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>iPhone5,1</key>
<array>
<string>powerDiagnostics</string>
</array>
<key>iPad2,5</key>
<array>
<string>powerDiagnostics</string>
</array>
<key>iPad2,1</key>
<array>
<string>powerDiagnostics</string>
</array>
<key>iPhone3,1</key>
<array>
<string>powerDiagnostics</string>
</array>
<key>iPhone3,3</key>
<array>
<string>powerDiagnostics</string>
</array>
<key>iPhone4,1</key>
<array>
<string>powerDiagnostics</string>
</array>
<key>iPad2,2</key>
<array>
<string>powerDiagnostics</string>
</array>
<key>iPad2,3</key>
<array>
<string>powerDiagnostics</string>
</array>
<key>iPad2,4</key>
<array>
<string>powerDiagnostics</string>
</array>
<key>iPad3,1</key>
<array>
<string>powerDiagnostics</string>
</array>
<key>iPad3,2</key>
<array>
<string>powerDiagnostics</string>
</array>
<key>iPad3,3</key>
<array>
<string>powerDiagnostics</string>
</array>
<key>iPad3,4</key>
<array>
<string>powerDiagnostics</string>
</array>
<key>iPad3,5</key>
<array>
<string>powerDiagnostics</string>
</array>
<key>iPad3,6</key>
<array>
<string>powerDiagnostics</string>
</array>
<key>iPad2,6</key>
<array>
<string>powerDiagnostics</string>
</array>
<key>iPad2,7</key>
<array>
<string>powerDiagnostics</string>
</array>
<key>iPad4,1</key>
<array>
<string>powerDiagnostics</string>
</array>
<key>iPad4,2</key>
<array>
<string>powerDiagnostics</string>
</array>
<key>iPad4,3</key>
<array>
<string>powerDiagnostics</string>
</array>
<key>iPad4,4</key>
<array>
<string>powerDiagnostics</string>
</array>
<key>iPad4,5</key>
<array>
<string>powerDiagnostics</string>
</array>
<key>iPad4,6</key>
<array>
<string>powerDiagnostics</string>
</array>
<key>iPod4,1</key>
<array>
<string>powerDiagnostics</string>
</array>
<key>iPod5,1</key>
<array>
<string>powerDiagnostics</string>
</array>
<key>iPhone5,2</key>
<array>
<string>powerDiagnostics</string>
</array>
<key>iPhone5,3</key>
<array>
<string>powerDiagnostics</string>
</array>
<key>iPhone5,4</key>
<array>
<string>powerDiagnostics</string>
</array>
<key>iPhone6,1</key>
<array>
<string>powerDiagnostics</string>
</array>
<key>iPhone6,2</key>
<array>
<string>powerDiagnostics</string>
</array>
<key>iPhone8,1</key>
<array>
<string>powerDiagnostics</string>
</array>
</dict>
</plist>'''
def request(context, flow):
path = flow.request.path
print 'Path is %s' % path
if path == '/ios/TestConfiguration/1.2':
respond(flow, XML_OK_RESPONSE)
elif path == '/MR3Server/ValidateTicket?ticket_number=123456':
respond(flow, XML_OK_RESPONSE)
elif path == '/MR3Server/MR3Post':
saveContent(flow, 'general')
respond(flow, XML_OK_RESPONSE)
elif path == '/ios/log/extendedUpload':
saveContent(flow, 'power')
respond(flow, XML_OK_RESPONSE)
def saveContent(flow, prefix):
decodedData = StringIO.StringIO()
decodedData.write(flow.request.get_decoded_content())
mimeType = flow.request.headers.get('Content-Type', '')
multipart_boundary_re = re.compile('^multipart/form-data; boundary=(.*)$')
matches = multipart_boundary_re.match(mimeType)
decodedData.seek(0)
query = cgi.parse_multipart( decodedData, {"boundary" : matches.group(1)})
with open("%s-%s.tar.gz" % (prefix, time.strftime("%Y%m%d-%H%M%S")), "w") as logs:
logs.write(query['log_archive'][0])
def respond(flow, content):
resp = HTTPResponse("HTTP/1.1", 200, "OK",
Headers(Content_Type="text/xml"),
content)
flow.reply(resp)