mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 13:05:53 +08:00
[Refactor] Utils
This commit is contained in:
1 parent
ad9d627175
commit
2df1e4c80f
6 files changed
+97
-31
No files matched your search
@@ -7,4 +7,13 @@ typedef struct InjectParameters{
|
||||
char loaderDir[MAX_PATH];
|
||||
} InjectParameters;
|
||||
|
||||
/*
|
||||
typedef struct InjectParameters{
|
||||
char *JuiceLoaderJarPath;
|
||||
// Optional:
|
||||
char *JuiceLoaderLibPath;
|
||||
// Optional:
|
||||
char *EntryJarPath;
|
||||
} InjectParameters;
|
||||
*/
|
||||
#endif
|
||||
+24
-23
@@ -6,6 +6,7 @@
|
||||
#include "jvmti.h"
|
||||
#include "InjectParameters.h"
|
||||
#include "log.h"
|
||||
#include "GlobalUtils.h"
|
||||
|
||||
#define WIN_X64
|
||||
extern HINSTANCE hAppInstance;
|
||||
@@ -44,38 +45,38 @@ DWORD WINAPI ThreadProc(LPVOID lpParam) {
|
||||
/// ======== Check Environment ======== ///
|
||||
|
||||
// Init vars
|
||||
JavaVM *jvm;
|
||||
JNIEnv *env;
|
||||
|
||||
// Get JavaVM
|
||||
if (JNI_GetCreatedJavaVMs(&jvm, 1, NULL) != JNI_OK || !jvm) {
|
||||
log_error("[%s] JNI_GetCreatedJavaVMs failed", LOGNAME);
|
||||
MessageBoxA(NULL, "JNI_GetCreatedJavaVMs failed", "Error", MB_OK);
|
||||
} else {
|
||||
log_trace("[%s] JNI_GetCreatedJavaVMs success", LOGNAME);
|
||||
}
|
||||
|
||||
// Get JNIEnv
|
||||
if ((*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL) != JNI_OK) {
|
||||
log_error("[%s] AttachCurrentThread failed", LOGNAME);
|
||||
MessageBoxA(NULL, "Failed to attach current thread to JVM!", "Error", MB_ICONERROR);
|
||||
jint result = JNI_ERR;
|
||||
JavaVM *jvm = NULL;
|
||||
|
||||
result = GetCreatedJVM(&jvm);
|
||||
if (result != JNI_OK) {
|
||||
log_error("[%s] Failed to get JVM (%d)", LOGNAME, result);
|
||||
MessageBoxA(NULL, "Failed to get JVM", "Error", MB_OK);
|
||||
return 1;
|
||||
} else {
|
||||
log_trace("[%s] AttachCurrentThread success", LOGNAME);
|
||||
}
|
||||
log_info("[%s] Got JVM", LOGNAME);
|
||||
|
||||
JNIEnv *env = NULL;
|
||||
result = GetJNIEnv(jvm, &env);
|
||||
if (result != JNI_OK) {
|
||||
log_error("[%s] Failed to get JNIEnv (%d)", LOGNAME, result);
|
||||
MessageBoxA(NULL, "Failed to get JNIEnv", "Error", MB_OK);
|
||||
return 1;
|
||||
}
|
||||
log_info("[%s] Got JNIEnv", LOGNAME);
|
||||
|
||||
|
||||
// Debug: JVM version
|
||||
jint version = (*env)->GetVersion(env);
|
||||
log_trace("[%s] JVM version: 0x%x", LOGNAME, version);
|
||||
|
||||
// Init and enable jvmti
|
||||
jvmtiEnv *jvmti;
|
||||
jint res = (*jvm)->GetEnv(jvm, (void**)&jvmti, JVMTI_VERSION_1_2);
|
||||
if (res != JNI_OK || jvmti == NULL) {
|
||||
log_error("[%s] GetEnv failed", LOGNAME);
|
||||
jvmtiEnv *jvmti = NULL;
|
||||
result = GetJVMTI(jvm, &jvmti);
|
||||
if (result != JNI_OK) {
|
||||
log_error("[%s] Failed to get JVMTI (%d)", LOGNAME, result);
|
||||
MessageBoxA(NULL, "Failed to get JVMTI", "Error", MB_OK);
|
||||
return 1;
|
||||
} else {
|
||||
log_trace("[%s] GetEnv success", LOGNAME);
|
||||
}
|
||||
log_info("[%s] enabled jvmti", LOGNAME);
|
||||
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
# src/utils/CMakeLists.txt
|
||||
|
||||
add_library(utils STATIC
|
||||
test.c)
|
||||
JVMUtils.c)
|
||||
|
||||
target_include_directories(utils PUBLIC ${CMAKE_CURRENT_LIST_DIR})
|
||||
target_include_directories(utils PUBLIC
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
${CMAKE_SOURCE_DIR}/includes
|
||||
${CMAKE_SOURCE_DIR}/includes/jvm
|
||||
${CMAKE_SOURCE_DIR}/includes/jvm/win32)
|
||||
|
||||
target_link_libraries(utils PRIVATE
|
||||
jvm)
|
||||
@@ -1,8 +1,13 @@
|
||||
#ifndef GLOBALUTILS_H
|
||||
#define GLOBALUTILS_H
|
||||
|
||||
#include "windows.h"
|
||||
#include "stdio.h"
|
||||
#include "jni.h"
|
||||
#include "jvmti.h"
|
||||
|
||||
void test();
|
||||
jint GetCreatedJVM(JavaVM**);
|
||||
jint GetJNIEnv(JavaVM*, JNIEnv**);
|
||||
jint GetJVMTI(JavaVM*, jvmtiEnv**);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,49 @@
|
||||
#include "./GlobalUtils.h"
|
||||
|
||||
// Get JavaVM
|
||||
jint GetCreatedJVM(JavaVM **out_jvm) {
|
||||
if (!out_jvm) return JNI_ERR;
|
||||
*out_jvm = NULL;
|
||||
|
||||
jint res = JNI_GetCreatedJavaVMs(out_jvm, 1, NULL);
|
||||
if (res != JNI_OK || *out_jvm == NULL) {
|
||||
MessageBoxA(NULL, "JNI_GetCreatedJavaVMs failed", "Error", MB_OK);
|
||||
return res;
|
||||
}
|
||||
return JNI_OK;
|
||||
}
|
||||
|
||||
// Get JNIEnv
|
||||
jint GetJNIEnv(JavaVM *jvm, JNIEnv **out_env) {
|
||||
if (!jvm || !out_env) return JNI_ERR;
|
||||
*out_env = NULL;
|
||||
|
||||
void *env = NULL;
|
||||
jint res = (*jvm)->GetEnv(jvm, &env, JNI_VERSION_1_8);
|
||||
if (res == JNI_OK && env != NULL) {
|
||||
*out_env = (JNIEnv *)env;
|
||||
return JNI_OK;
|
||||
}
|
||||
|
||||
// Attach current thread
|
||||
res = (*jvm)->AttachCurrentThread(jvm, &env, NULL);
|
||||
if (res != JNI_OK || env == NULL) {
|
||||
return res;
|
||||
}
|
||||
*out_env = (JNIEnv *)env;
|
||||
return JNI_OK;
|
||||
}
|
||||
|
||||
// Get JVMTI
|
||||
jint GetJVMTI(JavaVM *jvm, jvmtiEnv **out_jvmti) {
|
||||
if (!jvm || !out_jvmti) return JNI_ERR;
|
||||
*out_jvmti = NULL;
|
||||
|
||||
jint res = (*jvm)->GetEnv(jvm, (void**)out_jvmti, JVMTI_VERSION_1_2);
|
||||
if (res == JNI_OK && *out_jvmti != NULL) {
|
||||
return JNI_OK;
|
||||
}
|
||||
|
||||
*out_jvmti = NULL;
|
||||
return res;
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
#include "GlobalUtils.h"
|
||||
|
||||
void test() {
|
||||
printf("Hello World!\n");
|
||||
}
|
||||
Reference in new issue
Block a user