/
rpc-server.cpp
176 lines (167 loc) · 5.84 KB
/
rpc-server.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "rpc-server.hpp"
#include <base/llog.h>
namespace slp {
namespace rpc {
std::string stringReplace(std::string str, std::string src, std::string dst) {
std::string::size_type pos = str.find(src, 0);
auto srcLen = src.size();
auto dstLen = dst.size();
while (pos != std::string::npos) {
str.replace(pos, srcLen, dst);
pos = str.find(src, pos + dstLen);
}
return str;
}
std::string escape(std::string value) {
return "\"" + stringReplace(value, "\"", "\\\"") + "\"";
}
std::string kv(std::string key, std::string value) {
return key + "=" + escape(value) + "\n";
}
std::string kv(std::string key, const char *value) {
return key + "=" + escape(value) + "\n";
}
std::string kv(std::string key, int value) {
return key + "=" + std::to_string(value) + "\n";
}
std::string kv(std::string key, bool value) {
if (value) {
return key + "=true\n";
} else {
return key + "=false\n";
}
}
std::string kv(std::string key, uint64_t value) {
return key + "=" + std::to_string(value) + "\n";
}
std::string error(std::string value) {
return kv("error", value);
}
std::string getConfig(std::string prefix, const LanPlayConfig &config) {
std::string out;
out += "[" + prefix + "]\n";
out += kv("relayServer", config.relayServer);
out += kv("socks5Server", config.socks5Server);
out += kv("pmtu", config.pmtu);
out += kv("fakeInternet", config.fakeInternet);
out += kv("broadcast", config.broadcast);
return out;
}
std::string getConfig(const LanPlayConfig &config) {
return getConfig("config", config);
}
std::string getStats(const LanPlayStats &stats) {
std::string out;
out += "[stats]\n";
out += kv("time", stats.time);
out += "[stats.client]\n";
out += kv("downloadByte", stats.client.downloadByte);
out += kv("downloadPacket", stats.client.downloadPacket);
out += kv("uploadByte", stats.client.uploadByte);
out += kv("uploadPacket", stats.client.uploadPacket);
out += "[stats.packet]\n";
out += kv("downloadByte", stats.packet.downloadByte);
out += kv("downloadPacket", stats.packet.downloadPacket);
out += kv("uploadByte", stats.packet.uploadByte);
out += kv("uploadPacket", stats.packet.uploadPacket);
return out;
}
RPCServer::RPCServer(std::shared_ptr<uvw::Loop> loop):
loop(loop)
{};
std::shared_ptr<RPCServerSession> RPCServer::createSession() {
return std::make_shared<RPCServerSession>(this->shared_from_this());
}
RPCServerSession::RPCServerSession(std::shared_ptr<RPCServer> server):
weakServer(server),
sendBack([](std::string){ return false; })
{}
std::string RPCServerSession::onMessage(std::string message) {
auto server = weakServer.lock();
if (server) {
auto &lanPlay = server->lanPlay;
std::string out;
auto ePos = message.find('=');
std::string key, value;
if (ePos == std::string::npos) {
key = message;
} else {
key = message.substr(0, ePos);
value = message.substr(ePos + 1, message.length());
}
if (key == "status") {
auto status = lanPlay.getStatus();
if (status == LanPlayStatus::None) {
out = kv("status", "None");
} else if (status == LanPlayStatus::Running) {
out = kv("status", "Running");
}
} else if (key == "listIf") {
std::vector<NetInterface> list;
if (lanPlay.getNetInterfaces(list) == 0) {
for (auto netif : list) {
out += "[[interfaces]]\n";
out += kv("name", netif.name);
out += kv("description", netif.description);
out += "ips=[\n";
for (auto ip : netif.ips) {
out += " " + escape(ip) + ",\n";
}
out += "]\n";
}
} else {
out = error(lanPlay.getLastError());
}
} else if (key == "version") {
out = kv("version", lanPlay.getVersion());
} else if (key == "start") {
if (lanPlay.start() == 0) {
out = kv("status", "Running");
} else {
out = error(lanPlay.getLastError());
}
} else if (key == "stop") {
if (lanPlay.stop() == 0) {
out = kv("status", "None");
} else {
out = error(lanPlay.getLastError());
}
} else if (key == "config") {
out = getConfig(lanPlay.config);
} else if (key == "lastConfig") {
out = getConfig("lastConfig", lanPlay.getLastConfig());
} else if (key == "relayServer") {
lanPlay.config.relayServer = value;
out = getConfig(lanPlay.config);
} else if (key == "socks5Server") {
lanPlay.config.socks5Server = value;
out = getConfig(lanPlay.config);
} else if (key == "fakeInternet") {
lanPlay.config.fakeInternet = value == "true";
out = getConfig(lanPlay.config);
} else if (key == "broadcast") {
lanPlay.config.broadcast = value == "true";
out = getConfig(lanPlay.config);
} else if (key == "pmtu") {
try {
lanPlay.config.pmtu = std::stoi(value, nullptr, 0);
out = getConfig(lanPlay.config);
} catch (std::invalid_argument &e) {
out = error(e.what());
}
} else if (key == "stats") {
LanPlayStats stats;
if (lanPlay.getStats(stats) == 0) {
out = getStats(stats);
} else {
out = error(lanPlay.getLastError());
}
} else {
out = error("command not found: " + key);
}
return out;
}
return "server lost";
}
}
}