xukc commited on
Commit
57ff546
·
1 Parent(s): 633bdf8

[opt]project opt

Browse files
.vscode/launch.json CHANGED
@@ -6,7 +6,7 @@
6
  "type": "cppdbg",
7
  "request": "launch",
8
  "program": "${workspaceFolder}/proxyServer",
9
- "args": ["1", "8080"],
10
  "stopAtEntry": false,
11
  "cwd": "${workspaceFolder}",
12
  "environment": [],
 
6
  "type": "cppdbg",
7
  "request": "launch",
8
  "program": "${workspaceFolder}/proxyServer",
9
+ "args": ["0", "8080"],
10
  "stopAtEntry": false,
11
  "cwd": "${workspaceFolder}",
12
  "environment": [],
.vscode/tasks.json CHANGED
@@ -13,6 +13,7 @@
13
  "-I",
14
  "include",
15
  "src/*.cpp",
 
16
  "-lhv",
17
  "-lspdlog"
18
 
 
13
  "-I",
14
  "include",
15
  "src/*.cpp",
16
+ "main.cpp",
17
  "-lhv",
18
  "-lspdlog"
19
 
Dockerfile CHANGED
@@ -57,7 +57,7 @@ ENV TZ=Asia/Shanghai
57
  RUN dpkg-reconfigure --frontend noninteractive tzdata
58
 
59
  # Compile the C++ program
60
- RUN g++ -std=c++14 -g -o proxyServer src/*.cpp -I include -lhv -lspdlog
61
 
62
  EXPOSE 8080
63
  # Run my_program when the container launches
 
57
  RUN dpkg-reconfigure --frontend noninteractive tzdata
58
 
59
  # Compile the C++ program
60
+ RUN g++ -std=c++14 -g -o proxyServer src/*.cpp main.cpp -I include -lhv -lspdlog
61
 
62
  EXPOSE 8080
63
  # Run my_program when the container launches
src/main.cpp → main.cpp RENAMED
@@ -1,123 +1,3 @@
1
- // #include "hv/HttpServer.h"
2
- // using namespace hv;
3
-
4
- // int main() {
5
- // HttpService router;
6
- // router.GET("/ping", [](HttpRequest* req, HttpResponse* resp) {
7
- // print("/ping");
8
- // return resp->String("pong");
9
- // });
10
-
11
- // router.GET("/data", [](HttpRequest* req, HttpResponse* resp) {
12
- // print("/data");
13
- // static char data[] = "0123456789";
14
- // return resp->Data(data, 10);
15
- // });
16
-
17
- // router.GET("/paths", [&router](HttpRequest* req, HttpResponse* resp) {
18
- // print("/paths");
19
- // return resp->Json(router.Paths());
20
- // });
21
-
22
- // router.GET("/get", [](HttpRequest* req, HttpResponse* resp) {
23
- // print("/get");
24
- // resp->json["origin"] = req->client_addr.ip;
25
- // resp->json["url"] = req->url;
26
- // resp->json["args"] = req->query_params;
27
- // resp->json["headers"] = req->headers;
28
- // hv::Json myArray = hv::Json::array();
29
- // myArray.push_back("apple");
30
- // myArray.push_back("banana");
31
- // myArray.push_back("orange");
32
- // resp->json["fruits"] = myArray;
33
- // resp->json["test"]["a"] = "json_serializer";
34
- // return 200;
35
- // });
36
-
37
- // router.POST("/echo", [](const HttpContextPtr& ctx) {
38
- // print(ctx->body());
39
- // return ctx->send(ctx->body(), ctx->type());
40
- // });
41
-
42
- // HttpServer server(&router);
43
- // server.setPort(8080);
44
- // server.setThreadNum(4);
45
- // server.run();
46
- // return 0;
47
- // }
48
-
49
- // #include "hv/TcpServer.h"
50
- // using namespace hv;
51
-
52
- // int main() {
53
- // int port = 1234;
54
- // TcpServer srv;
55
- // int listenfd = srv.createsocket(port);
56
- // if (listenfd < 0) {
57
- // return -1;
58
- // }
59
- // printf("server listen on port %d, listenfd=%d ...\n", port, listenfd);
60
- // srv.onConnection = [](const SocketChannelPtr& channel) {
61
- // std::string peeraddr = channel->peeraddr();
62
- // if (channel->isConnected()) {
63
- // printf("%s connected! connfd=%d\n", peeraddr.c_str(), channel->fd());
64
- // } else {
65
- // printf("%s disconnected! connfd=%d\n", peeraddr.c_str(), channel->fd());
66
- // }
67
- // };
68
- // srv.onMessage = [](const SocketChannelPtr& channel, Buffer* buf) {
69
- // // echo
70
- // channel->write(buf);
71
- // };
72
- // srv.setThreadNum(4);
73
- // srv.start();
74
-
75
- // // press Enter to stop
76
- // while (getchar() != '\n');
77
- // return 0;
78
- // }
79
-
80
- // #include <iostream>
81
- // #include "hv/TcpClient.h"
82
- // using namespace hv;
83
-
84
- // int main() {
85
- // int port = 1234;
86
- // TcpClient cli;
87
- // int connfd = cli.createsocket(port);
88
- // if (connfd < 0) {
89
- // return -1;
90
- // }
91
- // cli.onConnection = [](const SocketChannelPtr& channel) {
92
- // std::string peeraddr = channel->peeraddr();
93
- // if (channel->isConnected()) {
94
- // printf("connected to %s! connfd=%d\n", peeraddr.c_str(), channel->fd());
95
- // } else {
96
- // printf("disconnected to %s! connfd=%d\n", peeraddr.c_str(), channel->fd());
97
- // }
98
- // };
99
- // cli.onMessage = [](const SocketChannelPtr& channel, Buffer* buf) {
100
- // printf("< %.*s\n", (int)buf->size(), (char*)buf->data());
101
- // };
102
- // cli.start();
103
-
104
- // std::string str;
105
- // while (std::getline(std::cin, str)) {
106
- // if (str == "close") {
107
- // cli.closesocket();
108
- // } else if (str == "start") {
109
- // cli.start();
110
- // } else if (str == "stop") {
111
- // cli.stop();
112
- // break;
113
- // } else {
114
- // if (!cli.isConnected()) break;
115
- // cli.send(str);
116
- // }
117
- // }
118
- // return 0;
119
- // }
120
-
121
  /*
122
  *
123
  * @build make examples
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  /*
2
  *
3
  * @build make examples
main_signal.cpp ADDED
@@ -0,0 +1,202 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include "hv/HttpServer.h"
2
+ #include <iostream>
3
+ #include <cstdlib>
4
+ #include <ctime>
5
+
6
+ #include "spdlog/spdlog.h"
7
+ #include "spdlog/async.h"
8
+ #include "spdlog/sinks/stdout_color_sinks.h"
9
+
10
+ using namespace hv;
11
+
12
+ // 生成一个指定长度的随机字符串
13
+ std::string generateRandomString(int length) {
14
+ const std::string characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
15
+ const int charactersLength = characters.length();
16
+ std::string randomString;
17
+
18
+ for (int i = 0; i < length; ++i) {
19
+ randomString += characters[rand() % charactersLength];
20
+ }
21
+
22
+ return randomString;
23
+ }
24
+ int generateRandomNumber() {
25
+ return rand() % 100000; // 生成 0 到 999 之间的随机数
26
+ }
27
+
28
+ std::string generateChannelToken(int csid) {
29
+ // 使用当前时间戳作为一部分
30
+ auto timestamp = std::to_string(std::chrono::system_clock::now().time_since_epoch().count());
31
+
32
+
33
+ // 拼接成最终的通道令牌
34
+ return "datachannel_" + std::to_string(csid) + "_" + timestamp + "_" + std::to_string(rand() % 256);
35
+ }
36
+
37
+ static int port = 8888;
38
+ int main(int argc, char** argv) {
39
+ if (argc == 2) {
40
+ port = atoi(argv[1]);
41
+ }
42
+
43
+ auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
44
+ auto logger = std::make_shared<spdlog::logger>("my_logger", console_sink);
45
+
46
+ // 设置异步模式,设置线程数(0 表示使用 CPU 核心数)
47
+ spdlog::init_thread_pool(8192, 2);
48
+
49
+ // 设置异步日志器
50
+ spdlog::set_default_logger(std::make_shared<spdlog::async_logger>(
51
+ "ProxyServer", console_sink, spdlog::thread_pool(), spdlog::async_overflow_policy::block));
52
+
53
+
54
+ std::srand(std::time(0));
55
+ HttpService router;
56
+ router.POST("/api/open.login.loginV2", [](const HttpContextPtr& ctx) {
57
+ auto tcp_chan_sid = generateRandomNumber();
58
+ auto udp_chan_sid = generateRandomNumber();
59
+ nlohmann::json data = {
60
+ {"signalSessionId", generateRandomString(32)},
61
+ {"channelAuthList", {
62
+ {
63
+ {"dataChannelSessionId", tcp_chan_sid},
64
+ {"channelIp", "192.168.0.1"},
65
+ {"port", 8080},
66
+ {"proType", "TCP"},
67
+ {"channelSt", generateChannelToken(tcp_chan_sid)},
68
+ {"secretType", "ST"}
69
+ },
70
+ {
71
+ {"dataChannelSessionId", udp_chan_sid},
72
+ {"channelIp", "192.168.0.2"},
73
+ {"port", 8081},
74
+ {"proType", "UDP"},
75
+ {"channelSt", generateChannelToken(udp_chan_sid)},
76
+ {"secretType", "ST"}
77
+ }
78
+ }}
79
+ };
80
+
81
+ // 创建 State 对象
82
+ nlohmann::json state = {
83
+ {"code", 2000000},
84
+ {"msg", "OK"}
85
+ };
86
+
87
+ // 创建 Result 对象
88
+ nlohmann::json result = {
89
+ {"data", data},
90
+ {"state", state}
91
+ };
92
+
93
+ // 创建 LoginBean 对象
94
+ nlohmann::json loginBean = {
95
+ {"result", result}
96
+ };
97
+
98
+ return ctx->send(loginBean.dump());
99
+ });
100
+
101
+ router.POST("/api/open.login.logoutV2", [](const HttpContextPtr& ctx) {
102
+ nlohmann::json result;
103
+
104
+ // 设置 data 部分
105
+ result["data"]["isSuccess"] = 1;
106
+ result["data"]["msg"] = "成功";
107
+
108
+ // 设置 state 部分
109
+ result["state"]["code"] = 2000000;
110
+ result["state"]["msg"] = "ok";
111
+
112
+ nlohmann::json logoutBean = {
113
+ {"result", result}
114
+ };
115
+ return ctx->send(logoutBean.dump());
116
+ });
117
+
118
+ router.POST("/api/open.heartbeat.heartbeatV2", [](const HttpContextPtr& ctx) {
119
+ nlohmann::json result;
120
+
121
+ // 设置 data 部分
122
+ result["data"]["isSuccess"] = 1;
123
+ result["data"]["msg"] = "成功";
124
+
125
+ // 设置 state 部分
126
+ result["state"]["code"] = 2000000;
127
+ result["state"]["msg"] = "ok";
128
+
129
+ nlohmann::json heartbeatBean = {
130
+ {"result", result}
131
+ };
132
+ return ctx->send(heartbeatBean.dump());
133
+ });
134
+
135
+ router.POST("/api/open.auth.getChannelStV2", [](const HttpContextPtr& ctx) {
136
+ std::string body = ctx->request->body;
137
+
138
+ nlohmann::json result;
139
+ // 解析 JSON
140
+ try {
141
+ nlohmann::json requestBody = nlohmann::json::parse(body);
142
+ nlohmann::json data;
143
+ data["dataChannelList"] = requestBody["result"]["data"]["channelAuthList"];
144
+ auto now = std::chrono::system_clock::now();
145
+ auto tomorrow = now + std::chrono::hours(24);
146
+ auto tomorrow_timestamp = std::chrono::duration_cast<std::chrono::seconds>(tomorrow.time_since_epoch()).count();
147
+ for (auto& item : data["dataChannelList"]) {
148
+ item["expireTime"] = tomorrow_timestamp;
149
+ }
150
+
151
+ // 创建 State 对象
152
+ nlohmann::json state = {
153
+ {"code", 2000000},
154
+ {"msg", "OK"}
155
+ };
156
+
157
+ // 创建 Result 对象
158
+ result = {
159
+ {"data", data},
160
+ {"state", state}
161
+ };
162
+ } catch (const nlohmann::json::parse_error& e) {
163
+
164
+ // 设置 data 部分
165
+ result["data"]["isSuccess"] = 1;
166
+ result["data"]["msg"] = "成功";
167
+
168
+ // 设置 state 部分
169
+ result["state"]["code"] = 4000000;
170
+ result["state"]["msg"] = "error";
171
+ }
172
+
173
+ nlohmann::json getChannelStV2 = {
174
+ {"result", result}
175
+ };
176
+ return ctx->send(getChannelStV2.dump());
177
+ });
178
+
179
+ router.GET("/test", [](HttpRequest* req, HttpResponse* resp) {
180
+ print("/test");
181
+ resp->json["origin"] = req->client_addr.ip;
182
+ resp->json["url"] = req->url;
183
+ resp->json["args"] = req->query_params;
184
+ resp->json["headers"] = req->headers;
185
+ hv::Json myArray = hv::Json::array();
186
+ myArray.push_back("apple");
187
+ myArray.push_back("banana");
188
+ myArray.push_back("orange");
189
+ resp->json["fruits"] = myArray;
190
+ resp->json["test"]["a"] = "json_serializer";
191
+ return 200;
192
+ });
193
+
194
+ HttpServer server(&router);
195
+ server.setPort(port);
196
+ server.setThreadNum(2);
197
+
198
+ spdlog::info("SignalServer start :{}", port);
199
+
200
+ server.run();
201
+ return 0;
202
+ }