-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrequest.cpp
More file actions
159 lines (131 loc) · 3.79 KB
/
request.cpp
File metadata and controls
159 lines (131 loc) · 3.79 KB
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
158
159
#include <iostream>
#include <jsoncpp/json/json.h>
#include "functions/functions.h"
#include "functions/files.h"
#include "crypt.h"
// ---------------------------------------------------------------------
/**
* Возвращает true в случае успеха, либо false в случае ошибки.
*/
bool request_processing(Json::Value & jroot, Json::Value & jsend)
{
if (jroot["type"].asString() == "commands") {
// Send commands
std::string sendcmd;
int jsize = jroot["commands"].size();
std::vector<std::string> command_results(jsize);
for (int i = 0; i < jsize; i++) {
sendcmd = jroot["commands"][i].asString();
// Replace
#ifdef WIN32
sendcmd = str_replace("%PROGRAMFILES%", "C:\\Programm Files", sendcmd);
sendcmd = str_replace("%WINDIR%", "C:\\Windows", sendcmd);
#endif
command_results[i] = exec(sendcmd);
std::cout << "Command exec: " << jroot["commands"][i].asString() << std::endl;
std::cout << "Result: " << command_results[i] << std::endl;
std::cout << "Result size: " << command_results[i].size() << std::endl;
jsend["command_results"][i] = command_results[i];
}
jsend["status"] = 10;
sendcmd.clear();
}
else if (jroot["type"].asString() == "read_dir") {
// Read directory
std::vector<std::string> files;
if (getdir(jroot["dir"].asString(), files) == -1) {
// Read failed
jsend["status"] = 31;
return 0;
}
for (unsigned int i = 2; i < files.size();i++) {
// File name
jsend["list"][i-2][0] = files[i];
// File mtime
jsend["list"][i-2][1] = filemtime(jroot["dir"].asString() + '/' + files[i]);
// File size
jsend["list"][i-2][2] = filesize(jroot["dir"].asString() + '/' + files[i]);
// Is dir?
jsend["list"][i-2][3] = is_dir(jroot["dir"].asString() + '/' + files[i]);
}
jsend["status"] = 10;
}
else if (jroot["type"].asString() == "read_file") {
// Read file
if (!file_exists(jroot["file"].asString())) {
jsend["status"] = 3;
return 0;
}
// 1Mb
if (filesize(jroot["file"].asString()) > 1000000) {
// File big
jsend["status"] = 42;
return 0;
}
jsend["contents"] = base64_encode(file_get_contents(jroot["file"].asString()));
jsend["filesize"] = std::to_string(jsend["contents"].asString().size());
jsend["status"] = 10;
}
else if (jroot["type"].asString() == "write_file") {
// Write file
if (file_put_contents(jroot["file"].asString(), base64_decode(jroot["contents"].asString()))) {
jsend["status"] = 10;
}
else {
jsend["status"] = 51;
}
}
else if (jroot["type"].asString() == "mkdir") {
if (make_dir(jroot["dir"].asString(), jroot["permissions"].asString())) {
jsend["status"] = 10;
}
else {
// Error make
jsend["status"] = 61;
}
}
else if (jroot["type"].asString() == "move") {
if (!file_exists(jroot["old_file"].asString())) {
jsend["status"] = 3;
return 0;
}
if (!rename(jroot["old_file"].asString().c_str(), jroot["new_file"].asString().c_str())) {
// Success
jsend["status"] = 10;
}
else {
jsend["status"] = 71;
}
}
else if (jroot["type"].asString() == "file_size") {
// File size
if (!file_exists(jroot["file"].asString())) {
jsend["status"] = 3;
return 0;
}
jsend["filesize"] = filesize(jroot["file"].asString());
jsend["status"] = 10;
}
else if (jroot["type"].asString() == "remove") {
// File delete
if (!file_exists(jroot["file"].asString())) {
jsend["status"] = 3;
return 0;
}
remove(jroot["file"].asString().c_str());
jsend["status"] = 10;
}
else if (jroot["type"].asString() == "install") {
// Install game server
jsend["status"] = 10;
}
else if (jroot["type"].asString() == "get_stats") {
// Get CPU and RAM stats
jsend["status"] = 10;
}
else {
// Unknown type
jsend["status"] = 1;
}
return 1;
}