root_api.dart 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import 'package:flutter/foundation.dart';
  2. import 'package:root/root.dart';
  3. class RootAPI {
  4. final String _revancedDirPath = '/data/adb/revanced';
  5. final String _serviceDDirPath = '/data/adb/service.d';
  6. Future<bool> isRooted() async {
  7. try {
  8. final bool? isRooted = await Root.isRootAvailable();
  9. return isRooted != null && isRooted;
  10. } on Exception catch (e) {
  11. if (kDebugMode) {
  12. print(e);
  13. }
  14. return false;
  15. }
  16. }
  17. Future<bool> hasRootPermissions() async {
  18. try {
  19. bool? isRooted = await Root.isRootAvailable();
  20. if (isRooted != null && isRooted) {
  21. isRooted = await Root.isRooted();
  22. return isRooted != null && isRooted;
  23. }
  24. return false;
  25. } on Exception catch (e) {
  26. if (kDebugMode) {
  27. print(e);
  28. }
  29. return false;
  30. }
  31. }
  32. Future<void> setPermissions(
  33. String permissions,
  34. ownerGroup,
  35. seLinux,
  36. String filePath,
  37. ) async {
  38. try {
  39. final StringBuffer commands = StringBuffer();
  40. if (permissions.isNotEmpty) {
  41. commands.writeln('chmod $permissions $filePath');
  42. }
  43. if (ownerGroup.isNotEmpty) {
  44. commands.writeln('chown $ownerGroup $filePath');
  45. }
  46. if (seLinux.isNotEmpty) {
  47. commands.writeln('chcon $seLinux $filePath');
  48. }
  49. await Root.exec(
  50. cmd: commands.toString(),
  51. );
  52. } on Exception catch (e) {
  53. if (kDebugMode) {
  54. print(e);
  55. }
  56. }
  57. }
  58. Future<bool> isAppInstalled(String packageName) async {
  59. if (packageName.isNotEmpty) {
  60. return fileExists('$_serviceDDirPath/$packageName.sh');
  61. }
  62. return false;
  63. }
  64. Future<List<String>> getInstalledApps() async {
  65. final List<String> apps = List.empty(growable: true);
  66. try {
  67. final String? res = await Root.exec(cmd: 'ls $_revancedDirPath');
  68. if (res != null) {
  69. final List<String> list = res.split('\n');
  70. list.removeWhere((pack) => pack.isEmpty);
  71. apps.addAll(list.map((pack) => pack.trim()).toList());
  72. }
  73. } on Exception catch (e) {
  74. if (kDebugMode) {
  75. print(e);
  76. }
  77. }
  78. return apps;
  79. }
  80. Future<void> uninstall(String packageName) async {
  81. await Root.exec(
  82. cmd: '''
  83. grep $packageName /proc/mounts | while read -r line; do echo \$line | cut -d " " -f 2 | sed "s/apk.*/apk/" | xargs -r umount -l; done
  84. rm -rf $_revancedDirPath/$packageName $_serviceDDirPath/$packageName.sh
  85. ''',
  86. );
  87. }
  88. Future<void> removeOrphanedFiles() async {
  89. await Root.exec(
  90. cmd: 'find $_revancedDirPath -type f -name original.apk -delete',
  91. );
  92. }
  93. Future<bool> install(
  94. String packageName,
  95. String originalFilePath,
  96. String patchedFilePath,
  97. ) async {
  98. try {
  99. await setPermissions(
  100. '0755',
  101. 'shell:shell',
  102. '',
  103. '$_revancedDirPath/$packageName',
  104. );
  105. await installPatchedApk(packageName, patchedFilePath);
  106. await installServiceDScript(packageName);
  107. await runMountScript(packageName);
  108. return true;
  109. } on Exception catch (e) {
  110. if (kDebugMode) {
  111. print(e);
  112. }
  113. return false;
  114. }
  115. }
  116. Future<void> installServiceDScript(String packageName) async {
  117. await Root.exec(
  118. cmd: 'mkdir -p $_serviceDDirPath',
  119. );
  120. final String mountScript = '''
  121. #!/system/bin/sh
  122. # Mount using Magisk mirror, if available.
  123. MAGISKTMP="\$( magisk --path )" || MAGISKTMP=/sbin
  124. MIRROR="\$MAGISKTMP/.magisk/mirror"
  125. if [ ! -f \$MIRROR ]; then
  126. MIRROR=""
  127. fi
  128. until [ "\$(getprop sys.boot_completed)" = 1 ]; do sleep 3; done
  129. until [ -d "/sdcard/Android" ]; do sleep 1; done
  130. # Unmount any existing installation to prevent multiple unnecessary mounts.
  131. grep $packageName /proc/mounts | while read -r line; do echo \$line | cut -d " " -f 2 | sed "s/apk.*/apk/" | xargs -r umount -l; done
  132. base_path=$_revancedDirPath/$packageName/base.apk
  133. stock_path=\$(pm path $packageName | grep base | sed "s/package://g" )
  134. chcon u:object_r:apk_data_file:s0 \$base_path
  135. mount -o bind \$MIRROR\$base_path \$stock_path
  136. # Kill the app to force it to restart the mounted APK in case it is already running
  137. am force-stop $packageName
  138. '''
  139. .trimMultilineString();
  140. final String scriptFilePath = '$_serviceDDirPath/$packageName.sh';
  141. await Root.exec(
  142. cmd: 'echo \'$mountScript\' > "$scriptFilePath"',
  143. );
  144. await setPermissions('0744', '', '', scriptFilePath);
  145. }
  146. Future<void> installPatchedApk(
  147. String packageName, String patchedFilePath,) async {
  148. final String newPatchedFilePath = '$_revancedDirPath/$packageName/base.apk';
  149. await Root.exec(
  150. cmd: '''
  151. mkdir -p $_revancedDirPath/$packageName
  152. cp "$patchedFilePath" $newPatchedFilePath
  153. ''',
  154. );
  155. await setPermissions(
  156. '0644',
  157. 'system:system',
  158. 'u:object_r:apk_data_file:s0',
  159. newPatchedFilePath,
  160. );
  161. }
  162. Future<void> runMountScript(
  163. String packageName,
  164. ) async {
  165. await Root.exec(cmd: '.$_serviceDDirPath/$packageName.sh');
  166. }
  167. Future<bool> fileExists(String path) async {
  168. try {
  169. final String? res = await Root.exec(
  170. cmd: 'ls $path',
  171. );
  172. return res != null && res.isNotEmpty;
  173. } on Exception catch (e) {
  174. if (kDebugMode) {
  175. print(e);
  176. }
  177. return false;
  178. }
  179. }
  180. }
  181. // Remove leading spaces manually until
  182. // https://github.com/dart-lang/language/issues/559 is closed
  183. extension StringExtension on String {
  184. String trimMultilineString() =>
  185. split('\n').map((line) => line.trim()).join('\n').trim();
  186. }