File size: 6,734 Bytes
722ae87
e4df9b0
722ae87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82d9774
722ae87
 
 
 
 
 
 
82d9774
722ae87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bdd6c58
 
 
 
 
722ae87
 
 
b27edac
 
722ae87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "hv/HttpServer.h"
#include "hv/requests.h"
#include <iostream>
#include <cstdlib>
#include <ctime>

#include "spdlog/spdlog.h"
#include "spdlog/async.h"
#include "spdlog/sinks/stdout_color_sinks.h"

using namespace hv;

// 生成一个指定长度的随机字符串
std::string generateRandomString(int length) {
    const std::string characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    const int charactersLength = characters.length();
    std::string randomString;

    for (int i = 0; i < length; ++i) {
        randomString += characters[rand() % charactersLength];
    }

    return randomString;
}
int generateRandomNumber() {
    return rand() % 100000; // 生成 0 到 999 之间的随机数
}

std::string generateChannelToken(int csid) {
    // 使用当前时间戳作为一部分
    auto timestamp = std::to_string(std::chrono::system_clock::now().time_since_epoch().count());
    

    // 拼接成最终的通道令牌
    return "datachannel_" + std::to_string(csid) + "_" + timestamp + "_" + std::to_string(rand() % 256);
}

static int port = 8888;
int main(int argc, char** argv) {
    if (argc == 2) {
        port = atoi(argv[1]);
    }
    
    auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
    auto logger = std::make_shared<spdlog::logger>("my_logger", console_sink);

    // 设置异步模式,设置线程数(0 表示使用 CPU 核心数)
    spdlog::init_thread_pool(8192, 2);

    // 设置异步日志器
    spdlog::set_default_logger(std::make_shared<spdlog::async_logger>(
        "ProxyServer", console_sink, spdlog::thread_pool(), spdlog::async_overflow_policy::block));


    std::srand(std::time(0));
    HttpService router;
    router.POST("/api/open.login.loginV2", [](const HttpContextPtr& ctx) {
        auto tcp_chan_sid = generateRandomNumber();
        auto udp_chan_sid = generateRandomNumber();
        nlohmann::json data = {
            {"signalSessionId", generateRandomString(32)},
            {"channelAuthList", {
                {
                    {"dataChannelSessionId", tcp_chan_sid},
                    {"channelIp", "https://dvc890-proxyserver.hf.space/"},
                    {"port", 8080},
                    {"proType", "TCP"},
                    {"channelSt", generateChannelToken(tcp_chan_sid)},
                    {"secretType", "ST"}
                },
                {
                    {"dataChannelSessionId", udp_chan_sid},
                    {"channelIp", "https://dvc890-proxyserver.hf.space/"},
                    {"port", 8081},
                    {"proType", "UDP"},
                    {"channelSt", generateChannelToken(udp_chan_sid)},
                    {"secretType", "ST"}
                }
            }}
        };

        // 创建 State 对象
        nlohmann::json state = {
            {"code", 2000000},
            {"msg", "OK"}
        };

        // 创建 Result 对象
        nlohmann::json result = {
            {"data", data},
            {"state", state}
        };

        // 创建 LoginBean 对象
        nlohmann::json loginBean = {
            {"result", result}
        };
    
        return ctx->send(loginBean.dump());
    });

    router.POST("/api/open.login.logoutV2", [](const HttpContextPtr& ctx) {
        nlohmann::json result;

        // 设置 data 部分
        result["data"]["isSuccess"] = 1;
        result["data"]["msg"] = "成功";

        // 设置 state 部分
        result["state"]["code"] = 2000000;
        result["state"]["msg"] = "ok";

        nlohmann::json logoutBean = {
            {"result", result}
        };
        return ctx->send(logoutBean.dump());
    });

    router.POST("/api/open.heartbeat.heartbeatV2", [](const HttpContextPtr& ctx) {
        nlohmann::json result;

        // 设置 data 部分
        result["data"]["isSuccess"] = 1;
        result["data"]["msg"] = "成功";

        // 设置 state 部分
        result["state"]["code"] = 2000000;
        result["state"]["msg"] = "ok";

        nlohmann::json heartbeatBean = {
            {"result", result}
        };
        return ctx->send(heartbeatBean.dump());
    });

    router.POST("/api/open.auth.getChannelStV2", [](const HttpContextPtr& ctx) {
        std::string body = ctx->request->body;
            
        nlohmann::json result;
        // 解析 JSON
        try {
            nlohmann::json requestBody = nlohmann::json::parse(body);
            nlohmann::json data;
            data["dataChannelList"] = requestBody["result"]["data"]["channelAuthList"];
            auto now = std::chrono::system_clock::now();
            auto tomorrow = now + std::chrono::hours(24);
            auto tomorrow_timestamp = std::chrono::duration_cast<std::chrono::seconds>(tomorrow.time_since_epoch()).count();
            for (auto& item : data["dataChannelList"]) {
                item["expireTime"] = tomorrow_timestamp;
            }

            // 创建 State 对象
            nlohmann::json state = {
                {"code", 2000000},
                {"msg", "OK"}
            };

            // 创建 Result 对象
            result = {
                {"data", data},
                {"state", state}
            };
        } catch (const nlohmann::json::parse_error& e) {

            // 设置 data 部分
            result["data"]["isSuccess"] = 1;
            result["data"]["msg"] = "成功";

            // 设置 state 部分
            result["state"]["code"] = 4000000;
            result["state"]["msg"] = "error";
        }

        nlohmann::json getChannelStV2 = {
            {"result", result}
        };
        return ctx->send(getChannelStV2.dump());
    });
    //router.GET("/", [](HttpRequest* req, HttpResponse* resp) {
    //    print("/test");
    //    auto _resp = requests::get("ipinfo.io/ip");
    //    return resp->String(_resp.body().c_str());
    //});

    router.GET("/test", [](HttpRequest* req, HttpResponse* resp) {
        print("/test");
        auto _resp = requests::get("ipinfo.io/ip");
        resp->json["server"] = _resp->body;
        resp->json["origin"] = req->client_addr.ip;
        resp->json["url"] = req->url;
        resp->json["args"] = req->query_params;
        resp->json["headers"] = req->headers;
        hv::Json myArray = hv::Json::array();
        myArray.push_back("apple");
        myArray.push_back("banana");
        myArray.push_back("orange");
        resp->json["fruits"] = myArray;
        resp->json["test"]["a"] = "json_serializer";
        return 200;
    });

    HttpServer server(&router);
    server.setPort(port);
    server.setThreadNum(2);

    spdlog::info("SignalServer start :{}", port);

    server.run();
    return 0;
}