mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 13:05:53 +08:00
Feat: support built-in and custom jar loading modes
- Split jar loading into load_builtin (extract and register the bundled JuiceAgent API jar) and load_custom (register a user-supplied jar) in a new JarLoader.cpp - Check JuiceAgent.Version against CONFIG_VERSION and dispatch on JarLoader.Mode (BuiltIn / CustomJar) during preload - Pass the parsed Config into Loader::initialize and add CONFIG_VERSION in JuiceAgent.hpp - Restore Config::Utils loader-config helpers on top of the new getPath API
This commit is contained in:
1 parent
11cf47c4c2
commit
92ad21003f
5 files changed
+158
-56
No files matched your search
@@ -8,6 +8,8 @@
|
||||
#include <vector>
|
||||
#include <cstddef>
|
||||
#include <JuiceAgent/Logger.hpp>
|
||||
#include <JuiceAgent/Utils.hpp>
|
||||
#include <JuiceAgent/ConfigStruct.hpp>
|
||||
|
||||
namespace JuiceAgent::Config {
|
||||
|
||||
@@ -89,7 +91,31 @@ public:
|
||||
}
|
||||
|
||||
std::string getPath(const std::string& key) const {
|
||||
const std::filesystem::path value(get<std::string>(key));
|
||||
const std::string value = get<std::string>(key);
|
||||
|
||||
if (value.empty()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return getPath(key, value);
|
||||
}
|
||||
|
||||
std::string getPath(
|
||||
const std::string& key,
|
||||
const std::string& defaultValue
|
||||
) const {
|
||||
std::string rawValue = get<std::string>(key, defaultValue);
|
||||
|
||||
// Use the default value when the configuration value is empty.
|
||||
if (rawValue.empty()) {
|
||||
rawValue = defaultValue;
|
||||
}
|
||||
|
||||
if (rawValue.empty()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const std::filesystem::path value(rawValue);
|
||||
|
||||
if (value.is_absolute()) {
|
||||
return value.lexically_normal().string();
|
||||
@@ -207,4 +233,34 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
namespace Utils {
|
||||
inline LoaderConfig get_loader_config(const Config& config)
|
||||
{
|
||||
LoaderConfig loader_config;
|
||||
loader_config.JuiceAgentNativeLibraryPath = config.getPath(
|
||||
"JuiceAgent.Loader.JuiceAgentNativeLibraryPath",
|
||||
(config.getRuntimeDir() / "libagent.dll").string()
|
||||
);
|
||||
loader_config.RuntimeDir = config.getRuntimeDir().string();
|
||||
|
||||
return loader_config;
|
||||
}
|
||||
|
||||
inline void print_loader_config(const LoaderConfig& config)
|
||||
{
|
||||
spdlog::info("JuiceAgentNativeLibraryPath: {}", config.JuiceAgentNativeLibraryPath);
|
||||
spdlog::info("RuntimeDir: {}", config.RuntimeDir);
|
||||
}
|
||||
|
||||
inline std::string serialize_loader_config(const Config& config, const LoaderConfig& loader_config)
|
||||
{
|
||||
JuiceAgent::Utils::Serializer ser;
|
||||
ser.add_kv("Version", config.get<int>("JuiceAgent.Version", -1));
|
||||
ser.add_kv("JuiceAgentNativeLibraryPath", loader_config.JuiceAgentNativeLibraryPath);
|
||||
ser.add_kv("RuntimeDir", loader_config.RuntimeDir);
|
||||
|
||||
return ser.serialize();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace JuiceAgent::Config
|
||||
@@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
const int CONFIG_VERSION = 2;
|
||||
@@ -0,0 +1,55 @@
|
||||
#include <Loader.hpp>
|
||||
#include <jni.h>
|
||||
#include <jvmti.h>
|
||||
#include <string>
|
||||
|
||||
#include <JuiceAgent/Utils.hpp>
|
||||
#include <JuiceAgent/Logger.hpp>
|
||||
#include <JuiceAgent/JuiceAgent-API-1.4.1+build.1.hpp>
|
||||
|
||||
int JuiceAgent::Loader::load_builtin(jvmtiEnv* jvmti) {
|
||||
// Extract JuiceAgent API to temp 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
|
||||
);
|
||||
|
||||
// Debug
|
||||
spdlog::debug("JuiceAgent API path: {}", juiceagent_api_path);
|
||||
spdlog::debug("JuiceAgent API size: {}", JuiceAgent::Resource::juiceagent_api_bytes_len);
|
||||
spdlog::debug("JuiceAgent API sha256: {}", JuiceAgent::Resource::juiceagent_api_bytes_sha256);
|
||||
|
||||
// Load JuiceAgent API
|
||||
jvmtiError status = jvmti->AddToSystemClassLoaderSearch(juiceagent_api_path.c_str());
|
||||
if (status != JVMTI_ERROR_NONE) {
|
||||
spdlog::error("Failed to add JuiceAgent API to system classloader! code: {}", static_cast<int>(status));
|
||||
return false;
|
||||
}
|
||||
|
||||
spdlog::info("JuiceAgent API loaded to system classloader!");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int JuiceAgent::Loader::load_custom(jvmtiEnv* jvmti, const char* jar_path) {
|
||||
spdlog::info("Loading custom jar: {}", jar_path);
|
||||
|
||||
jvmtiError status = jvmti->AddToSystemClassLoaderSearch(jar_path);
|
||||
if (status != JVMTI_ERROR_NONE) {
|
||||
spdlog::error("Failed to add custom jar to system classloader! code: {}", static_cast<int>(status));
|
||||
return false;
|
||||
}
|
||||
|
||||
spdlog::info("Custom jar loaded to system classloader!");
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -2,10 +2,10 @@
|
||||
#include <Loader.hpp>
|
||||
#include <filesystem>
|
||||
#include <JuiceAgent/Logger.hpp>
|
||||
#include <JuiceAgent/Utils.hpp>
|
||||
#include <JuiceAgent/Config.hpp>
|
||||
#include <JuiceAgent/ConfigStruct.hpp>
|
||||
#include <JuiceAgent/JuiceAgent-API-1.4.1+build.1.hpp>
|
||||
|
||||
#include <JuiceAgent/JuiceAgent.hpp>
|
||||
|
||||
// Preload section
|
||||
// Check parameters and load the runtime
|
||||
@@ -25,64 +25,48 @@ void JuiceAgent::Loader::preload(const char* runtime_dir, JNIEnv* env, jvmtiEnv*
|
||||
|
||||
spdlog::info("[PreLoad] Runtime directory: {}", runtime_dir_new);
|
||||
|
||||
// TODO: Advanced options
|
||||
// Extract JuiceAgent API to temp 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
|
||||
);
|
||||
|
||||
// Debug
|
||||
spdlog::debug("JuiceAgent API path: {}", juiceagent_api_path);
|
||||
spdlog::debug("JuiceAgent API size: {}", JuiceAgent::Resource::juiceagent_api_bytes_len);
|
||||
spdlog::debug("JuiceAgent API sha256: {}", JuiceAgent::Resource::juiceagent_api_bytes_sha256);
|
||||
|
||||
// Load JuiceAgent API
|
||||
jvmtiError status = jvmti->AddToSystemClassLoaderSearch(juiceagent_api_path.c_str());
|
||||
if (status != JVMTI_ERROR_NONE) {
|
||||
spdlog::error("Failed to add JuiceAgent API to system classloader! code: {}", static_cast<int>(status));
|
||||
spdlog::trace("[PreLoad] Loading config...");
|
||||
JuiceAgent::Config::Config cfg(runtime_dir ? runtime_dir : "");
|
||||
if(!cfg.isValid()) {
|
||||
spdlog::error("[PreLoad] Failed to load config!");
|
||||
return;
|
||||
}
|
||||
|
||||
spdlog::info("JuiceAgent API loaded to system classloader!");
|
||||
// Check config version
|
||||
int version = cfg.get<int>("JuiceAgent.Version");
|
||||
if(version != CONFIG_VERSION) {
|
||||
spdlog::error("[PreLoad] Config version mismatch! Expected: {}, Got: {}, Please update your config!", CONFIG_VERSION, version);
|
||||
return;
|
||||
}
|
||||
|
||||
// Init JuiceAgent via JuiceAgent API
|
||||
JuiceAgent::Loader::initialize(runtime_dir_new.c_str(), env, jvmti);
|
||||
// JarLoader mode check and load jars
|
||||
std::string jar_loader_mode = cfg.get<std::string>("JarLoader.Mode");
|
||||
if(jar_loader_mode == "BuiltIn") {
|
||||
spdlog::info("[PreLoad] JarLoader mode: BuiltIn");
|
||||
|
||||
if(!load_builtin(jvmti)) {
|
||||
spdlog::error("[PreLoad] Failed to load built-in jars!");
|
||||
}
|
||||
} else if(jar_loader_mode == "CustomJar") {
|
||||
spdlog::info("[PreLoad] JarLoader mode: CustomJar");
|
||||
|
||||
std::string jar_path = cfg.get<std::string>("JarLoader.JarPath");
|
||||
|
||||
if(!load_custom(jvmti, jar_path.c_str())) {
|
||||
spdlog::error("[PreLoad] Failed to load custom jar: {}", jar_path);
|
||||
}
|
||||
} else {
|
||||
spdlog::error("[PreLoad] Invalid JarLoader mode: {}", jar_loader_mode);
|
||||
return;
|
||||
}
|
||||
|
||||
JuiceAgent::Loader::initialize(runtime_dir_new.c_str(), env, jvmti, cfg);
|
||||
}
|
||||
|
||||
void JuiceAgent::Loader::initialize(const char* runtime_dir, JNIEnv* env, jvmtiEnv* jvmti) {
|
||||
const char* bootstrap_class = "cn/xiaozhou233/juiceagent/api/JuiceAgentBootstrap";
|
||||
const char* method_name = "start";
|
||||
void JuiceAgent::Loader::initialize(const char* runtime_dir, JNIEnv* env, jvmtiEnv* jvmti, JuiceAgent::Config::Config& cfg) {
|
||||
std::string jar_loader_mode = cfg.get<std::string>("JarLoader.Mode");
|
||||
|
||||
// Read JuiceAgent config
|
||||
JuiceAgent::Config::Config cfg(runtime_dir ? runtime_dir : "");
|
||||
if (!cfg.is_valid()) {
|
||||
spdlog::error("JuiceAgent configuration is invalid!");
|
||||
return;
|
||||
}
|
||||
// TODO: Implement this
|
||||
|
||||
// TODO: Refactor config
|
||||
LoaderConfig config = JuiceAgent::Config::Utils::get_loader_config(cfg);
|
||||
JuiceAgent::Config::Utils::print_loader_config(config);
|
||||
|
||||
// Serialize InjectionInfo to string
|
||||
std::string args = JuiceAgent::Config::Utils::serialize_loader_config(cfg, config);
|
||||
spdlog::debug("Serialized config: {}", args);
|
||||
|
||||
bool result = JuiceAgent::Utils::call_java_impl(env, bootstrap_class, method_name, args.c_str());
|
||||
if (!result) {
|
||||
spdlog::error("Failed to initialize JuiceAgent!");
|
||||
return;
|
||||
}
|
||||
|
||||
spdlog::info("JuiceAgent initialized!");
|
||||
// spdlog::info("JuiceAgent initialized!");
|
||||
}
|
||||
@@ -2,9 +2,13 @@
|
||||
|
||||
#include <jvm/jni.h>
|
||||
#include <jvm/jvmti.h>
|
||||
#include <JuiceAgent/Config.hpp>
|
||||
|
||||
namespace JuiceAgent::Loader {
|
||||
void entrypoint(const char* runtime_dir);
|
||||
void preload(const char* runtime_dir, JNIEnv* env, jvmtiEnv* jvmti);
|
||||
void initialize(const char* runtime_dir, JNIEnv* env, jvmtiEnv* jvmti);
|
||||
void initialize(const char* runtime_dir, JNIEnv* env, jvmtiEnv* jvmti, JuiceAgent::Config::Config& cfg);
|
||||
|
||||
int load_builtin(jvmtiEnv* jvmti);
|
||||
int load_custom(jvmtiEnv* jvmti, const char* jar_path);
|
||||
} // namespace JuiceAgent::Loader
|
||||
Reference in new issue
Block a user