2022-08-14 20:40:34 +02:00
|
|
|
import 'package:root/root.dart';
|
|
|
|
|
|
|
|
class RootAPI {
|
2022-09-13 17:54:43 +02:00
|
|
|
final String _managerDirPath = '/data/adb/revanced-manager';
|
2022-09-14 10:59:56 +02:00
|
|
|
final String _managerTmpDirPath = '/data/local/tmp/revanced-manager';
|
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-06 15:40:49 +02:00
|
|
|
Future<bool> hasRootPermissions() async {
|
|
|
|
bool? isRooted = await Root.isRooted();
|
|
|
|
return isRooted != null && isRooted;
|
|
|
|
}
|
|
|
|
|
2022-09-14 10:59:56 +02:00
|
|
|
Future<void> setPermissions(
|
|
|
|
String permissions,
|
|
|
|
ownerGroup,
|
|
|
|
seLinux,
|
|
|
|
String filePath,
|
|
|
|
) async {
|
|
|
|
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"',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-25 01:51:47 +02:00
|
|
|
Future<bool> isAppInstalled(String packageName) async {
|
|
|
|
if (packageName.isNotEmpty) {
|
|
|
|
String? res = await Root.exec(
|
|
|
|
cmd: 'ls "$_managerDirPath/$packageName"',
|
|
|
|
);
|
|
|
|
if (res != null && res.isNotEmpty) {
|
|
|
|
res = await Root.exec(
|
|
|
|
cmd: 'ls "$_serviceDDirPath/$packageName.sh"',
|
|
|
|
);
|
|
|
|
return res != null && res.isNotEmpty;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<String>> getInstalledApps() async {
|
2022-08-14 20:40:34 +02:00
|
|
|
try {
|
2022-08-15 04:31:36 +02:00
|
|
|
String? res = await Root.exec(
|
2022-08-25 01:51:47 +02:00
|
|
|
cmd: 'ls "$_managerDirPath"',
|
2022-08-15 04:31:36 +02:00
|
|
|
);
|
2022-08-25 01:51:47 +02:00
|
|
|
if (res != null) {
|
|
|
|
List<String> apps = res.split('\n');
|
2022-08-26 03:01:53 +02:00
|
|
|
List<String> toRemove = [];
|
2022-08-25 01:51:47 +02:00
|
|
|
for (String packageName in apps) {
|
|
|
|
bool isInstalled = await isAppInstalled(packageName);
|
|
|
|
if (!isInstalled) {
|
2022-08-26 03:01:53 +02:00
|
|
|
toRemove.add(packageName);
|
2022-08-25 01:51:47 +02:00
|
|
|
}
|
|
|
|
}
|
2022-08-26 03:01:53 +02:00
|
|
|
apps.removeWhere((a) => toRemove.contains(a));
|
|
|
|
return apps;
|
2022-08-25 01:51:47 +02:00
|
|
|
}
|
2022-08-14 20:40:34 +02:00
|
|
|
} on Exception {
|
2022-08-25 01:51:47 +02:00
|
|
|
return List.empty();
|
2022-08-14 20:40:34 +02:00
|
|
|
}
|
2022-08-25 01:51:47 +02:00
|
|
|
return List.empty();
|
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"',
|
|
|
|
);
|
|
|
|
await Root.exec(
|
2022-08-18 16:33:33 +02:00
|
|
|
cmd: 'rm -rf "$_managerDirPath/$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(
|
2022-08-18 16:33:33 +02:00
|
|
|
cmd: 'mkdir -p "$_managerDirPath/$packageName"',
|
2022-08-14 20:40:34 +02:00
|
|
|
);
|
2022-09-14 10:59:56 +02:00
|
|
|
await setPermissions('0755', '', '', "$_managerDirPath/$packageName");
|
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;
|
|
|
|
} on Exception {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-15 04:31:36 +02:00
|
|
|
Future<void> installServiceDScript(String packageName) async {
|
2022-08-14 20:40:34 +02:00
|
|
|
String content = '#!/system/bin/sh\n'
|
2022-08-15 04:31:36 +02:00
|
|
|
'while [ "\$(getprop sys.boot_completed | tr -d \'"\'"\'\\\\r\'"\'"\')" != "1" ]; do sleep 1; done\n'
|
2022-08-18 16:33:33 +02:00
|
|
|
'base_path=$_managerDirPath/$packageName/base.apk\n'
|
2022-08-15 04:31:36 +02:00
|
|
|
'stock_path=\$(pm path $packageName | grep base | sed \'"\'"\'s/package://g\'"\'"\')\n'
|
|
|
|
'[ ! -z \$stock_path ] && mount -o bind \$base_path \$stock_path';
|
2022-08-18 16:33:33 +02:00
|
|
|
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 {
|
|
|
|
String content = '#!/system/bin/sh\n'
|
|
|
|
'stock_path=\$(pm path $packageName | grep base | sed \'"\'"\'s/package://g\'"\'"\')\n'
|
|
|
|
'[ ! -z \$stock_path ] && umount -l \$stock_path';
|
2022-08-18 16:33:33 +02:00
|
|
|
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 {
|
2022-08-18 16:33:33 +02:00
|
|
|
String newPatchedFilePath = '$_managerDirPath/$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 {
|
2022-08-18 16:33:33 +02:00
|
|
|
String newPatchedFilePath = '$_managerDirPath/$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 {
|
|
|
|
String? res = await Root.exec(
|
|
|
|
cmd: 'cat /proc/mounts | grep $packageName',
|
|
|
|
);
|
|
|
|
return res != null && res.isNotEmpty;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<String> getOriginalFilePath(
|
|
|
|
String packageName,
|
|
|
|
String originalFilePath,
|
|
|
|
) async {
|
|
|
|
bool isInstalled = await isAppInstalled(packageName);
|
|
|
|
if (isInstalled && await isMounted(packageName)) {
|
|
|
|
originalFilePath = '$_managerTmpDirPath/$packageName/original.apk';
|
|
|
|
await setPermissions(
|
|
|
|
'0644',
|
|
|
|
'shell:shell',
|
|
|
|
'u:object_r:apk_data_file:s0',
|
|
|
|
originalFilePath,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return originalFilePath;
|
2022-09-13 17:54:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> saveOriginalFilePath(
|
|
|
|
String packageName,
|
|
|
|
String originalFilePath,
|
|
|
|
) async {
|
2022-09-14 10:59:56 +02:00
|
|
|
String originalRootPath = '$_managerTmpDirPath/$packageName/original.apk';
|
|
|
|
await Root.exec(
|
|
|
|
cmd: 'mkdir -p "$_managerTmpDirPath/$packageName"',
|
|
|
|
);
|
|
|
|
await setPermissions(
|
|
|
|
'0755',
|
|
|
|
'shell:shell',
|
|
|
|
'',
|
|
|
|
'$_managerTmpDirPath/$packageName',
|
|
|
|
);
|
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
|
|
|
}
|
2022-08-14 20:40:34 +02:00
|
|
|
}
|