From ecd85a2da646acddea3961f01e6bf356569592f6 Mon Sep 17 00:00:00 2001 From: Linwenxuan Date: Thu, 9 May 2024 22:40:41 +0800 Subject: [PATCH] add additional endpoints --- src/server.cpp | 100 +++++++++++++++++++++++++++++++++++++------------ src/server.h | 1 + src/sign.cpp | 56 ++++++++++++--------------- 3 files changed, 101 insertions(+), 56 deletions(-) diff --git a/src/server.cpp b/src/server.cpp index 52a1460..bed2c1a 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -7,39 +7,93 @@ Server::Server(int port) { - svr.Post("/sign", [this](const httplib::Request &req, httplib::Response &res) - { - try - { + std::atomic counter(0); + + svr.Post("/sign", [this, &counter](const httplib::Request &req, httplib::Response &res) + { + try { rapidjson::Document doc; doc.Parse(req.body.c_str(), req.body.size()); std::string_view cmd = doc["cmd"].GetString(); std::string_view src = doc["src"].GetString(); - int seq = doc["seq"].GetInt64(); + int seq = doc["seq"].GetInt(); auto [signDataHex, extraDataHex, tokenDataHex] = sign.Call(cmd, src, seq); + std::string buffer = ConstructResponse(signDataHex, extraDataHex, tokenDataHex); + counter++; - // Construct response - rapidjson::StringBuffer buffer; - rapidjson::Writer writer(buffer); - writer.StartObject(); - writer.Key("sign"); - writer.String(signDataHex.c_str()); - writer.Key("extra"); - writer.String(extraDataHex.c_str()); - writer.Key("token"); - writer.String(tokenDataHex.c_str()); - writer.EndObject(); - - res.set_content(buffer.GetString(), "application/json"); + res.set_content(buffer, "application/json"); } - catch (...) - { + catch (...) { res.set_content("Bad Request", "text/plain"); res.status = httplib::StatusCode::BadRequest_400; - } }); + } + }); - std::thread([this, port] - { svr.listen("0.0.0.0", port); }).detach(); + svr.Get("/sign", [this, &counter](const httplib::Request &req, httplib::Response &res) + { + try { + std::string cmd = req.get_param_value("cmd"); + std::string src = req.get_param_value("src"); + int seq = std::stoi(req.get_param_value("seq")); + + auto [signDataHex, extraDataHex, tokenDataHex] = sign.Call(cmd, src, seq); + std::string buffer = ConstructResponse(signDataHex, extraDataHex, tokenDataHex); + counter++; + + res.set_content(buffer, "application/json"); + } + catch (...) { + res.set_content("Bad Request", "text/plain"); + res.status = httplib::StatusCode::BadRequest_400; + } + }); + + svr.Get("/ping", [](const httplib::Request &req, httplib::Response &res) + { + rapidjson::StringBuffer buffer; + rapidjson::Writer writer(buffer); + + writer.StartObject(); + writer.Key("code"); + writer.Int(0); + writer.EndObject(); + + res.set_content(buffer.GetString(), "application/json"); + }); + + svr.Get("/count", [&counter](const httplib::Request &req, httplib::Response &res) + { + rapidjson::StringBuffer buffer; + rapidjson::Writer writer(buffer); + + writer.StartObject(); + writer.Key("count"); + writer.String(std::to_string(counter.load()).c_str()); + writer.EndObject(); + + res.set_content(buffer.GetString(), "application/json"); + }); + + std::thread([this, port]{ svr.listen("0.0.0.0", port); }).detach(); +} + +std::string Server::ConstructResponse(const std::string &sign, const std::string &extra, const std::string &token) { + rapidjson::StringBuffer buffer; + rapidjson::Writer writer(buffer); + + writer.StartObject(); + writer.Key("value"); + writer.StartObject(); + writer.Key("sign"); + writer.String(sign.c_str()); + writer.Key("extra"); + writer.String(extra.c_str()); + writer.Key("token"); + writer.String(token.c_str()); + writer.EndObject(); + writer.EndObject(); + + return buffer.GetString(); } \ No newline at end of file diff --git a/src/server.h b/src/server.h index bc064ca..860f2e9 100644 --- a/src/server.h +++ b/src/server.h @@ -9,4 +9,5 @@ public: private: httplib::Server svr; Sign sign; + static std::string ConstructResponse(const std::string &sign, const std::string &extra, const std::string &token); }; \ No newline at end of file diff --git a/src/sign.cpp b/src/sign.cpp index 5321d07..b29b0da 100644 --- a/src/sign.cpp +++ b/src/sign.cpp @@ -95,17 +95,15 @@ void Sign::Init() uint64_t HookAddress = 0; #if defined(_WIN_PLATFORM_) HMODULE wrapperModule = GetModuleHandleW(L"wrapper.node"); - if (wrapperModule == NULL) - throw std::runtime_error("Can't find wrapper.node module"); + if (wrapperModule == NULL) { + throw std::runtime_error("Can't find wrapper.node module"); + } HookAddress = reinterpret_cast(wrapperModule) + addrMap[CURRENT_VERSION]; printf("HookAddress: %llx\n", HookAddress); #elif defined(_MAC_PLATFORM_) auto pmap = hak::get_maps(); - do - { - // printf("start: %llx, end: %llx, offset: %x, module_name: %s\n", pmap->start(), pmap->end(), pmap->offset, pmap->module_name.c_str()); - if (pmap->module_name.find("wrapper.node") != std::string::npos && pmap->offset == 0) - { + do { + if (pmap->module_name.find("wrapper.node") != std::string::npos && pmap->offset == 0) { HookAddress = pmap->start() + addrMap[CURRENT_VERSION]; printf("HookAddress: %llx\n", HookAddress); break; @@ -113,45 +111,39 @@ void Sign::Init() } while ((pmap = pmap->next()) != nullptr); #elif defined(_LINUX_PLATFORM_) auto pmap = hak::get_maps(); - do - { - // printf("start: %lx, end: %lx, offset: %x, module_name: %s\n", pmap->start(), pmap->end(), pmap->offset, pmap->module_name.c_str()); - if (pmap->module_name.find("wrapper.node") != std::string::npos && pmap->offset == 0) - { + do { + if (pmap->module_name.find("wrapper.node") != std::string::npos && pmap->offset == 0) { HookAddress = pmap->start() + addrMap[CURRENT_VERSION]; printf("HookAddress: %lx\n", HookAddress); break; } } while ((pmap = pmap->next()) != nullptr); #endif - if (HookAddress == 0) - throw std::runtime_error("Can't find hook address"); + if (HookAddress == 0) { + throw std::runtime_error("Can't find hook address"); + } SignFunction = reinterpret_cast(HookAddress); } void Sign::InitEx() { - while (true) - try - { - Init(); - break; - } - catch (const std::exception &e) - { - std::cerr << e.what() << '\n'; - std::this_thread::sleep_for(std::chrono::seconds(1)); - } + while (true) { + try { + Init(); + break; + } + catch (const std::exception &e) { + std::cerr << e.what() << '\n'; + std::this_thread::sleep_for(std::chrono::seconds(1)); + } + } } std::tuple Sign::Call(const std::string_view cmd, const std::string_view src, int seq) { - if (SignFunction == nullptr) - throw std::runtime_error("Sign function not initialized"); - - // printf("cmd: %s\n", cmd.data()); - // printf("src: %s\n", src.data()); - // printf("seq: %d\n", seq); + if (SignFunction == nullptr) { + throw std::runtime_error("Sign function not initialized"); + } const std::vector signArgSrc = Hex2Bin(src); @@ -160,8 +152,6 @@ std::tuple Sign::Call(const std::string_v SignFunction(cmd.data(), signArgSrc.data(), signArgSrc.size(), seq, signResult); - // printf("signResult: %s\n", Bin2Hex(signResult, resultSize).c_str()); - std::string signDataHex = Bin2Hex(signResult + 512, *(signResult + SignOffsets)); std::string extraDataHex = Bin2Hex(signResult + 256, *(signResult + ExtraOffsets)); std::string tokenDataHex = Bin2Hex(signResult, *(signResult + TokenOffsets));