diff --git a/src/libagent/includes/modules/IModule.hpp b/src/libagent/includes/modules/IModule.hpp index 96226c0..9b6130d 100644 --- a/src/libagent/includes/modules/IModule.hpp +++ b/src/libagent/includes/modules/IModule.hpp @@ -12,6 +12,9 @@ public: 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 new file mode 100644 index 0000000..8aa8fc9 --- /dev/null +++ b/src/libagent/includes/modules/ModuleBase.hpp @@ -0,0 +1,74 @@ +#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/modules/Dumper.cpp b/src/libagent/modules/Dumper.cpp index 757cf48..dd433fd 100644 --- a/src/libagent/modules/Dumper.cpp +++ b/src/libagent/modules/Dumper.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -21,17 +22,16 @@ struct DumperConfig { static DumperConfig config; static JuiceAgent::Agent& agent = JuiceAgent::Agent::instance(); -class Dumper : public IModule { +class Dumper : public ModuleBase { + using super = ModuleBase; + public: std::string name() const override { return "Dumper"; } - bool init() override { - if (_initialized) { - return true; - } - +protected: + bool on_init() override { auto& cfg = agent.get_config(); config.enabled = cfg.get( @@ -45,19 +45,10 @@ public: true ); - _initialized = true; return true; } - bool start() override { - if (!_initialized) { - return false; - } - - if (_running) { - return true; - } - + bool on_start() override { if (!config.enabled) { return true; } @@ -70,27 +61,30 @@ public: } ); - _running = true; return true; } - void stop() override { - if (!_running) { + void on_stop() override { + if (!config.enabled) { return; } auto& bus = agent.get_eventbus(); - bus.unsubscribe(_classFileToken); - _running = false; + 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; } @@ -104,6 +98,7 @@ public: } std::ofstream file(output, std::ios::binary); + if (!file.is_open()) { PLOGW << "Dumper: Failed to open file: " << output.string(); return; @@ -113,28 +108,19 @@ public: reinterpret_cast(event.classbytes), static_cast(event.class_data_len) ); - - file.close(); } catch (const std::exception& e) { PLOGE << "Dumper: " << e.what(); } } -private: - fs::path build_class_path(std::string_view className) { + fs::path build_class_path(std::string_view class_name) { fs::path path(config.DumpDir); - - // JVM name format: cn/xiaozhou233/Test - path /= fs::path(className); + path /= fs::path(class_name); path += ".class"; - return path; } private: - bool _initialized = false; - bool _running = false; - EventBus::Token _classFileToken = 0; std::mutex _mutex; }; diff --git a/src/libagent/modules/JarLoader.cpp b/src/libagent/modules/JarLoader.cpp index fd8b35e..db77d55 100644 --- a/src/libagent/modules/JarLoader.cpp +++ b/src/libagent/modules/JarLoader.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -16,20 +17,21 @@ struct JarLoaderConfig { 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_CLASS = + "cn/xiaozhou233/juiceagent/api/modules/JarLoader"; + static const char* MODULE_METHOD = "loadJar"; -class JarLoaderModule : public IModule { +class JarLoaderModule : public ModuleBase { + using super = ModuleBase; + public: std::string name() const override { return "JarLoader"; } - bool init() override { - if (_initialized) { - return true; - } - +protected: + bool on_init() override { auto& cfg = agent.get_config(); config.enabled = cfg.get( @@ -62,20 +64,16 @@ public: ); PLOGI << "JarLoader init"; - _initialized = true; return true; } - bool start() override { - if (!_initialized) { - return false; - } - - if (_running || !config.enabled) { + bool on_start() override { + if (!config.enabled) { return true; } JuiceAgent::Utils::Serializer ser; + ser.add_kv("InjectionDir", config.injection_dir); ser.add_kv("JarPath", config.jar_path); ser.add_kv("EntryClass", config.entry_class); @@ -91,22 +89,12 @@ public: ); PLOGI << "JarLoader start"; - _running = true; return true; } - void stop() override { - if (!_running) { - return; - } - + void on_stop() override { PLOGI << "JarLoader stop"; - _running = false; } - -private: - bool _initialized = false; - bool _running = false; }; } diff --git a/src/libagent/modules/Remapper.cpp b/src/libagent/modules/Remapper.cpp index 1227527..c0b93e8 100644 --- a/src/libagent/modules/Remapper.cpp +++ b/src/libagent/modules/Remapper.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -17,7 +18,9 @@ struct RemapperConfig { static RemapperConfig config; static JuiceAgent::Agent& agent = JuiceAgent::Agent::instance(); -class RemapperModule : public IModule { +class RemapperModule : public ModuleBase { + using super = ModuleBase; + struct _Remapper { jclass handler_class = nullptr; jmethodID onClassFileLoadMethod = nullptr; @@ -30,11 +33,8 @@ public: return "Remapper"; } - bool init() override { - if (_initialized) { - return true; - } - +protected: + bool on_init() override { auto& cfg = agent.get_config(); config.enabled = cfg.get( @@ -49,7 +49,6 @@ public: ); if (!config.enabled) { - _initialized = true; return true; } @@ -114,21 +113,11 @@ public: return false; } - _initialized = true; return true; } - bool start() override { - if (!_initialized) { - return false; - } - - if (_running) { - return true; - } - + bool on_start() override { if (!config.enabled) { - _running = true; return true; } @@ -152,26 +141,38 @@ public: } ); - _running = true; return true; } - void stop() override { - if (!_running) { + void on_stop() override { + if (!config.enabled) { return; } auto& bus = agent.get_eventbus(); - bus.unsubscribe(_classFileToken); - bus.unsubscribe(_methodEntryToken); - bus.unsubscribe(_methodExitToken); + if (_classFileToken != 0) { + bus.unsubscribe(_classFileToken); + } + + if (_methodEntryToken != 0) { + bus.unsubscribe(_methodEntryToken); + } + + if (_methodExitToken != 0) { + bus.unsubscribe(_methodExitToken); + } _classFileToken = 0; _methodEntryToken = 0; _methodExitToken = 0; - _running = false; + JNIEnv* env = agent.get_env(); + + if (env != nullptr && Remapper.handler_class != nullptr) { + env->DeleteGlobalRef(Remapper.handler_class); + Remapper.handler_class = nullptr; + } } private: @@ -184,15 +185,16 @@ private: JNIEnv* env = event.jni_env; - std::string className(name); - jstring jName = env->NewStringUTF(className.c_str()); - if (jName == nullptr) { + std::string class_name(name); + + jstring j_name = env->NewStringUTF(class_name.c_str()); + if (j_name == nullptr) { return; } jbyteArray input = env->NewByteArray(event.class_data_len); if (input == nullptr) { - env->DeleteLocalRef(jName); + env->DeleteLocalRef(j_name); return; } @@ -209,7 +211,7 @@ private: Remapper.onClassFileLoadMethod, event.class_being_redefined, event.loader, - jName, + j_name, event.protection_domain, event.class_data_len, input @@ -238,8 +240,9 @@ private: } env->DeleteLocalRef(input); - env->DeleteLocalRef(jName); + env->DeleteLocalRef(j_name); } + void on_method_entry_hook(const EventMethodEntry& event) { // event.jni_env->CallStaticVoidMethod( // Remapper.handler_class, @@ -265,9 +268,6 @@ private: } private: - bool _initialized = false; - bool _running = false; - EventBus::Token _classFileToken = 0; EventBus::Token _methodEntryToken = 0; EventBus::Token _methodExitToken = 0; diff --git a/src/libagent/modules/Template.cpp b/src/libagent/modules/Template.cpp index 897c2e7..b5f539a 100644 --- a/src/libagent/modules/Template.cpp +++ b/src/libagent/modules/Template.cpp @@ -1,58 +1,28 @@ -#include +#include #include namespace JuiceAgent::Core::Modules { -class TemplateModule : public IModule { +class TemplateModule : public ModuleBase { public: std::string name() const override { return "Template"; } - bool init() override { - if (_initialized) { - return true; - } - +protected: + bool on_init() override { // load config / prepare resource - - _initialized = true; return true; } - bool start() override { - if (!_initialized) { - return false; - } - - if (_running) { - return true; - } - - // start logic - - _running = true; + bool on_start() override { + // start module return true; } - void stop() override { - if (!_running) { - return; - } - - // cleanup logic - - _running = false; + void on_stop() override { + // stop module } - -private: - bool _initialized = false; - bool _running = false; }; -} - -JUICEAGENT_REGISTER_MODULE( - JuiceAgent::Core::Modules::TemplateModule, - Template -) \ No newline at end of file +} \ No newline at end of file