Skip to content
This repository has been archived by the owner on Sep 2, 2021. It is now read-only.

Commit

Permalink
New configuration implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
RikkaW committed Nov 15, 2020
1 parent 90e6eb3 commit a3c8e4e
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 76 deletions.
18 changes: 5 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<package name>
rm /data/misc/riru/modules/location_report_enabler/packages/<package name>
```
`/data/adb/riru/modules/location_report_enabler/packages/<package name>`

* 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**
`/data/misc/riru/modules/location_report_enabler/properties/<key>` (file content is value)
1 change: 1 addition & 0 deletions module/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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\""
Expand Down
3 changes: 2 additions & 1 deletion module/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -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)
71 changes: 71 additions & 0 deletions module/src/main/cpp/config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <map>
#include <vector>
#include <string>
#include <fcntl.h>
#include <unistd.h>
#include <sys/system_properties.h>
#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<std::string, Property *> props;
static std::vector<std::string> 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);
});
}
30 changes: 30 additions & 0 deletions module/src/main/cpp/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include <string>

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);
}
}
63 changes: 17 additions & 46 deletions module/src/main/cpp/hook.cpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,13 @@
#include <cstdio>
#include <cstring>
#include <chrono>
#include <fcntl.h>
#include <unistd.h>
#include <sys/vfs.h>
#include <sys/stat.h>
#include <dirent.h>
#include <dlfcn.h>
#include <cstdlib>
#include <string>
#include <sys/system_properties.h>
#include <xhook/xhook.h>

#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) { \
Expand All @@ -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)
Expand Down
11 changes: 3 additions & 8 deletions module/src/main/cpp/hook.h
Original file line number Diff line number Diff line change
@@ -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
namespace Hook {
void install();
}
13 changes: 6 additions & 7 deletions module/src/main/cpp/main.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#include <cstdio>
#include <cstdlib>
#include <jni.h>
#include <sys/types.h>
#include <riru.h>
#include <malloc.h>
#include <cstring>
#include <nativehelper/scoped_utf_chars.h>
#include <sys/system_properties.h>

#include "logging.h"
#include "hook.h"
#include "android.h"
#include "config.h"

static char saved_package_name[256] = {0};
static int saved_uid;
Expand Down Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -99,6 +97,7 @@ static void specializeAppProcessPost(
}

static void onModuleLoaded() {
Config::Load();
}

extern "C" {
Expand Down
21 changes: 21 additions & 0 deletions module/src/main/cpp/misc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <dirent.h>

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;
}
5 changes: 5 additions & 0 deletions module/src/main/cpp/misc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include <dirent.h>

int foreach_dir(const char *path, void(*callback)(int dirfd, struct dirent * entry));
22 changes: 21 additions & 1 deletion template/magisk_module/customize.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
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

0 comments on commit a3c8e4e

Please sign in to comment.