mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 21:15:56 +08:00
Patch: AI Fix
This commit is contained in:
1 parent
72c0518ebc
commit
3362e2071c
2 files changed
+281
-204
No files matched your search
@@ -1,115 +1,133 @@
|
||||
#pragma once
|
||||
|
||||
#include <JuiceAgent/Logger.hpp>
|
||||
#include <libloader.hpp>
|
||||
#include <config.hpp>
|
||||
#include <jvm.hpp>
|
||||
|
||||
namespace libloader
|
||||
{
|
||||
// 累了,这几把的代码扔给AI算了 气死了
|
||||
bool invoke_juiceagent_init(JNIEnv* env, const InjectionInfo& info) {
|
||||
std::string bootstrap_class = "cn/xiaozhou233/juiceagent/api/JuiceAgentBootstrap";
|
||||
std::string method_name = "start";
|
||||
std::string method_desc = "([Ljava/lang/String;)V";
|
||||
namespace libloader {
|
||||
|
||||
// Find Class
|
||||
jclass bootstrap_class_obj = env->FindClass(bootstrap_class.c_str());
|
||||
if (!bootstrap_class_obj) {
|
||||
if (env->ExceptionCheck()) {
|
||||
env->ExceptionDescribe();
|
||||
env->ExceptionClear();
|
||||
}
|
||||
PLOGE << "Failed to find JuiceAgentBootstrap class: " << bootstrap_class;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Find Method
|
||||
jmethodID method_id = env->GetStaticMethodID(bootstrap_class_obj, method_name.c_str(), method_desc.c_str());
|
||||
if (!method_id) {
|
||||
if (env->ExceptionCheck()) {
|
||||
env->ExceptionDescribe();
|
||||
env->ExceptionClear();
|
||||
}
|
||||
PLOGE << "Failed to find method: " << method_name;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Prepare arguments (order matters)
|
||||
const char* args[] = {
|
||||
info.EntryJarPath.c_str(),
|
||||
info.EntryClass.c_str(),
|
||||
info.EntryMethod.c_str(),
|
||||
info.InjectionDir.c_str(),
|
||||
info.JuiceAgentNativePath.c_str()
|
||||
};
|
||||
int argCount = sizeof(args) / sizeof(args[0]);
|
||||
|
||||
jclass stringCls = env->FindClass("java/lang/String");
|
||||
jobjectArray jArgs = env->NewObjectArray(argCount, stringCls, nullptr);
|
||||
for (int i = 0; i < argCount; i++) {
|
||||
env->SetObjectArrayElement(jArgs, i, env->NewStringUTF(args[i]));
|
||||
}
|
||||
|
||||
// Invoke the method
|
||||
env->CallStaticVoidMethod(bootstrap_class_obj, method_id, jArgs);
|
||||
namespace {
|
||||
// Check for JNI exceptions and clear them
|
||||
bool check_and_clear_exception(JNIEnv* env, const char* context) {
|
||||
if (env->ExceptionCheck()) {
|
||||
PLOGE << "JNI Exception occurred at: " << context;
|
||||
env->ExceptionDescribe();
|
||||
env->ExceptionClear();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
env->DeleteLocalRef(jArgs);
|
||||
env->DeleteLocalRef(stringCls);
|
||||
|
||||
PLOGI << "JuiceAgentBootstrap.start invoked successfully";
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void entrypoint(const char* runtime_dir)
|
||||
{
|
||||
if (runtime_dir == nullptr) {
|
||||
PLOGE << "runtime_dir is null, fallback may fail";
|
||||
// RAII wrapper for local JNI references
|
||||
template<typename T>
|
||||
class LocalRef {
|
||||
private:
|
||||
JNIEnv* env;
|
||||
T ref;
|
||||
public:
|
||||
LocalRef(JNIEnv* env, T ref) : env(env), ref(ref) {}
|
||||
~LocalRef() {
|
||||
if (ref) env->DeleteLocalRef(ref);
|
||||
}
|
||||
T get() const { return ref; }
|
||||
operator T() const { return ref; }
|
||||
};
|
||||
}
|
||||
|
||||
// Load Config (allow fallback)
|
||||
config::Config cfg(runtime_dir);
|
||||
if (!cfg.is_valid()) {
|
||||
PLOGW << "Invalid config, using default values";
|
||||
}
|
||||
// Invoke JuiceAgentBootstrap.start(String) via JNI
|
||||
bool invoke_juiceagent_init(JNIEnv* env, const InjectionInfo& info) {
|
||||
const char* bootstrap_class = "cn/xiaozhou233/juiceagent/api/JuiceAgentBootstrap";
|
||||
const char* method_name = "start";
|
||||
const char* method_desc = "(Ljava/lang/String;)V";
|
||||
|
||||
InjectionInfo info = cfg.get_injection_info();
|
||||
config::print_injection_info(info);
|
||||
|
||||
// Validate injection path early
|
||||
if (info.JuiceAgentAPIJarPath.empty()) {
|
||||
PLOGE << "JuiceAgentAPIJarPath is empty, abort injection";
|
||||
return;
|
||||
}
|
||||
|
||||
// Attach to JVM
|
||||
jvm::Jvm jvm;
|
||||
if (!jvm.attach()) {
|
||||
PLOGE << "Attach to JVM failed";
|
||||
return;
|
||||
}
|
||||
|
||||
auto* jvmti = jvm.get_jvmti();
|
||||
if (jvmti == nullptr) {
|
||||
PLOGE << "JVMTI is null";
|
||||
return;
|
||||
}
|
||||
|
||||
// Inject JuiceAgentAPI.jar
|
||||
const char* jar_path = info.JuiceAgentAPIJarPath.c_str();
|
||||
jint status = jvmti->AddToSystemClassLoaderSearch(jar_path);
|
||||
|
||||
if (status != JNI_OK) {
|
||||
PLOGE << "AddToSystemClassLoaderSearch failed: " << status;
|
||||
return;
|
||||
}
|
||||
|
||||
PLOGI << "Jar injected successfully: " << jar_path;
|
||||
|
||||
invoke_juiceagent_init(jvm.get_env(), info);
|
||||
// Find Java class
|
||||
LocalRef<jclass> cls(env, env->FindClass(bootstrap_class));
|
||||
if (!cls.get() || check_and_clear_exception(env, "FindClass")) {
|
||||
PLOGE << "Failed to find class: " << bootstrap_class;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Find static method
|
||||
jmethodID method_id = env->GetStaticMethodID(cls, method_name, method_desc);
|
||||
if (!method_id || check_and_clear_exception(env, "GetStaticMethodID")) {
|
||||
PLOGE << "Failed to find method: " << method_name;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Serialize InjectionInfo to string
|
||||
std::string args = config::Config::serialize(info);
|
||||
PLOGD << "Invoke args: " << args;
|
||||
|
||||
// Create Java string
|
||||
LocalRef<jstring> jArgs(env, env->NewStringUTF(args.c_str()));
|
||||
if (!jArgs.get() || check_and_clear_exception(env, "NewStringUTF")) {
|
||||
PLOGE << "Failed to create jstring";
|
||||
return false;
|
||||
}
|
||||
|
||||
// Call the static method
|
||||
env->CallStaticVoidMethod(cls, method_id, jArgs);
|
||||
|
||||
// Check for exceptions
|
||||
if (check_and_clear_exception(env, "CallStaticVoidMethod")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
PLOGI << "JuiceAgentBootstrap.start invoked successfully";
|
||||
return true;
|
||||
}
|
||||
|
||||
// Entrypoint function for loader
|
||||
void entrypoint(const char* runtime_dir) {
|
||||
if (!runtime_dir) {
|
||||
PLOGW << "runtime_dir is null, using empty path";
|
||||
}
|
||||
|
||||
// Load configuration
|
||||
config::Config cfg(runtime_dir);
|
||||
if (!cfg.is_valid()) {
|
||||
PLOGW << "Invalid config, using default values";
|
||||
}
|
||||
|
||||
// Get InjectionInfo
|
||||
InjectionInfo info = cfg.get_injection_info();
|
||||
config::print_injection_info(info);
|
||||
|
||||
// Validate essential paths
|
||||
if (info.JuiceAgentAPIJarPath.empty()) {
|
||||
PLOGE << "JuiceAgentAPIJarPath is empty, abort injection";
|
||||
return;
|
||||
}
|
||||
|
||||
// Attach to JVM
|
||||
jvm::Jvm jvm;
|
||||
if (!jvm.attach()) {
|
||||
PLOGE << "Attach to JVM failed";
|
||||
return;
|
||||
}
|
||||
|
||||
JNIEnv* env = jvm.get_env();
|
||||
auto* jvmti = jvm.get_jvmti();
|
||||
|
||||
if (!env || !jvmti) {
|
||||
PLOGE << "JNIEnv or JVMTI is null";
|
||||
return;
|
||||
}
|
||||
|
||||
// Inject JuiceAgent jar into system class loader
|
||||
jint status = jvmti->AddToSystemClassLoaderSearch(info.JuiceAgentAPIJarPath.c_str());
|
||||
if (status != JNI_OK) {
|
||||
PLOGE << "AddToSystemClassLoaderSearch failed: " << status;
|
||||
return;
|
||||
}
|
||||
|
||||
PLOGI << "Jar injected successfully: " << info.JuiceAgentAPIJarPath;
|
||||
|
||||
// Invoke JuiceAgent initialization
|
||||
if (!invoke_juiceagent_init(env, info)) {
|
||||
PLOGE << "invoke_juiceagent_init failed";
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace libloader
|
||||
+165
-106
@@ -5,115 +5,174 @@
|
||||
#include <tinytoml/toml.h>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
namespace config
|
||||
{
|
||||
class Config {
|
||||
private:
|
||||
std::string runtime_dir;
|
||||
toml::Value config{};
|
||||
bool valid = false;
|
||||
namespace config {
|
||||
|
||||
public:
|
||||
explicit Config(const char* c_runtime_dir)
|
||||
: runtime_dir(c_runtime_dir) {
|
||||
std::filesystem::path path = std::filesystem::path(c_runtime_dir) / "config.toml";
|
||||
|
||||
std::ifstream ifs(path);
|
||||
if (!ifs.is_open()) {
|
||||
PLOGE << "Failed to open config file: " << path;
|
||||
class Config {
|
||||
private:
|
||||
std::string runtime_dir; // Runtime directory
|
||||
toml::Value config{}; // Parsed TOML configuration
|
||||
bool valid = false; // Whether the config is successfully loaded
|
||||
|
||||
// Escape special characters ';' = '\'
|
||||
static std::string escape(const std::string& s) {
|
||||
std::string out;
|
||||
out.reserve(s.size());
|
||||
for (char c : s) {
|
||||
if (c == ';' || c == '=' || c == '\\') {
|
||||
out += '\\';
|
||||
}
|
||||
out += c;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// Append key=value pair to string
|
||||
static void append_kv(std::string& out, const char* key, const std::string& value, bool& first) {
|
||||
if (!first) out += ';';
|
||||
first = false;
|
||||
out += key;
|
||||
out += '=';
|
||||
out += escape(value);
|
||||
}
|
||||
|
||||
// Helper function: find value by dot-separated key
|
||||
const toml::Value* find_value(const std::string& key) const {
|
||||
if (!valid) return nullptr;
|
||||
const toml::Value* current = &config;
|
||||
size_t start = 0;
|
||||
while (true) {
|
||||
size_t dot = key.find('.', start);
|
||||
std::string part = key.substr(start, dot - start);
|
||||
|
||||
if (!current->is<toml::Table>()) return nullptr;
|
||||
|
||||
const auto& table = current->as<toml::Table>();
|
||||
auto it = table.find(part);
|
||||
if (it == table.end()) return nullptr;
|
||||
|
||||
current = &it->second;
|
||||
|
||||
if (dot == std::string::npos) break;
|
||||
start = dot + 1;
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
public:
|
||||
explicit Config(const char* c_runtime_dir)
|
||||
: runtime_dir(c_runtime_dir ? c_runtime_dir : "")
|
||||
{
|
||||
if (runtime_dir.empty()) {
|
||||
PLOGW << "runtime_dir is empty";
|
||||
return;
|
||||
}
|
||||
|
||||
std::filesystem::path path = std::filesystem::path(runtime_dir) / "config.toml";
|
||||
std::ifstream ifs(path);
|
||||
if (!ifs.is_open()) {
|
||||
PLOGE << "Failed to open config file: " << path;
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
auto pr = toml::parse(ifs);
|
||||
if (!pr.valid()) {
|
||||
PLOGE << "Invalid TOML file: " << pr.errorReason;
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
auto pr = toml::parse(ifs);
|
||||
|
||||
if (!pr.valid()) {
|
||||
PLOGE << "Invalid TOML file: " << pr.errorReason;
|
||||
return;
|
||||
}
|
||||
|
||||
config = pr.value;
|
||||
valid = true;
|
||||
} catch (const std::exception& e) {
|
||||
PLOGE << "TOML parse error: " << e.what();
|
||||
}
|
||||
config = pr.value;
|
||||
valid = true;
|
||||
} catch (const std::exception& e) {
|
||||
PLOGE << "TOML parse error: " << e.what();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T get_or_default(const std::string& key, const T& default_value, bool use_default_if_empty = false) const {
|
||||
if (!valid) return default_value;
|
||||
|
||||
const toml::Value* current = &config;
|
||||
size_t start = 0;
|
||||
|
||||
while (true) {
|
||||
size_t dot = key.find('.', start);
|
||||
std::string part = key.substr(start, dot - start);
|
||||
|
||||
if (!current->is<toml::Table>()) return default_value;
|
||||
|
||||
auto it = current->as<toml::Table>().find(part);
|
||||
if (it == current->as<toml::Table>().end()) return default_value;
|
||||
|
||||
current = &it->second;
|
||||
|
||||
if (dot == std::string::npos) break;
|
||||
start = dot + 1;
|
||||
}
|
||||
|
||||
try {
|
||||
if constexpr (std::is_same_v<T, std::string>) {
|
||||
std::string value = current->as<std::string>();
|
||||
if (use_default_if_empty && value.empty()) return default_value;
|
||||
return value;
|
||||
} else {
|
||||
return current->as<T>();
|
||||
}
|
||||
} catch (...) {
|
||||
return default_value;
|
||||
}
|
||||
}
|
||||
|
||||
bool is_valid() const {
|
||||
return valid;
|
||||
}
|
||||
|
||||
InjectionInfo get_injection_info() {
|
||||
InjectionInfo info;
|
||||
auto get_default_path = [&](const std::string& name) -> std::string {
|
||||
return (std::filesystem::path(runtime_dir) / name).string();
|
||||
};
|
||||
|
||||
// [JuiceAgent]
|
||||
// -- APIJarPath = "runtime_dir/JuiceAgent.jar"
|
||||
info.JuiceAgentAPIJarPath = get_or_default<std::string>("JuiceAgent.JuiceAgentAPIJarPath", get_default_path("JuiceAgent.jar"), true);
|
||||
// -- JuiceAgentNativePath = "runtime_dir/libagent.dll/.so"
|
||||
info.JuiceAgentNativePath = get_or_default<std::string>("JuiceAgent.JuiceAgentNativePath", get_default_path("libagent.dll"), true);
|
||||
|
||||
// [Entry]
|
||||
// -- EntryJarPath = "runtime_dir/entry.jar"
|
||||
info.EntryJarPath = get_or_default<std::string>("Entry.EntryJarPath", get_default_path("entry.jar"), true);
|
||||
// -- EntryClass = "com.example.Entry"
|
||||
info.EntryClass = get_or_default<std::string>("Entry.EntryClass", "com.example.Entry", true);
|
||||
// -- EntryMethod = "start"
|
||||
info.EntryMethod = get_or_default<std::string>("Entry.EntryMethod", "start", true);
|
||||
|
||||
// [Runtime]
|
||||
// -- InjectionDir = "runtime_dir/injection"
|
||||
info.InjectionDir = get_or_default<std::string>("Runtime.InjectionDir", get_default_path("injection"), true);
|
||||
|
||||
return info;
|
||||
}
|
||||
};
|
||||
|
||||
void print_injection_info(const InjectionInfo& info) {
|
||||
PLOGI << "JuiceAgent Injection Info:";
|
||||
PLOGI << " JuiceAgentAPIJarPath: " << info.JuiceAgentAPIJarPath;
|
||||
PLOGI << " JuiceAgentNativePath: " << info.JuiceAgentNativePath;
|
||||
PLOGI << " EntryJarPath: " << info.EntryJarPath;
|
||||
PLOGI << " EntryClass: " << info.EntryClass;
|
||||
PLOGI << " EntryMethod: " << info.EntryMethod;
|
||||
PLOGI << " InjectionDir: " << info.InjectionDir;
|
||||
}
|
||||
}
|
||||
|
||||
// Get value by key with default
|
||||
template<typename T>
|
||||
T get_or_default(const std::string& key, const T& default_value, bool use_default_if_empty = false) const {
|
||||
const toml::Value* val = find_value(key);
|
||||
if (!val) return default_value;
|
||||
|
||||
try {
|
||||
if constexpr (std::is_same_v<T, std::string>) {
|
||||
std::string s = val->as<std::string>();
|
||||
if (use_default_if_empty && s.empty()) return default_value;
|
||||
return s;
|
||||
} else {
|
||||
return val->as<T>();
|
||||
}
|
||||
} catch (...) {
|
||||
return default_value;
|
||||
}
|
||||
}
|
||||
|
||||
bool is_valid() const {
|
||||
return valid;
|
||||
}
|
||||
|
||||
// Get InjectionInfo struct from config
|
||||
InjectionInfo get_injection_info() const {
|
||||
InjectionInfo info;
|
||||
|
||||
auto get_default_path = [&](const std::string& name) {
|
||||
return (std::filesystem::path(runtime_dir) / name).string();
|
||||
};
|
||||
|
||||
info.JuiceAgentAPIJarPath = get_or_default<std::string>(
|
||||
"JuiceAgent.JuiceAgentAPIJarPath", get_default_path("JuiceAgent.jar"), true);
|
||||
|
||||
info.JuiceAgentNativePath = get_or_default<std::string>(
|
||||
"JuiceAgent.JuiceAgentNativePath", get_default_path("libagent.dll"), true);
|
||||
|
||||
info.EntryJarPath = get_or_default<std::string>(
|
||||
"Entry.EntryJarPath", get_default_path("entry.jar"), true);
|
||||
|
||||
info.EntryClass = get_or_default<std::string>(
|
||||
"Entry.EntryClass", "com.example.Entry", true);
|
||||
|
||||
info.EntryMethod = get_or_default<std::string>(
|
||||
"Entry.EntryMethod", "start", true);
|
||||
|
||||
info.InjectionDir = get_or_default<std::string>(
|
||||
"Runtime.InjectionDir", get_default_path("injection"), true);
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
// Serialize InjectionInfo to key=value; format
|
||||
static std::string serialize(const InjectionInfo& info) {
|
||||
std::string result;
|
||||
result.reserve(256);
|
||||
bool first = true;
|
||||
|
||||
append_kv(result, "version", "1", first);
|
||||
append_kv(result, "juiceAgentAPIJarPath", info.JuiceAgentAPIJarPath, first);
|
||||
append_kv(result, "juiceAgentNativePath", info.JuiceAgentNativePath, first);
|
||||
append_kv(result, "entryJarPath", info.EntryJarPath, first);
|
||||
append_kv(result, "entryClass", info.EntryClass, first);
|
||||
append_kv(result, "entryMethod", info.EntryMethod, first);
|
||||
append_kv(result, "injectionDir", info.InjectionDir, first);
|
||||
|
||||
PLOGD << "Serialized args: " << result;
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string serialize() const {
|
||||
return serialize(get_injection_info());
|
||||
}
|
||||
};
|
||||
|
||||
// Print InjectionInfo (debug)
|
||||
inline void print_injection_info(const InjectionInfo& info) {
|
||||
PLOGI << "JuiceAgent Injection Info:";
|
||||
PLOGI << " JuiceAgentAPIJarPath: " << info.JuiceAgentAPIJarPath;
|
||||
PLOGI << " JuiceAgentNativePath: " << info.JuiceAgentNativePath;
|
||||
PLOGI << " EntryJarPath: " << info.EntryJarPath;
|
||||
PLOGI << " EntryClass: " << info.EntryClass;
|
||||
PLOGI << " EntryMethod: " << info.EntryMethod;
|
||||
PLOGI << " InjectionDir: " << info.InjectionDir;
|
||||
}
|
||||
|
||||
} // namespace config
|
||||
Reference in new issue
Block a user