From a3c8e4e5e46bc1392f1aca5641b069928d663a35 Mon Sep 17 00:00:00 2001 From: RikkaW Date: Sun, 15 Nov 2020 18:01:05 +0800 Subject: [PATCH] New configuration implementation --- README.md | 18 ++------ module/build.gradle | 1 + module/src/main/cpp/CMakeLists.txt | 3 +- module/src/main/cpp/config.cpp | 71 +++++++++++++++++++++++++++++ module/src/main/cpp/config.h | 30 ++++++++++++ module/src/main/cpp/hook.cpp | 63 +++++++------------------ module/src/main/cpp/hook.h | 11 ++--- module/src/main/cpp/main.cpp | 13 +++--- module/src/main/cpp/misc.cpp | 21 +++++++++ module/src/main/cpp/misc.h | 5 ++ template/magisk_module/customize.sh | 22 ++++++++- 11 files changed, 182 insertions(+), 76 deletions(-) create mode 100644 module/src/main/cpp/config.cpp create mode 100644 module/src/main/cpp/config.h create mode 100644 module/src/main/cpp/misc.cpp create mode 100644 module/src/main/cpp/misc.h diff --git a/README.md b/README.md index 38a861f..99502cd 100644 --- a/README.md +++ b/README.md @@ -15,20 +15,12 @@ and the return value will be changed * `gsm.sim.operator.numeric` -> `310030` * `gsm.sim.operator.iso-country` -> `us` -## Customize +## Configuration -* Add / remove enabled package +### Packages - ``` - touch /data/misc/riru/modules/location_report_enabler/packages/ - rm /data/misc/riru/modules/location_report_enabler/packages/ - ``` +`/data/adb/riru/modules/location_report_enabler/packages/` -* Return value +### Properties - ``` - echo -n 310030 > /data/misc/riru/modules/location_report_enabler/gsm.sim.operator.numeric - echo -n us > /data/misc/riru/modules/location_report_enabler/gsm.sim.operator.iso-country - ``` - - **Don't forget `-n`, or the return data will contain a newline** \ No newline at end of file +`/data/misc/riru/modules/location_report_enabler/properties/` (file content is value) \ No newline at end of file diff --git a/module/build.gradle b/module/build.gradle index ebb7612..fdc9200 100644 --- a/module/build.gradle +++ b/module/build.gradle @@ -10,6 +10,7 @@ android { externalNativeBuild { cmake { arguments "-DMODULE_NAME:STRING=riru_$moduleId", + "-DRIRU_MODULE_ID=$moduleId", "-DRIRU_MODULE_API_VERSION=$moduleMaxRiruApiVersion", "-DRIRU_MODULE_VERSION=$moduleVersionCode", "-DRIRU_MODULE_VERSION_NAME:STRING=\"$moduleVersion\"" diff --git a/module/src/main/cpp/CMakeLists.txt b/module/src/main/cpp/CMakeLists.txt index e10c670..cddc97b 100644 --- a/module/src/main/cpp/CMakeLists.txt +++ b/module/src/main/cpp/CMakeLists.txt @@ -5,6 +5,7 @@ if (NOT DEFINED MODULE_NAME) endif () add_definitions(-DRIRU_MODULE) +add_definitions(-DRIRU_MODULE_ID=\"${RIRU_MODULE_ID}\") add_definitions(-DRIRU_MODULE_API_VERSION=${RIRU_MODULE_API_VERSION}) add_definitions(-DRIRU_MODULE_VERSION=${RIRU_MODULE_VERSION}) add_definitions(-DRIRU_MODULE_VERSION_NAME=${RIRU_MODULE_VERSION_NAME}) @@ -34,6 +35,6 @@ find_package(riru REQUIRED CONFIG) find_package(nativehelper REQUIRED CONFIG) find_package(xhook REQUIRED CONFIG) -add_library(${MODULE_NAME} SHARED main.cpp hook.cpp android.cpp) +add_library(${MODULE_NAME} SHARED main.cpp hook.cpp android.cpp misc.cpp config.cpp) target_link_libraries(${MODULE_NAME} log riru::riru nativehelper::nativehelper_header_only xhook::xhook) set_target_properties(${MODULE_NAME} PROPERTIES LINK_FLAGS_RELEASE -s) diff --git a/module/src/main/cpp/config.cpp b/module/src/main/cpp/config.cpp new file mode 100644 index 0000000..476b1fc --- /dev/null +++ b/module/src/main/cpp/config.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include +#include +#include +#include "config.h" +#include "misc.h" +#include "logging.h" + +using namespace Config; + +#define CONFIG_PATH "/data/adb/riru/modules/" RIRU_MODULE_ID "/config" +#define PROPS_PATH CONFIG_PATH "/properties" +#define PACKAGES_PATH CONFIG_PATH "/packages" + +static std::map props; +static std::vector packages; + +Property *Properties::Find(const char *name) { + if (!name) return nullptr; + + auto it = props.find(name); + if (it != props.end()) { + return it->second; + } + return nullptr; +} + +void Properties::Put(const char *name, const char *value) { + if (!name) return; + + auto prop = Find(name); + delete prop; + + props[name] = new Property(name, value ? value : ""); + + LOGD("property: %s %s", name, value); +} + +bool Packages::Find(const char *name) { + if (!name) return false; + return std::find(packages.begin(), packages.end(), name) != packages.end(); +} + +void Packages::Add(const char *name) { + if (!name) return; + packages.emplace_back(name); + + LOGD("package: %s", name); +} + +void Config::Load() { + foreach_dir(PROPS_PATH, [](int dirfd, struct dirent *entry) { + auto name = entry->d_name; + int fd = openat(dirfd, name, O_RDONLY); + if (fd == -1) return; + + char buf[PROP_VALUE_MAX]{0}; + if (read(fd, buf, PROP_VALUE_MAX) >= 0) { + Properties::Put(name, buf); + } + + close(fd); + }); + + foreach_dir(PACKAGES_PATH, [](int, struct dirent *entry) { + auto name = entry->d_name; + Packages::Add(name); + }); +} \ No newline at end of file diff --git a/module/src/main/cpp/config.h b/module/src/main/cpp/config.h new file mode 100644 index 0000000..f695665 --- /dev/null +++ b/module/src/main/cpp/config.h @@ -0,0 +1,30 @@ +#pragma once + +#include + +namespace Config { + + struct Property { + + std::string name; + std::string value; + + Property(const char *name, const char *value) : name(name), value(value) {} + }; + + void Load(); + + namespace Properties { + + Property *Find(const char *name); + + void Put(const char *name, const char *value); + } + + namespace Packages { + + bool Find(const char *name); + + void Add(const char *name); + } +} diff --git a/module/src/main/cpp/hook.cpp b/module/src/main/cpp/hook.cpp index 988c231..ef9d2fc 100644 --- a/module/src/main/cpp/hook.cpp +++ b/module/src/main/cpp/hook.cpp @@ -1,31 +1,13 @@ -#include #include -#include -#include #include -#include -#include -#include -#include -#include -#include #include #include #include "riru.h" #include "logging.h" #include "android.h" - -static const char *sim_operator_numeric = "310030"; -static const char *sim_operator_country = "us"; - -void set_sim_operator_numeric(const char *string) { - sim_operator_numeric = strdup(string); -} - -void set_sim_operator_country(const char *string) { - sim_operator_country = strdup(string); -} +#include "hook.h" +#include "config.h" #define XHOOK_REGISTER(NAME) \ if (xhook_register(".*", #NAME, (void*) new_##NAME, (void **) &old_##NAME) != 0) { \ @@ -38,52 +20,41 @@ void set_sim_operator_country(const char *string) { NEW_FUNC_DEF(int, __system_property_get, const char *key, char *value) { int res = old___system_property_get(key, value); - LOGD("get: %s=%s", key, value); - if (key) { - if (strcmp("gsm.sim.operator.numeric", key) == 0) { - strcpy(value, sim_operator_numeric); - LOGI("system_property_get: %s -> %s", key, value); - } else if (strcmp("gsm.sim.operator.iso-country", key) == 0) { - strcpy(value, sim_operator_country); - LOGI("system_property_get: %s -> %s", key, value); - } + auto prop = Config::Properties::Find(key); + if (prop) { + LOGI("system_property_get: %s=%s -> %s", key, value, prop->value.c_str()); + strcpy(value, prop->value.c_str()); } return res; } using callback_func = void(void *cookie, const char *name, const char *value, uint32_t serial); -thread_local callback_func *last_callback = nullptr; +thread_local callback_func *saved_callback = nullptr; static void my_callback(void *cookie, const char *name, const char *value, uint32_t serial) { - if (!last_callback) return; - - if (name && strcmp("gsm.sim.operator.numeric", name) == 0) { - LOGI("system_property_read_callback: %s -> %s", name, sim_operator_numeric); - last_callback(cookie, name, sim_operator_numeric, serial); + if (!saved_callback) return; - } else if (name && strcmp("gsm.sim.operator.iso-country", name) == 0) { - LOGI("system_property_read_callback: %s -> %s", name, sim_operator_country); - last_callback(cookie, name, sim_operator_country, serial); - - } else { - LOGD("callback: %s=%s", name, value); - last_callback(cookie, name, value, serial); + auto prop = Config::Properties::Find(name); + if (!prop) { + saved_callback(cookie, name, value, serial); + return; } + + LOGI("system_property_read_callback: %s=%s -> %s", name, value, prop->value.c_str()); + saved_callback(cookie, name, prop->value.c_str(), serial); } NEW_FUNC_DEF(void, __system_property_read_callback, const prop_info *pi, callback_func *callback, void *cookie) { - last_callback = callback; + saved_callback = callback; old___system_property_read_callback(pi, my_callback, cookie); } -void install_hook() { +void Hook::install() { XHOOK_REGISTER(__system_property_get); - LOGD("__system_property_get"); if (android::GetApiLevel() >= 26) { XHOOK_REGISTER(__system_property_read_callback); - LOGD("__system_property_read_callback"); } if (xhook_refresh(0) == 0) diff --git a/module/src/main/cpp/hook.h b/module/src/main/cpp/hook.h index 9530de8..fbb757c 100644 --- a/module/src/main/cpp/hook.h +++ b/module/src/main/cpp/hook.h @@ -1,10 +1,5 @@ #pragma once -#ifndef HOOK_H -#define HOOK_H - -void install_hook(); -void set_sim_operator_numeric(const char *string); -void set_sim_operator_country(const char* string); - -#endif // HOOK_H \ No newline at end of file +namespace Hook { + void install(); +} \ No newline at end of file diff --git a/module/src/main/cpp/main.cpp b/module/src/main/cpp/main.cpp index 1e90e8f..840bc1c 100644 --- a/module/src/main/cpp/main.cpp +++ b/module/src/main/cpp/main.cpp @@ -1,14 +1,14 @@ +#include +#include #include -#include #include -#include -#include #include #include #include "logging.h" #include "hook.h" #include "android.h" +#include "config.h" static char saved_package_name[256] = {0}; static int saved_uid; @@ -59,11 +59,9 @@ static void appProcessPost( LOGD("%s: uid=%d, package=%s, process=%s", from, uid, package_name, saved_process_name); - if (strcmp("com.google.android.gsf", package_name) == 0 - || strcmp("com.google.android.gms", package_name) == 0 - || strcmp("com.google.android.apps.maps", package_name) == 0) { + if (Config::Packages::Find(package_name)) { LOGI("install hook for %d:%s", uid / 100000, package_name); - install_hook(); + Hook::install(); } } @@ -99,6 +97,7 @@ static void specializeAppProcessPost( } static void onModuleLoaded() { + Config::Load(); } extern "C" { diff --git a/module/src/main/cpp/misc.cpp b/module/src/main/cpp/misc.cpp new file mode 100644 index 0000000..70a47ce --- /dev/null +++ b/module/src/main/cpp/misc.cpp @@ -0,0 +1,21 @@ +#include + +int foreach_dir(const char *path, void(*callback)(int, struct dirent *)) { + DIR *dir; + struct dirent *entry; + int fd; + + if ((dir = opendir(path)) == nullptr) + return -1; + + fd = dirfd(dir); + + while ((entry = readdir(dir))) { + if (entry->d_name[0] == '.') continue; + callback(fd, entry); + } + + closedir(dir); + + return 0; +} \ No newline at end of file diff --git a/module/src/main/cpp/misc.h b/module/src/main/cpp/misc.h new file mode 100644 index 0000000..e99c3a9 --- /dev/null +++ b/module/src/main/cpp/misc.h @@ -0,0 +1,5 @@ +#pragma once + +#include + +int foreach_dir(const char *path, void(*callback)(int dirfd, struct dirent * entry)); \ No newline at end of file diff --git a/template/magisk_module/customize.sh b/template/magisk_module/customize.sh index fead88e..5975de1 100644 --- a/template/magisk_module/customize.sh +++ b/template/magisk_module/customize.sh @@ -54,4 +54,24 @@ ui_print "- Extracting extra files" rm -f "$RIRU_MODULE_PATH/module.prop.new" extract "$ZIPFILE" 'riru/module.prop.new' "$RIRU_MODULE_PATH" true -set_perm "$RIRU_MODULE_PATH/module.prop.new" 0 0 0600 $RIRU_SECONTEXT \ No newline at end of file +set_perm "$RIRU_MODULE_PATH/module.prop.new" 0 0 0600 $RIRU_SECONTEXT + +# Create default config if necessary +CONFIG_PATH="$RIRU_MODULE_PATH/config" + +if [ ! -d "$CONFIG_PATH/properties" ]; then + ui_print "- Creating default configuration (1)" + mkdir -p "$CONFIG_PATH/properties" + echo -n "310030" > "$CONFIG_PATH/properties/gsm.sim.operator.numeric" + echo -n "us" > "$CONFIG_PATH/properties/gsm.sim.operator.iso-country" +fi + +if [ ! -d "$CONFIG_PATH/packages" ]; then + ui_print "- Creating default configuration (2)" + mkdir -p "$CONFIG_PATH/packages" + touch "$CONFIG_PATH/packages/com.google.android.gsf" + touch "$CONFIG_PATH/packages/com.google.android.gms" + touch "$CONFIG_PATH/packages/com.google.android.apps.maps" +fi + +set_perm $CONFIG_PATH 0 0 0600 $RIRU_SECONTEXT \ No newline at end of file