diff --git a/includes/JuiceAgent/JuiceAgent.h b/includes/JuiceAgent/JuiceAgent.h index 18fa729..5d94747 100644 --- a/includes/JuiceAgent/JuiceAgent.h +++ b/includes/JuiceAgent/JuiceAgent.h @@ -4,13 +4,14 @@ #define INJECT_PATH_MAX 260 typedef struct _InjectionInfo { - char BootstrapAPIPath[INJECT_PATH_MAX]; - char JuiceLoaderJarPath[INJECT_PATH_MAX]; + char JuiceAgentAPIJarPath[INJECT_PATH_MAX]; + char JuiceAgentNativePath[INJECT_PATH_MAX]; + char EntryJarPath[INJECT_PATH_MAX]; char EntryClass[INJECT_PATH_MAX]; char EntryMethod[INJECT_PATH_MAX]; + char InjectionDir[INJECT_PATH_MAX]; - char JuiceLoaderLibraryPath[INJECT_PATH_MAX]; } InjectionInfoType; extern InjectionInfoType InjectionInfo; diff --git a/src/libloader/common/config.cpp b/src/libloader/common/config.cpp new file mode 100644 index 0000000..8bc8e61 --- /dev/null +++ b/src/libloader/common/config.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include +#include +#include + +class config +{ +private: + const char* runtime_dir; + std::string config_path; + std::ifstream config_file; + toml::Value config_values; + InjectionInfoType injection_info; +public: + config(const char* runtime_dir); + ~config(); + + std::string get_or_default(const char* key, const char* default_value); + InjectionInfoType get_injection_info(); + void printInjectionInfo(); +}; + +config::config(const char* runtime_dir) { + // config file path + std::string config_path = std::string(runtime_dir) + "/config.toml"; + + this->runtime_dir = runtime_dir; + this->config_path = config_path; + + // read config file + this->config_file.open(config_path); + if (!this->config_file) + { + PLOGE << "Failed to open config file: " << config_path; + } + + // Parse config file + toml::ParseResult pr = toml::parse(this->config_file); + if (!pr.valid()) + { + PLOGE << "Failed to parse config file: " << config_path; + return; + } + + this->config_values = pr.value; +} + +std::string config::get_or_default(const char* key, const char* default_value) { + const toml::Value* value = this->config_values.find(key); + + // key exists and is string + if (value && value->is()) { + std::string result = value->as(); + + if (!result.empty()) { + PLOGD << "Found parameter: " << key << " = " << result; + return result; + } + + PLOGD << "Parameter " << key << " is empty, use default: " << default_value; + return std::string(default_value); + } + + // key not found + PLOGD << "Not found parameter: " << key << ", use default: " << default_value; + return std::string(default_value); +} + +InjectionInfoType config::get_injection_info() { + this->injection_info = {}; + + auto copy_from_config = [this](char* dst, size_t dst_size, const char* key, const char* default_value) { + std::string value = this->get_or_default(key, default_value); + strncpy_s(dst, dst_size, value.c_str(), _TRUNCATE); + }; + + copy_from_config(this->injection_info.JuiceAgentAPIJarPath, sizeof(injection_info.JuiceAgentAPIJarPath), "JuiceAgentAPIJarPath", ""); + copy_from_config(this->injection_info.JuiceAgentNativePath, sizeof(injection_info.JuiceAgentNativePath), "JuiceAgentNativePath", ""); + copy_from_config(this->injection_info.EntryJarPath, sizeof(injection_info.EntryJarPath), "EntryJarPath", ""); + copy_from_config(this->injection_info.EntryClass, sizeof(injection_info.EntryClass), "EntryClass", ""); + copy_from_config(this->injection_info.EntryMethod, sizeof(injection_info.EntryMethod), "EntryMethod", ""); + copy_from_config(this->injection_info.InjectionDir, sizeof(injection_info.InjectionDir), "InjectionDir", this->runtime_dir); + + return injection_info; +} + +void config::printInjectionInfo() { + PLOGI << "Injection Info:"; + PLOGI << "JuiceAgentAPIJarPath: " << this->injection_info.JuiceAgentAPIJarPath; + PLOGI << "JuiceAgentNativePath: " << this->injection_info.JuiceAgentNativePath; + PLOGI << "EntryJarPath: " << this->injection_info.EntryJarPath; + PLOGI << "EntryClass: " << this->injection_info.EntryClass; + PLOGI << "EntryMethod: " << this->injection_info.EntryMethod; + PLOGI << "InjectionDir: " << this->injection_info.InjectionDir; +} \ No newline at end of file diff --git a/src/libloader/common/libloader.cpp b/src/libloader/common/libloader.cpp.temp similarity index 100% rename from src/libloader/common/libloader.cpp rename to src/libloader/common/libloader.cpp.temp