Refactor: Remove unused module files and streamline service management

This commit is contained in:
xiaozhou233 committed 2026-05-07 15:25:40 +08:00
1 parent 490e4ce2ac
commit 637640b938
14 files changed
+81 -761

No files matched your search

+1 -1
View File
@@ -3,7 +3,7 @@
#include <jni_impl.hpp>
// ClassFileLoadHook
struct EventClassFileLoadHook {
struct EventClassFileLoadHook {
jvmtiEnv* jvmti_env;
JNIEnv* jni_env;
jclass class_being_redefined;
+1 -3
View File
@@ -5,7 +5,6 @@
#include <jvmti.h>
#include <event/eventbus.hpp>
#include <JuiceAgent/Config.hpp>
#include <modules/ModuleManager.hpp>
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;
-20
View File
@@ -1,20 +0,0 @@
#pragma once
#include <string>
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;
};
}
@@ -1,74 +0,0 @@
#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,27 +0,0 @@
#pragma once
#include <memory>
#include <vector>
#include <string>
#include <modules/IModule.hpp>
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<std::unique_ptr<IModule>> _modules;
};
}
@@ -1,56 +0,0 @@
#pragma once
#include <vector>
#include <memory>
#include <functional>
#include <string>
#include <modules/IModule.hpp>
namespace JuiceAgent::Core::Modules {
class ModuleRegistry {
public:
using Factory = std::function<std::unique_ptr<IModule>()>;
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<Entry>& entries() const {
return _entries;
}
private:
std::vector<Entry> _entries;
};
}
#define JUICEAGENT_REGISTER_MODULE(TYPE, NAME) \
namespace { \
const bool registered_##NAME = \
JuiceAgent::Core::Modules::ModuleRegistry::instance().add( \
#NAME, \
[]() -> std::unique_ptr<JuiceAgent::Core::Modules::IModule> { \
return std::make_unique<TYPE>(); \
} \
); \
}
+32
View File
@@ -0,0 +1,32 @@
#pragma once
#include <string>
#include <libagent.hpp>
#include <event/event_type.hpp>
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
+3 -10
View File
@@ -4,6 +4,7 @@
#include <event/event_type.hpp>
#include <event/eventbus.hpp>
#include <JuiceAgent/Config.hpp>
#include <services.hpp>
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,
-133
View File
@@ -1,133 +0,0 @@
#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
)
-64
View File
@@ -1,64 +0,0 @@
#include <modules/ModuleManager.hpp>
#include <modules/ModuleRegistry.hpp>
#include <JuiceAgent/Logger.hpp>
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;
}
}
-290
View File
@@ -1,290 +0,0 @@
#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 <string>
#include <string_view>
#include <cstring>
#include <unordered_set>
#include <global.hpp>
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<std::string>& 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<bool>(
"JuiceAgent.Modules.Remapper.Enabled",
false
);
config.JuiceRemapperJarPath = cfg.get<std::string>(
"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<jclass>(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<EventClassFileLoadHook>(
[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<EventClassFileLoadHook>(_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<const jbyte*>(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<jbyteArray>(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
-35
View File
@@ -1,35 +0,0 @@
#include <modules/ModuleRegistry.hpp>
#include <modules/ModuleBase.hpp>
#include <JuiceAgent/Logger.hpp>
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
// )
}
@@ -1,37 +1,12 @@
#include <modules/ModuleRegistry.hpp>
#include <modules/ModuleBase.hpp>
#include <JuiceAgent/Logger.hpp>
#include <libagent.hpp>
#include <JuiceAgent/Utils.hpp>
#include <services.hpp>
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<bool>(
@@ -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
)
}
+33
View File
@@ -0,0 +1,33 @@
#include <services.hpp>
#include <event/event_type.hpp>
namespace JuiceAgent::services::Manager {
void register_events() {
// PreLoad
agent.get_eventbus().subscribe<EventPreLoad>(init);
// Loaded
agent.get_eventbus().subscribe<EventLoaded>(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() {
}
}