2023-03-05 10:12:46 +01:00
|
|
|
import 'package:flutter/foundation.dart';
|
2022-08-14 20:40:34 +02:00
|
|
|
import 'package:root/root.dart';
|
|
|
|
|
|
|
|
class RootAPI {
|
2023-06-23 00:03:03 +02:00
|
|
|
// TODO(ponces): remove in the future, keep it for now during migration.
|
|
|
|
final String _revancedOldDirPath = '/data/local/tmp/revanced-manager';
|
|
|
|
final String _revancedDirPath = '/data/adb/revanced';
|
2022-08-31 10:36:36 +02:00
|
|
|
final String _postFsDataDirPath = '/data/adb/post-fs-data.d';
|
|
|
|
final String _serviceDDirPath = '/data/adb/service.d';
|
2022-08-14 20:40:34 +02:00
|
|
|
|
2022-09-23 16:47:30 +02:00
|
|
|
Future<bool> isRooted() async {
|
|
|
|
try {
|
2023-01-30 13:35:06 +01:00
|
|
|
final bool? isRooted = await Root.isRootAvailable();
|
2022-09-23 16:47:30 +02:00
|
|
|
return isRooted != null && isRooted;
|
2023-03-05 10:12:46 +01:00
|
|
|
} on Exception catch (e) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
print(e);
|
|
|
|
}
|
2022-09-23 16:47:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 15:40:49 +02:00
|
|
|
Future<bool> hasRootPermissions() async {
|
2022-09-16 17:39:37 +02:00
|
|
|
try {
|
2022-09-22 15:01:05 +02:00
|
|
|
bool? isRooted = await Root.isRootAvailable();
|
|
|
|
if (isRooted != null && isRooted) {
|
|
|
|
isRooted = await Root.isRooted();
|
|
|
|
return isRooted != null && isRooted;
|
|
|
|
}
|
|
|
|
return false;
|
2023-03-05 10:12:46 +01:00
|
|
|
} on Exception catch (e) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
print(e);
|
|
|
|
}
|
2022-09-16 17:39:37 +02:00
|
|
|
return false;
|
|
|
|
}
|
2022-09-06 15:40:49 +02:00
|
|
|
}
|
|
|
|
|
2022-09-14 10:59:56 +02:00
|
|
|
Future<void> setPermissions(
|
|
|
|
String permissions,
|
|
|
|
ownerGroup,
|
|
|
|
seLinux,
|
|
|
|
String filePath,
|
|
|
|
) async {
|
2023-06-23 00:03:03 +02:00
|
|
|
try {
|
|
|
|
if (permissions.isNotEmpty) {
|
|
|
|
await Root.exec(
|
|
|
|
cmd: 'chmod $permissions "$filePath"',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (ownerGroup.isNotEmpty) {
|
|
|
|
await Root.exec(
|
|
|
|
cmd: 'chown $ownerGroup "$filePath"',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (seLinux.isNotEmpty) {
|
|
|
|
await Root.exec(
|
|
|
|
cmd: 'chcon $seLinux "$filePath"',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} on Exception catch (e) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
print(e);
|
|
|
|
}
|
2022-09-14 10:59:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-25 01:51:47 +02:00
|
|
|
Future<bool> isAppInstalled(String packageName) async {
|
|
|
|
if (packageName.isNotEmpty) {
|
2023-06-23 00:03:03 +02:00
|
|
|
return fileExists('$_serviceDDirPath/$packageName.sh');
|
2022-08-25 01:51:47 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<String>> getInstalledApps() async {
|
2023-06-23 00:03:03 +02:00
|
|
|
final List<String> apps = List.empty();
|
2022-08-14 20:40:34 +02:00
|
|
|
try {
|
2023-06-23 00:03:03 +02:00
|
|
|
String? res = await Root.exec(
|
|
|
|
cmd: 'ls "$_revancedDirPath"',
|
|
|
|
);
|
|
|
|
if (res != null) {
|
|
|
|
final List<String> list = res.split('\n');
|
|
|
|
list.removeWhere((pack) => pack.isEmpty);
|
|
|
|
apps.addAll(list.map((pack) => pack.trim()).toList());
|
|
|
|
}
|
|
|
|
// TODO(ponces): remove in the future, keep it for now during migration.
|
|
|
|
res = await Root.exec(
|
|
|
|
cmd: 'ls "$_revancedOldDirPath"',
|
2022-08-15 04:31:36 +02:00
|
|
|
);
|
2022-08-25 01:51:47 +02:00
|
|
|
if (res != null) {
|
2023-06-23 00:03:03 +02:00
|
|
|
final List<String> list = res.split('\n');
|
|
|
|
list.removeWhere((pack) => pack.isEmpty);
|
|
|
|
apps.addAll(list.map((pack) => pack.trim()).toList());
|
2022-08-25 01:51:47 +02:00
|
|
|
}
|
2023-03-05 10:12:46 +01:00
|
|
|
} on Exception catch (e) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
print(e);
|
|
|
|
}
|
2022-08-14 20:40:34 +02:00
|
|
|
}
|
2023-06-23 00:03:03 +02:00
|
|
|
return apps;
|
2022-08-14 20:40:34 +02:00
|
|
|
}
|
|
|
|
|
2022-08-15 04:31:36 +02:00
|
|
|
Future<void> deleteApp(String packageName, String originalFilePath) async {
|
|
|
|
await Root.exec(
|
|
|
|
cmd: 'am force-stop "$packageName"',
|
|
|
|
);
|
|
|
|
await Root.exec(
|
|
|
|
cmd: 'su -mm -c "umount -l $originalFilePath"',
|
|
|
|
);
|
2023-06-23 00:03:03 +02:00
|
|
|
// TODO(ponces): remove in the future, keep it for now during migration.
|
|
|
|
await Root.exec(
|
|
|
|
cmd: 'rm -rf "$_revancedOldDirPath/$packageName"',
|
|
|
|
);
|
2022-08-15 04:31:36 +02:00
|
|
|
await Root.exec(
|
2023-06-23 00:03:03 +02:00
|
|
|
cmd: 'rm -rf "$_revancedDirPath/$packageName"',
|
2022-08-15 04:31:36 +02:00
|
|
|
);
|
|
|
|
await Root.exec(
|
2022-08-18 16:33:33 +02:00
|
|
|
cmd: 'rm -rf "$_serviceDDirPath/$packageName.sh"',
|
2022-08-15 04:31:36 +02:00
|
|
|
);
|
|
|
|
await Root.exec(
|
2022-08-18 16:33:33 +02:00
|
|
|
cmd: 'rm -rf "$_postFsDataDirPath/$packageName.sh"',
|
2022-08-15 04:31:36 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-14 20:40:34 +02:00
|
|
|
Future<bool> installApp(
|
|
|
|
String packageName,
|
|
|
|
String originalFilePath,
|
|
|
|
String patchedFilePath,
|
|
|
|
) async {
|
|
|
|
try {
|
2022-09-13 17:54:43 +02:00
|
|
|
await deleteApp(packageName, originalFilePath);
|
2022-08-14 22:32:03 +02:00
|
|
|
await Root.exec(
|
2023-06-23 00:03:03 +02:00
|
|
|
cmd: 'mkdir -p "$_revancedDirPath/$packageName"',
|
2022-08-14 20:40:34 +02:00
|
|
|
);
|
2022-09-14 11:34:16 +02:00
|
|
|
await setPermissions(
|
|
|
|
'0755',
|
|
|
|
'shell:shell',
|
|
|
|
'',
|
2023-06-23 00:03:03 +02:00
|
|
|
'$_revancedDirPath/$packageName',
|
2022-09-14 11:34:16 +02:00
|
|
|
);
|
2022-09-13 17:54:43 +02:00
|
|
|
await saveOriginalFilePath(packageName, originalFilePath);
|
|
|
|
await installServiceDScript(packageName);
|
|
|
|
await installPostFsDataScript(packageName);
|
|
|
|
await installApk(packageName, patchedFilePath);
|
|
|
|
await mountApk(packageName, originalFilePath);
|
2022-08-14 20:40:34 +02:00
|
|
|
return true;
|
2023-03-05 10:12:46 +01:00
|
|
|
} on Exception catch (e) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
print(e);
|
|
|
|
}
|
2022-08-14 20:40:34 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-15 04:31:36 +02:00
|
|
|
Future<void> installServiceDScript(String packageName) async {
|
2023-01-30 13:35:06 +01:00
|
|
|
final String content = '#!/system/bin/sh\n'
|
2022-10-16 21:16:48 +02:00
|
|
|
'while [ "\$(getprop sys.boot_completed | tr -d \'"\'"\'\\\\r\'"\'"\')" != "1" ]; do sleep 3; done\n'
|
2023-06-23 00:03:03 +02:00
|
|
|
'base_path=$_revancedDirPath/$packageName/base.apk\n'
|
2022-08-15 04:31:36 +02:00
|
|
|
'stock_path=\$(pm path $packageName | grep base | sed \'"\'"\'s/package://g\'"\'"\')\n'
|
2023-01-30 13:35:06 +01:00
|
|
|
r'[ ! -z $stock_path ] && mount -o bind $base_path $stock_path';
|
|
|
|
final String scriptFilePath = '$_serviceDDirPath/$packageName.sh';
|
2022-08-14 22:32:03 +02:00
|
|
|
await Root.exec(
|
2022-08-15 04:31:36 +02:00
|
|
|
cmd: 'echo \'$content\' > "$scriptFilePath"',
|
|
|
|
);
|
2022-09-14 10:59:56 +02:00
|
|
|
await setPermissions('0744', '', '', scriptFilePath);
|
2022-08-15 04:31:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> installPostFsDataScript(String packageName) async {
|
2023-01-30 13:35:06 +01:00
|
|
|
final String content = '#!/system/bin/sh\n'
|
2022-08-15 04:31:36 +02:00
|
|
|
'stock_path=\$(pm path $packageName | grep base | sed \'"\'"\'s/package://g\'"\'"\')\n'
|
2023-01-30 13:35:06 +01:00
|
|
|
r'[ ! -z $stock_path ] && umount -l $stock_path';
|
|
|
|
final String scriptFilePath = '$_postFsDataDirPath/$packageName.sh';
|
2022-08-15 04:31:36 +02:00
|
|
|
await Root.exec(
|
|
|
|
cmd: 'echo \'$content\' > "$scriptFilePath"',
|
|
|
|
);
|
2022-09-14 10:59:56 +02:00
|
|
|
await setPermissions('0744', '', '', scriptFilePath);
|
2022-08-14 20:40:34 +02:00
|
|
|
}
|
|
|
|
|
2022-08-15 04:31:36 +02:00
|
|
|
Future<void> installApk(String packageName, String patchedFilePath) async {
|
2023-06-23 00:03:03 +02:00
|
|
|
final String newPatchedFilePath = '$_revancedDirPath/$packageName/base.apk';
|
2022-08-15 04:31:36 +02:00
|
|
|
await Root.exec(
|
|
|
|
cmd: 'cp "$patchedFilePath" "$newPatchedFilePath"',
|
|
|
|
);
|
2022-09-14 10:59:56 +02:00
|
|
|
await setPermissions(
|
|
|
|
'0644',
|
|
|
|
'system:system',
|
|
|
|
'u:object_r:apk_data_file:s0',
|
|
|
|
newPatchedFilePath,
|
2022-08-15 04:31:36 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-13 17:54:43 +02:00
|
|
|
Future<void> mountApk(String packageName, String originalFilePath) async {
|
2023-06-23 00:03:03 +02:00
|
|
|
final String newPatchedFilePath = '$_revancedDirPath/$packageName/base.apk';
|
2022-08-15 04:31:36 +02:00
|
|
|
await Root.exec(
|
|
|
|
cmd: 'am force-stop "$packageName"',
|
|
|
|
);
|
|
|
|
await Root.exec(
|
|
|
|
cmd: 'su -mm -c "umount -l $originalFilePath"',
|
|
|
|
);
|
2022-08-14 22:32:03 +02:00
|
|
|
await Root.exec(
|
2022-08-15 04:31:36 +02:00
|
|
|
cmd: 'su -mm -c "mount -o bind $newPatchedFilePath $originalFilePath"',
|
2022-08-14 22:32:03 +02:00
|
|
|
);
|
2022-08-14 20:40:34 +02:00
|
|
|
}
|
2022-09-13 17:54:43 +02:00
|
|
|
|
2022-09-14 10:59:56 +02:00
|
|
|
Future<bool> isMounted(String packageName) async {
|
2023-01-30 13:35:06 +01:00
|
|
|
final String? res = await Root.exec(
|
2022-09-14 10:59:56 +02:00
|
|
|
cmd: 'cat /proc/mounts | grep $packageName',
|
|
|
|
);
|
|
|
|
return res != null && res.isNotEmpty;
|
|
|
|
}
|
|
|
|
|
2023-06-23 00:03:03 +02:00
|
|
|
Future<String> getOriginalFilePath(String packageName) async {
|
|
|
|
final String originalPath = '$_revancedDirPath/$packageName/original.apk';
|
|
|
|
final String oldOrigPath = '$_revancedOldDirPath/$packageName/original.apk';
|
2023-01-30 13:35:06 +01:00
|
|
|
final bool isInstalled = await isAppInstalled(packageName);
|
2022-09-14 10:59:56 +02:00
|
|
|
if (isInstalled && await isMounted(packageName)) {
|
2023-06-23 00:03:03 +02:00
|
|
|
if (await fileExists(originalPath)) {
|
|
|
|
return originalPath;
|
|
|
|
} else if (await fileExists(oldOrigPath)) {
|
|
|
|
return oldOrigPath;
|
|
|
|
}
|
2022-09-14 10:59:56 +02:00
|
|
|
}
|
2023-06-23 00:03:03 +02:00
|
|
|
return '';
|
2022-09-13 17:54:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> saveOriginalFilePath(
|
|
|
|
String packageName,
|
|
|
|
String originalFilePath,
|
|
|
|
) async {
|
2023-01-30 13:35:06 +01:00
|
|
|
final String originalRootPath =
|
2023-06-23 00:03:03 +02:00
|
|
|
'$_revancedDirPath/$packageName/original.apk';
|
2022-09-14 10:59:56 +02:00
|
|
|
await Root.exec(
|
2023-06-23 00:03:03 +02:00
|
|
|
cmd: 'mkdir -p "$_revancedDirPath/$packageName"',
|
2022-09-14 10:59:56 +02:00
|
|
|
);
|
|
|
|
await setPermissions(
|
|
|
|
'0755',
|
|
|
|
'shell:shell',
|
|
|
|
'',
|
2023-06-23 00:03:03 +02:00
|
|
|
'$_revancedDirPath/$packageName',
|
2022-09-14 10:59:56 +02:00
|
|
|
);
|
2022-09-13 17:54:43 +02:00
|
|
|
await Root.exec(
|
|
|
|
cmd: 'cp "$originalFilePath" "$originalRootPath"',
|
|
|
|
);
|
2022-09-14 10:59:56 +02:00
|
|
|
await setPermissions(
|
|
|
|
'0644',
|
|
|
|
'shell:shell',
|
|
|
|
'u:object_r:apk_data_file:s0',
|
|
|
|
originalFilePath,
|
|
|
|
);
|
2022-09-13 17:54:43 +02:00
|
|
|
}
|
2023-06-23 00:03:03 +02:00
|
|
|
|
|
|
|
Future<bool> fileExists(String path) async {
|
|
|
|
try {
|
|
|
|
final String? res = await Root.exec(
|
|
|
|
cmd: 'ls $path',
|
|
|
|
);
|
|
|
|
return res != null && res.isNotEmpty;
|
|
|
|
} on Exception catch (e) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
print(e);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2022-08-14 20:40:34 +02:00
|
|
|
}
|