forked from Mishiranu/Dashchan-Meta
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate.py
executable file
·331 lines (301 loc) · 11.4 KB
/
generate.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/env python3
import base64
import hashlib
import json
import os
import requests
import subprocess
import sys
import zipfile
class ManifestEntry:
def __init__(self, element, attributes):
self.element = element
self.attributes = attributes
def get(self, key):
return self.attributes.get(key)
class Apk:
def __init__(self, title, manifest_entries, fingerprints, length, sha256sum):
self.title = title
self.manifest_entries = manifest_entries
self.fingerprints = fingerprints
self.length = length
self.sha256sum = sha256sum
class Package:
def __init__(self, title, extension, version_name, version_code,
min_sdk, length, source, source_legacy, fingerprint, sha256sum):
self.title = title
self.extension = extension
self.version_name = version_name
self.version_code = version_code
self.min_sdk = min_sdk
self.length = length
self.source = source
self.source_legacy = source_legacy
self.fingerprint = fingerprint
self.sha256sum = sha256sum
class Extension:
def __init__(self, etype, title):
self.etype = etype
self.title = title
self.packages = []
os.chdir(os.path.dirname(sys.argv[0]))
os.chdir('update')
with open('source.json') as file:
source_config = json.load(file)
def dumpapk(path):
length = os.path.getsize(path)
m = hashlib.sha256()
with open(path, 'rb') as file:
m.update(file.read())
sha256sum = ':'.join('{:02X}'.format(x) for x in m.digest())
manifest_entries = []
output = subprocess.run(['aapt', 'd', 'xmltree', path.replace('\\', '/'), 'AndroidManifest.xml'],
capture_output=True).stdout.decode('utf-8')
element = None
attributes = None
for line in output.split('\n'):
line = line.strip()
if line.startswith('E: '):
if element:
manifest_entries.append(ManifestEntry(element, attributes))
element = line[3:]
index = element.find(' (')
if index >= 0:
element = element[:index]
attributes = {}
elif line.startswith('A: '):
line = line[3:]
index = line.find('=')
if index >= 0:
key = line[:index]
value = line[index + 1:]
index = key.find('(')
if index >= 0:
key = key[:index]
if key.startswith('android:'):
key = key[8:]
index = value.find(' (Raw: ')
if index >= 0:
value = value[index + 7:-1]
if value[0] == '"':
value = value[1:-1]
if value.startswith('(type'):
index = value.find(')')
if index >= 0:
value = value[index + 1:]
if value.startswith('0x'):
value = int(value, 16)
attributes[key] = value
if element:
manifest_entries.append(ManifestEntry(element, attributes))
output = subprocess.run(['aapt', 'd', 'badging', path],
capture_output=True).stdout.decode('utf-8')
title = None
for line in output.split('\n'):
if line.startswith('application-label:'):
line = line[18:]
if line[0] == '\'':
line = line[1:-1]
title = line
if title is None:
for entry in manifest_entries:
if entry.element == 'application':
title = entry.attributes['label']
if title.startswith('DashchanFork for '):
title = title[13:]
elif title.startswith('DashchanFork '):
title = title[9:]
fingerprints = []
with zipfile.ZipFile(path, 'r') as zip:
for name in zip.namelist():
if name.startswith('META-INF/') and name.endswith('.RSA'):
with zip.open(name) as cert_entry:
cert = cert_entry.read()
output = subprocess.run(['openssl', 'pkcs7', '-inform', 'DER', '-print_certs'],
input=cert, capture_output=True).stdout.decode('utf-8')
output = output.replace("\r", "")
b64 = None
for line in output.split("\n"):
if line == '-----BEGIN CERTIFICATE-----':
b64 = ''
elif line == '-----END CERTIFICATE-----':
m = hashlib.sha256()
m.update(base64.b64decode(b64))
fingerprint = ':'.join('{:02X}'.format(x) for x in m.digest())
fingerprints.append(fingerprint)
b64 = None
elif b64 is not None:
b64 += line
return Apk(title, manifest_entries, fingerprints, length, sha256sum)
client_title = ''
clients = []
extensions = {}
def addpackage(apk, title, source, source_legacy):
package_title = None
is_chan = False
is_library = False
name = None
version_name = None
version_code = None
min_sdk = None
fingerprint = None
for entry in apk.manifest_entries:
if entry.element == 'manifest':
version_name = entry.get('versionName')
version_code = entry.get('versionCode')
elif entry.element == 'uses-sdk':
min_sdk = entry.get('minSdkVersion')
elif entry.element == 'uses-feature':
feature_name = entry.get('name')
is_chan = feature_name == 'chan.extension'
is_library = feature_name == 'lib.extension'
elif entry.element == 'meta-data':
metadata_name = entry.get('name')
if is_chan and metadata_name == 'chan.extension.name':
name = entry.get('value')
elif is_library and metadata_name == 'lib.extension.name':
name = entry.get('value')
elif is_chan and metadata_name == 'chan.extension.title':
package_title = entry.get('value')
elif is_library and metadata_name == 'lib.extension.title':
package_title = entry.get('value')
if package_title is None:
package_title = apk.title
if len(apk.fingerprints) == 1:
fingerprint = apk.fingerprints[0]
if version_name and version_code and min_sdk and fingerprint:
if is_chan or is_library or name:
if (is_chan or is_library) and name:
if title.lower().startswith(name):
title = title[len(name):]
extension = extensions.get(name)
if extension is None:
etype = ''
if is_chan:
etype = 'chan'
elif is_library:
etype = 'library'
extension = Extension(etype, package_title)
extensions[name] = extension
elif title == '':
extension.title = package_title
extension.packages.append(Package(title, name, version_name, version_code, min_sdk,
apk.length, source, source_legacy, fingerprint, apk.sha256sum))
else:
global client_title
if len(client_title) == 0 or title == '':
client_title = package_title
clients.append(Package(title, 'client', version_name, version_code, min_sdk,
apk.length, source, source_legacy, fingerprint, apk.sha256sum))
client_url = source_config.get('client_url')
if client_url:
m = hashlib.sha1()
m.update(client_url.encode())
name = ''.join('{:02x}'.format(x) for x in m.digest()) + '.apk'
path = os.getcwd() + "\\" + name
if not os.path.exists(path) or os.path.getsize(path) == 0:
with open(path, 'wb') as file:
file.write(requests.get(client_url, allow_redirects=True).content)
index = client_url.find('://')
if index >= 0:
client_url = client_url[index + 1:]
addpackage(dumpapk(path), '', client_url, client_url)
relative_url_legacy = source_config['relative_url_legacy']
for f in os.listdir('package'):
if f.endswith('.apk'):
title = f[:-4]
if title.startswith('Dashchan'):
title = title[8:]
source = os.path.join('package', f).replace("\\", "/")
source_legacy = relative_url_legacy + source
addpackage(dumpapk(source), title, source, source_legacy)
json_dict = {}
json_v1_dict = {}
repository = source_config.get('repository')
if repository:
json_dict['meta'] = {'repository': repository}
json_v1_dict['title'] = repository
applications_v1 = []
if len(clients) > 0 or len(extensions) > 0:
json_v1_dict['applications'] = applications_v1
if len(clients) > 0:
packages = []
packages_v1 = []
json_dict['client'] = packages
applications_v1.append({
'name': 'client',
'type': 'client',
'title': client_title,
'packages': packages_v1
})
clients.sort(key=lambda package: package.title)
for package in clients:
title = 'Release'
if len(package.title):
title = package.title
packages.append({
'title': title,
'name': package.version_name,
'code': package.version_code,
'minVersion': 1,
'maxVersion': 1,
'minSdk': package.min_sdk,
'length': package.length,
'source': package.source_legacy,
'fingerprint': package.fingerprint
})
packages_v1.append({
'title': title,
'version_name': package.version_name,
'version_code': package.version_code,
'min_api_version': 1,
'max_api_version': 1,
'min_sdk': package.min_sdk,
'length': package.length,
'source': package.source,
'fingerprint': package.fingerprint,
'sha256sum': package.sha256sum
})
for name in sorted(extensions):
extension = extensions[name]
extension.packages.sort(key=lambda package: package.title)
packages = []
packages_v1 = []
json_dict[name] = packages
applications_v1.append({
'name': name,
'type': extension.etype,
'title': extension.title,
'packages': packages_v1
})
for package in extension.packages:
title = 'Release'
if len(package.title):
title = package.title
packages.append({
'title': title,
'name': package.version_name,
'code': package.version_code,
'version': 1,
'minSdk': package.min_sdk,
'length': package.length,
'source': package.source_legacy,
'fingerprint': package.fingerprint
})
packages_v1.append({
'title': title,
'version_name': package.version_name,
'version_code': package.version_code,
'api_version': 1,
'min_sdk': package.min_sdk,
'length': package.length,
'source': package.source,
'fingerprint': package.fingerprint,
'sha256sum': package.sha256sum
})
with open('data.json', 'w') as file:
file.write(json.dumps(json_dict, indent='\t'))
file.write('\n')
with open('data-v1.json', 'w') as file:
file.write(json.dumps(json_v1_dict, indent='\t'))
file.write('\n')