Skip to content

Instantly share code, notes, and snippets.

@Kr328
Last active May 27, 2023 10:30
Show Gist options
  • Save Kr328/f739b71d8116573e93c8df82ca47b65a to your computer and use it in GitHub Desktop.
Save Kr328/f739b71d8116573e93c8df82ca47b65a to your computer and use it in GitHub Desktop.
Receive broadcast in root process.
package com.github.kr328.clash.shizukutest;
import android.content.Intent;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
public class Main {
public static void main(String[] args) throws Exception {
final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
final Element action = document.createElement("action");
action.setAttribute("name", Intent.ACTION_SCREEN_ON);
final Element intentFilter = document.createElement("intent-filter");
intentFilter.appendChild(action);
final Element sender = document.createElement("sender");
sender.setAttribute("type", "system");
final Element broadcast = document.createElement("broadcast");
broadcast.setAttribute("block", "false");
broadcast.setAttribute("log", "true");
broadcast.appendChild(intentFilter);
broadcast.appendChild(sender);
final Element rules = document.createElement("rules");
rules.appendChild(broadcast);
final Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
try (final FileOutputStream output = new FileOutputStream("/data/system/ifw/clash-broadcast-intercept.xml")) {
transformer.transform(new DOMSource(rules), new StreamResult(output));
}
final Process process = new ProcessBuilder()
.command("logcat", "-b", "events", "-v", "raw", "ifw_intent_matched")
.redirectOutput(ProcessBuilder.Redirect.PIPE)
.start();
final BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while (true) {
String line = reader.readLine();
if (line == null) {
break;
}
if (!line.startsWith("[") || !line.endsWith("]")) {
continue;
}
line = line.substring(1, line.length() - 1);
final String[] fields = line.split(",");
if (fields.length < 9) {
continue;
}
final String type = fields[0];
final String shortComponent = fields[1];
final String callerUid = fields[2];
final String callerPackagesLength = fields[3];
final String flags = fields[fields.length - 1];
final String dataString = fields[fields.length - 2];
final String resolvedType = fields[fields.length - 3];
final String act = fields[fields.length - 4];
System.out.println(String.join("|", type, shortComponent, callerUid, callerPackagesLength, act, resolvedType, dataString, flags));
}
System.out.println("Exited");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment