Spaces:
Paused
Paused
xukc
commited on
Commit
·
722ae87
1
Parent(s):
3d90f64
[feat]project create
Browse files- .vscode/c_cpp_properties.json +19 -0
- .vscode/launch.json +25 -0
- .vscode/settings.json +84 -0
- .vscode/tasks.json +26 -0
- Dockerfile +64 -0
- src/main_signal.cpp +202 -0
.vscode/c_cpp_properties.json
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"configurations": [
|
3 |
+
{
|
4 |
+
"name": "Mac",
|
5 |
+
"includePath": [
|
6 |
+
"${workspaceFolder}/**"
|
7 |
+
],
|
8 |
+
"defines": [],
|
9 |
+
"macFrameworkPath": [
|
10 |
+
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
|
11 |
+
],
|
12 |
+
"compilerPath": "/usr/bin/clang",
|
13 |
+
"cStandard": "c17",
|
14 |
+
"cppStandard": "c++17",
|
15 |
+
"intelliSenseMode": "macos-clang-arm64"
|
16 |
+
}
|
17 |
+
],
|
18 |
+
"version": 4
|
19 |
+
}
|
.vscode/launch.json
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"version": "0.2.0",
|
3 |
+
"configurations": [
|
4 |
+
{
|
5 |
+
"name": "Debug",
|
6 |
+
"type": "cppdbg",
|
7 |
+
"request": "launch",
|
8 |
+
"program": "${workspaceFolder}/signalServer",
|
9 |
+
"args": ["8888"],
|
10 |
+
"stopAtEntry": false,
|
11 |
+
"cwd": "${workspaceFolder}",
|
12 |
+
"environment": [],
|
13 |
+
"externalConsole": false,
|
14 |
+
"MIMode": "lldb",
|
15 |
+
"preLaunchTask": "build",
|
16 |
+
"setupCommands": [
|
17 |
+
{
|
18 |
+
"description": "Enable pretty-printing for lldb",
|
19 |
+
"text": "enable",
|
20 |
+
"ignoreFailures": true
|
21 |
+
}
|
22 |
+
]
|
23 |
+
}
|
24 |
+
]
|
25 |
+
}
|
.vscode/settings.json
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"files.associations": {
|
3 |
+
"*.cc": "cpp",
|
4 |
+
"__bit_reference": "cpp",
|
5 |
+
"__bits": "cpp",
|
6 |
+
"__config": "cpp",
|
7 |
+
"__debug": "cpp",
|
8 |
+
"__errc": "cpp",
|
9 |
+
"__hash_table": "cpp",
|
10 |
+
"__locale": "cpp",
|
11 |
+
"__mutex_base": "cpp",
|
12 |
+
"__node_handle": "cpp",
|
13 |
+
"__nullptr": "cpp",
|
14 |
+
"__split_buffer": "cpp",
|
15 |
+
"__string": "cpp",
|
16 |
+
"__threading_support": "cpp",
|
17 |
+
"__tree": "cpp",
|
18 |
+
"__tuple": "cpp",
|
19 |
+
"array": "cpp",
|
20 |
+
"atomic": "cpp",
|
21 |
+
"bitset": "cpp",
|
22 |
+
"cctype": "cpp",
|
23 |
+
"chrono": "cpp",
|
24 |
+
"clocale": "cpp",
|
25 |
+
"cmath": "cpp",
|
26 |
+
"compare": "cpp",
|
27 |
+
"complex": "cpp",
|
28 |
+
"concepts": "cpp",
|
29 |
+
"cstdarg": "cpp",
|
30 |
+
"cstddef": "cpp",
|
31 |
+
"cstdint": "cpp",
|
32 |
+
"cstdio": "cpp",
|
33 |
+
"cstdlib": "cpp",
|
34 |
+
"cstring": "cpp",
|
35 |
+
"ctime": "cpp",
|
36 |
+
"cwchar": "cpp",
|
37 |
+
"cwctype": "cpp",
|
38 |
+
"deque": "cpp",
|
39 |
+
"exception": "cpp",
|
40 |
+
"forward_list": "cpp",
|
41 |
+
"initializer_list": "cpp",
|
42 |
+
"ios": "cpp",
|
43 |
+
"iosfwd": "cpp",
|
44 |
+
"iostream": "cpp",
|
45 |
+
"istream": "cpp",
|
46 |
+
"limits": "cpp",
|
47 |
+
"list": "cpp",
|
48 |
+
"locale": "cpp",
|
49 |
+
"map": "cpp",
|
50 |
+
"memory": "cpp",
|
51 |
+
"mutex": "cpp",
|
52 |
+
"new": "cpp",
|
53 |
+
"numeric": "cpp",
|
54 |
+
"optional": "cpp",
|
55 |
+
"ostream": "cpp",
|
56 |
+
"queue": "cpp",
|
57 |
+
"ratio": "cpp",
|
58 |
+
"sstream": "cpp",
|
59 |
+
"stdexcept": "cpp",
|
60 |
+
"streambuf": "cpp",
|
61 |
+
"string": "cpp",
|
62 |
+
"string_view": "cpp",
|
63 |
+
"system_error": "cpp",
|
64 |
+
"tuple": "cpp",
|
65 |
+
"type_traits": "cpp",
|
66 |
+
"typeinfo": "cpp",
|
67 |
+
"unordered_map": "cpp",
|
68 |
+
"valarray": "cpp",
|
69 |
+
"variant": "cpp",
|
70 |
+
"vector": "cpp",
|
71 |
+
"algorithm": "cpp",
|
72 |
+
"shared_mutex": "cpp",
|
73 |
+
"__memory": "cpp",
|
74 |
+
"thread": "cpp",
|
75 |
+
"bit": "cpp",
|
76 |
+
"charconv": "cpp",
|
77 |
+
"condition_variable": "cpp",
|
78 |
+
"format": "cpp",
|
79 |
+
"future": "cpp",
|
80 |
+
"functional": "cpp",
|
81 |
+
"memory_resource": "cpp",
|
82 |
+
"utility": "cpp"
|
83 |
+
}
|
84 |
+
}
|
.vscode/tasks.json
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"version": "2.0.0",
|
3 |
+
"tasks": [
|
4 |
+
{
|
5 |
+
"label": "build",
|
6 |
+
"type": "shell",
|
7 |
+
"command": "g++",
|
8 |
+
"args": [
|
9 |
+
"-std=c++14",
|
10 |
+
"-g",
|
11 |
+
"-o",
|
12 |
+
"signalServer",
|
13 |
+
"-I",
|
14 |
+
"include",
|
15 |
+
"src/*.cpp",
|
16 |
+
"-lhv",
|
17 |
+
"-lspdlog"
|
18 |
+
|
19 |
+
],
|
20 |
+
"group": {
|
21 |
+
"kind": "build",
|
22 |
+
"isDefault": true
|
23 |
+
}
|
24 |
+
}
|
25 |
+
]
|
26 |
+
}
|
Dockerfile
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use an official Ubuntu runtime as a parent image
|
2 |
+
FROM ubuntu:latest
|
3 |
+
|
4 |
+
# 设置非交互式环境变量,以避免 tzdata 安装时的交互
|
5 |
+
ENV DEBIAN_FRONTEND=noninteractive
|
6 |
+
|
7 |
+
# Use Aliyun mirrors for apt-get
|
8 |
+
RUN sed -i 's/http:\/\/archive.ubuntu.com\/ubuntu\//http:\/\/mirrors.aliyun.com\/ubuntu\//g' /etc/apt/sources.list
|
9 |
+
|
10 |
+
# Update the package lists
|
11 |
+
RUN apt-get update
|
12 |
+
|
13 |
+
# Install build tools
|
14 |
+
RUN apt-get install -y g++ make cmake autoconf automake libtool
|
15 |
+
|
16 |
+
# 安装 spdlog 依赖
|
17 |
+
RUN apt-get install -y \
|
18 |
+
libfmt-dev
|
19 |
+
|
20 |
+
# Download and build libhv from source
|
21 |
+
RUN apt-get install -y git
|
22 |
+
RUN git clone https://github.com/ithewei/libhv.git /usr/src/libhv
|
23 |
+
WORKDIR /usr/src/libhv
|
24 |
+
RUN make
|
25 |
+
RUN make install
|
26 |
+
|
27 |
+
# Add the libhv library path
|
28 |
+
RUN ldconfig
|
29 |
+
|
30 |
+
# 克隆 spdlog 仓库
|
31 |
+
RUN git clone https://github.com/gabime/spdlog.git /usr/src/spdlog
|
32 |
+
|
33 |
+
WORKDIR /usr/src/spdlog
|
34 |
+
|
35 |
+
# 运行 CMake 配置
|
36 |
+
RUN cmake .
|
37 |
+
|
38 |
+
# 运行构建
|
39 |
+
RUN make
|
40 |
+
|
41 |
+
# 运行安装
|
42 |
+
RUN make install
|
43 |
+
|
44 |
+
RUN ldconfig
|
45 |
+
|
46 |
+
# Set the working directory to /app
|
47 |
+
WORKDIR /app
|
48 |
+
|
49 |
+
# Copy the source code into the container
|
50 |
+
COPY . .
|
51 |
+
|
52 |
+
RUN apt-get install -y tzdata
|
53 |
+
|
54 |
+
# 设置时区为 Asia/Shanghai
|
55 |
+
ENV TZ=Asia/Shanghai
|
56 |
+
|
57 |
+
RUN dpkg-reconfigure --frontend noninteractive tzdata
|
58 |
+
|
59 |
+
# Compile the C++ program
|
60 |
+
RUN g++ -std=c++14 -g -o signalServer src/*.cpp -I include -lhv -lspdlog
|
61 |
+
|
62 |
+
EXPOSE 8888
|
63 |
+
# Run my_program when the container launches
|
64 |
+
CMD ["./signalServer"]
|
src/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 |
+
}
|