add additional endpoints

This commit is contained in:
Linwenxuan 2024-05-09 22:40:41 +08:00
parent e796e13e6f
commit ecd85a2da6
No known key found for this signature in database
GPG Key ID: 13F70E0AB45D3EA4
3 changed files with 101 additions and 56 deletions

View File

@ -7,39 +7,93 @@
Server::Server(int port) Server::Server(int port)
{ {
svr.Post("/sign", [this](const httplib::Request &req, httplib::Response &res) std::atomic<uint64_t> counter(0);
{
try svr.Post("/sign", [this, &counter](const httplib::Request &req, httplib::Response &res)
{ {
try {
rapidjson::Document doc; rapidjson::Document doc;
doc.Parse(req.body.c_str(), req.body.size()); doc.Parse(req.body.c_str(), req.body.size());
std::string_view cmd = doc["cmd"].GetString(); std::string_view cmd = doc["cmd"].GetString();
std::string_view src = doc["src"].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); auto [signDataHex, extraDataHex, tokenDataHex] = sign.Call(cmd, src, seq);
std::string buffer = ConstructResponse(signDataHex, extraDataHex, tokenDataHex);
counter++;
// Construct response res.set_content(buffer, "application/json");
}
catch (...) {
res.set_content("Bad Request", "text/plain");
res.status = httplib::StatusCode::BadRequest_400;
}
});
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::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
writer.StartObject(); writer.StartObject();
writer.Key("sign"); writer.Key("code");
writer.String(signDataHex.c_str()); writer.Int(0);
writer.Key("extra");
writer.String(extraDataHex.c_str());
writer.Key("token");
writer.String(tokenDataHex.c_str());
writer.EndObject(); writer.EndObject();
res.set_content(buffer.GetString(), "application/json"); res.set_content(buffer.GetString(), "application/json");
} });
catch (...)
{
res.set_content("Bad Request", "text/plain");
res.status = httplib::StatusCode::BadRequest_400;
} });
std::thread([this, port] svr.Get("/count", [&counter](const httplib::Request &req, httplib::Response &res)
{ svr.listen("0.0.0.0", port); }).detach(); {
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> 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<rapidjson::StringBuffer> 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();
} }

View File

@ -9,4 +9,5 @@ public:
private: private:
httplib::Server svr; httplib::Server svr;
Sign sign; Sign sign;
static std::string ConstructResponse(const std::string &sign, const std::string &extra, const std::string &token);
}; };

View File

@ -95,17 +95,15 @@ void Sign::Init()
uint64_t HookAddress = 0; uint64_t HookAddress = 0;
#if defined(_WIN_PLATFORM_) #if defined(_WIN_PLATFORM_)
HMODULE wrapperModule = GetModuleHandleW(L"wrapper.node"); HMODULE wrapperModule = GetModuleHandleW(L"wrapper.node");
if (wrapperModule == NULL) if (wrapperModule == NULL) {
throw std::runtime_error("Can't find wrapper.node module"); throw std::runtime_error("Can't find wrapper.node module");
}
HookAddress = reinterpret_cast<uint64_t>(wrapperModule) + addrMap[CURRENT_VERSION]; HookAddress = reinterpret_cast<uint64_t>(wrapperModule) + addrMap[CURRENT_VERSION];
printf("HookAddress: %llx\n", HookAddress); printf("HookAddress: %llx\n", HookAddress);
#elif defined(_MAC_PLATFORM_) #elif defined(_MAC_PLATFORM_)
auto pmap = hak::get_maps(); auto pmap = hak::get_maps();
do do {
{ if (pmap->module_name.find("wrapper.node") != std::string::npos && pmap->offset == 0) {
// 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)
{
HookAddress = pmap->start() + addrMap[CURRENT_VERSION]; HookAddress = pmap->start() + addrMap[CURRENT_VERSION];
printf("HookAddress: %llx\n", HookAddress); printf("HookAddress: %llx\n", HookAddress);
break; break;
@ -113,45 +111,39 @@ void Sign::Init()
} while ((pmap = pmap->next()) != nullptr); } while ((pmap = pmap->next()) != nullptr);
#elif defined(_LINUX_PLATFORM_) #elif defined(_LINUX_PLATFORM_)
auto pmap = hak::get_maps(); auto pmap = hak::get_maps();
do do {
{ if (pmap->module_name.find("wrapper.node") != std::string::npos && pmap->offset == 0) {
// 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)
{
HookAddress = pmap->start() + addrMap[CURRENT_VERSION]; HookAddress = pmap->start() + addrMap[CURRENT_VERSION];
printf("HookAddress: %lx\n", HookAddress); printf("HookAddress: %lx\n", HookAddress);
break; break;
} }
} while ((pmap = pmap->next()) != nullptr); } while ((pmap = pmap->next()) != nullptr);
#endif #endif
if (HookAddress == 0) if (HookAddress == 0) {
throw std::runtime_error("Can't find hook address"); throw std::runtime_error("Can't find hook address");
}
SignFunction = reinterpret_cast<SignFunctionType>(HookAddress); SignFunction = reinterpret_cast<SignFunctionType>(HookAddress);
} }
void Sign::InitEx() void Sign::InitEx()
{ {
while (true) while (true) {
try try {
{
Init(); Init();
break; break;
} }
catch (const std::exception &e) catch (const std::exception &e) {
{
std::cerr << e.what() << '\n'; std::cerr << e.what() << '\n';
std::this_thread::sleep_for(std::chrono::seconds(1)); std::this_thread::sleep_for(std::chrono::seconds(1));
} }
}
} }
std::tuple<std::string, std::string, std::string> Sign::Call(const std::string_view cmd, const std::string_view src, int seq) std::tuple<std::string, std::string, std::string> Sign::Call(const std::string_view cmd, const std::string_view src, int seq)
{ {
if (SignFunction == nullptr) if (SignFunction == nullptr) {
throw std::runtime_error("Sign function not initialized"); 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);
const std::vector<uint8_t> signArgSrc = Hex2Bin(src); const std::vector<uint8_t> signArgSrc = Hex2Bin(src);
@ -160,8 +152,6 @@ std::tuple<std::string, std::string, std::string> Sign::Call(const std::string_v
SignFunction(cmd.data(), signArgSrc.data(), signArgSrc.size(), seq, signResult); 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 signDataHex = Bin2Hex(signResult + 512, *(signResult + SignOffsets));
std::string extraDataHex = Bin2Hex(signResult + 256, *(signResult + ExtraOffsets)); std::string extraDataHex = Bin2Hex(signResult + 256, *(signResult + ExtraOffsets));
std::string tokenDataHex = Bin2Hex(signResult, *(signResult + TokenOffsets)); std::string tokenDataHex = Bin2Hex(signResult, *(signResult + TokenOffsets));