mirror of
https://github.com/rhunk/SnapEnhance.git
synced 2025-06-13 13:47:47 +02:00
feat: experimental native hooks
This commit is contained in:
1
native/.gitignore
vendored
Normal file
1
native/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/build
|
28
native/build.gradle.kts
Normal file
28
native/build.gradle.kts
Normal file
@ -0,0 +1,28 @@
|
||||
@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
|
||||
plugins {
|
||||
alias(libs.plugins.androidLibrary)
|
||||
alias(libs.plugins.kotlinAndroid)
|
||||
}
|
||||
|
||||
android {
|
||||
namespace = "me.rhunk.snapenhance.nativelib"
|
||||
compileSdk = 34
|
||||
|
||||
defaultConfig {
|
||||
externalNativeBuild {
|
||||
cmake {
|
||||
cppFlags("")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
externalNativeBuild {
|
||||
cmake {
|
||||
path("jni/CMakeLists.txt")
|
||||
version = "3.22.1"
|
||||
}
|
||||
}
|
||||
kotlinOptions {
|
||||
jvmTarget = "1.8"
|
||||
}
|
||||
}
|
16
native/jni/CMakeLists.txt
Normal file
16
native/jni/CMakeLists.txt
Normal file
@ -0,0 +1,16 @@
|
||||
cmake_minimum_required(VERSION 3.22.1)
|
||||
|
||||
project("nativelib")
|
||||
|
||||
set(DOBBY_GENERATE_SHARED OFF)
|
||||
add_subdirectory(external/dobby)
|
||||
|
||||
add_library(${CMAKE_PROJECT_NAME} SHARED
|
||||
src/library.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(${CMAKE_PROJECT_NAME}
|
||||
android
|
||||
log
|
||||
dobby_static
|
||||
)
|
1
native/jni/external/dobby
vendored
Submodule
1
native/jni/external/dobby
vendored
Submodule
Submodule native/jni/external/dobby added at b0176de574
6
native/jni/src/config.h
Normal file
6
native/jni/src/config.h
Normal file
@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
typedef struct {
|
||||
bool disable_bitmoji;
|
||||
bool disable_metrics;
|
||||
} native_config_t;
|
76
native/jni/src/library.cpp
Normal file
76
native/jni/src/library.cpp
Normal file
@ -0,0 +1,76 @@
|
||||
#include <jni.h>
|
||||
#include <string>
|
||||
#include <dobby.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
|
||||
#include "logger.h"
|
||||
#include "config.h"
|
||||
|
||||
static native_config_t *native_config;
|
||||
|
||||
static auto fstat_original = fstat;
|
||||
static int fstat_hook(int fd, struct stat *buf) {
|
||||
char name[256];
|
||||
memset(name, 0, 256);
|
||||
snprintf(name, sizeof(name), "/proc/self/fd/%d", fd);
|
||||
readlink(name, name, sizeof(name));
|
||||
|
||||
auto fileName = std::string(name);
|
||||
|
||||
//prevent blizzardv2 metrics
|
||||
if (native_config->disable_metrics && fileName.find("files/blizzardv2/queues") != std::string::npos) {
|
||||
unlink(name);
|
||||
return -1;
|
||||
}
|
||||
|
||||
//prevent bitmoji to load
|
||||
if (native_config->disable_bitmoji && fileName.find("com.snap.file_manager_4_SCContent") != std::string::npos) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return fstat_original(fd, buf);
|
||||
}
|
||||
|
||||
|
||||
#define GET_BOOL_FIELD(env, clazz, field) env->GetBooleanField(clazz, env->GetFieldID(clazz, field, "Z"))
|
||||
|
||||
extern "C" JNIEXPORT void JNICALL
|
||||
loadConfig(JNIEnv *env, jobject clazz, jobject config_object) {
|
||||
auto native_config_class = env->GetObjectClass(config_object);
|
||||
|
||||
native_config->disable_bitmoji = GET_BOOL_FIELD(env, native_config_class, "disableBitmoji");
|
||||
native_config->disable_metrics = GET_BOOL_FIELD(env, native_config_class, "disableMetrics");
|
||||
|
||||
LOGD("config loaded");
|
||||
}
|
||||
|
||||
//jni onload
|
||||
extern "C" JNIEXPORT jint JNICALL
|
||||
JNI_OnLoad(JavaVM *vm, void *reserved) {
|
||||
LOGD("initializing native");
|
||||
// config
|
||||
native_config = new native_config_t;
|
||||
|
||||
// hooks
|
||||
DobbyHook((void *) fstat_original,(void *) fstat_hook,(void **) &fstat_original);
|
||||
|
||||
// register native methods
|
||||
JNIEnv *env = nullptr;
|
||||
vm->GetEnv((void **) &env, JNI_VERSION_1_6);
|
||||
|
||||
auto methods = std::vector<JNINativeMethod>();
|
||||
methods.push_back({"loadConfig", "(Lme/rhunk/snapenhance/nativelib/NativeConfig;)V", (void *) loadConfig});
|
||||
|
||||
env->RegisterNatives(
|
||||
env->FindClass("me/rhunk/snapenhance/nativelib/NativeLib"),
|
||||
methods.data(),
|
||||
methods.size()
|
||||
);
|
||||
|
||||
LOGD("native initialized");
|
||||
|
||||
return JNI_VERSION_1_6;
|
||||
}
|
10
native/jni/src/logger.h
Normal file
10
native/jni/src/logger.h
Normal file
@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <android/log.h>
|
||||
|
||||
#define LOG_TAG "SnapEnhanceNative"
|
||||
|
||||
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
|
||||
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
|
||||
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)
|
||||
|
@ -0,0 +1,6 @@
|
||||
package me.rhunk.snapenhance.nativelib
|
||||
|
||||
data class NativeConfig(
|
||||
val disableBitmoji: Boolean = false,
|
||||
val disableMetrics: Boolean = false
|
||||
)
|
@ -0,0 +1,10 @@
|
||||
package me.rhunk.snapenhance.nativelib
|
||||
|
||||
class NativeLib {
|
||||
fun init() {
|
||||
System.loadLibrary("nativelib")
|
||||
}
|
||||
|
||||
|
||||
external fun loadConfig(config: NativeConfig)
|
||||
}
|
Reference in New Issue
Block a user