-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetifeBasicLib.cpp
157 lines (124 loc) · 4.45 KB
/
NetifeBasicLib.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//
// Created by Administrator on 2023/5/2.
//
#include "NetifeBasicLib.h"
#include "TextHelper.h"
#include <Poco/JSON/Parser.h>
#include <bitset>
#include <fstream>
#include "Poco/UUID.h"
#include "Poco/UUIDGenerator.h"
using Poco::UUID;
using Poco::UUIDGenerator;
NetifeBasicLib::~NetifeBasicLib() {}
std::string NetifeBasicLib::GetName() const{
return "NetifeBasicLib";
}
std::string NetifeBasicLib::GetVersion() const{
return "1.0.0";
}
void NetifeBasicLib::OnLoaded() {
agent->LogInfo("NetifeBasicLib loaded success!");
agent->LogInfo("NetifeBasicLib is a basic component of Netife, it is updated without bound of Netife Primary Component, please "
"check update at ");
}
bool NetifeBasicLib::OnEnable() {
return true;
}
bool NetifeBasicLib::OnDisable() {
return true;
}
void NetifeBasicLib::OnExiting() {
agent->LogInfo("NetifeBasicLib is trying to clear the cache of shared lib...");
}
std::string
NetifeBasicLib::DispatcherCommand(std::string commandHead, std::map<std::string, std::optional<std::string>> params) {
if (commandHead == "parserRawHtml"){
return ParserRawHtml(params["rawHtml"].value());
}
if (commandHead == "parserHexString"){
return ParserHexString(params["content"].value());
}
if (commandHead == "parserStringHex"){
return ParserHexString(params["content"].value());
}
if (commandHead == "writeHexFile"){
return WriteHexFile(params["content"].value());
}
if (commandHead == "generateUUID"){
return GenerateUUID(params["content"]);
}
if (commandHead == "getRawRequestByUUID"){
return GetRawRequestByUUID(params["UUID"].value(), params["uuid_sub"]);
}
return "";
}
std::string NetifeBasicLib::ParserRawHtml(const string &rawHtml) {
vector<string> htmlArr = Netife::TextHelper::splitMulti(rawHtml, "\r\n\r\n");
Poco::JSON::Object json;
if (htmlArr.size() == 2){
json.set(std::string("dataBody"), htmlArr[1]);
}
vector<string> htmlHeadLine = Netife::TextHelper::splitMulti(htmlArr[0],"\r\n");
json.set(string("status"), htmlHeadLine[0]);
for (int i = 1; i < htmlHeadLine.size(); ++i) {
vector<string> temp = Netife::TextHelper::splitMulti(htmlHeadLine[i], ":");
json.set(string("head::") + temp[0],temp[1]);
}
std::ostringstream oss;
json.stringify(oss);
return oss.str();
}
std::string NetifeBasicLib::ParserHexString(std::string content) {
std::string result;
for (size_t i = 0; i < content.length(); i += 2) {
std::string byte = content.substr(i, 2);
char chr = (char) (int) strtol(byte.c_str(), nullptr, 16);
result.push_back(chr);
}
return result;
}
std::string NetifeBasicLib::ParserStringHex(std::string content) {
const std::string hex = "0123456789ABCDEF";
std::stringstream ss;
for (std::string::size_type i = 0; i < content.size(); ++i)
ss << hex[(unsigned char)content[i] >> 4] << hex[(unsigned char)content[i] & 0xf];
std::cout << ss.str() << std::endl;
return ss.str();
}
std::string NetifeBasicLib::WriteHexFile(std::string content) {
std::bitset<8> bits;
UUIDGenerator& generator = UUIDGenerator::defaultGenerator();
UUID uuid1(generator.create());
std::string name = uuid1.toString();
std::filesystem::path rootPath(agent->GetPluginDataPath());
std::ofstream ofs( (rootPath / "fileStreamPath" / (name + ".temp")).string(), std::ios::binary);
for (size_t i = 0; i < content.size(); i += 2)
{
bits = std::bitset<8>(content.substr(i, 2));
auto output = static_cast<unsigned char>(bits.to_ulong());
ofs.write((char*)(&output), sizeof(output));
}
ofs.close();
return (rootPath / "fileStreamPath" / (name + ".temp")).string();
}
std::string NetifeBasicLib::GenerateUUID(optional<std::string> content) {
UUIDGenerator& generator = UUIDGenerator::defaultGenerator();
if (content.has_value()){
UUID uuid(generator.createFromName(UUID::uri(), content.value()));
return uuid.toString();
}else{
UUID uuid(generator.create());
return uuid.toString();
}
}
std::string NetifeBasicLib::GetRawRequestByUUID(std::string UUID, optional<std::string> uuid_sub) {
auto res = agent->GetRawTextByUUID(UUID, uuid_sub);
if (res.has_value()){
return res.value()[0];
}
return "No Result";
}
POCO_BEGIN_MANIFEST(NetifePlugins)
POCO_EXPORT_CLASS(NetifeBasicLib)
POCO_END_MANIFEST