mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 13:05:53 +08:00
Compare commits
14
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e4844abba1 | ||
|
|
d359438e5d | ||
|
|
e8bee3040f | ||
|
|
535ed0f117 | ||
|
|
2f7eb532a7 | ||
|
|
174197badb | ||
|
|
56dead0725 | ||
|
|
fcb6be0b03 | ||
|
|
455986b08c | ||
|
|
5b2c3c77ef | ||
|
|
11aaac0f0b | ||
|
|
6d3e29db03 | ||
|
|
655039745a | ||
|
|
09f44f1aff |
No files matched your search
@@ -1,40 +1,44 @@
|
||||
|
||||
# JuiceAgent
|
||||
[English](https://github.com/xiaozhou233/JuiceAgent/blob/main/README.md) | [中文](https://github.com/xiaozhou233/JuiceAgent/blob/main/README_zh.md)
|
||||
|
||||
JuiceAgent is a JVMTI-based injection library for loading external JARs and transforming Java bytecode, even when `DisableAttachMechanism` is enabled.
|
||||
|
||||
JuiceAgent — a JVMTI-based injection library for loading external JARs and transforming Java bytecode, even when DisableAttachMechanism is enabled.
|
||||
**Notice:** Currently, only **Windows** is supported. **Linux** support may be added in the future.
|
||||
|
||||
**NOTICE:** Currently only supports **Windows**, and plans to support **Linux** in the future.
|
||||
**Notice:** The current repository version is **Version 3.1 Build 3**.
|
||||
|
||||
**NOTICE:** **Version 2.3 Build 1** is the current **stable release and is no longer being updated.** Version 3.0 and later have been rewritten and may contain unknown issues.
|
||||
|
||||
**WARNING:** This project is still in the experimental stage and is not recommended for production use.
|
||||
**Use this at your own risk.**
|
||||
**Warning:** This project is still experimental and is not recommended for production use. Use it at your own risk.
|
||||
|
||||
## Summary
|
||||
JuiceAgent is a JVMTI-native injection framework that injects native code into a target JVM to dynamically load external JARs and perform bytecode redefinition/transformations without requiring a javaagent (works when DisableAttachMechanism=true).
|
||||
JuiceAgent is a JVMTI-native injection framework that injects native code into a target JVM to dynamically load external JARs and perform bytecode redefinition or retransformation without requiring a `javaagent`. It can still work when `DisableAttachMechanism=true`.
|
||||
|
||||
## Features
|
||||
This project is an implementation of the JuiceAgent-API. For more details, please see [This](https://github.com/xiaozhou233/JuiceAgent-API/blob/master/src/main/java/cn/xiaozhou233/juiceagent/api/JuiceAgent.java)
|
||||
This project is the native implementation of JuiceAgent-API. For the API definition, see [JuiceAgent.java](https://github.com/xiaozhou233/JuiceAgent-API/blob/master/src/main/java/cn/xiaozhou233/juiceagent/api/JuiceAgent.java).
|
||||
|
||||
- addToBootstrapClassLoaderSearch
|
||||
- addToSystemClassLoaderSearch
|
||||
- addToClassLoader
|
||||
- defineClass
|
||||
- redefineClass(ByName)
|
||||
- retransformClass(ByName)
|
||||
- getLoadedClasses
|
||||
- getClassByName
|
||||
- getClassBytes(ByName)
|
||||
- `addToBootstrapClassLoaderSearch`
|
||||
- `addToSystemClassLoaderSearch`
|
||||
- `addToClassLoader`
|
||||
- `defineClass`
|
||||
- `redefineClass(ByName)`
|
||||
- `retransformClass(ByName)`
|
||||
- `getLoadedClasses`
|
||||
- `getClassByName`
|
||||
- `getClassBytes(ByName)`
|
||||
|
||||
## How To Use (**For Version 3.0 Build 2**)
|
||||
## How To Use
|
||||
|
||||
### 1. ⬇️ Download Files
|
||||
- Download `libagent.dll` `libinject.dll` `libloader.dll` `injector.exe` from [Release](https://github.com/xiaozhou233/JuiceAgent/releases) and put them in `YourDir`
|
||||
- Download JARs `JuiceAgent-API-X.X.X+build.X.jar` from [JuiceAgent-API Release] (https://github.com/xiaozhou233/JuiceAgent-API/releases) and put them in `YourDir`
|
||||
### 1. Download Files
|
||||
Download the following files from [Releases](https://github.com/xiaozhou233/JuiceAgent/releases) and place them in `YourDir`:
|
||||
|
||||
### 2. ✍️ Write Config File
|
||||
Create a config file named `config.toml` in `YourDir`
|
||||
- `libagent.dll`
|
||||
- `libinject.dll`
|
||||
- `libloader.dll`
|
||||
- `injector.exe`
|
||||
|
||||
Download `JuiceAgent-API-X.X.X+build.X.jar` from [JuiceAgent-API Releases](https://github.com/xiaozhou233/JuiceAgent-API/releases) and place it in `YourDir`.
|
||||
|
||||
### 2. Write the Config File
|
||||
Create a file named `config.toml` in `YourDir`.
|
||||
|
||||
```toml
|
||||
[JuiceAgent]
|
||||
@@ -56,74 +60,89 @@ EntryMethod = "start"
|
||||
InjectionDir = ""
|
||||
```
|
||||
|
||||
### 3. 💉 Run Injector
|
||||
#### **Method A** : Use `injector.exe`
|
||||
Run `injector.exe`
|
||||
```
|
||||
114514 loop
|
||||
### 3. Run the Injector
|
||||
|
||||
#### Method A: Use `injector.exe`
|
||||
Run `injector.exe` from `YourDir`.
|
||||
|
||||
```text
|
||||
<jps output>
|
||||
Input PID:
|
||||
```
|
||||
|
||||
Input PID of target process and press Enter
|
||||
Enter the PID of the target JVM process and press Enter.
|
||||
|
||||
#### Method B: Use JNI to call `inject`
|
||||
Create `cn/xiaozhou233/juiceagent/injector/InjectorNative.java` in your project:
|
||||
|
||||
#### **Method B** : Use JNI to call `inject` function
|
||||
Create a file named cn/xiaozhou233/juiceagent/injector.java in your project
|
||||
```java
|
||||
package cn.xiaozhou233.juiceagent.injector;
|
||||
|
||||
public class InjectorNative {
|
||||
/*
|
||||
* @param pid target process id
|
||||
*/
|
||||
public native boolean inject(int pid, String path);
|
||||
/*
|
||||
* @param pid target process id
|
||||
* @param path injection DLL path
|
||||
*/
|
||||
public native boolean inject(int pid, String path);
|
||||
|
||||
/*
|
||||
* @param pid target process id
|
||||
* @param path injection dll path
|
||||
* @param configPath config file path
|
||||
*/
|
||||
public native boolean inject(int pid, String path, String configPath);
|
||||
/*
|
||||
* @param pid target process id
|
||||
* @param path injection DLL path
|
||||
* @param configDir directory containing config.toml
|
||||
*/
|
||||
public native boolean inject(int pid, String path, String configDir);
|
||||
}
|
||||
```
|
||||
Then write it down somewhere
|
||||
|
||||
Example:
|
||||
|
||||
```java
|
||||
// Your class
|
||||
import cn.xiaozhou233.juiceagent.injector.InjectorNative;
|
||||
|
||||
System.load("<path-to-libinject>");
|
||||
InjectorNative injectorNative = new InjectorNative();
|
||||
|
||||
injectorNative.inject(<pid>, "<path-to-libloader>", "<path-to-toml-config-dir>");
|
||||
injectorNative.inject(<pid>, "<path-to-libloader>", "<path-to-config-directory>");
|
||||
```
|
||||
### 4. ✅ Done!
|
||||
if injection is successful, target process will load `Entry.jar` (or `EntryJarPath` in config file) and run `EntryClass`.`EntryMethod` method(or `start` method in config file)
|
||||
|
||||
## Hot To Build
|
||||
### Environment
|
||||
### 4. Done
|
||||
If the injection succeeds, the target process will load `Entry.jar` (or the file specified by `EntryJarPath`) and invoke `EntryClass.EntryMethod` (default: `com.example.Entry.start`).
|
||||
|
||||
## How To Build
|
||||
|
||||
### Requirements
|
||||
- Java 8+
|
||||
- MinGW
|
||||
- CMake
|
||||
- CMake 3.15+
|
||||
- Ninja
|
||||
```
|
||||
|
||||
### Build with CMake Presets
|
||||
```powershell
|
||||
git clone https://github.com/xiaozhou233/JuiceAgent.git
|
||||
cd JuiceAgent
|
||||
|
||||
mkdir -p build
|
||||
cmake --build build --
|
||||
cmake --preset ninja-release
|
||||
cmake --build build
|
||||
```
|
||||
|
||||
Dlls will be generated in `build/bin` directory
|
||||
### Manual Build
|
||||
```powershell
|
||||
git clone https://github.com/xiaozhou233/JuiceAgent.git
|
||||
cd JuiceAgent
|
||||
|
||||
Jars can be build or download in project [JuiceAgent-API](https://github.com/xiaozhou233/JuiceAgent-API)
|
||||
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release
|
||||
cmake --build build
|
||||
```
|
||||
|
||||
## Full Disclaimer/ 完整免责声明
|
||||
Disclaimer: This project is intended for learning and research in controlled environments only. The author is not responsible for any consequences arising from unauthorized use, modification, cracking, or violation of any applicable agreements or laws. Users are responsible for ensuring their actions are legal and compliant.
|
||||
The generated binaries will be placed in `build/bin`.
|
||||
|
||||
免责声明: 本项目仅建议在受控环境中用于学习与研究。任何利用本项目实施破解、篡改、未经授权获取源码或其他违反目标协议或法律的行为所导致的一切后果,作者概不负责。使用者应自行确保其行为合法合规,并对相应后果承担全部责任。
|
||||
`JuiceAgent-API.jar` can be built from or downloaded from [JuiceAgent-API](https://github.com/xiaozhou233/JuiceAgent-API).
|
||||
|
||||
## Full Disclaimer
|
||||
This project is intended for learning and research in controlled environments only. The author is not responsible for any consequences arising from unauthorized use, modification, cracking, or violation of any applicable agreements or laws. Users are responsible for ensuring that their actions are legal and compliant.
|
||||
|
||||
## Acknowledgements
|
||||
|
||||
- [ReflectiveDLLInjection](https://github.com/stephenfewer/ReflectiveDLLInjection) - DLL Inject
|
||||
- [plog](https://github.com/SergiusTheBest/plog) - portable, simple and extensible C++ logging library
|
||||
- [tinytoml](https://github.com/mayah/tinytoml) - A header only C++11 library for parsing TOML.
|
||||
- [eventpp](https://github.com/wqking/eventpp) - C++ library for event dispatcher and callback list
|
||||
- [ReflectiveDLLInjection](https://github.com/stephenfewer/ReflectiveDLLInjection) - DLL injection implementation
|
||||
- [plog](https://github.com/SergiusTheBest/plog) - Portable, simple, and extensible C++ logging library
|
||||
- [tinytoml](https://github.com/mayah/tinytoml) - Header-only C++11 TOML parser
|
||||
- [eventpp](https://github.com/wqking/eventpp) - C++ library for event dispatchers and callback lists
|
||||
+66
-96
@@ -1,59 +1,44 @@
|
||||
# JuiceAgent
|
||||
[English](https://github.com/xiaozhou233/JuiceAgent/blob/main/README.md) | [中文](https://github.com/xiaozhou233/JuiceAgent/blob/main/README_zh.md)
|
||||
|
||||
JuiceAgent —— 一个基于 JVMTI 的注入库,用于加载外部 JAR 并对 Java 字节码进行热替换,即使在 DisableAttachMechanism 启用的情况下也可使用。
|
||||
JuiceAgent 是一个基于 JVMTI 的注入库,用于加载外部 JAR 并对 Java 字节码进行转换;即使启用了 `DisableAttachMechanism` 也可以使用。
|
||||
|
||||
**注意:** 当前仅支持 **Windows**,未来计划支持 **Linux**。
|
||||
**注意:** 当前仅支持 **Windows**,未来可能会支持 **Linux**。
|
||||
|
||||
**注意:** **Version 2.3 Build 1** 是当前的 **稳定版本且已停止更新**。3.0 及之后版本已重构,可能存在未知问题。
|
||||
**注意:** 当前仓库版本为 **Version 3.1 Build 3**。
|
||||
|
||||
**警告:** 本项目仍处于实验阶段,不建议用于生产环境。
|
||||
**使用风险自负。**
|
||||
|
||||
---
|
||||
**警告:** 本项目仍处于实验阶段,不建议在生产环境中使用。使用风险请自行承担。
|
||||
|
||||
## 简介
|
||||
JuiceAgent 是一个基于 JVMTI 的原生注入框架,通过向目标 JVM 注入代码,实现动态加载外部 JAR 以及字节码热替换,无需使用 javaagent(在 DisableAttachMechanism=true 时仍可工作)。
|
||||
|
||||
---
|
||||
JuiceAgent 是一个基于 JVMTI 的原生注入框架。它通过向目标 JVM 注入原生代码,动态加载外部 JAR,并执行字节码重定义或重转换,而不需要 `javaagent`。在 `DisableAttachMechanism=true` 的情况下依然可以工作。
|
||||
|
||||
## 功能特性
|
||||
本项目是 JuiceAgent-API 的原生实现。API 定义可参考 [JuiceAgent.java](https://github.com/xiaozhou233/JuiceAgent-API/blob/master/src/main/java/cn/xiaozhou233/juiceagent/api/JuiceAgent.java)。
|
||||
|
||||
本项目是 JuiceAgent-API 的Native实现,详细内容请参考:
|
||||
https://github.com/xiaozhou233/JuiceAgent-API/blob/master/src/main/java/cn/xiaozhou233/juiceagent/api/JuiceAgent.java
|
||||
- `addToBootstrapClassLoaderSearch`
|
||||
- `addToSystemClassLoaderSearch`
|
||||
- `addToClassLoader`
|
||||
- `defineClass`
|
||||
- `redefineClass(ByName)`
|
||||
- `retransformClass(ByName)`
|
||||
- `getLoadedClasses`
|
||||
- `getClassByName`
|
||||
- `getClassBytes(ByName)`
|
||||
|
||||
- addToBootstrapClassLoaderSearch
|
||||
- addToSystemClassLoaderSearch
|
||||
- addToClassLoader
|
||||
- defineClass
|
||||
- redefineClass(ByName)
|
||||
- retransformClass(ByName)
|
||||
- getLoadedClasses
|
||||
- getClassByName
|
||||
- getClassBytes(ByName)
|
||||
## 使用方法
|
||||
|
||||
---
|
||||
### 1. 下载文件
|
||||
从 [Releases](https://github.com/xiaozhou233/JuiceAgent/releases) 下载以下文件,并放到 `YourDir` 中:
|
||||
|
||||
## 使用方法(适用于 Version 3.0 Build 2)
|
||||
- `libagent.dll`
|
||||
- `libinject.dll`
|
||||
- `libloader.dll`
|
||||
- `injector.exe`
|
||||
|
||||
### 1. ⬇️ 下载文件
|
||||
从 [JuiceAgent-API Releases](https://github.com/xiaozhou233/JuiceAgent-API/releases) 下载 `JuiceAgent-API-X.X.X+build.X.jar`,并放到 `YourDir` 中。
|
||||
|
||||
- 从 Release 下载:
|
||||
- libagent.dll
|
||||
- libinject.dll
|
||||
- libloader.dll
|
||||
- injector.exe
|
||||
并放入 `YourDir`
|
||||
|
||||
- 从 JuiceAgent-API Release 下载:
|
||||
- JuiceAgent-API-X.X.X+build.X.jar
|
||||
并放入 `YourDir`
|
||||
|
||||
---
|
||||
|
||||
### 2. ✍️ 编写配置文件
|
||||
|
||||
在 `YourDir` 下创建 `config.toml`
|
||||
### 2. 编写配置文件
|
||||
在 `YourDir` 下创建 `config.toml`:
|
||||
|
||||
```toml
|
||||
[JuiceAgent]
|
||||
@@ -75,43 +60,37 @@ EntryMethod = "start"
|
||||
InjectionDir = ""
|
||||
```
|
||||
|
||||
---
|
||||
### 3. 执行注入
|
||||
|
||||
### 3. 💉 执行注入
|
||||
#### 方法 A:使用 `injector.exe`
|
||||
在 `YourDir` 中运行 `injector.exe`。
|
||||
|
||||
#### 方法 A:使用 injector.exe
|
||||
|
||||
运行`inject.exe`:
|
||||
|
||||
```
|
||||
114514 loop
|
||||
```text
|
||||
<jps 输出>
|
||||
Input PID:
|
||||
```
|
||||
|
||||
输入目标进程 PID 后回车
|
||||
输入目标 JVM 进程的 PID 后按回车。
|
||||
|
||||
---
|
||||
|
||||
#### 方法 B:使用 JNI 调用 inject
|
||||
|
||||
创建文件:
|
||||
`cn/xiaozhou233/juiceagent/injector/InjectorNative.java`
|
||||
#### 方法 B:通过 JNI 调用 `inject`
|
||||
在你的项目中创建 `cn/xiaozhou233/juiceagent/injector/InjectorNative.java`:
|
||||
|
||||
```java
|
||||
package cn.xiaozhou233.juiceagent.injector;
|
||||
|
||||
public class InjectorNative {
|
||||
/*
|
||||
* @param pid target process id
|
||||
*/
|
||||
public native boolean inject(int pid, String path);
|
||||
/*
|
||||
* @param pid target process id
|
||||
* @param path injection DLL path
|
||||
*/
|
||||
public native boolean inject(int pid, String path);
|
||||
|
||||
/*
|
||||
* @param pid target process id
|
||||
* @param path injection dll path
|
||||
* @param configPath config file path
|
||||
*/
|
||||
public native boolean inject(int pid, String path, String configPath);
|
||||
/*
|
||||
* @param pid target process id
|
||||
* @param path injection DLL path
|
||||
* @param configDir 包含 config.toml 的目录
|
||||
*/
|
||||
public native boolean inject(int pid, String path, String configDir);
|
||||
}
|
||||
```
|
||||
|
||||
@@ -123,56 +102,47 @@ import cn.xiaozhou233.juiceagent.injector.InjectorNative;
|
||||
System.load("<path-to-libinject>");
|
||||
InjectorNative injectorNative = new InjectorNative();
|
||||
|
||||
injectorNative.inject(<pid>, "<path-to-libloader>", "<path-to-toml-config-dir>");
|
||||
injectorNative.inject(<pid>, "<path-to-libloader>", "<path-to-config-directory>");
|
||||
```
|
||||
|
||||
---
|
||||
### 4. 完成
|
||||
如果注入成功,目标进程会加载 `Entry.jar`(或 `EntryJarPath` 指定的文件),并调用 `EntryClass.EntryMethod`(默认值为 `com.example.Entry.start`)。
|
||||
|
||||
### 4. ✅ 完成
|
||||
|
||||
注入成功后,目标进程将加载:
|
||||
|
||||
- Entry.jar(或配置中的 EntryJarPath)
|
||||
- 并执行 EntryClass.EntryMethod(默认 com.example.Entry.start() )
|
||||
|
||||
---
|
||||
|
||||
## 如何构建
|
||||
## 构建方法
|
||||
|
||||
### 环境要求
|
||||
|
||||
- Java 8+
|
||||
- MinGW
|
||||
- CMake
|
||||
- CMake 3.15+
|
||||
- Ninja
|
||||
|
||||
```bash
|
||||
### 使用 CMake Presets 构建
|
||||
```powershell
|
||||
git clone https://github.com/xiaozhou233/JuiceAgent.git
|
||||
cd JuiceAgent
|
||||
|
||||
mkdir -p build
|
||||
cmake --build build --
|
||||
cmake --preset ninja-release
|
||||
cmake --build build
|
||||
```
|
||||
|
||||
生成的 DLL 位于:
|
||||
### 手动构建
|
||||
```powershell
|
||||
git clone https://github.com/xiaozhou233/JuiceAgent.git
|
||||
cd JuiceAgent
|
||||
|
||||
```
|
||||
build/bin
|
||||
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release
|
||||
cmake --build build
|
||||
```
|
||||
|
||||
JAR 可在 JuiceAgent-API 项目中构建或下载。
|
||||
生成的二进制文件位于 `build/bin`。
|
||||
|
||||
---
|
||||
`JuiceAgent-API.jar` 可以在 [JuiceAgent-API](https://github.com/xiaozhou233/JuiceAgent-API) 项目中构建,或直接从其发布页下载。
|
||||
|
||||
## 完整免责声明
|
||||
|
||||
免责声明:本项目仅建议在受控环境中用于学习与研究。作者不对任何因滥用、破解、修改或违反协议/法律行为所产生的后果负责。使用者需自行确保合法合规,并承担全部责任。
|
||||
|
||||
---
|
||||
本项目仅用于受控环境中的学习与研究。对于任何未经授权的使用、修改、破解,或违反适用协议与法律的行为所产生的后果,作者概不负责。使用者应自行确保其行为合法合规,并承担全部责任。
|
||||
|
||||
## 致谢
|
||||
|
||||
- ReflectiveDLLInjection — DLL 注入实现
|
||||
- plog — 轻量级 C++ 日志库
|
||||
- tinytoml — C++11 TOML 解析库
|
||||
- [eventpp](https://github.com/wqking/eventpp) - 用于事件分发器和回调列表的 C++ 库
|
||||
- [ReflectiveDLLInjection](https://github.com/stephenfewer/ReflectiveDLLInjection) - DLL 注入实现
|
||||
- [plog](https://github.com/SergiusTheBest/plog) - 轻量、可扩展的 C++ 日志库
|
||||
- [tinytoml](https://github.com/mayah/tinytoml) - Header-only C++11 TOML 解析库
|
||||
- [eventpp](https://github.com/wqking/eventpp) - 用于事件分发和回调列表的 C++ 库
|
||||
@@ -0,0 +1,17 @@
|
||||
# TODO - 开发待办事项
|
||||
这个文档用来记录开发时的待办事项以及以后的开发计划。
|
||||
---
|
||||
|
||||
## 2026-04-16
|
||||
- [ ] ~~把读取toml的库换成支持toml 1.0的库~~
|
||||
- [x] 在 `includes/JuiceAgent` 文件夹下实现 `Config.hpp` 配置系统
|
||||
- [ ] ~~新建测试 读取配置文件 (放弃)~~
|
||||
|
||||
- [x] 重构 `libloader` 的配置系统
|
||||
|
||||
## 2026-04-17
|
||||
- [ ] 重构项目为模块化项目
|
||||
- [x] `libloader` 模块只负责加载`JuiceAgent-API`和`JuiceAgent`, Jar文件由`JuiceAgent`接管
|
||||
- [x] `libloader` 读取配置文件并传参`JuiceAgentNativePath`和`ConfigDir`到 `JuiceAgent` 中,由`JuiceAgent`读取配置文件继续进行操作
|
||||
- [x] 替换toml库
|
||||
- [x] 重构 `libloader` 的配置系统
|
||||
+1
-1
@@ -1 +1 @@
|
||||
VERSION = Version 3.1 Build 3
|
||||
VERSION = Version 3.2 Build 1 PreRelease
|
||||
@@ -0,0 +1,184 @@
|
||||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <JuiceAgent/Logger.hpp>
|
||||
#include <toml.hpp>
|
||||
|
||||
namespace JuiceAgent::Config {
|
||||
|
||||
class Config {
|
||||
|
||||
private:
|
||||
std::filesystem::path config_dir_;
|
||||
toml::value data_;
|
||||
bool valid_ = false;
|
||||
|
||||
private:
|
||||
// Split "A.B.C" -> {"A", "B", "C"}
|
||||
static std::vector<std::string> split_path(const std::string& path) {
|
||||
std::vector<std::string> result;
|
||||
std::stringstream stream(path);
|
||||
std::string token;
|
||||
|
||||
while (std::getline(stream, token, '.')) {
|
||||
if (!token.empty()) {
|
||||
result.push_back(token);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Find node by dot path
|
||||
const toml::value* find(const std::string& path) const {
|
||||
const toml::value* current = &data_;
|
||||
|
||||
for (const auto& key : split_path(path)) {
|
||||
if (!current->is_table()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!current->contains(key)) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
current = ¤t->at(key);
|
||||
}
|
||||
|
||||
return current;
|
||||
}
|
||||
|
||||
// Load and parse config file
|
||||
void load() {
|
||||
if (!std::filesystem::exists(config_dir_)) {
|
||||
PLOGE << "Config directory not found: " << config_dir_;
|
||||
config_dir_ = std::filesystem::current_path();
|
||||
PLOGW << "Using runtime directory: " << std::filesystem::current_path();
|
||||
}
|
||||
|
||||
const auto path = config_dir_ / "config.toml";
|
||||
|
||||
if (!std::filesystem::exists(path)) {
|
||||
PLOGE << "Config file not found: " << path;
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
data_ = toml::parse(path);
|
||||
valid_ = true;
|
||||
} catch (const std::exception& e) {
|
||||
PLOGE << "Failed to parse config: " << e.what();
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
explicit Config(std::filesystem::path dir)
|
||||
: config_dir_(std::move(dir)) {
|
||||
load();
|
||||
}
|
||||
|
||||
public:
|
||||
bool is_valid() const {
|
||||
return valid_;
|
||||
}
|
||||
|
||||
bool contains(const std::string& path) const {
|
||||
return find(path) != nullptr;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T get(const std::string& path) const {
|
||||
const auto* node = find(path);
|
||||
|
||||
if (!node) {
|
||||
throw std::runtime_error("Config key not found: " + path);
|
||||
}
|
||||
|
||||
return toml::get<T>(*node);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T get_or(const std::string& path, const T& default_value) const {
|
||||
const auto* node = find(path);
|
||||
|
||||
if (!node) {
|
||||
return default_value;
|
||||
}
|
||||
|
||||
try {
|
||||
auto result = toml::get<T>(*node);
|
||||
if (result.empty())
|
||||
return default_value;
|
||||
return result;
|
||||
} catch (...) {
|
||||
return default_value;
|
||||
}
|
||||
}
|
||||
|
||||
std::string get_path_or_default(const std::string& path, const std::string& default_value) const {
|
||||
std::string result = get_or<std::string>(path, default_value);
|
||||
if(result.starts_with(".")) {
|
||||
result.erase(0, 1);
|
||||
|
||||
// Check if string starts with '/'
|
||||
// MUST REMOVE '/', or it will cause path error (root directory)
|
||||
if (result.starts_with('/')) {
|
||||
result.erase(0, 1);
|
||||
}
|
||||
|
||||
result = (config_dir_ / result).string();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
InjectionInfo get_injection_info() const {
|
||||
InjectionInfo info;
|
||||
|
||||
auto get_default_path = [&](const std::string& name) {
|
||||
return (std::filesystem::path(config_dir_) / name).string();
|
||||
};
|
||||
|
||||
info.JuiceAgentAPIJarPath = get_path_or_default("Loader.JuiceAgentAPIJarPath", get_default_path("JuiceAgentAPI.jar"));
|
||||
info.JuiceAgentNativeLibraryPath = get_path_or_default("Loader.JuiceAgentNativeLibraryPath", get_default_path("libagent.dll"));
|
||||
info.ConfigDir = config_dir_.string();
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
static std::string serialize(InjectionInfo info) {
|
||||
|
||||
auto escape = [](const std::string& value) -> std::string {
|
||||
std::string result;
|
||||
result.reserve(value.size());
|
||||
|
||||
for (char ch : value) {
|
||||
if (ch == ';' || ch == '=' || ch == '\\') {
|
||||
result += '\\';
|
||||
}
|
||||
result += ch;
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
return
|
||||
"JuiceAgentAPIJarPath=" + escape(info.JuiceAgentAPIJarPath) + ";" +
|
||||
"JuiceAgentNativeLibraryPath=" + escape(info.JuiceAgentNativeLibraryPath) + ";" +
|
||||
"ConfigDir=" + escape(info.ConfigDir);
|
||||
}
|
||||
};
|
||||
|
||||
void print_injection_info(const InjectionInfo& info) {
|
||||
|
||||
PLOGI << "JuiceAgentAPIJarPath: " << info.JuiceAgentAPIJarPath;
|
||||
PLOGI << "JuiceAgentNativeLibraryPath: " << info.JuiceAgentNativeLibraryPath;
|
||||
PLOGI << "ConfigDir: " << info.ConfigDir;
|
||||
}
|
||||
|
||||
|
||||
} // namespace JuiceAgent::Config
|
||||
@@ -4,11 +4,7 @@
|
||||
|
||||
struct InjectionInfo {
|
||||
std::string JuiceAgentAPIJarPath;
|
||||
std::string JuiceAgentNativePath;
|
||||
std::string JuiceAgentNativeLibraryPath;
|
||||
|
||||
std::string EntryJarPath;
|
||||
std::string EntryClass;
|
||||
std::string EntryMethod;
|
||||
|
||||
std::string InjectionDir;
|
||||
std::string ConfigDir;
|
||||
};
|
||||
File diff suppressed because it is too large.
Load diff
+18162
File diff suppressed because it is too large.
Load diff
@@ -1,23 +0,0 @@
|
||||
Copyright (c) 2014, MAYAH
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2017 Toru Niina
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
@@ -1,6 +1,6 @@
|
||||
#include <JuiceAgent/Logger.hpp>
|
||||
#include <libloader.hpp>
|
||||
#include <config.hpp>
|
||||
#include <JuiceAgent/Config.hpp>
|
||||
#include <jvm.hpp>
|
||||
|
||||
namespace libloader {
|
||||
@@ -54,7 +54,7 @@ bool invoke_juiceagent_init(JNIEnv* env, const InjectionInfo& info) {
|
||||
}
|
||||
|
||||
// Serialize InjectionInfo to string
|
||||
std::string args = config::Config::serialize(info);
|
||||
std::string args = JuiceAgent::Config::Config::serialize(info);
|
||||
PLOGD << "Invoke args: " << args;
|
||||
|
||||
// Create Java string
|
||||
@@ -83,20 +83,21 @@ void entrypoint(const char* runtime_dir) {
|
||||
}
|
||||
|
||||
// Load configuration
|
||||
config::Config cfg(runtime_dir);
|
||||
JuiceAgent::Config::Config cfg(runtime_dir);
|
||||
if (!cfg.is_valid()) {
|
||||
PLOGW << "Invalid config, using default values";
|
||||
}
|
||||
|
||||
PLOGI << cfg.get_path_or_default("Loader.JuiceAgentAPIJarPath", "NONE");
|
||||
// Get InjectionInfo
|
||||
InjectionInfo info = cfg.get_injection_info();
|
||||
config::print_injection_info(info);
|
||||
JuiceAgent::Config::print_injection_info(info);
|
||||
|
||||
// Validate essential paths
|
||||
if (info.JuiceAgentAPIJarPath.empty()) {
|
||||
PLOGE << "JuiceAgentAPIJarPath is empty, abort injection";
|
||||
return;
|
||||
}
|
||||
if (info.JuiceAgentAPIJarPath.empty()) {
|
||||
PLOGE << "JuiceAgentAPIJarPath is empty, abort injection";
|
||||
return;
|
||||
}
|
||||
|
||||
// Attach to JVM
|
||||
jvm::Jvm jvm;
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
namespace config {
|
||||
namespace JuiceAgent::Config {
|
||||
|
||||
class Config {
|
||||
private:
|
||||
Reference in new issue
Block a user