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)
{
svr.Post("/sign", [this](const httplib::Request &req, httplib::Response &res)
{
try
{
std::atomic<uint64_t> 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<rapidjson::StringBuffer> 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<rapidjson::StringBuffer> 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<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:
httplib::Server svr;
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;
#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<uint64_t>(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<SignFunctionType>(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<std::string, std::string, std::string> 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<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);
// 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));