-
Notifications
You must be signed in to change notification settings - Fork 62
Support exec via broadcast #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b0f7807
check io.github.vvb2060.magisk
Howard20181 f0838ce
Update Gradle wrapper
Howard20181 5d8564e
AGP 9.0
Howard20181 25be81c
Separate lib
Howard20181 669d208
support exec
Howard20181 e7a45f1
print exec output
Howard20181 b3eac57
Fix actions name mismatch
Howard20181 ced37ee
Fix the world
Howard20181 8460789
Update app/src/main/java/io/github/vvb2060/puellamagi/CommandExecutor…
Howard20181 67cb533
Update app/src/main/java/io/github/vvb2060/puellamagi/CommandReceiver…
Howard20181 2ab9d4b
Harden broadcast handling and manifest security
Howard20181 8ae5711
test
Howard20181 adb25da
fix start service failed
Howard20181 f36c031
Update CommandExecutor.java
Howard20181 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| [submodule "libmagica/src/main/jni/external/lsplt"] | ||
| path = libmagica/src/main/jni/external/lsplt | ||
| url = https://github.com/LSPosed/LSPlt.git |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
app/src/main/java/io/github/vvb2060/puellamagi/BootCompletedReceiver.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package io.github.vvb2060.puellamagi; | ||
|
|
||
| import static io.github.vvb2060.puellamagi.App.TAG; | ||
|
|
||
| import android.content.BroadcastReceiver; | ||
| import android.content.Context; | ||
| import android.content.Intent; | ||
| import android.util.Log; | ||
|
|
||
| public final class BootCompletedReceiver extends BroadcastReceiver { | ||
| @Override | ||
| public void onReceive(Context context, Intent intent) { | ||
| if (intent == null) { | ||
| return; | ||
| } | ||
| var action = intent.getAction(); | ||
| if (!Intent.ACTION_LOCKED_BOOT_COMPLETED.equals(action) | ||
| && !Intent.ACTION_BOOT_COMPLETED.equals(action)) { | ||
| return; | ||
| } | ||
| try { | ||
| context.startService(new Intent(context, MagicaService.class)); | ||
| Log.i(TAG, "MagicaService started from boot action: " + action); | ||
| } catch (RuntimeException e) { | ||
| Log.e(TAG, "Failed to start MagicaService from boot action: " + action, e); | ||
| } | ||
| try { | ||
| context.startService(new Intent(context, CommandService.class)); | ||
| Log.i(TAG, "CommandService started from boot action: " + action); | ||
| } catch (RuntimeException e) { | ||
| Log.e(TAG, "Failed to start CommandService from boot action: " + action, e); | ||
| } | ||
| } | ||
| } | ||
|
|
64 changes: 64 additions & 0 deletions
64
app/src/main/java/io/github/vvb2060/puellamagi/CommandExecutor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package io.github.vvb2060.puellamagi; | ||
|
|
||
| import android.util.Log; | ||
|
|
||
| import io.github.vvb2060.magica.lib.MagicaRoot; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.IOException; | ||
| import java.io.InputStreamReader; | ||
|
|
||
| final class CommandExecutor { | ||
|
|
||
| private CommandExecutor() { | ||
| } | ||
|
|
||
| static String normalize(String command) { | ||
| if (command == null) { | ||
| return ""; | ||
| } | ||
| return command.trim(); | ||
| } | ||
|
|
||
| static String execute(String command) throws IOException { | ||
| var normalized = normalize(command); | ||
Howard20181 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (normalized.isEmpty()) { | ||
| throw new IllegalArgumentException("Command must not be empty"); | ||
| } | ||
|
|
||
| MagicaRoot.root(); | ||
| var process = Runtime.getRuntime().exec(new String[]{"sh", "-c", normalized}); | ||
| var stdout = readAll(process.getInputStream()); | ||
| var stderr = readAll(process.getErrorStream()); | ||
|
|
||
| try { | ||
| int code = process.waitFor(); | ||
| var output = stdout.isEmpty() ? stderr : stdout; | ||
| Log.d(App.TAG, output + " (exit=" + code + ")"); | ||
| if (output.isEmpty()) { | ||
| output = "<empty output>"; | ||
| } | ||
| return "exit=" + code + ", output=" + output; | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| throw new IOException("Command interrupted", e); | ||
| } finally { | ||
| process.destroy(); | ||
| } | ||
| } | ||
|
|
||
| private static String readAll(java.io.InputStream in) throws IOException { | ||
| var sb = new StringBuilder(); | ||
| try (var reader = new BufferedReader(new InputStreamReader(in))) { | ||
| String line; | ||
| while ((line = reader.readLine()) != null) { | ||
| if (sb.length() > 0) { | ||
| sb.append('\n'); | ||
| } | ||
| sb.append(line); | ||
| } | ||
| } | ||
| return sb.toString(); | ||
| } | ||
| } | ||
|
|
||
62 changes: 62 additions & 0 deletions
62
app/src/main/java/io/github/vvb2060/puellamagi/CommandReceiver.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package io.github.vvb2060.puellamagi; | ||
|
|
||
| import static io.github.vvb2060.puellamagi.App.TAG; | ||
|
|
||
| import android.content.BroadcastReceiver; | ||
| import android.content.Context; | ||
| import android.content.Intent; | ||
| import android.os.Bundle; | ||
| import android.os.Handler; | ||
| import android.os.Looper; | ||
| import android.os.ResultReceiver; | ||
| import android.util.Log; | ||
|
|
||
| public final class CommandReceiver extends BroadcastReceiver { | ||
| public static final String ACTION_EXEC = "io.github.vvb2060.puellamagi.action.EXEC"; | ||
| public static final String EXTRA_COMMAND = "cmd"; | ||
|
|
||
| // Keys used to pass command results through the ResultReceiver bundle. | ||
| static final String KEY_RESULT = "result"; | ||
|
|
||
| @Override | ||
| public void onReceive(Context context, Intent intent) { | ||
| if (intent == null || !ACTION_EXEC.equals(intent.getAction())) { | ||
| return; | ||
| } | ||
|
|
||
| var command = intent.getStringExtra(EXTRA_COMMAND); | ||
|
|
||
| // BroadcastReceivers are NOT allowed to bind to services. | ||
| // Instead, we start MagicaService (isolatedProcess + useAppZygote, already rooted) | ||
| // and pass a ResultReceiver so the service can send the command output back. | ||
| var pending = goAsync(); | ||
| var receiver = new ResultReceiver(new Handler(Looper.getMainLooper())) { | ||
| @Override | ||
| protected void onReceiveResult(int resultCode, Bundle resultData) { | ||
| pending.setResultCode(resultCode); | ||
| if (resultData != null) { | ||
| pending.setResultData(resultData.getString(KEY_RESULT, "")); | ||
| } | ||
| pending.finish(); | ||
| } | ||
| }; | ||
|
|
||
| // Target CommandService (non-isolated): startForegroundService is allowed from | ||
| // background, unlike startService. CommandService holds a persistent binding to | ||
| // the isolated MagicaService (which cannot be started directly) and proxies the | ||
| // command there. | ||
| var serviceIntent = new Intent(context, CommandService.class); | ||
| serviceIntent.setAction(CommandService.ACTION_EXEC); | ||
| serviceIntent.putExtra(CommandService.EXTRA_COMMAND, command); | ||
| serviceIntent.putExtra(CommandService.EXTRA_RESULT_RECEIVER, receiver); | ||
| try { | ||
| context.startService(serviceIntent); | ||
| } catch (Exception e) { | ||
| Log.e(TAG, "Failed to start CommandService", e); | ||
| pending.setResultCode(1); | ||
| pending.setResultData("exec failed: " + e.getMessage()); | ||
| pending.finish(); | ||
| } | ||
Howard20181 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.