-
Notifications
You must be signed in to change notification settings - Fork 1
/
revanced.py
966 lines (866 loc) · 36 KB
/
revanced.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
import requests
import os
import json
import subprocess
import sys
import re
import argparse
import random
import shutil
import http.client
from urllib.parse import urlparse
def download_file(file_url: str, file_name: str):
print("Downloading", file_url, "as", file_name)
chunk_size = 1024
response = requests.get(file_url, stream=True)
total_size = int(response.headers.get("content-length", 0))
downloaded_bytes = 0
# Open the file for writing in binary mode
with open(file_name, "wb") as file:
for data in response.iter_content(chunk_size=chunk_size):
file.write(data)
downloaded_bytes += len(data)
if total_size == 0:
print(
f"\r[{downloaded_bytes} bytes / unknown] [?%]", end="", flush=True
)
else:
progress_percent = (downloaded_bytes / total_size) * 100
max_length = shutil.get_terminal_size()[0] - 10
progress = "#" * int(progress_percent / 100 * max_length)
empty = " " * (max_length - len(progress))
# 8 fixed characters here, 4 square brackets, 1 space, 1 percent sign, 2 percent digits
print(
f"\r[{progress}{empty}] [{int(progress_percent)}%]",
end="",
flush=True,
)
print()
def update_revanced(
repo: str, fallback_repo: str, cli: str, patches: str, integrations: str, dev: bool
):
def get_github_assets(links: list):
links = [x for x in links if x is not None]
if dev:
links = [a.replace("/latest", "") for a in links]
if len(links) > 0:
url = links[0]
response = requests.get(url)
if response.status_code == 200:
response = response.json()
links = []
if dev:
response = response[0]
for item in response["assets"]:
links.append(item["name"])
links.append(item["browser_download_url"])
return links # [name, url(, name, url)*]
print(url, "not found. Attempting fallback")
links = [x for x in links if x != url]
if len(links) > 0:
return get_github_assets(links)
print("Exiting: Failed to get download urls for one or more patching tools")
sys.exit(1)
def files_still_there():
return (
os.path.exists("cli.jar")
and os.path.exists("patches.json")
and os.path.exists("patches.jar")
and os.path.exists("integrations.apk")
)
localfiles = []
if os.path.exists(".revanced_versions.txt") and files_still_there():
with open(".revanced_versions.txt", "r") as file:
localfiles = [file.readline().strip() for _ in range(3)]
with open(".revanced_versions.txt", "w") as file:
pass
provided_repo = f"https://api.github.com/repos/{repo}"
revanced_repo = f"https://api.github.com/repos/{fallback_repo}"
specific_cli = (
None
if cli is None
else (
f"https://api.github.com/repos/{cli}/releases/latest"
if "/" in cli
else f"https://api.github.com/repos/{cli}/revanced-cli/releases/latest"
)
)
specific_patches = (
None
if patches is None
else (
f"https://api.github.com/repos/{patches}/releases/latest"
if "/" in patches
else f"https://api.github.com/repos/{patches}/revanced-patches/releases/latest"
)
)
specific_integrations = (
None
if integrations is None
else (
f"https://api.github.com/repos/{integrations}/releases/latest"
if "/" in integrations
else f"https://api.github.com/repos/{integrations}/revanced-integrations/releases/latest"
)
)
not_asc = lambda x: ".asc" not in x
# cli
cli_version, cli_url = list(
filter(
not_asc,
get_github_assets(
[
specific_cli,
provided_repo + "/revanced-cli/releases/latest",
revanced_repo + "/revanced-cli/releases/latest",
]
),
)
)
if cli_version in localfiles:
print("cli.jar is up-to-date")
else:
download_file(cli_url, "cli.jar")
# patches
temp = list(
filter(
not_asc,
get_github_assets(
[
specific_patches,
provided_repo + "/revanced-patches/releases/latest",
revanced_repo + "/revanced-patches/releases/latest",
]
),
)
)
temp.sort(
key=lambda x: ".json" in x, reverse=True
) # because https://github.com/rufusin/revanced-patches/releases changes the default release format
patches_json, patches_json_url, patches_version, patches_url = temp
if patches_version in localfiles:
print("patches.jar is up-to-date")
else:
download_file(patches_json_url, "patches.json")
download_file(patches_url, "patches.jar")
# integrations
integrations_version, integrations_url = list(
filter(
not_asc,
get_github_assets(
[
specific_integrations,
provided_repo + "/revanced-integrations/releases/latest",
revanced_repo + "/revanced-integrations/releases/latest",
]
),
)
)
if integrations_version in localfiles:
print("integrations.apk is up-to-date")
else:
download_file(integrations_url, "integrations.apk")
with open(".revanced_versions.txt", "w") as file:
file.write("\n".join([cli_version, patches_version, integrations_version]))
def get_apk(package_name: str, version: str, local: bool, scan_folder_for_apks: bool):
default_apk = "app.apk"
url = ""
def scan_for_apk_files(folder_path):
files = os.listdir(folder_path)
apk_files = [file for file in files if file.endswith(".apk")]
return apk_files
current_request = 0
total_requests = 0
last_progress_msg = ""
# update one line as we navigate apk host sites, looking for download urls
# this function should be called once before the first request with the total number of requests, and then called empty before each subsequent request
# reuse url for the request link, call with over="<fail message>" to move on a new line early
def progress(steps: int = 0, over: str = ""):
nonlocal current_request, total_requests, last_progress_msg
if steps > 0:
current_request = 1
total_requests = steps
else:
current_request += 1
msg = over or f"Fetching [{current_request}/{total_requests}]: {url}"
spaces_to_clear = " " * len(last_progress_msg)
print(f"\r{spaces_to_clear}", end="", flush=True)
last_progress_msg = msg
print(f"\r{msg}", end="", flush=True)
if over or current_request >= total_requests:
print()
# apkcombo is a good source but they shill their own apk installer with xapks as the only option sometimes
# https://apkcombo.com/youtube/com.google.android.youtube/download/phone-18.38.44-apk case in point, whereas apkmirror provides the normal apk
def apkcombo():
nonlocal url
# this is either 404 or redirects to the app page with the specified version
url = (
"https://apkcombo.com/search/"
+ package_name
+ "/download/"
+ ("apk" if version == "" else f"phone-{version}-apk")
)
progress(2)
response = requests.get(url, headers={"Referer": "https://apkcombo.com/"})
if response.status_code == 404:
progress(over="app not found on apkcombo!")
return
if response.status_code == 200:
# https://download.apkcombo.com/com.google.android.youtube/YouTube_18.43.41_apkcombo.com.apk
# ?ecp=Y29tLmdvb2dsZS5hbmRyb2lkLnlvdXR1YmUvMTguNDMuNDEvMTU0MDg4NTk1Mi41ODU3M2FmOGFhY2U5YjAxZmY0NTQwMDFhNGI4NDM2MzVhNGM0YjNhLmFwaw==
# &iat=1699141714&sig=a4201aefd1136aaf098d1d5333988fa3&size=131564206&from=cf&version=old&lang=en
regex = r'(?<=a href=")https://download\.apkcombo\.com/.*\.apk\?[^"]+'
# for /r2?u=https..... external download links?, found at least on com.zhiliaoapp.musically, guardian tales
regex2 = r'(?<=a href=")/r2.*\.apk[^"]+'
url_fist_part = re.search(regex, response.text) or re.search(
regex2, response.text
)
if url_fist_part == None:
progress(
over="did not find standalone apk for this app/version on apkcombo, doesn't exits? "
+ url
)
return
else:
url_fist_part = url_fist_part.group()
if re.match(r"^http", url_fist_part) is None:
url_fist_part = "https://apkcombo.com" + url_fist_part
url = "https://apkcombo.com/checkin"
progress()
response = requests.get(url)
if response.status_code == 200:
# fp=e1aa154442d600ccbfa78e01a042344e&ip=yourip
url_second_part = response.text
return url_fist_part + "&" + url_second_part
progress(over="apkcombo failed at url: " + url)
# apkmirror doesn't auto-redirect package name searches so we have to deal with scraping the search page
# also hosts some patched apps and watch/tv versions that show up as results when searching for the regular app packages, thus the need for user choice
def apkmirror(found_app_url: str = ""):
nonlocal url
base = "https://www.apkmirror.com"
supported_apps = {
"com.google.android.youtube": "https://www.apkmirror.com/apk/google-inc/youtube/",
"com.google.android.apps.youtube.music": "https://www.apkmirror.com/apk/google-inc/youtube-music/",
}
if found_app_url:
supported_apps[package_name] = found_app_url
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/119.0"
}
if package_name in supported_apps:
url = supported_apps[package_name]
if not version:
progress(5)
response = requests.get(url, headers=headers)
if response.status_code == 200:
# this is the first list item and should be the latest version available
# /apk/google-inc/youtube/youtube-18-43-45-release/
regex = r'<div class="widgetHeader[^"]+">All versions <\/div>.*?<a class="fontBlack" href="([^"]+)'
match = re.search(regex, response.text, re.S)
if match:
url = base + match.group(1)
else:
progress(
over="failed to find the latest version of "
+ package_name
+ " on apkmirror"
)
return
else:
slug = re.search(r"(?<=/)[^\/]+(?=\/$)", url).group()
url = (
url
+ slug
+ f"-{version.replace('.', '-').replace(':', '')}-release"
)
# url should look like this now: https://www.apkmirror.com/apk/google-inc/youtube/youtube-18-43-45-release/
regex = r'(?<=apkm-badge">APK).*?href="([^"]+)'
progress(4)
response = requests.get(url, headers=headers)
if response.status_code == 200:
# https://www.apkmirror.com/apk/google-inc/youtube/youtube-18-38-44-release/youtube-18-38-44-2-android-apk-download/
url = base + re.search(regex, response.text, re.S).group(1)
regex = r'(?<=href=")(\/apk\/.*?\?key=\w+[^"]+)'
progress()
response = requests.get(url, headers=headers)
if response.status_code == 200:
# https://www.apkmirror.com/apk/google-inc/youtube/youtube-18-38-44-release/youtube-18-38-44-2-android-apk-download/
# download/?key=714ea56dd827493337e50628f937846729feaf94&forcebaseapk=true
url = base + re.search(regex, response.text).group(1).replace(
"&", "&"
)
regex = r'<form id="filedownload".*?<\/form>'
progress()
response = requests.get(url, headers=headers)
if response.status_code == 200:
download_form = re.search(regex, response.text, re.S).group()
if download_form:
parms = re.findall(
r"name=\"([^\"]+)|value=\"([^\"]+)", download_form
)
parms = list(
map(
lambda x: x[0] if parms.index(x) % 2 == 0 else x[1],
parms,
)
)
url = (
base
+ re.search(
r'(?<=action=")([^"]+)', download_form
).group()
+ "?"
+ "".join(
[
"&" + value if index % 2 == 0 else "=" + value
for index, value in enumerate(parms)
]
)[1:]
)
progress()
response = requests.get(
url, headers=headers, allow_redirects=False
)
if response.status_code == 302:
return response.headers["Location"]
progress(over="apkmirror failed at url: " + url)
else:
url = f'https://www.apkmirror.com/?post_type=app_release&searchtype=app&s="{package_name}"'
regex = r'<div class="listWidget">.<div class="widgetHeader search-header">.*?<div class="listWidget">'
progress(5)
response = requests.get(url, headers=headers)
if response.status_code == 200:
app_search_result_html = re.search(regex, response.text, re.S).group()
regex = r'<h5 title="([^"]+).*?href="([^"]+).*?<\/h5>'
if app_search_result_html:
if "No results found matching your query" in app_search_result_html:
# print("no results on apkmirror for " + package_name)
progress(over="no results on apkmirror for " + package_name)
return
search_results = re.findall(regex, app_search_result_html, re.S)
if search_results:
if len(search_results) > 1:
filter_function = lambda x: f"{x[0]} : {x[1]}"
item = select_item(
"Select apkmirror search result (empty for none): ",
search_results,
filter_function,
True,
)
if item:
url = base + item[1]
return apkmirror(url)
else:
return
# don't ask if it's just one result
url = base + search_results[0][1]
# print(f"found apkmirror url for {package_name}: {url}")
return apkmirror(url)
progress(over=f"failed to search for {package_name} on apkmirror")
# apkpure's downside is that they don't keep many old versions, some revanced recomended versions are pretty old and won't be found here
def apkpure():
nonlocal url
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0"
}
url = (
"https://apkpure.com/search/"
+ package_name
+ ("/download" if not version else f"/download/{version}")
)
progress(2)
# idk what requests does that causes it to get 403 on a request that works fine in borwsers and curl
# https://stackoverflow.com/questions/74446830/how-to-fix-403-forbidden-errors-with-python-requests-even-with-user-agent-head
# # #
# response = requests.get(url, headers=headers)
# print("\n\n", url, response, response.request.headers)
# if response.status_code == 200:
conn = http.client.HTTPSConnection("apkpure.com")
conn.request("GET", url.replace("https://apkpure.com", ""), headers=headers)
response = conn.getresponse()
location_header = response.getheader("location")
print("\n\nhttps://apkpure.com" + location_header)
# this also fails ///
# response = requests.get("https://apkpure.com" + location_header)
conn = http.client.HTTPSConnection("apkpure.com")
conn.request("GET", location_header, headers=headers)
response = conn.getresponse()
if response.status == 200:
regex = r'https://d\.apkpure\.com/b/APK/.*?\?versionCode=\d+.*?(?=")'
# match = re.search(regex, response.text)
match = re.search(regex, response.read().decode())
if match:
url = match.group().replace("&", "&")
progress()
# response = requests.get(url, headers=headers, allow_redirects=False)
parsedUrl = urlparse(url)
conn = http.client.HTTPSConnection(parsedUrl.netloc)
conn.request("GET", url.split(parsedUrl.netloc)[1], headers=headers)
response = conn.getresponse()
# if response.status_code == 302:
# url = response.headers["Location"]
if response.status == 302:
url = response.getheader("location")
return url
progress(
over="did not find standalone apk for this app/version on apkpure, does it exits? "
+ url
)
else:
progress(over="app not found on apkpure")
sources = [
apkcombo,
apkmirror,
apkpure,
]
if scan_folder_for_apks:
# scan script folder, not repo folder inside it
folder = os.path.dirname(os.getcwd())
apks = scan_for_apk_files(folder)
apk = None
if len(apks) == 1:
apk = apks[0]
if len(apks) > 1:
apk = (
select_item(
"Select apk to use (empty for none): ", apks, allow_empty=True
)
or False
)
if apk:
apk = f"../{apk}"
print("Using user-provided apk file at:", os.path.abspath(apk))
check_apk_signature("", apk)
return apk
if apk != False: # if user selected none
print("No user-provided apk files found in the working directory")
localversion = []
if os.path.exists(".apk_version.txt") and os.path.exists(default_apk):
with open(".apk_version.txt", "r") as file:
localversion = [file.readline().strip() for _ in range(1)]
# this is inside get_apk to also allow user-provided apks when using --local
if local:
if not localversion:
print("No local apk file available, downloading...")
elif package_name not in localversion[0]:
print(
f"Warning: Local app ({localversion[0]}) differs from current one ({package_name})"
)
fn = lambda x: (
f"Download {package_name}"
if x
else "Patch anyways (only universal patches will apply, if any, and buld name will be wrong)"
)
ignore_local = select_item(
"Your choice (empy for download): ", [True, False], fn, True
)
if ignore_local == False:
check_apk_signature(package_name, default_apk)
return default_apk
if f"{package_name}-{version}" in localversion:
print("app.apk is up-to-date")
check_apk_signature(package_name, default_apk)
return default_apk
with open(".apk_version.txt", "w") as file:
pass
random.shuffle(sources)
download_link = None
i = 0
while download_link == None and i in range(0, len(sources)):
try:
download_link = sources[i]()
except Exception as e:
print("\tfailed", e, "\n")
i += 1
assert download_link, "Completely failed to download apk"
download_file(download_link, default_apk)
with open(".apk_version.txt", "w") as file:
file.write(f"{package_name}-{version or 'latest'}")
check_apk_signature(package_name, default_apk)
return default_apk
def select_item(
message: str, item_list: list, map_function=None, allow_empty: bool = False
):
printable_item_list = list(map(map_function, item_list)) if map_function else None
for index, item in enumerate(printable_item_list or item_list, start=1):
print(f"{index:{len(str(len(item_list)))}}. {item}")
while True:
try:
choice = input(message)
if choice == "" and allow_empty:
return None
choice = int(choice)
if 1 <= choice <= len(item_list):
return item_list[choice - 1]
else:
print("Please enter a valid number corresponding to the options.")
except ValueError:
print("Please enter a valid number.")
def select_multiple_items(
message: str, item_list: list, map_function=None, allow_empty: bool = False
):
printable_item_list = list(map(map_function, item_list)) if map_function else None
for index, item in enumerate(printable_item_list or item_list, start=1):
print(f"{index:{len(str(len(item_list)))}}. {item}")
selected_items = []
while True:
selection = input(message)
indices = set()
try:
if selection == "" and allow_empty:
return []
for part in selection.split(","):
if "-" in part:
start, end = map(int, part.split("-"))
indices.update(range(int(start), int(end) + 1))
else:
indices.add(int(part))
for index in indices:
if 1 <= index <= len(item_list):
selected_items.append(item_list[index - 1])
return selected_items
except (ValueError, IndexError):
print("Invalid input. Please enter valid numbers and/or ranges.")
def check_java():
cmd = ["java", "-version"]
try:
output = subprocess.check_output(cmd, stderr=subprocess.STDOUT, text=True)
first_line = output.split("\n")[0]
regex = r"^\w+ version \"?(\d{1,2})"
version = int(re.match(regex, first_line).group(1))
if version >= 11:
return
else:
print(
"Exiting: Found incompatible java verson, revanced requires at least java 11"
)
subprocess.run(cmd) # show user's java version before exiting
sys.exit(1)
except FileNotFoundError:
print("Exiting: Java not found, install jdk11 or higher")
sys.exit(1)
def check_keystore_type(keystore_file: str):
print("Using keystore file:", os.path.abspath(keystore_file), end="")
command = [
"keytool",
"-list",
"-keystore",
keystore_file,
"-storetype",
"BKS",
"-provider",
"org.bouncycastle.jce.provider.BouncyCastleProvider",
"-providerpath",
"../bcprov-jdk18on-176.jar",
"-storepass",
"",
]
process = subprocess.run(command, capture_output=True, text=True)
if (
process.returncode == 1
and "keytool error: java.lang.Exception: Keystore file does not exist:"
in process.stdout
):
type = "to_be_generated"
print(f"\t[{type}]")
return type
if process.returncode == 0 and "Your keystore contains 1 entry" in process.stdout:
if "alias," in process.stdout:
type = "old"
print(f"\t[{type}]")
return type
if "ReVanced Key," in process.stdout:
type = "new"
print(f"\t[{type}]")
return type
if (
process.returncode == 1
and 'java.lang.Exception: Provider "org.bouncycastle.jce.provider.BouncyCastleProvider" not found'
in process.stdout
):
print(
"\nKeycheck failed as BouncyCastle jar file is missing from the working directory"
)
return "unexpected"
print("Unexpected key, patching might fail")
return "unexpected"
def handleTermux():
# could import platform and check platform.machine here, and use a different aapt2 file for each architecture
# https://github.com/ReVanced/revanced-cli/blob/main/docs/0_prerequisites.md
# problem is i don't have armv7 or x86 devices to test this, so only armv8 is supported for now
if os.path.exists("aapt2"):
if not os.access("aapt2", os.X_OK):
subprocess.run(["chmod", "+x", "aapt2"], capture_output=True)
if not os.access("aapt2", os.X_OK):
print(
"Can't grant execute permission to aapt2. I don't know why but this can happen if the repo is cloned "
+ "outside termux root, like inside the download folder. (tested on android 10)"
)
else:
print("aapt2 file is missing, patching will probably fail")
def check_apk_signature(package_name: str, path_to_apk: str):
knownSignatures = {
"com.google.android.youtube": ["24bb24c05e47e0aefa68a58a766179d9b613a600"],
"com.google.android.apps.youtube.music": [
"afb0fed5eeaebdd86f56a97742f4b6b33ef59875"
],
}
command = [
"java",
"-jar",
"../apksigner.jar",
"verify",
"--print-certs",
path_to_apk,
]
try:
process = subprocess.run(command, capture_output=True, text=True)
output = process.stdout
output = list(
filter(
lambda x: x.startswith("Signer") and "SHA-1 digest" in x,
output.split("\n"),
)
)
sha1 = output[-1].split(" ")[-1]
verifiable = package_name in knownSignatures
status = (
"¯\\_(ツ)_/¯"
if not verifiable
else "known" if sha1 in knownSignatures[package_name] else "unknown"
)
print(f"apk signature: {sha1} [{status}]")
if status == "unknown":
# print("this apk has a different signature than usual,")
temp = select_item(
"This apk has a different signature than usual, continue? ",
[True, False],
lambda x: "yes" if x else "exit",
)
if temp == False:
sys.exit()
except Exception as e:
print("apk signature check crashed", e)
def main():
default_repo = "revanced"
default_app = "com.google.android.youtube"
non_default_app = False
is_termux = False
parser = argparse.ArgumentParser(
description="Build patched apps with ReVanced tools",
epilog='The script looks for a general "revanced.keystore" file inside the working directory. '
+ "Both pre and past revanced-cli4.0 keys are supported",
)
parser.add_argument(
"-l",
"--local",
action="store_true",
help="build using local files to avoid unnecessary github api requests(max 60/h) and re-downloads",
)
parser.add_argument(
"-e",
"--export",
action="store_true",
help="stop after printing the patch command instead of running it",
)
parser.add_argument(
"repository",
type=str,
default=default_repo,
nargs="?",
help="github username to download revanced-cli, patches and integrations form, "
+ "also acts as download folder (default and fallback: %(default)s)",
)
parser.add_argument(
"--cli",
type=str,
default=None,
help="github username to download revanced-cli from (priority over repository). use username/reponame for different repo names like YT-Advanced",
)
parser.add_argument(
"--patches",
type=str,
default=None,
help="github username to download revanced-patches from (priority over repository). use username/reponame for different repo names like YT-Advanced",
)
parser.add_argument(
"--integrations",
type=str,
default=None,
help="github username to download revanced-integrations from (priority over repository). use username/reponame for different repo names like YT-Advanced",
)
parser.add_argument(
"-a",
"--app",
"--package",
nargs="?",
const="",
default=default_app,
help="specify an app to patch or be prompted to choose based on patches (default: %(default)s). "
+ "The script will scan the working directory for apk files before trying to download when this option is used. "
+ "No checks are done with this option, you can provide any apk and use at least the universal patches on it",
)
parser.add_argument(
"--dev",
action="store_true",
help="don't ignore prereleases when downloading the latest version of patching tools from github",
)
args = parser.parse_args()
if args.app != default_app:
non_default_app = True
if "com.termux" in sys.prefix:
is_termux = True
handleTermux()
check_java()
for folder in ["_builds", args.repository]:
if not os.path.exists(folder):
os.makedirs(folder)
os.chdir(args.repository)
if not args.local:
update_revanced(
args.repository,
default_repo,
args.cli,
args.patches,
args.integrations,
args.dev,
)
all_apps = []
app_patches = []
recomended_version = ""
with open("patches.json", "r") as file:
data = json.load(file)
if args.app == "":
for key in data:
if key["compatiblePackages"] == None:
continue
for package in key["compatiblePackages"]:
if package["name"] not in all_apps:
all_apps.append(package["name"])
all_apps.sort()
args.app = select_item("Select app to patch: ", all_apps)
print("Working with:", args.app)
for key in data:
# include universal patches
if key["compatiblePackages"] == None:
app_patches.append(key)
continue
for package in key["compatiblePackages"]:
if package["name"] == args.app:
app_patches.append(key)
# this isn't optimal but so far patches were updated all at once so it works
if not recomended_version and package["versions"]:
recomended_version = package["versions"][-1]
print(
f'Presuming recomended {"app" if non_default_app else "youtube"} version: {recomended_version}'
)
print('"(-)" prefix means not used by default')
filter_function = lambda x: (
f'{x["name"]} - {x["description"]}'
if x["use"]
else f'(-) {x["name"]} - {x["description"]}'
)
selected_options = select_multiple_items(
"Select patches (e.g. 4,7-12,1) or empty for default: ",
app_patches,
filter_function,
True,
)
command_patches = []
if selected_options:
choices = [
"Only include the selected patches",
"Exclude the selected patches (from the default ones)",
"Include selected patches (together with default ones)",
]
selected_item = choices.index(
select_item("Enter the number of your choice: ", choices)
)
if selected_item == 0:
command_patches.append("--exclusive")
for patch in selected_options:
command_patches.append(f'--include={patch["name"]}')
elif selected_item == 1:
for patch in selected_options:
command_patches.append(f'--exclude={patch["name"]}')
else:
for patch in selected_options:
command_patches.append(f'--include={patch["name"]}')
keystore_file = (
"../revanced.keystore"
if os.path.exists(
os.path.join(os.path.dirname(os.getcwd()), "revanced.keystore")
)
else "revanced.keystore"
)
output_file = (
# f'../_builds/revanced({args.repository})[{args.app.replace(".", "_")}].apk'
f'revanced({args.repository})[{args.app.replace(".", "_")}].apk'
)
apk_file = get_apk(args.app, recomended_version, args.local, non_default_app)
base_command = [
"java",
"-jar",
"cli.jar",
"patch",
"--options=options.json",
# "--resource-cache=.cache", # seems like this can't just be set to a path, it wants a folder with cache already in it. move output file to _builds for now
"--patch-bundle=patches.jar",
"--merge=integrations.apk",
f"--keystore={keystore_file}",
f"--out={output_file}",
apk_file,
]
# cli 4.0 doesn't work with old keys
# https://github.com/ReVanced/revanced-cli/issues/277
# https://github.com/ReVanced/revanced-cli/issues/272
# we are in a situation where revanced-cli only works with new keys and inotia00's fork only works with old keys.....
compatibility_patch_old_key = [
"--alias=alias",
"--keystore-entry-password=ReVanced",
"--keystore-password=ReVanced",
]
compatibility_patch_new_key = [
"--alias=ReVanced Key",
"--keystore-entry-password=",
"--keystore-password=",
]
key_type = check_keystore_type(keystore_file)
if key_type == "old":
base_command += compatibility_patch_old_key
if key_type == "new":
base_command += compatibility_patch_new_key
if is_termux:
base_command += ["--custom-aapt2-binary=../aapt2"]
base_command += command_patches
# quotes around patch names that contain spaces
printable_command = [
(
f'{item.split("=")[0]}="{item.split("=")[1]}"'
if " " in item and "=" in item and item.startswith("--")
else item
)
for item in base_command
]
# for user-provided apks that have spaces
printable_command = [
f'"{item}"' if " " in item and not item.startswith("--") else item
for item in printable_command
]
# add empty quotes to possibly blank arguments like keystore-password
printable_command = [
f'{item.split("=")[0]}=""' if re.match(r".*=$", item) else item
for item in printable_command
]
print("Running:", " ".join(printable_command))
if not args.export:
subprocess.run(base_command)
print(
"Moved to",
os.path.abspath(shutil.move(output_file, "../_builds/" + output_file)),
)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\n\nThe script was interrupted by the user.")