mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 21:15:56 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0169e5e133 | ||
|
|
fc5d23109d | ||
|
|
2493a9243f | ||
|
|
28804c0af3 | ||
|
|
9d0fdecf92 | ||
|
|
dfc5886812 |
No files matched your search
+1
-1
@@ -1 +1 @@
|
|||||||
VERSION = Version 3.3.1 Build 1
|
VERSION = Version 3.3.2 Build 1
|
||||||
@@ -13,6 +13,6 @@ JarPath = ""
|
|||||||
EntryClass = ""
|
EntryClass = ""
|
||||||
EntryMethod = ""
|
EntryMethod = ""
|
||||||
|
|
||||||
[JuiceAgent.Modules.Remapper]
|
[JuiceAgent.Modules.Dumper]
|
||||||
Enabled = false
|
Enabled = false
|
||||||
JuiceRemapperJarPath = ""
|
DumpDir = ""
|
||||||
@@ -0,0 +1,119 @@
|
|||||||
|
#include <jni.h>
|
||||||
|
#include <jvmti.h>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <string>
|
||||||
|
#include <jni_impl.hpp>
|
||||||
|
#include <JuiceAgent/JuiceAgent-API-1.4.1+build.1.hpp>
|
||||||
|
|
||||||
|
// Global state
|
||||||
|
static JavaVM* g_vm = nullptr;
|
||||||
|
static jvmtiEnv* g_jvmti = nullptr;
|
||||||
|
|
||||||
|
// Forward declaration
|
||||||
|
void JNICALL OnVMInit(jvmtiEnv* jvmti, JNIEnv* env, jthread thread);
|
||||||
|
|
||||||
|
// Utility: check JVMTI error
|
||||||
|
static bool CheckJvmti(jvmtiError err, const char* action) {
|
||||||
|
if (err != JVMTI_ERROR_NONE) {
|
||||||
|
PLOGE << action << " failed, code=" << err;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// VMInit callback
|
||||||
|
void JNICALL OnVMInit(jvmtiEnv* jvmti, JNIEnv* env, jthread thread) {
|
||||||
|
PLOGI << "JVMTI VMInit triggered";
|
||||||
|
|
||||||
|
std::string runtime_dir = std::filesystem::current_path().string();
|
||||||
|
|
||||||
|
if (runtime_dir.empty()) {
|
||||||
|
PLOGE << "runtime_dir is empty";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
PLOGI << "Runtime directory: " << runtime_dir;
|
||||||
|
|
||||||
|
// Inject JuiceAgent-API
|
||||||
|
// Save bytes to file
|
||||||
|
std::string hash =
|
||||||
|
JuiceAgent::Resource::juiceagent_api_bytes_sha256;
|
||||||
|
|
||||||
|
std::string file_name =
|
||||||
|
hash.substr(0, 8) + "_" +
|
||||||
|
JuiceAgent::Resource::juiceagent_api_bytes_name;
|
||||||
|
|
||||||
|
std::string juiceagent_api_path =
|
||||||
|
JuiceAgent::Utils::File::write_to_tempfile(
|
||||||
|
JuiceAgent::Resource::juiceagent_api_bytes,
|
||||||
|
JuiceAgent::Resource::juiceagent_api_bytes_len,
|
||||||
|
file_name
|
||||||
|
);
|
||||||
|
|
||||||
|
PLOGD << "Saved JuiceAgent-API to: " << juiceagent_api_path;
|
||||||
|
PLOGD << "JuiceAgent-API size: " << JuiceAgent::Resource::juiceagent_api_bytes_len;
|
||||||
|
PLOGD << "JuiceAgent-API SHA256: " << JuiceAgent::Resource::juiceagent_api_bytes_sha256;
|
||||||
|
|
||||||
|
// Inject JuiceAgent jar into system class loader
|
||||||
|
jint status = jvmti->AddToSystemClassLoaderSearch(juiceagent_api_path.c_str());
|
||||||
|
if (status != JNI_OK) {
|
||||||
|
PLOGE << "AddToSystemClassLoaderSearch failed: " << status;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
PLOGI << "Jar injected successfully: " << juiceagent_api_path;
|
||||||
|
|
||||||
|
JuiceAgent::Agent& agent = JuiceAgent::Agent::instance();
|
||||||
|
agent.init(g_vm, env, g_jvmti, runtime_dir);
|
||||||
|
|
||||||
|
PLOGI << "JuiceAgent initialized successfully";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Agent entry
|
||||||
|
JNIEXPORT jint JNICALL
|
||||||
|
Agent_OnLoad(JavaVM* vm, char* options, void* reserved) {
|
||||||
|
InitLogger();
|
||||||
|
|
||||||
|
PLOGI << "Agent_OnLoad";
|
||||||
|
|
||||||
|
g_vm = vm;
|
||||||
|
|
||||||
|
// Get JVMTI first
|
||||||
|
jint result = vm->GetEnv(reinterpret_cast<void**>(&g_jvmti), JVMTI_VERSION_1_2);
|
||||||
|
if (result != JNI_OK || g_jvmti == nullptr) {
|
||||||
|
PLOGE << "Failed to get JVMTI, code=" << result;
|
||||||
|
return JNI_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Optional capabilities
|
||||||
|
jvmtiCapabilities caps{};
|
||||||
|
CheckJvmti(g_jvmti->AddCapabilities(&caps), "AddCapabilities");
|
||||||
|
|
||||||
|
// Register callbacks
|
||||||
|
jvmtiEventCallbacks callbacks{};
|
||||||
|
callbacks.VMInit = &OnVMInit;
|
||||||
|
|
||||||
|
if (!CheckJvmti(
|
||||||
|
g_jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks)),
|
||||||
|
"SetEventCallbacks")) {
|
||||||
|
return JNI_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!CheckJvmti(
|
||||||
|
g_jvmti->SetEventNotificationMode(
|
||||||
|
JVMTI_ENABLE,
|
||||||
|
JVMTI_EVENT_VM_INIT,
|
||||||
|
nullptr),
|
||||||
|
"Enable VM_INIT")) {
|
||||||
|
return JNI_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
PLOGI << "Agent loaded, waiting for VMInit...";
|
||||||
|
return JNI_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Optional unload
|
||||||
|
JNIEXPORT void JNICALL
|
||||||
|
Agent_OnUnload(JavaVM* vm) {
|
||||||
|
PLOGI << "Agent_OnUnload";
|
||||||
|
}
|
||||||
@@ -12,6 +12,9 @@ public:
|
|||||||
virtual bool init() = 0;
|
virtual bool init() = 0;
|
||||||
virtual bool start() = 0;
|
virtual bool start() = 0;
|
||||||
virtual void stop() = 0;
|
virtual void stop() = 0;
|
||||||
|
|
||||||
|
virtual bool initialized() const = 0;
|
||||||
|
virtual bool running() const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <modules/IModule.hpp>
|
||||||
|
|
||||||
|
namespace JuiceAgent::Core::Modules {
|
||||||
|
|
||||||
|
class ModuleBase : public IModule {
|
||||||
|
public:
|
||||||
|
virtual ~ModuleBase() = default;
|
||||||
|
|
||||||
|
bool init() override {
|
||||||
|
if (_initialized) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!on_init()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
_initialized = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool start() override {
|
||||||
|
if (!_initialized) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_running) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!on_start()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
_running = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void stop() override {
|
||||||
|
if (!_running) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
on_stop();
|
||||||
|
_running = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool initialized() const override {
|
||||||
|
return _initialized;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool running() const override {
|
||||||
|
return _running;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual bool on_init() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual bool on_start() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void on_stop() {
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool _initialized = false;
|
||||||
|
bool _running = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
#include <jni_impl.hpp>
|
|
||||||
#include <global.hpp>
|
|
||||||
|
|
||||||
JNIEXPORT void JNICALL Java_cn_xiaozhou233_juiceagent_api_JuiceAgent_addRemapperWhiteList
|
|
||||||
(JNIEnv *env, jclass, jstring package_name) {
|
|
||||||
std::string pkg_name = env->GetStringUTFChars(package_name, nullptr);
|
|
||||||
if (pkg_name.empty()) {
|
|
||||||
PLOGW << "package name is empty";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
remapperWhiteList.insert(pkg_name);
|
|
||||||
|
|
||||||
PLOGI << "add remapper white list: " << pkg_name;
|
|
||||||
}
|
|
||||||
|
|
||||||
JNIEXPORT void JNICALL Java_cn_xiaozhou233_juiceagent_api_JuiceAgent_removeRemapperWhiteList
|
|
||||||
(JNIEnv *env, jclass, jstring package_name) {
|
|
||||||
std::string pkg_name = env->GetStringUTFChars(package_name, nullptr);
|
|
||||||
if (pkg_name.empty()) {
|
|
||||||
PLOGW << "package name is empty";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
remapperWhiteList.erase(pkg_name);
|
|
||||||
|
|
||||||
PLOGI << "remove remapper white list: " << pkg_name;
|
|
||||||
}
|
|
||||||
|
|
||||||
JNIEXPORT void JNICALL Java_cn_xiaozhou233_juiceagent_api_JuiceAgent_clearRemapperWhiteList
|
|
||||||
(JNIEnv *env, jclass) {
|
|
||||||
remapperWhiteList.clear();
|
|
||||||
|
|
||||||
PLOGI << "clear remapper white list";
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,133 @@
|
|||||||
|
#include <modules/ModuleRegistry.hpp>
|
||||||
|
#include <modules/ModuleBase.hpp>
|
||||||
|
#include <JuiceAgent/Logger.hpp>
|
||||||
|
#include <libagent.hpp>
|
||||||
|
#include <event/eventbus.hpp>
|
||||||
|
#include <event/event_type.hpp>
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
#include <fstream>
|
||||||
|
#include <mutex>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
namespace JuiceAgent::Core::Modules {
|
||||||
|
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
|
struct DumperConfig {
|
||||||
|
bool enabled = false;
|
||||||
|
std::string DumpDir;
|
||||||
|
};
|
||||||
|
|
||||||
|
static DumperConfig config;
|
||||||
|
static JuiceAgent::Agent& agent = JuiceAgent::Agent::instance();
|
||||||
|
|
||||||
|
class Dumper : public ModuleBase {
|
||||||
|
using super = ModuleBase;
|
||||||
|
|
||||||
|
public:
|
||||||
|
std::string name() const override {
|
||||||
|
return "Dumper";
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
bool on_init() override {
|
||||||
|
auto& cfg = agent.get_config();
|
||||||
|
|
||||||
|
config.enabled = cfg.get<bool>(
|
||||||
|
"JuiceAgent.Modules.Dumper.Enabled",
|
||||||
|
false
|
||||||
|
);
|
||||||
|
|
||||||
|
config.DumpDir = cfg.get<std::string>(
|
||||||
|
"JuiceAgent.Modules.Dumper.DumpDir",
|
||||||
|
"./dump",
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool on_start() override {
|
||||||
|
if (!config.enabled) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto& bus = agent.get_eventbus();
|
||||||
|
|
||||||
|
_classFileToken = bus.subscribe<EventClassFileLoadHook>(
|
||||||
|
[this](const auto& e) {
|
||||||
|
on_class_file_load_hook(e);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void on_stop() override {
|
||||||
|
if (!config.enabled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto& bus = agent.get_eventbus();
|
||||||
|
|
||||||
|
if (_classFileToken != 0) {
|
||||||
|
bus.unsubscribe<EventClassFileLoadHook>(_classFileToken);
|
||||||
|
_classFileToken = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
void on_class_file_load_hook(const EventClassFileLoadHook& event) {
|
||||||
|
if (!event.classbytes || event.class_data_len <= 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string_view name = event.name ? event.name : "";
|
||||||
|
|
||||||
|
if (name.empty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
fs::path output = build_class_path(name);
|
||||||
|
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(_mutex);
|
||||||
|
fs::create_directories(output.parent_path());
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ofstream file(output, std::ios::binary);
|
||||||
|
|
||||||
|
if (!file.is_open()) {
|
||||||
|
PLOGW << "Dumper: Failed to open file: " << output.string();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
file.write(
|
||||||
|
reinterpret_cast<const char*>(event.classbytes),
|
||||||
|
static_cast<std::streamsize>(event.class_data_len)
|
||||||
|
);
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
PLOGE << "Dumper: " << e.what();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fs::path build_class_path(std::string_view class_name) {
|
||||||
|
fs::path path(config.DumpDir);
|
||||||
|
path /= fs::path(class_name);
|
||||||
|
path += ".class";
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
EventBus::Token _classFileToken = 0;
|
||||||
|
std::mutex _mutex;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
JUICEAGENT_REGISTER_MODULE(
|
||||||
|
JuiceAgent::Core::Modules::Dumper,
|
||||||
|
Dumper
|
||||||
|
)
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
#include <modules/ModuleRegistry.hpp>
|
#include <modules/ModuleRegistry.hpp>
|
||||||
|
#include <modules/ModuleBase.hpp>
|
||||||
#include <JuiceAgent/Logger.hpp>
|
#include <JuiceAgent/Logger.hpp>
|
||||||
#include <libagent.hpp>
|
#include <libagent.hpp>
|
||||||
#include <JuiceAgent/Utils.hpp>
|
#include <JuiceAgent/Utils.hpp>
|
||||||
@@ -16,20 +17,21 @@ struct JarLoaderConfig {
|
|||||||
static JarLoaderConfig config;
|
static JarLoaderConfig config;
|
||||||
static JuiceAgent::Agent& agent = JuiceAgent::Agent::instance();
|
static JuiceAgent::Agent& agent = JuiceAgent::Agent::instance();
|
||||||
|
|
||||||
static const char* MODULE_CLASS = "cn/xiaozhou233/juiceagent/api/modules/JarLoader";
|
static const char* MODULE_CLASS =
|
||||||
|
"cn/xiaozhou233/juiceagent/api/modules/JarLoader";
|
||||||
|
|
||||||
static const char* MODULE_METHOD = "loadJar";
|
static const char* MODULE_METHOD = "loadJar";
|
||||||
|
|
||||||
class JarLoaderModule : public IModule {
|
class JarLoaderModule : public ModuleBase {
|
||||||
|
using super = ModuleBase;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
std::string name() const override {
|
std::string name() const override {
|
||||||
return "JarLoader";
|
return "JarLoader";
|
||||||
}
|
}
|
||||||
|
|
||||||
bool init() override {
|
protected:
|
||||||
if (_initialized) {
|
bool on_init() override {
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto& cfg = agent.get_config();
|
auto& cfg = agent.get_config();
|
||||||
|
|
||||||
config.enabled = cfg.get<bool>(
|
config.enabled = cfg.get<bool>(
|
||||||
@@ -62,20 +64,16 @@ public:
|
|||||||
);
|
);
|
||||||
|
|
||||||
PLOGI << "JarLoader init";
|
PLOGI << "JarLoader init";
|
||||||
_initialized = true;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool start() override {
|
bool on_start() override {
|
||||||
if (!_initialized) {
|
if (!config.enabled) {
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_running || !config.enabled) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
JuiceAgent::Utils::Serializer ser;
|
JuiceAgent::Utils::Serializer ser;
|
||||||
|
|
||||||
ser.add_kv("InjectionDir", config.injection_dir);
|
ser.add_kv("InjectionDir", config.injection_dir);
|
||||||
ser.add_kv("JarPath", config.jar_path);
|
ser.add_kv("JarPath", config.jar_path);
|
||||||
ser.add_kv("EntryClass", config.entry_class);
|
ser.add_kv("EntryClass", config.entry_class);
|
||||||
@@ -91,22 +89,12 @@ public:
|
|||||||
);
|
);
|
||||||
|
|
||||||
PLOGI << "JarLoader start";
|
PLOGI << "JarLoader start";
|
||||||
_running = true;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void stop() override {
|
void on_stop() override {
|
||||||
if (!_running) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
PLOGI << "JarLoader stop";
|
PLOGI << "JarLoader stop";
|
||||||
_running = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
|
||||||
bool _initialized = false;
|
|
||||||
bool _running = false;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,281 +0,0 @@
|
|||||||
#include <modules/ModuleRegistry.hpp>
|
|
||||||
#include <JuiceAgent/Logger.hpp>
|
|
||||||
#include <libagent.hpp>
|
|
||||||
#include <event/event_type.hpp>
|
|
||||||
#include <global.hpp>
|
|
||||||
#include <string_view>
|
|
||||||
#include <string>
|
|
||||||
#include <JuiceAgent/Utils.hpp>
|
|
||||||
|
|
||||||
namespace JuiceAgent::Core::Modules {
|
|
||||||
|
|
||||||
struct RemapperConfig {
|
|
||||||
bool enabled = false;
|
|
||||||
std::string JuiceRemapperJarPath;
|
|
||||||
};
|
|
||||||
|
|
||||||
static RemapperConfig config;
|
|
||||||
static JuiceAgent::Agent& agent = JuiceAgent::Agent::instance();
|
|
||||||
|
|
||||||
class RemapperModule : public IModule {
|
|
||||||
struct _Remapper {
|
|
||||||
jclass handler_class = nullptr;
|
|
||||||
jmethodID onClassFileLoadMethod = nullptr;
|
|
||||||
jmethodID onMethodEntryMethod = nullptr;
|
|
||||||
jmethodID onMethodExitMethod = nullptr;
|
|
||||||
} Remapper;
|
|
||||||
|
|
||||||
public:
|
|
||||||
std::string name() const override {
|
|
||||||
return "Remapper";
|
|
||||||
}
|
|
||||||
|
|
||||||
bool init() override {
|
|
||||||
if (_initialized) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto& cfg = agent.get_config();
|
|
||||||
|
|
||||||
config.enabled = cfg.get<bool>(
|
|
||||||
"JuiceAgent.Modules.Remapper.Enabled",
|
|
||||||
false
|
|
||||||
);
|
|
||||||
|
|
||||||
config.JuiceRemapperJarPath = cfg.get<std::string>(
|
|
||||||
"JuiceAgent.Modules.Remapper.JuiceRemapperJarPath",
|
|
||||||
"./JuiceRemapper.jar",
|
|
||||||
true
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!config.enabled) {
|
|
||||||
_initialized = true;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (agent.get_jvmti()->AddToSystemClassLoaderSearch(
|
|
||||||
config.JuiceRemapperJarPath.c_str()
|
|
||||||
) != JNI_OK) {
|
|
||||||
PLOGE << "Failed to load JuiceRemapper.jar";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
JNIEnv* env = agent.get_env();
|
|
||||||
|
|
||||||
jclass local = env->FindClass(
|
|
||||||
"cn/xiaozhou233/juiceremapper/api/Handler"
|
|
||||||
);
|
|
||||||
|
|
||||||
if (local == nullptr) {
|
|
||||||
check_and_clear_exception(env, "FindClass Handler");
|
|
||||||
PLOGE << "Failed to find Handler";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
Remapper.handler_class =
|
|
||||||
static_cast<jclass>(env->NewGlobalRef(local));
|
|
||||||
|
|
||||||
env->DeleteLocalRef(local);
|
|
||||||
|
|
||||||
if (Remapper.handler_class == nullptr) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
Remapper.onClassFileLoadMethod = env->GetStaticMethodID(
|
|
||||||
Remapper.handler_class,
|
|
||||||
"onClassFileLoadHook",
|
|
||||||
"(Ljava/lang/Class;Ljava/lang/ClassLoader;Ljava/lang/String;Ljava/lang/Object;I[B)[B"
|
|
||||||
);
|
|
||||||
|
|
||||||
if (Remapper.onClassFileLoadMethod == nullptr) {
|
|
||||||
check_and_clear_exception(env, "onClassFileLoadHook");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
Remapper.onMethodEntryMethod = env->GetStaticMethodID(
|
|
||||||
Remapper.handler_class,
|
|
||||||
"onMethodEntry",
|
|
||||||
"(Ljava/lang/Thread;Ljava/lang/reflect/Method;)V"
|
|
||||||
);
|
|
||||||
|
|
||||||
if (Remapper.onMethodEntryMethod == nullptr) {
|
|
||||||
check_and_clear_exception(env, "onMethodEntry");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
Remapper.onMethodExitMethod = env->GetStaticMethodID(
|
|
||||||
Remapper.handler_class,
|
|
||||||
"onMethodExit",
|
|
||||||
"(Ljava/lang/Thread;Ljava/lang/reflect/Method;ZLjava/lang/Object;)V"
|
|
||||||
);
|
|
||||||
|
|
||||||
if (Remapper.onMethodExitMethod == nullptr) {
|
|
||||||
check_and_clear_exception(env, "onMethodExit");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
_initialized = true;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool start() override {
|
|
||||||
if (!_initialized) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_running) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!config.enabled) {
|
|
||||||
_running = true;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto& bus = agent.get_eventbus();
|
|
||||||
|
|
||||||
_classFileToken = bus.subscribe<EventClassFileLoadHook>(
|
|
||||||
[this](const auto& e) {
|
|
||||||
on_class_file_load_hook(e);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
_methodEntryToken = bus.subscribe<EventMethodEntry>(
|
|
||||||
[this](const auto& e) {
|
|
||||||
on_method_entry_hook(e);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
_methodExitToken = bus.subscribe<EventMethodExit>(
|
|
||||||
[this](const auto& e) {
|
|
||||||
on_method_exit_hook(e);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
_running = true;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void stop() override {
|
|
||||||
if (!_running) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto& bus = agent.get_eventbus();
|
|
||||||
|
|
||||||
bus.unsubscribe<EventClassFileLoadHook>(_classFileToken);
|
|
||||||
bus.unsubscribe<EventMethodEntry>(_methodEntryToken);
|
|
||||||
bus.unsubscribe<EventMethodExit>(_methodExitToken);
|
|
||||||
|
|
||||||
_classFileToken = 0;
|
|
||||||
_methodEntryToken = 0;
|
|
||||||
_methodExitToken = 0;
|
|
||||||
|
|
||||||
_running = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
void on_class_file_load_hook(const EventClassFileLoadHook& event) {
|
|
||||||
std::string_view name = event.name ? event.name : "";
|
|
||||||
|
|
||||||
if (name.starts_with("cn/xiaozhou233/juiceremapper/")) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
JNIEnv* env = event.jni_env;
|
|
||||||
|
|
||||||
std::string className(name);
|
|
||||||
jstring jName = env->NewStringUTF(className.c_str());
|
|
||||||
if (jName == nullptr) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
jbyteArray input = env->NewByteArray(event.class_data_len);
|
|
||||||
if (input == nullptr) {
|
|
||||||
env->DeleteLocalRef(jName);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
env->SetByteArrayRegion(
|
|
||||||
input,
|
|
||||||
0,
|
|
||||||
event.class_data_len,
|
|
||||||
reinterpret_cast<const jbyte*>(event.classbytes)
|
|
||||||
);
|
|
||||||
|
|
||||||
jbyteArray output = static_cast<jbyteArray>(
|
|
||||||
env->CallStaticObjectMethod(
|
|
||||||
Remapper.handler_class,
|
|
||||||
Remapper.onClassFileLoadMethod,
|
|
||||||
event.class_being_redefined,
|
|
||||||
event.loader,
|
|
||||||
jName,
|
|
||||||
event.protection_domain,
|
|
||||||
event.class_data_len,
|
|
||||||
input
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
check_and_clear_exception(env, "on_class_file_load_hook");
|
|
||||||
|
|
||||||
if (output != nullptr) {
|
|
||||||
jsize len = env->GetArrayLength(output);
|
|
||||||
unsigned char* buffer = nullptr;
|
|
||||||
|
|
||||||
if (event.jvmti_env->Allocate(len, &buffer) == JVMTI_ERROR_NONE) {
|
|
||||||
env->GetByteArrayRegion(
|
|
||||||
output,
|
|
||||||
0,
|
|
||||||
len,
|
|
||||||
reinterpret_cast<jbyte*>(buffer)
|
|
||||||
);
|
|
||||||
|
|
||||||
*event.new_class_data_len = len;
|
|
||||||
*event.new_classbytes = buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
env->DeleteLocalRef(output);
|
|
||||||
}
|
|
||||||
|
|
||||||
env->DeleteLocalRef(input);
|
|
||||||
env->DeleteLocalRef(jName);
|
|
||||||
}
|
|
||||||
void on_method_entry_hook(const EventMethodEntry& event) {
|
|
||||||
// event.jni_env->CallStaticVoidMethod(
|
|
||||||
// Remapper.handler_class,
|
|
||||||
// Remapper.onMethodEntryMethod,
|
|
||||||
// event.thread,
|
|
||||||
// event.method
|
|
||||||
// );
|
|
||||||
|
|
||||||
check_and_clear_exception(event.jni_env, "on_method_entry_hook");
|
|
||||||
}
|
|
||||||
|
|
||||||
void on_method_exit_hook(const EventMethodExit& event) {
|
|
||||||
// event.jni_env->CallStaticVoidMethod(
|
|
||||||
// Remapper.handler_class,
|
|
||||||
// Remapper.onMethodExitMethod,
|
|
||||||
// event.thread,
|
|
||||||
// event.method,
|
|
||||||
// event.was_popped_by_exception,
|
|
||||||
// event.return_value.l
|
|
||||||
// );
|
|
||||||
|
|
||||||
check_and_clear_exception(event.jni_env, "on_method_exit_hook");
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
bool _initialized = false;
|
|
||||||
bool _running = false;
|
|
||||||
|
|
||||||
EventBus::Token _classFileToken = 0;
|
|
||||||
EventBus::Token _methodEntryToken = 0;
|
|
||||||
EventBus::Token _methodExitToken = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
JUICEAGENT_REGISTER_MODULE(
|
|
||||||
JuiceAgent::Core::Modules::RemapperModule,
|
|
||||||
Remapper
|
|
||||||
)
|
|
||||||
@@ -1,58 +1,28 @@
|
|||||||
#include <modules/ModuleRegistry.hpp>
|
#include <modules/ModuleBase.hpp>
|
||||||
#include <JuiceAgent/Logger.hpp>
|
#include <JuiceAgent/Logger.hpp>
|
||||||
|
|
||||||
namespace JuiceAgent::Core::Modules {
|
namespace JuiceAgent::Core::Modules {
|
||||||
|
|
||||||
class TemplateModule : public IModule {
|
class TemplateModule : public ModuleBase {
|
||||||
public:
|
public:
|
||||||
std::string name() const override {
|
std::string name() const override {
|
||||||
return "Template";
|
return "Template";
|
||||||
}
|
}
|
||||||
|
|
||||||
bool init() override {
|
protected:
|
||||||
if (_initialized) {
|
bool on_init() override {
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// load config / prepare resource
|
// load config / prepare resource
|
||||||
|
|
||||||
_initialized = true;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool start() override {
|
bool on_start() override {
|
||||||
if (!_initialized) {
|
// start module
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_running) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// start logic
|
|
||||||
|
|
||||||
_running = true;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void stop() override {
|
void on_stop() override {
|
||||||
if (!_running) {
|
// stop module
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// cleanup logic
|
|
||||||
|
|
||||||
_running = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
|
||||||
bool _initialized = false;
|
|
||||||
bool _running = false;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
JUICEAGENT_REGISTER_MODULE(
|
|
||||||
JuiceAgent::Core::Modules::TemplateModule,
|
|
||||||
Template
|
|
||||||
)
|
|
||||||
Reference in new issue
Block a user