diff --git a/src/libagent/includes/event/event_type.hpp b/src/libagent/includes/event/event_type.hpp index f27269b..f7a20d6 100644 --- a/src/libagent/includes/event/event_type.hpp +++ b/src/libagent/includes/event/event_type.hpp @@ -3,7 +3,7 @@ #include // ClassFileLoadHook -struct EventClassFileLoadHook { +struct EventClassFileLoadHook { jvmtiEnv* jvmti_env; JNIEnv* jni_env; jclass class_being_redefined; diff --git a/src/libagent/includes/libagent.hpp b/src/libagent/includes/libagent.hpp index 3b26ca7..6b94c17 100644 --- a/src/libagent/includes/libagent.hpp +++ b/src/libagent/includes/libagent.hpp @@ -5,7 +5,6 @@ #include #include #include -#include namespace JuiceAgent { class Agent { @@ -15,8 +14,7 @@ namespace JuiceAgent { JNIEnv* env; EventBus eventbus; JuiceAgent::Config::Config config; - JuiceAgent::Core::Modules::ModuleManager module_manager; - + private: Agent() = default; ~Agent() = default; diff --git a/src/libagent/includes/modules/IModule.hpp b/src/libagent/includes/modules/IModule.hpp deleted file mode 100644 index 9b6130d..0000000 --- a/src/libagent/includes/modules/IModule.hpp +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once -#include - -namespace JuiceAgent::Core::Modules { - -class IModule { -public: - virtual ~IModule() = default; - - virtual std::string name() const = 0; - - virtual bool init() = 0; - virtual bool start() = 0; - virtual void stop() = 0; - - virtual bool initialized() const = 0; - virtual bool running() const = 0; -}; - -} \ No newline at end of file diff --git a/src/libagent/includes/modules/ModuleBase.hpp b/src/libagent/includes/modules/ModuleBase.hpp deleted file mode 100644 index 8aa8fc9..0000000 --- a/src/libagent/includes/modules/ModuleBase.hpp +++ /dev/null @@ -1,74 +0,0 @@ -#pragma once -#include - -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; -}; - -} \ No newline at end of file diff --git a/src/libagent/includes/modules/ModuleManager.hpp b/src/libagent/includes/modules/ModuleManager.hpp deleted file mode 100644 index 2f33b77..0000000 --- a/src/libagent/includes/modules/ModuleManager.hpp +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -namespace JuiceAgent::Core::Modules { - -class ModuleManager { -public: - ModuleManager() = default; - ~ModuleManager() = default; - - void load_all(); - - bool init_all(); - bool start_all(); - void stop_all(); - - IModule* find_module(const std::string& name); - -private: - std::vector> _modules; -}; - -} \ No newline at end of file diff --git a/src/libagent/includes/modules/ModuleRegistry.hpp b/src/libagent/includes/modules/ModuleRegistry.hpp deleted file mode 100644 index a75881a..0000000 --- a/src/libagent/includes/modules/ModuleRegistry.hpp +++ /dev/null @@ -1,56 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include - -namespace JuiceAgent::Core::Modules { - -class ModuleRegistry { -public: - using Factory = std::function()>; - - struct Entry { - std::string name; - Factory factory; - }; - - static ModuleRegistry& instance() { - static ModuleRegistry inst; - return inst; - } - - bool add(const std::string& name, Factory factory) { - for (const auto& item : _entries) { - if (item.name == name) { - return false; - } - } - - _entries.push_back({name, std::move(factory)}); - return true; - } - - const std::vector& entries() const { - return _entries; - } - -private: - std::vector _entries; -}; - -} - -#define JUICEAGENT_REGISTER_MODULE(TYPE, NAME) \ -namespace { \ - const bool registered_##NAME = \ - JuiceAgent::Core::Modules::ModuleRegistry::instance().add( \ - #NAME, \ - []() -> std::unique_ptr { \ - return std::make_unique(); \ - } \ - ); \ -} - diff --git a/src/libagent/includes/services.hpp b/src/libagent/includes/services.hpp new file mode 100644 index 0000000..6927211 --- /dev/null +++ b/src/libagent/includes/services.hpp @@ -0,0 +1,32 @@ +#pragma once + +#include +#include +#include + +namespace JuiceAgent::services +{ + static JuiceAgent::Agent& agent = JuiceAgent::Agent::instance(); + + namespace Manager { + void register_events(); + void init(const EventPreLoad&); + void start(const EventLoaded&); + void stop(); + } + + namespace JarLoader + { + struct JarLoaderConfig { + bool enabled = false; + std::string injection_dir; + std::string jar_path; + std::string entry_class; + std::string entry_method; + }; + + void init(); + void start(); + } // namespace JarLoader + +} // namespace JuiceAgent::services \ No newline at end of file diff --git a/src/libagent/libagent.cpp b/src/libagent/libagent.cpp index 5a53c7e..ab3de89 100644 --- a/src/libagent/libagent.cpp +++ b/src/libagent/libagent.cpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace JuiceAgent { // ===== Singleton ===== @@ -41,12 +42,8 @@ namespace JuiceAgent { set_config(cfg); PLOGD << "env: " << env << ", jvmti: " << jvmti; - // Load and Initialize Modules - module_manager.load_all(); - module_manager.init_all(); - - // Initialize Modules - module_manager.init_all(); + // Register events for services + JuiceAgent::services::Manager::register_events(); // PreLoad Event eventbus.post(EventPreLoad{ @@ -88,10 +85,6 @@ namespace JuiceAgent { // register bytecodes_opt JuiceAgent::Bytecodes::register_bytecodes(); - // Start Modules - module_manager.start_all(); - - // Loaded Event eventbus.post(EventLoaded{ .jvm = jvm, diff --git a/src/libagent/modules/Dumper.cpp b/src/libagent/modules/Dumper.cpp deleted file mode 100644 index dd433fd..0000000 --- a/src/libagent/modules/Dumper.cpp +++ /dev/null @@ -1,133 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -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( - "JuiceAgent.Modules.Dumper.Enabled", - false - ); - - config.DumpDir = cfg.get( - "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( - [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(_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 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(event.classbytes), - static_cast(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 -) \ No newline at end of file diff --git a/src/libagent/modules/ModuleManager.cpp b/src/libagent/modules/ModuleManager.cpp deleted file mode 100644 index 8c98e8e..0000000 --- a/src/libagent/modules/ModuleManager.cpp +++ /dev/null @@ -1,64 +0,0 @@ -#include -#include -#include - -namespace JuiceAgent::Core::Modules { - -void ModuleManager::load_all() { - for (const auto& entry : ModuleRegistry::instance().entries()) { - auto module = entry.factory(); - - if (!module) { - PLOGE << "Failed to create module: " << entry.name; - continue; - } - - PLOGI << "Register module: " << module->name(); - _modules.push_back(std::move(module)); - } -} - -bool ModuleManager::init_all() { - for (auto& module : _modules) { - PLOGI << "Init module: " << module->name(); - - if (!module->init()) { - PLOGE << "Failed to init module: " << module->name(); - continue; - } - } - - return true; -} - -bool ModuleManager::start_all() { - for (auto& module : _modules) { - PLOGI << "Start module: " << module->name(); - - if (!module->start()) { - PLOGE << "Failed to start module: " << module->name(); - continue; - } - } - - return true; -} - -void ModuleManager::stop_all() { - for (auto it = _modules.rbegin(); it != _modules.rend(); ++it) { - PLOGI << "Stop module: " << (*it)->name(); - (*it)->stop(); - } -} - -IModule* ModuleManager::find_module(const std::string& name) { - for (auto& module : _modules) { - if (module->name() == name) { - return module.get(); - } - } - - return nullptr; -} - -} \ No newline at end of file diff --git a/src/libagent/modules/Remapper.cpp b/src/libagent/modules/Remapper.cpp deleted file mode 100644 index f8b6be1..0000000 --- a/src/libagent/modules/Remapper.cpp +++ /dev/null @@ -1,290 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include - -namespace JuiceAgent::Core::Modules { - -struct RemapperConfig { - bool enabled = false; - std::string JuiceRemapperJarPath; -}; - -struct RemapperBridge { - static jclass bridge_class; - static jmethodID remap_method; -}; - -jclass RemapperBridge::bridge_class = nullptr; -jmethodID RemapperBridge::remap_method = nullptr; - -static JuiceAgent::Agent& agent = JuiceAgent::Agent::instance(); -static RemapperConfig config; - -// ===== match result ===== -struct MatchResult { - size_t length = 0; // length of matched prefix - bool matched = false; -}; - -// ===== find longest prefix match ===== -static MatchResult match_longest( - const std::unordered_set& list, - std::string_view name -) { - MatchResult result; - - for (const auto& prefix : list) { - if (name.starts_with(prefix)) { - if (!result.matched || prefix.length() > result.length) { - result.matched = true; - result.length = prefix.length(); - } - } - } - - return result; -} - -// ===== final decision ===== -static bool should_remap(std::string_view name) { - auto white = match_longest(remapperWhiteList, name); - auto black = match_longest(remapperBlackList, name); - - // no match at all -> deny - if (!white.matched && !black.matched) { - return false; - } - - // only white matched - if (white.matched && !black.matched) { - return true; - } - - // only black matched - if (!white.matched && black.matched) { - return false; - } - - // both matched -> choose longer prefix - if (white.length >= black.length) { - return true; // whitelist wins - } else { - return false; // blacklist wins (more specific) - } -} - -// recursion guard -static thread_local bool in_remap = false; - -class RemapperModule : public ModuleBase { -public: - std::string name() const override { - return "Remapper"; - } - -protected: - bool on_init() override { - auto& cfg = agent.get_config(); - - config.enabled = cfg.get( - "JuiceAgent.Modules.Remapper.Enabled", - false - ); - - config.JuiceRemapperJarPath = cfg.get( - "JuiceAgent.Modules.Remapper.JuiceRemapperJarPath", - "./JuiceRemapper.jar", - true - ); - - if (!config.enabled) { - return true; - } - - JNIEnv* env = agent.get_env(); - jvmtiEnv* jvmti = agent.get_jvmti(); - - // Load JuiceRemapper - if (jvmti->AddToSystemClassLoaderSearch(config.JuiceRemapperJarPath.c_str()) != JNI_OK) { - PLOGE << "Failed to add JuiceRemapper to system class loader search"; - return false; - } - - jclass local_class = env->FindClass( - "cn/xiaozhou233/juiceremapper/bridge/JuiceAgentBridge" - ); - if (!local_class) { - PLOGE << "Failed to find JuiceAgentBridge"; - return false; - } - - RemapperBridge::bridge_class = - reinterpret_cast(env->NewGlobalRef(local_class)); - env->DeleteLocalRef(local_class); - - RemapperBridge::remap_method = env->GetStaticMethodID( - RemapperBridge::bridge_class, - "remap", - "(Ljava/lang/Class;Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;I[B)[B" - ); - if (!RemapperBridge::remap_method) { - PLOGE << "Failed to find remap method"; - return false; - } - - return true; - } - - bool on_start() override { - if (!config.enabled) { - return true; - } - - auto& bus = agent.get_eventbus(); - - _classFileToken = bus.subscribe_mutable( - [this](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(_classFileToken); - _classFileToken = 0; - } - - JNIEnv* env = agent.get_env(); - if (RemapperBridge::bridge_class) { - env->DeleteGlobalRef(RemapperBridge::bridge_class); - RemapperBridge::bridge_class = nullptr; - } - } - -private: - void on_class_file_load_hook(EventClassFileLoadHook& event) { - if (!event.classbytes || event.class_data_len <= 0) { - return; - } - - std::string_view name = event.name ? event.name : ""; - if (name.empty()) return; - - if (in_remap) return; - - // ===== apply whitelist/blacklist decision ===== - if (!should_remap(name)) { - return; - } - - JNIEnv* env = event.jni_env; - jvmtiEnv* jvmti = event.jvmti_env; - - if (!env || !jvmti) return; - - in_remap = true; - - jbyteArray inputArray = env->NewByteArray(event.class_data_len); - if (!inputArray) { - in_remap = false; - return; - } - - env->SetByteArrayRegion( - inputArray, - 0, - event.class_data_len, - reinterpret_cast(event.classbytes) - ); - - jstring jname = env->NewStringUTF(name.data()); - - jobject result = env->CallStaticObjectMethod( - RemapperBridge::bridge_class, - RemapperBridge::remap_method, - nullptr, - event.loader, - jname, - event.protection_domain, - 0, - inputArray - ); - - if (env->ExceptionCheck()) { - env->ExceptionDescribe(); - env->ExceptionClear(); - cleanup(env, inputArray, jname, nullptr); - in_remap = false; - return; - } - - if (!result) { - cleanup(env, inputArray, jname, nullptr); - in_remap = false; - return; - } - - jbyteArray outputArray = reinterpret_cast(result); - - jsize new_len = env->GetArrayLength(outputArray); - - jbyte* new_bytes = env->GetByteArrayElements(outputArray, nullptr); - if (!new_bytes) { - cleanup(env, inputArray, jname, outputArray); - in_remap = false; - return; - } - - unsigned char* new_class_data = nullptr; - - if (jvmti->Allocate(new_len, &new_class_data) != JVMTI_ERROR_NONE) { - env->ReleaseByteArrayElements(outputArray, new_bytes, JNI_ABORT); - cleanup(env, inputArray, jname, outputArray); - in_remap = false; - return; - } - - std::memcpy(new_class_data, new_bytes, new_len); - - *event.new_classbytes = new_class_data; - *event.new_class_data_len = new_len; - - env->ReleaseByteArrayElements(outputArray, new_bytes, JNI_ABORT); - cleanup(env, inputArray, jname, outputArray); - - in_remap = false; - } - - void cleanup(JNIEnv* env, jbyteArray in, jstring name, jbyteArray out) { - if (in) env->DeleteLocalRef(in); - if (name) env->DeleteLocalRef(name); - if (out) env->DeleteLocalRef(out); - } - -private: - EventBus::Token _classFileToken = 0; -}; - -JUICEAGENT_REGISTER_MODULE( - JuiceAgent::Core::Modules::RemapperModule, - RemapperModule -) - -} // namespace \ No newline at end of file diff --git a/src/libagent/modules/Template.cpp b/src/libagent/modules/Template.cpp deleted file mode 100644 index ff06184..0000000 --- a/src/libagent/modules/Template.cpp +++ /dev/null @@ -1,35 +0,0 @@ -#include -#include -#include - -namespace JuiceAgent::Core::Modules { - -class TemplateModule : public ModuleBase { -public: - std::string name() const override { - return "Template"; - } - -protected: - bool on_init() override { - // load config / prepare resource - return true; - } - - bool on_start() override { - // start module - return true; - } - - void on_stop() override { - // stop module - } -}; - -// If you want to register the module, uncomment the following lines -// JUICEAGENT_REGISTER_MODULE( -// JuiceAgent::Core::Modules::TemplateModule, -// TemplateModule -// ) - -} \ No newline at end of file diff --git a/src/libagent/modules/JarLoader.cpp b/src/libagent/services/JarLoaderService.cpp similarity index 55% rename from src/libagent/modules/JarLoader.cpp rename to src/libagent/services/JarLoaderService.cpp index db77d55..952e614 100644 --- a/src/libagent/modules/JarLoader.cpp +++ b/src/libagent/services/JarLoaderService.cpp @@ -1,37 +1,12 @@ -#include -#include -#include -#include -#include +#include -namespace JuiceAgent::Core::Modules { +namespace JuiceAgent::services::JarLoader { + static JarLoaderConfig config; -struct JarLoaderConfig { - bool enabled = true; - std::string injection_dir; - std::string jar_path; - std::string entry_class; - std::string entry_method; -}; + static const char* MODULE_CLASS = "cn/xiaozhou233/juiceagent/api/modules/JarLoader"; + static const char* MODULE_METHOD = "loadJar"; -static JarLoaderConfig config; -static JuiceAgent::Agent& agent = JuiceAgent::Agent::instance(); - -static const char* MODULE_CLASS = - "cn/xiaozhou233/juiceagent/api/modules/JarLoader"; - -static const char* MODULE_METHOD = "loadJar"; - -class JarLoaderModule : public ModuleBase { - using super = ModuleBase; - -public: - std::string name() const override { - return "JarLoader"; - } - -protected: - bool on_init() override { + void init() { auto& cfg = agent.get_config(); config.enabled = cfg.get( @@ -63,13 +38,12 @@ protected: false ); - PLOGI << "JarLoader init"; - return true; + PLOGI << "JarLoaderService initialized"; } - bool on_start() override { + void start() { if (!config.enabled) { - return true; + return; } JuiceAgent::Utils::Serializer ser; @@ -89,17 +63,6 @@ protected: ); PLOGI << "JarLoader start"; - return true; + return; } - - void on_stop() override { - PLOGI << "JarLoader stop"; - } -}; - -} - -JUICEAGENT_REGISTER_MODULE( - JuiceAgent::Core::Modules::JarLoaderModule, - JarLoader -) \ No newline at end of file +} \ No newline at end of file diff --git a/src/libagent/services/ServicesManager.cpp b/src/libagent/services/ServicesManager.cpp new file mode 100644 index 0000000..7ac27ac --- /dev/null +++ b/src/libagent/services/ServicesManager.cpp @@ -0,0 +1,33 @@ +#include +#include + +namespace JuiceAgent::services::Manager { + void register_events() { + // PreLoad + agent.get_eventbus().subscribe(init); + + // Loaded + agent.get_eventbus().subscribe(start); + + // TODO: Stopped + + PLOGI << "Services Manager registered events"; + } + + void init(const EventPreLoad& event) { + PLOGI << "Initializing services"; + // JarLoader + JuiceAgent::services::JarLoader::init(); + + } + + void start(const EventLoaded& event) { + PLOGI << "Starting services"; + // JarLoader + JuiceAgent::services::JarLoader::start(); + } + + void stop() { + + } +} \ No newline at end of file