Merge branch 'feature/add_inject_hide' of https://github.com/jiqiu2022/Zygisk-MyInjector into feature/add_inject_hide

This commit is contained in:
Yuuki
2025-10-25 21:10:58 +08:00
3 changed files with 64 additions and 22 deletions

View File

@@ -19,12 +19,13 @@ public class ConfigManager {
public static final String CONFIG_FILE = MODULE_PATH + "/config.json"; public static final String CONFIG_FILE = MODULE_PATH + "/config.json";
public static final String SO_STORAGE_DIR = MODULE_PATH + "/so_files"; public static final String SO_STORAGE_DIR = MODULE_PATH + "/so_files";
public static final String KPM_MODULE_PATH = MODULE_PATH + "/injectHide.kpm"; public static final String KPM_MODULE_PATH = MODULE_PATH + "/injectHide.kpm";
public static final String KPM_HIDE_CONFIG = MODULE_PATH + "/kpm_hide_config.txt"; public static final String KPM_HIDE_CONFIG = "/data/local/tmp/kpm_hide_config.txt";
private static final String KPM_MODULE_NAME = "hideInject"; private static final String KPM_MODULE_NAME = "hideInject";
private final Context context; private final Context context;
private final Gson gson; private final Gson gson;
private ModuleConfig config; private ModuleConfig config;
private final Object kpmLock = new Object(); // 用于同步 KPM 操作
static { static {
// Configure Shell to use root // Configure Shell to use root
@@ -725,24 +726,59 @@ public class ConfigManager {
* 重新加载 KPM 模块 * 重新加载 KPM 模块
*/ */
public boolean reloadKpmModule() { public boolean reloadKpmModule() {
Log.i(TAG, "Reloading KPM module..."); // 使用锁防止并发重载
synchronized (kpmLock) {
// Unload first Log.i(TAG, "Reloading KPM module...");
if (isKpmModuleLoaded()) {
if (!unloadKpmModule()) { // Unload first
Log.e(TAG, "Failed to unload module before reload"); if (isKpmModuleLoaded()) {
return false; if (!unloadKpmModule()) {
Log.e(TAG, "Failed to unload module before reload");
return false;
}
// Give kernel more time to cleanup (1 second for safety)
// 等待时间足够让内核完全清理资源
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Log.w(TAG, "Sleep interrupted during module reload", e);
Thread.currentThread().interrupt();
return false;
}
// Verify unload was successful
int retries = 5;
for (int i = 0; i < retries; i++) {
if (!isKpmModuleLoaded()) {
break;
}
Log.w(TAG, "Module still loaded, waiting... (" + (i+1) + "/" + retries + ")");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return false;
}
}
if (isKpmModuleLoaded()) {
Log.e(TAG, "Module still loaded after unload attempts, aborting reload");
return false;
}
} }
// Give kernel time to cleanup
try { // Load again
Thread.sleep(500); boolean success = loadKpmModule();
} catch (InterruptedException e) {
e.printStackTrace(); if (success) {
Log.i(TAG, "Module reloaded successfully");
} else {
Log.e(TAG, "Failed to load module after unload");
} }
}
return success;
// Load again } // synchronized
return loadKpmModule();
} }
/** /**
@@ -770,14 +806,17 @@ public class ConfigManager {
// Ensure module directory exists // Ensure module directory exists
Shell.cmd("mkdir -p \"" + MODULE_PATH + "\"").exec(); Shell.cmd("mkdir -p \"" + MODULE_PATH + "\"").exec();
// Copy to module directory with root // Ensure /data/local/tmp exists and is writable
Shell.cmd("mkdir -p /data/local/tmp && chmod 777 /data/local/tmp").exec();
// Copy to /data/local/tmp with root
Shell.Result copyResult = Shell.cmd("cp \"" + tempFile + "\" \"" + KPM_HIDE_CONFIG + "\"").exec(); Shell.Result copyResult = Shell.cmd("cp \"" + tempFile + "\" \"" + KPM_HIDE_CONFIG + "\"").exec();
if (!copyResult.isSuccess()) { if (!copyResult.isSuccess()) {
Log.e(TAG, "Failed to copy KPM config: " + String.join("\n", copyResult.getErr())); Log.e(TAG, "Failed to copy KPM config: " + String.join("\n", copyResult.getErr()));
return false; return false;
} }
Shell.cmd("chmod 644 \"" + KPM_HIDE_CONFIG + "\"").exec(); Shell.cmd("chmod 666 \"" + KPM_HIDE_CONFIG + "\"").exec();
// Clean up temp file // Clean up temp file
new File(tempFile).delete(); new File(tempFile).delete();

View File

@@ -106,7 +106,7 @@
android:id="@+id/tvConfigPath" android:id="@+id/tvConfigPath"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="配置文件: /data/adb/modules/zygisk-myinjector/kpm_hide_config.txt" android:text="配置文件: /data/local/tmp/kpm_hide_config.txt"
android:textSize="10sp" android:textSize="10sp"
android:textColor="@android:color/darker_gray" android:textColor="@android:color/darker_gray"
android:fontFamily="monospace" /> android:fontFamily="monospace" />

View File

@@ -98,7 +98,7 @@ log "ConfigApp 安装脚本执行完成"
# KPM 模块路径 # KPM 模块路径
KPM_MODULE="$MODDIR/injectHide.kpm" KPM_MODULE="$MODDIR/injectHide.kpm"
KPM_CONFIG="$MODDIR/kpm_hide_config.txt" KPM_CONFIG="/data/local/tmp/kpm_hide_config.txt"
log "开始加载 KPM 内核模块" log "开始加载 KPM 内核模块"
@@ -111,8 +111,11 @@ else
# 创建初始配置文件(如果不存在) # 创建初始配置文件(如果不存在)
if [ ! -f "$KPM_CONFIG" ]; then if [ ! -f "$KPM_CONFIG" ]; then
log "创建初始 KPM 配置文件" log "创建初始 KPM 配置文件"
# 确保 /data/local/tmp 目录存在且权限正确
mkdir -p /data/local/tmp
chmod 777 /data/local/tmp
echo "libmyinjector.so" > "$KPM_CONFIG" echo "libmyinjector.so" > "$KPM_CONFIG"
chmod 644 "$KPM_CONFIG" chmod 666 "$KPM_CONFIG"
fi fi
# 等待一段时间确保系统稳定 # 等待一段时间确保系统稳定