forked from reisxd/revanced-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
579 lines (513 loc) · 17.4 KB
/
index.js
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
const fetchURL = require('node-fetch');
const Progress = require('node-fetch-progress');
const { SingleBar: ProgressBar, Presets } = require('cli-progress');
const fs = require('fs');
const util = require('util');
const { exec } = require('child_process');
const { load } = require('cheerio');
const opteric = require('./opteric.js');
const inquirer = require('inquirer');
const argParser = opteric(process.argv.join(' '));
const actualExec = util.promisify(exec);
let adbExists = true;
let foundDevice = false;
const jarNames = {
cli: './revanced/',
patchesJar: './revanced/',
integrations: './revanced/',
deviceId: '',
microG: './revanced/'
};
process.on('uncaughtException', async (err, origin) => {
console.log(
`Caught exception: ${err}\nException origin: ${origin}\nPlease make an issue at https://github.com/reisxd/revanced-builder/issues.`
);
await exitProcess();
});
process.on('unhandledRejection', async (reason) => {
console.log('An error occured.');
console.log(reason);
console.log(
'\nPlease make an issue at https://github.com/reisxd/revanced-builder/issues.'
);
await exitProcess();
});
async function exitProcess () {
console.log('Press any key to exit...');
process.stdin.setRawMode(true);
process.stdin.resume();
await new Promise(() => process.stdin.on('data', () => process.exit()));
}
async function overWriteJarNames (link) {
const fileName = link.split('/').pop();
// i have to use ifs for this sorry
if (fileName.includes('revanced-cli')) jarNames.cli += fileName;
if (fileName.includes('revanced-patches') && fileName.endsWith('.jar')) {
jarNames.patchesJar += fileName;
}
if (fileName.endsWith('unsigned.apk')) jarNames.integrations += fileName;
if (fileName.startsWith('microg')) jarNames.microG += fileName;
}
async function getDownloadLink (json) {
const apiRequest = await fetchURL(
`https://api.github.com/repos/${json.owner}/${json.repo}/releases/latest`
);
const jsonResponse = await apiRequest.json();
let assets = jsonResponse?.assets;
if (jsonResponse.error || !apiRequest.ok) {
const assetsGH = [];
const releasesPage = await fetchURL(
`https://github.com/${json.owner}/${json.repo}/releases/latest`
);
if (!releasesPage.ok) {
throw new Error(
'You got ratelimited from GitHub\n...Completely? What did you even do?'
);
}
const releasePage = await releasesPage.text();
const $ = load(releasePage);
for (const downloadLink of $('a[data-skip-pjax=""]').get()) {
if (!downloadLink.attribs.href.endsWith('.tar.gz')) {
if (!downloadLink.attribs.href.endsWith('.zip')) {
assetsGH.push({
browser_download_url: `https://github.com${downloadLink.attribs.href}`
});
}
}
}
assets = assetsGH;
}
return assets;
}
async function getPage (pageUrl) {
const pageRequest = await fetchURL(pageUrl, {
headers: {
'user-agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36'
}
});
return await pageRequest.text();
}
async function dloadFromURL (url, outputPath) {
const request = await fetchURL(url, {
headers: {
'user-agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36'
}
});
const writeStream = fs.createWriteStream(outputPath);
const downloadStream = request.body.pipe(writeStream);
const progress = new Progress(request, { throttle: 50 });
const pbar = new ProgressBar(
{
format: `[${outputPath
.split('/')
.pop()}] {bar} | {percentage}% | ETA: {eta_formatted} | Speed: {rate}`,
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591',
hideCursor: true,
clearOnComplete: true
},
Presets.shades_classic
);
pbar.start(100, 0);
return new Promise((resolve, reject) => {
progress.on('progress', (p) => {
pbar.update(parseInt(p.progress * 100), {
rate: p.rateh
});
});
downloadStream.once('finish', () => {
pbar.stop();
console.log(`[${outputPath.split('/').pop()}] \u2713`);
resolve();
});
downloadStream.once('error', (err) => {
fs.unlink(outputPath, () => {
pbar.stop();
console.log(`[${outputPath.split('/').pop()}] \u2714`);
reject(new Error('Download failed.', err));
});
});
});
}
async function downloadYTApk (ytVersion) {
const versionsList = await getPage(
'https://www.apkmirror.com/apk/google-inc/youtube'
);
const versionList = [];
let indx = 0;
let versionChoosen;
const $ = load(versionsList);
if (!ytVersion) {
for (const version of $(
'h5[class="appRowTitle wrapText marginZero block-on-mobile"]'
).get()) {
if (indx === 10) continue;
const versionName = version.attribs.title.replace('YouTube ', '');
indx++;
if (versionName.includes('beta')) continue;
versionList.push({
name: versionName
});
}
versionChoosen = await inquirer.prompt([
{
type: 'list',
name: 'version',
message: 'Select a YT version to download.',
choices: versionList
}
]);
}
let apkVersion;
if (ytVersion) apkVersion = ytVersion.replace(/\./g, '-');
if (versionChoosen) apkVersion = versionChoosen.version.replace(/\./g, '-');
const versionDownload = await fetchURL(
`https://www.apkmirror.com/apk/google-inc/youtube/youtube-${apkVersion}-release/`
);
const versionDownloadList = await versionDownload.text();
const vDLL = load(versionDownloadList);
const dlLink = vDLL('span[class="apkm-badge"]')
.first()
.parent()
.children('a[class="accent_color"]')
.first()
.attr('href');
const downloadLink = await fetchURL(`https://www.apkmirror.com${dlLink}`);
const downloadLinkPage = await downloadLink.text();
const dlPage = load(downloadLinkPage);
const pageLink = dlPage('a[class^="accent_bg btn btn-flat downloadButton"]')
.first()
.attr('href');
const downloadPage = await getPage(`https://www.apkmirror.com${pageLink}`);
const apkPage = load(downloadPage);
const apkLink = apkPage('a[rel="nofollow"]').first().attr('href');
console.log("Downloading the YouTube APK, this'll take some time!");
await dloadFromURL(
`https://www.apkmirror.com${apkLink}`,
'./revanced/youtube.apk'
);
return console.log('Download complete!');
}
async function downloadFile (assets) {
for (const asset of assets) {
const dir = fs.readdirSync('./revanced/');
overWriteJarNames(asset.browser_download_url);
if (dir.includes(asset.browser_download_url.split('/').pop())) {
if (
asset.browser_download_url.split('/').pop() !==
'app-release-unsigned.apk'
) {
if (asset.browser_download_url.split('/').pop() !== 'microg.apk') {
continue;
}
}
}
await dloadFromURL(
asset.browser_download_url,
`./revanced/${asset.browser_download_url.split('/').pop()}`
);
}
}
async function downloadFiles (repos) {
for (const repo of repos) {
const downloadLink = await getDownloadLink(repo);
await downloadFile(downloadLink);
}
}
async function getADBDeviceID () {
let deviceId;
const { stdout } = await actualExec('adb devices');
const adbDeviceIdRegex = new RegExp(`${require('os').EOL}(.*?)\t`);
const match = stdout.match(adbDeviceIdRegex);
if (match === null) {
console.log('No device found! Fallback to only build.');
return;
}
const deviceIdN = match[1];
jarNames.deviceId = `-d ${deviceIdN} -c`;
foundDevice = true;
return deviceId;
}
async function checkForJavaADB () {
try {
const javaCheck = await actualExec('java -version');
const javaVerLog = javaCheck.stderr || javaCheck.stdout;
const javaVer = Array.from(javaVerLog.matchAll(/version\s([^:]+)/g))
.map((match) => match[1])[0]
.match(/"(.*)"/)[1];
if (javaVer.split('.')[0] < 17) {
console.log(
'You have an outdated version of JDK.\nPlease get it from here: https://www.azul.com/downloads/?version=java-17-lts&package=jdk'
);
return await exitProcess();
}
if (!javaVerLog.includes('Zulu')) {
console.log(
'You have Java, but not Zulu JDK. You need to install it because of signing problems.\nPlease get it from here: https://www.azul.com/downloads/?version=java-17-lts&package=jdk'
);
return await exitProcess();
}
await actualExec('adb');
} catch (e) {
if (e.stderr.includes('java')) {
console.log(
"You don't have JDK installed.\nPlease get it from here: https://www.azul.com/downloads/?version=java-17-lts&package=jdk"
);
return await exitProcess();
}
if (e.stderr.includes('adb')) {
console.log(
"You don't have ADB installed.\nPlease get it from here: https://developer.android.com/studio/releases/platform-tools\nIf you are gonna use rooted ReVanced, this is needed."
);
adbExists = false;
}
}
}
async function getYTVersion () {
const { stdout, stderr } = await actualExec(
'adb shell dumpsys package com.google.android.youtube'
);
const dumpSysOut = stdout || stderr;
if (!dumpSysOut.match(/versionName=([^=]+)/)) {
throw new Error(
"YouTube is not installed on your device\nIt's needed for rooted ReVanced."
);
}
return dumpSysOut
.match(/versionName=([^=]+)/)[1]
.replace(`${require('os').EOL} `, '')
.match(/[\d]+(\.\d+)+/g)[0];
}
async function preflight (listOnly) {
if (!fs.existsSync('./revanced')) {
fs.mkdirSync('./revanced');
}
if (fs.existsSync('./revanced-cache')) {
fs.rmSync('./revanced-cache', { recursive: true, force: true });
}
console.log('Downloading required files...');
const filesToDownload = [
{
owner: 'revanced',
repo: 'revanced-cli'
},
{
owner: 'revanced',
repo: 'revanced-patches'
}
];
if (!listOnly) {
filesToDownload.push(
{
owner: 'revanced',
repo: 'revanced-integrations'
},
{
owner: 'TeamVanced',
repo: 'VancedMicroG'
}
);
}
await downloadFiles(filesToDownload);
if (!listOnly) {
await checkForJavaADB();
if (adbExists) {
await getADBDeviceID();
}
}
}
(async () => {
switch (argParser.flags[0]) {
case 'patches': {
await preflight(true);
const { stdout, stderr } = await actualExec(
`java -jar ${jarNames.cli} -a NeverGonnaGiveYouUp -b ${jarNames.patchesJar} -l`
);
console.log(stdout || stderr);
break;
}
case 'patch': {
let excludedPatches = '';
let includedPatches = '';
let ytVersion;
let isRooted = false;
await preflight(false);
if (argParser.options.exclude) {
if (argParser.options.exclude.includes('microg-support')) {
if (!adbExists) {
console.log(
"You don't have ADB installed.\nPlease get it from here: https://developer.android.com/studio/releases/platform-tools\n"
);
return await exitProcess();
}
if (foundDevice) {
ytVersion = await getYTVersion();
excludedPatches += ' --mount';
isRooted = true;
} else {
throw new Error(
"Couldn't find the device. Please plug in the device."
);
}
}
if (!argParser.options.exclude.includes(',')) {
excludedPatches = ` -e ${argParser.options.exclude}`;
} else {
for (const patch of argParser.options.exclude.split(',')) {
excludedPatches += ` -e ${patch}`;
}
}
}
if (argParser.options.include) {
if (!argParser.options.include.includes(',')) {
includedPatches = ` -i ${argParser.options.include}`;
} else {
for (const patch of argParser.options.include.split(',')) {
includedPatches += ` -i ${patch}`;
}
}
}
if (!argParser.flags.includes('manual-apk')) {
await downloadYTApk(ytVersion);
}
console.log('Building ReVanced, please be patient!');
const { stdout, stderr } = await actualExec(
`java -jar ${jarNames.cli} -b ${jarNames.patchesJar} --experimental -a ./revanced/youtube.apk ${jarNames.deviceId} -o ./revanced/revanced.apk -m ${jarNames.integrations} ${excludedPatches} ${includedPatches}`,
{ maxBuffer: 5120 * 1024 }
);
console.log(stdout || stderr);
if (
stdout.includes('INSTALL_FAILED_UPDATE_INCOMPATIBLE') ||
stderr.includes('INSTALL_FAILED_UPDATE_INCOMPATIBLE')
) {
console.log(
"Couldn't install ReVanced properly. Reinstalling ReVanced..."
);
await actualExec('adb uninstall app.revanced.android.youtube');
await actualExec('adb install revanced/revanced.apk');
}
if (adbExists && !isRooted && foundDevice) {
console.log('Installing MicroG...');
await actualExec(`adb install ${jarNames.microG}`);
await exitProcess();
} else {
console.log(
'You now can install ReVanced and MicroG by transferring revanced/revanced.apk and revanced/microg.apk!'
);
await exitProcess();
}
break;
}
default: {
let patches = '';
let useManualAPK;
let ytVersion;
let isRooted;
await preflight(false);
const getPatches = await actualExec(
`java -jar ${jarNames.cli} -a ${jarNames.integrations} -b ${jarNames.patchesJar} -l`
);
const patchesText = getPatches.stderr || getPatches.stdout;
const firstWord = patchesText.slice(0, patchesText.indexOf(' '));
const patchRegex = new RegExp(`${firstWord}\\s([^\\t]+)`, 'g');
const patchesArray = patchesText.match(patchRegex);
const patchDescRegex = new RegExp(`\\t(.*) ${require('os').EOL}`, 'g');
const patchDescsArray = patchesText.match(patchDescRegex);
const patchesChoice = [];
let index = 0;
for (const patchName of patchesArray) {
let patch = patchName.replace(firstWord, '').replace(/\s/g, '');
patch += `| ${patchDescsArray[index]
.replace('\t', '')
.replace(require('os').EOL, '')}`;
if (patch.includes('microg-support')) patch += '(Root required)';
if (patch.includes('hide-cast-button')) patch += '(Root required)';
patchesChoice.push({
name: patch
});
index++;
}
const patchesChoosed = await inquirer.prompt([
{
type: 'checkbox',
message: 'Select the patches you want to exclude.',
name: 'patches',
choices: patchesChoice
}
]);
if (patchesChoice.length === patchesChoosed.patches.length) {
throw new Error(
'You excluded every single patch... I guess you want to use YouTube?'
);
}
for (const patch of patchesChoosed.patches) {
if (patch.includes('microg-support')) {
if (!adbExists) {
console.log(
"You don't have ADB installed.\nPlease get it from here: https://developer.android.com/studio/releases/platform-tools\n"
);
return await exitProcess();
}
if (foundDevice) {
console.log(
'WARNING: Rooted builds might or might not work.\nKeep in mind that I (reisxd) cannot test it.'
);
ytVersion = await getYTVersion();
patches += ' --mount';
isRooted = true;
} else {
throw new Error(
"Couldn't find the device. Please plug in the device."
);
}
}
const patchName = patch.replace(/\|.+(.*)$/, '');
patches += ` -e ${patchName}`;
}
if (fs.existsSync('./revanced/youtube.apk') && !isRooted) {
const useManualAPKAnswer = await inquirer.prompt([
{
type: 'confirm',
name: 'useManualAPK',
message:
'The YouTube APK already exists in the revanced folder. Do you want to use it?',
default: false
}
]);
useManualAPK = useManualAPKAnswer.useManualAPK;
}
if (!useManualAPK) {
await downloadYTApk(ytVersion);
}
console.log('Building ReVanced, please be patient!');
const { stdout, stderr } = await actualExec(
`java -jar ${jarNames.cli} -b ${jarNames.patchesJar} --experimental -a ./revanced/youtube.apk ${jarNames.deviceId} -o ./revanced/revanced.apk -m ${jarNames.integrations} ${patches}`,
{ maxBuffer: 5120 * 1024 }
);
console.log(stdout || stderr);
if (
stdout.includes('INSTALL_FAILED_UPDATE_INCOMPATIBLE') ||
stderr.includes('INSTALL_FAILED_UPDATE_INCOMPATIBLE')
) {
console.log(
"Couldn't install ReVanced properly. Reinstalling ReVanced..."
);
await actualExec('adb uninstall app.revanced.android.youtube');
await actualExec('adb install revanced/revanced.apk');
}
if (adbExists && !isRooted && foundDevice) {
console.log('Installing MicroG...');
await actualExec(`adb install ${jarNames.microG}`);
await exitProcess();
} else {
console.log(
'You now can install ReVanced and MicroG by transferring revanced/revanced.apk and revaned/microg.apk!'
);
await exitProcess();
}
break;
}
}
})();