-
Notifications
You must be signed in to change notification settings - Fork 18
/
serve.py
executable file
·173 lines (141 loc) · 5.07 KB
/
serve.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
#!/usr/bin/env python3
from http.server import HTTPServer, SimpleHTTPRequestHandler
import http.server
import xml.etree.ElementTree as ET
import os,sys, io, socket, hashlib, base64
import binascii
port = 8000
updates_folder = 'updates'
response_ok = """<?xml version='1.0' encoding='UTF-8'?>
<response protocol="3.0" server="prod">
<daystart elapsed_seconds="42548" elapsed_days="5179"/>
<app status="ok" appid="{98DA7DF2-4E3E-4744-9DE6-EC931886ABAB}">
<event status="ok"/>
<ping status="ok"/>
</app>
</response>
"""
response_template = """<?xml version="1.0" encoding="UTF-8"?>
<response protocol="3.0" server="prod">
<daystart elapsed_seconds="37145" elapsed_days="5179"/>
<app status="ok" appid="{{98DA7DF2-4E3E-4744-9DE6-EC931886ABAB}}">
<event status="ok"/>
<updatecheck status="ok">
<urls>
<url codebase="{codebase_url}"/>
</urls>
<manifest version="{version}">
<packages>
<package required="true" hash="{update_sha1}" name="{update_name}" size="{update_size}"/>
</packages>
<actions>
<action successsaction="default" sha256="{update_sha256}" event="postinstall" DisablePayloadBackoff="true"/>
</actions>
</manifest>
</updatecheck>
<ping status="ok"/>
</app>
</response>
"""
def hostname(host):
url = f"http://{host}:{port}/"
return url
def getupdateinfo(platform, version, update_name):
full_path = os.path.join(updates_folder, update_name)
update_size = str(os.path.getsize(full_path))
BUF_SIZE = 8192
sha1 = hashlib.sha1()
sha256 = hashlib.sha256()
with open(full_path, 'rb') as f:
while True:
data = f.read(BUF_SIZE)
if not data:
break
sha1.update(data)
sha256.update(data)
update_sha1 = binascii.b2a_base64(sha1.digest(), newline=False).decode()
update_sha256 = binascii.b2a_base64(sha256.digest(), newline=False).decode()
return (update_sha1, update_sha256, update_size)
def scan_updates():
files = os.listdir(updates_folder)
versions={}
for f in files:
p = f.split('_')
if(p[0].startswith('rm')):
p.pop(0)
if len(p) != 2:
continue
t = p[1].split('.')
if len(t) != 2:
continue
z = t[0].split('-')
version = p[0]
print(version)
product = z[0]
if not product in versions or versions[product][0] < version:
versions[product]=(version, f)
return versions
class MySimpleHTTPRequestHandler(SimpleHTTPRequestHandler):
def do_POST(self):
length = int(self.headers.get('Content-Length'))
body = self.rfile.read(length).decode('utf-8')
print(body)
xml = ET.fromstring(body)
updatecheck_node = xml.find('app/updatecheck')
#check for update
if updatecheck_node is not None:
version = xml.attrib["version"]
platform = xml.find('os').attrib['platform']
print("requested: ", version)
print("platform: ", platform)
version, update_name = available_versions[platform]
update_sha1, update_sha256, update_size = getupdateinfo(platform, version, update_name)
params = {
"version": version,
"update_name": f"{updates_folder}/{update_name}",
"update_sha1": update_sha1,
"update_sha256": update_sha256,
"update_size" : update_size,
"codebase_url": host_name,
}
response = response_template.format(**params)
print("Response:")
print(response)
self.send_response(200)
self.end_headers()
self.wfile.write(response.encode())
return
event_node = xml.find('app/event')
event_type = int(event_node.attrib["eventtype"])
event_result = int(event_node.attrib["eventresult"])
#post install status
if event_result != 0:
print("Update done")
if "errorcode" in event_node.attrib:
print("With errorcode:", event_node.attrib["errorcode"])
return
#update done
if event_type == 14:
print("OK Response:")
print(response_ok)
self.send_response(200)
self.end_headers()
self.wfile.write(response_ok.encode())
return
available_versions = scan_updates()
host_name = None
if __name__ == "__main__":
if len(sys.argv) > 1:
host = sys.argv[1]
else:
print("Using hostname, to override use: ./serve.py hostname")
host = socket.gethostname()
host_name = hostname(host)
print("Device should use: ", host_name)
print("Available updates:", available_versions)
if not available_versions:
raise FileNotFoundError("Could not find any update files")
handler = MySimpleHTTPRequestHandler
httpd = HTTPServer(('0.0.0.0', port), handler)
print(f"Starting fake updater: {port}")
httpd.serve_forever()