Spaces:
Paused
Paused
| 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; | |
| } |