From 119802f691216d3525369fc1dac402083343835e Mon Sep 17 00:00:00 2001 From: KAZI MMT <82371061+kazimmt@users.noreply.github.com> Date: Fri, 16 Feb 2024 21:12:54 +0600 Subject: [PATCH] Squashed commit of the following: commit 4b0bfe6b42b9fc5eb739653392e43e1e93999da6 Author: j-hc Date: Wed Jan 24 20:13:53 2024 +0300 v1.11.0 commit c22fdf7e2caf24b563b6ceacddc3201525440fbe Author: j-hc Date: Wed Jan 24 20:13:09 2024 +0300 remove detach limit commit 1a79ead5fcc180049abed7488334cd400b779262 Author: j-hc Date: Wed Jan 24 00:36:11 2024 +0300 update readme commit 2a656074849048e1ce8bdaabeede0b2cffba092d Author: j-hc Date: Sun Jan 21 18:23:46 2024 +0300 v1.10.1 commit 651a3c3d8ffc732f08bec1deca0859f612539808 Author: j-hc Date: Sun Jan 21 18:16:42 2024 +0300 Revert "cli: fix some formatting bugs" This reverts commit bc7fe3749d21bcc2fad4d5084af17364d7123539. commit 30e7dcefb8604d1e9b4deeacf660588da6ea2500 Author: j-hc Date: Tue Jan 16 12:44:33 2024 +0300 v1.10.0 commit 52325540fe118b1613059621784a2e0a57a7497b Author: j-hc Date: Tue Jan 16 12:43:53 2024 +0300 hook less commit 563a7b03a7330311b028123bb85c99dbb78503d6 Author: j-hc Date: Fri Dec 29 22:16:22 2023 +0300 loaded log commit e4e28bd83262bf7da9964818ad73a82c3cc6aa2d Author: j-hc Date: Thu Dec 28 19:23:21 2023 +0300 few checks because some cant read commit 95daee508543180ac77e1eddae95384a4cd3d5f5 Author: j-hc Date: Sat Dec 23 12:45:51 2023 +0300 v1.9.1 commit 04287dd867a71fcc56d62e4cc0ef7bddbeb53199 Author: j-hc Date: Sat Dec 23 12:44:13 2023 +0300 whatever commit e80c9dec6e699ceca85e2a7957445f22a28c4520 Author: j-hc Date: Fri Dec 22 14:39:16 2023 +0300 v1.9 commit 283590c6040214768f15bb26061fdf743ee499de Author: j-hc Date: Fri Dec 22 14:09:02 2023 +0300 increase detach limit commit 7e69ad72962345676c2257a73e7a1f0e1ff9ed51 Author: j-hc Date: Fri Dec 22 14:06:39 2023 +0300 update cli and clarify readme commit bc7fe3749d21bcc2fad4d5084af17364d7123539 Author: j-hc Date: Wed Dec 13 20:06:00 2023 +0300 cli: fix some formatting bugs --- .github/ISSUE_TEMPLATE/bug_report.md | 6 ++- README.md | 13 ++++--- cli/src/main.rs | 55 +++++++++++++--------------- magisk/customize.sh | 7 +++- magisk/module.prop | 4 +- update.json | 8 ++-- zygisk/jni/module.cpp | 28 +++++++------- 7 files changed, 63 insertions(+), 58 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index 35b6cd6..ff3a2f8 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -7,10 +7,14 @@ assignees: '' --- +**Are you sure you read the [readme](https://github.com/j-hc/zygisk-detach#usage)?** + +**Android version and skin (MIUI etc... is MIUI still a thing? idk):** + **Screenshot of Play Store page of the app you are trying to detach:** **Screenshot of the helper CLI showing detached apps (from Option 2):** -**Logs taken with `su -c logcat -s "zygisk-detach" > /sdcard/log.txt`:** +**Logs taken with `su -c logcat -d -s "zygisk-detach" > /sdcard/log.txt`:** Not providing this information will get your issue closed diff --git a/README.md b/README.md index d83c50a..5965ba8 100644 --- a/README.md +++ b/README.md @@ -3,16 +3,17 @@ Detaches installed apps from Play Store against its aggressive updating policy which ignores the auto-update option user sets. ### How -Hooks libbinder with zygisk instead of applying SQL queries to database files +Hooks libbinder with zygisk ### Usage -* Enable **zygisk** in magisk or [ZygiskOnKernelSU](https://github.com/Dr-TSNG/ZygiskOnKernelSU/releases/tag/v4-0.7.1) module if you are using **KernelSU** +* Enable zygisk in Magisk **or** [ZygiskNext](https://github.com/Dr-TSNG/ZygiskNext) module if you are using KernelSU * Flash zygisk-detach module * Reboot -* Run helper cli in termux: - `$ detach` or `$ su -c detach` +* Run the cli in termux: `su -c detach` + or use [zygisk-detach-app](https://github.com/j-hc/zygisk-detach-app) if you dont wanna use the terminal * Select apps you wish to detach. Changes are applied immediately, no need to reboot. ### Notes -To make the usage portable, zygisk-detach reads `detach.bin` in the magisk module folder (`/data/adb/modules/zygisk-detach/detach.bin`) which means by putting your cli generated `detach.bin` inside the module zip, you can flash and detach apps without needing to run the cli again. You can copy your generated `detach.bin` using the cli or from magisk module folder. -Or the same way you can put a `detach.txt` with the package names inside the module and it will be serialized into a `detach.bin`. +zygisk-detach reads the generated `detach.bin` in the magisk module folder (`/data/adb/modules/zygisk-detach/detach.bin`) which means to make the usage portable, by putting your cli generated `detach.bin` inside the module zip, you can flash and detach apps without needing to run the cli again (for example between switching roms). You can get your generated `detach.bin` using the cli or from magisk module folder. + +Or the same way you can put a `detach.txt` with the package names inside the module zip and it will be serialized into a `detach.bin` when flashing \ No newline at end of file diff --git a/cli/src/main.rs b/cli/src/main.rs index 6be370b..a39fba7 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -1,8 +1,6 @@ -#![feature(iter_intersperse, print_internals)] - use std::error::Error; use std::fmt::{Debug, Display}; -use std::fs::{self, OpenOptions}; +use std::fs::{self, File, OpenOptions}; use std::io::{self, Seek}; use std::io::{BufWriter, Read, Write}; use std::mem::size_of; @@ -12,7 +10,7 @@ use std::process::{Command, ExitCode}; use termion::raw::IntoRawMode; use termion::event::Key; -use termion::{clear, cursor}; +use termion::{clear, cursor, terminal_size}; mod colorize; use colorize::ToColored; @@ -34,22 +32,22 @@ extern "C" { fn kill(pid: i32, sig: i32) -> i32; } -struct CLIErr { +struct LocErr { source: E, loc: &'static Location<'static>, } -impl Error for CLIErr {} -impl Debug for CLIErr { +impl Error for LocErr {} +impl Debug for LocErr { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}\r\nat {}", self.source, self.loc) } } -impl Display for CLIErr { +impl Display for LocErr { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { Debug::fmt(self, f) } } -impl From for CLIErr { +impl From for LocErr { #[track_caller] fn from(err: io::Error) -> Self { Self { @@ -58,8 +56,7 @@ impl From for CLIErr { } } } - -type IOResult = Result>; +type IOResult = Result>; fn main() -> ExitCode { std::panic::set_hook(Box::new(|panic| { @@ -133,19 +130,18 @@ fn detach_bin_changed() { } fn serialize_txt(txt: &str, bin: &str) -> IOResult<()> { - let detach_bin = OpenOptions::new() + let mut detach_bin = OpenOptions::new() .create(true) .truncate(true) .write(true) .open(bin)?; - let mut sink = BufWriter::new(detach_bin); for app in std::fs::read_to_string(txt)? .lines() .map(|s| s.trim()) .filter(|l| !l.is_empty() && !l.starts_with('#')) { println!(" '{}'", app); - bin_serialize(app, &mut sink)?; + bin_serialize(app, &mut detach_bin)?; } Ok(()) } @@ -246,7 +242,7 @@ fn get_detached_apps(menus: &mut Menus, detach_txt: &[u8]) -> Vec<(String, Range #[cfg(target_os = "linux")] fn get_installed_apps() -> IOResult> { - Ok("package:com.app1\npackage:org.xxx2".as_bytes().to_vec()) + Ok("package:com.app1\npackage:org.xxx2\ncom.apppppppp.tooolonnggggtooolonnggggtooolonnggggtooolonngggg".as_bytes().to_vec()) } #[cfg(target_os = "android")] @@ -306,19 +302,18 @@ fn main_menu(menus: &mut Menus) -> IOResult { } } -fn bin_serialize(app: &str, sink: impl Write) -> IOResult<()> { - let w = app - .as_bytes() - .iter() - .intersperse(&0) - .cloned() - .collect::>(); - let mut f = BufWriter::new(sink); - f.write_all(std::slice::from_ref( - &w.len() - .try_into() - .expect("app name cannot be longer than 255"), - ))?; +fn bin_serialize(app: &str, f: &mut File) -> IOResult<()> { + let mut w = Vec::with_capacity(2 * app.as_bytes().len() - 1); + for b in app.as_bytes()[..app.len() - 1].iter().cloned() { + w.push(b); + w.push(0); + } + w.push(app.as_bytes()[app.len() - 1]); + let mut f = BufWriter::new(f); + f.write(&[w + .len() + .try_into() + .expect("app name cannot be longer than 255")])?; f.write_all(&w)?; f.flush()?; Ok(()) @@ -335,6 +330,7 @@ fn detach_menu(menus: &mut Menus) -> IOResult<()> { .map(|e| std::str::from_utf8(e).expect("non utf-8 package names?")) .collect(); menus.cursor_show()?; + let col = terminal_size().expect("could not get terminal size").0 as usize - 2; let selected = menus.select_menu_with_input( |input| { let input = input.trim(); @@ -345,6 +341,7 @@ fn detach_menu(menus: &mut Menus) -> IOResult<()> { .contains(&input.to_ascii_lowercase()) }) .take(5) + .map(|s| &s[..col.min(s.bytes().len())]) .collect() } else { Vec::new() @@ -367,7 +364,7 @@ fn detach_menu(menus: &mut Menus) -> IOResult<()> { .iter() .any(|(s, _)| s == detach_app) { - bin_serialize(detach_app, f)?; + bin_serialize(detach_app, &mut f)?; textln!(menus, "{} {}", "detach:".green(), detach_app); textln!(menus, "Changes are applied. No need for a reboot!"); detach_bin_changed(); diff --git a/magisk/customize.sh b/magisk/customize.sh index 1268f04..81b9ff2 100644 --- a/magisk/customize.sh +++ b/magisk/customize.sh @@ -1,5 +1,9 @@ #!/system/bin/sh +if [ -n "$KSU_VER" ] && [ ! -d "$NVBASE/modules/zygisksu" ]; then + abort "You do not have ZygiskNext installed. Bye." +fi + mv -f "$MODPATH/system/bin/detach-${ARCH}" "$MODPATH/system/bin/detach" rm "$MODPATH"/system/bin/detach-* @@ -17,7 +21,8 @@ fi ALIAS="alias detach='su -c detach'" BASHRC="/data/data/com.termux/files/home/.bashrc" -if grep -qxF "$ALIAS" "$BASHRC" || echo "$ALIAS" >>"$BASHRC"; then +if [ -d "/data/data/com.termux/files/home/" ]; then + grep -qxF "$ALIAS" "$BASHRC" || echo "$ALIAS" >>"$BASHRC" ui_print "- Run 'detach' in termux after the reboot" else ui_print "- Run 'su -c detach' in terminal after the reboot" diff --git a/magisk/module.prop b/magisk/module.prop index 8ec848c..7ee3799 100644 --- a/magisk/module.prop +++ b/magisk/module.prop @@ -1,7 +1,7 @@ id=zygisk-detach name=zygisk-detach -version=v1.9 -versionCode=10 +version=v1.11.0 +versionCode=14 author=j-hc description=Detaches installed apps from Play Store updateJson=https://raw.githubusercontent.com/kazimmt/zygisk-detach/master/update.json \ No newline at end of file diff --git a/update.json b/update.json index 6b2db69..ac55ed3 100644 --- a/update.json +++ b/update.json @@ -1,6 +1,6 @@ { - "version": "v1.9", - "versionCode": 10, - "zipUrl": "https://github.com/kazimmt/zygisk-detach/releases/latest/download/zygisk-detach-v1.9.zip", - "changelog": "https://raw.githubusercontent.com/kazimmt/zygisk-detach/master/README.md" + "version": "v1.11.0", + "versionCode": 14, + "zipUrl": "https://github.com/j-hc/zygisk-detach/releases/latest/download/zygisk-detach-v1.11.0.zip", + "changelog": "https://raw.githubusercontent.com/j-hc/zygisk-detach/master/README.md" } \ No newline at end of file diff --git a/zygisk/jni/module.cpp b/zygisk/jni/module.cpp index b1a2ddc..e0f8098 100644 --- a/zygisk/jni/module.cpp +++ b/zygisk/jni/module.cpp @@ -17,11 +17,10 @@ using zygisk::ServerSpecializeArgs; #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, "zygisk-detach", __VA_ARGS__) -#define DETACH_CAP 512 -static unsigned char DETACH_TXT[DETACH_CAP] = {0}; +static unsigned char* DETACH_TXT; static uint8_t HEADERS_COUNT; -void handle_transact(uint8_t* data, size_t data_size) { +static inline void handle_transact(uint8_t* data, size_t data_size) { auto p = FakeParcel{data, 0}; if (!p.enforceInterface(data_size, HEADERS_COUNT)) return; uint32_t pkg_len = p.readInt32(); @@ -64,7 +63,7 @@ class Sigringe : public zygisk::ModuleBase { void preAppSpecialize(AppSpecializeArgs* args) override { const char* process = env->GetStringUTFChars(args->nice_name, nullptr); - if (memcmp(process, "com.android.vending", 19)) { + if (memcmp(process, "com.android.vending\0", 20)) { env->ReleaseStringUTFChars(args->nice_name, process); api->setOption(zygisk::Option::DLCLOSE_MODULE_LIBRARY); return; @@ -96,7 +95,7 @@ class Sigringe : public zygisk::ModuleBase { this->api->pltHookRegister(dev, inode, "_ZN7android14IPCThreadState8transactEijRKNS_6ParcelEPS1_j", (void**)&transact_hook, (void**)&transact_orig); if (this->api->pltHookCommit()) { - // LOGD("Loaded!"); + LOGD("Loaded!"); } else { LOGD("ERROR: pltHookCommit"); api->setOption(zygisk::Option::DLCLOSE_MODULE_LIBRARY); @@ -139,19 +138,18 @@ class Sigringe : public zygisk::ModuleBase { if (size <= 0) { LOGD("ERROR: detach.bin <= 0"); return 0; - } else if (size > DETACH_CAP - 1) { // -1 because of the null terminator - LOGD("ERROR: detach.bin > %d", DETACH_CAP - 1); + } + DETACH_TXT = (unsigned char*)malloc(size + 1); + auto r = read(fd, DETACH_TXT, size); + if (r < 0) { + LOGD("ERROR: read companion"); return 0; } - int received = 0; - while (received < size) { - auto red = read(fd, DETACH_TXT + received, size - received); - if (red < 0) { - LOGD("ERROR: read companion"); - return 0; - } - received += red; + if (r != size) { + LOGD("ERROR: read companion not whole"); + return 0; } + DETACH_TXT[size] = 0; return (size_t)size; } };