增加注释和打印
This commit is contained in:
parent
d4d2267305
commit
35a7aae3d0
@ -1,28 +1,47 @@
|
|||||||
#include <WinSock2.h>
|
#include <WinSock2.h>
|
||||||
#include "hook.h"
|
#include "hook.h"
|
||||||
|
|
||||||
|
|
||||||
#include "client_socket.h"
|
#include "client_socket.h"
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "detours.h"
|
#include "detours.h"
|
||||||
#include "http_client.h"
|
#include "http_client.h"
|
||||||
#include "nlohmann/json.hpp"
|
#include "nlohmann/json.hpp"
|
||||||
#include "spdlog/spdlog.h"
|
#include "spdlog/spdlog.h"
|
||||||
// #include "thread_pool.h"
|
#include "thread_pool.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include "wxutils.h"
|
#include "wxutils.h"
|
||||||
namespace hook {
|
#include <iostream>
|
||||||
void SendHttpMsgCallback(PTP_CALLBACK_INSTANCE instance, PVOID context,
|
|
||||||
PTP_WORK Work) {
|
namespace hook
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @brief 发送 HTTP 消息的回调函数
|
||||||
|
* @param instance 回调实例
|
||||||
|
* @param context 上下文
|
||||||
|
* @param Work 工作项
|
||||||
|
*/
|
||||||
|
void SendHttpMsgCallback(PTP_CALLBACK_INSTANCE instance, PVOID context, PTP_WORK Work)
|
||||||
|
{
|
||||||
|
wxhelper::wxutils::print_utf8_to_console("SendHttpMsgCallback called"); // 调试打印
|
||||||
|
|
||||||
|
SPDLOG_INFO("SendHttpMsgCallback called"); // 调试打印
|
||||||
|
|
||||||
InnerMessageStruct *msg = (InnerMessageStruct *)context;
|
InnerMessageStruct *msg = (InnerMessageStruct *)context;
|
||||||
if (msg == NULL) {
|
if (msg == NULL)
|
||||||
SPDLOG_INFO("http msg is null");
|
{
|
||||||
|
wxhelper::wxutils::print_utf8_to_console("httpMsg is null"); // 调试打印
|
||||||
|
|
||||||
|
SPDLOG_INFO("httpMsg is null");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
std::unique_ptr<InnerMessageStruct> sms(msg);
|
std::unique_ptr<InnerMessageStruct> sms(msg);
|
||||||
nlohmann::json j_msg = nlohmann::json::parse(
|
nlohmann::json j_msg = nlohmann::json::parse(
|
||||||
msg->buffer, msg->buffer + msg->length, nullptr, false);
|
msg->buffer, msg->buffer + msg->length, nullptr, false);
|
||||||
if (j_msg.is_discarded()) {
|
if (j_msg.is_discarded())
|
||||||
|
{
|
||||||
|
wxhelper::wxutils::print_utf8_to_console("jsonMsg discarded"); // 调试打印
|
||||||
|
|
||||||
|
SPDLOG_INFO("jsonMsg discarded"); // 调试打印
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
std::string jstr = j_msg.dump() + "\n";
|
std::string jstr = j_msg.dump() + "\n";
|
||||||
@ -30,51 +49,99 @@ void SendHttpMsgCallback(PTP_CALLBACK_INSTANCE instance, PVOID context,
|
|||||||
int timeout = wxhelper::Config::GetInstance().GetRecvHttpTimeout();
|
int timeout = wxhelper::Config::GetInstance().GetRecvHttpTimeout();
|
||||||
http::HttpClient client{url, timeout};
|
http::HttpClient client{url, timeout};
|
||||||
client.SendRequest(jstr);
|
client.SendRequest(jstr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 发送 TCP 消息的回调函数
|
||||||
|
* @param instance 回调实例
|
||||||
|
* @param context 上下文
|
||||||
|
* @param Work 工作项
|
||||||
|
*/
|
||||||
|
void SendTcpMsgCallback(PTP_CALLBACK_INSTANCE instance, PVOID context, PTP_WORK Work)
|
||||||
|
{
|
||||||
|
wxhelper::wxutils::print_utf8_to_console("SendTcpMsgCallback called"); // 调试打印
|
||||||
|
|
||||||
void SendTcpMsgCallback(PTP_CALLBACK_INSTANCE instance, PVOID context,
|
SPDLOG_INFO("SendTcpMsgCallback called"); // 调试打印
|
||||||
PTP_WORK Work) {
|
|
||||||
InnerMessageStruct *msg = (InnerMessageStruct *)context;
|
InnerMessageStruct *msg = (InnerMessageStruct *)context;
|
||||||
if (msg == NULL) {
|
if (msg == NULL)
|
||||||
|
{
|
||||||
|
wxhelper::wxutils::print_utf8_to_console("add work:msg is null"); // 调试打印
|
||||||
|
|
||||||
SPDLOG_INFO("add work:msg is null");
|
SPDLOG_INFO("add work:msg is null");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
std::unique_ptr<InnerMessageStruct> sms(msg);
|
std::unique_ptr<InnerMessageStruct> sms(msg);
|
||||||
nlohmann::json j_msg = nlohmann::json::parse(
|
nlohmann::json j_msg = nlohmann::json::parse(
|
||||||
msg->buffer, msg->buffer + msg->length, nullptr, false);
|
msg->buffer, msg->buffer + msg->length, nullptr, false);
|
||||||
if (j_msg.is_discarded()) {
|
if (j_msg.is_discarded())
|
||||||
|
{
|
||||||
|
wxhelper::wxutils::print_utf8_to_console("jsonMsg discarded"); // 调试打印
|
||||||
|
SPDLOG_INFO("jsonMsg discarded"); // 调试打印
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
std::string jstr = j_msg.dump() + "\n";
|
std::string jstr = j_msg.dump() + "\n";
|
||||||
|
// wxhelper::wxutils::print_utf8_to_console("SendTcpMsgCallback jstr:" + jstr);
|
||||||
|
wxhelper::wxutils::print_utf8_to_console("SendTcpMsgCallback jstr return JSON");
|
||||||
|
|
||||||
std::string ip = wxhelper::Config::GetInstance().GetRecvTcpIp();
|
std::string ip = wxhelper::Config::GetInstance().GetRecvTcpIp();
|
||||||
int port = wxhelper::Config::GetInstance().GetRecvTcpPort();
|
int port = wxhelper::Config::GetInstance().GetRecvTcpPort();
|
||||||
wxhelper::TcpClient client{ip, port};
|
wxhelper::TcpClient client{ip, port};
|
||||||
client.SendAndCloseSocket(jstr);
|
client.SendAndCloseSocket(jstr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 钩子类的构造函数
|
||||||
|
* @param origin 原始函数指针
|
||||||
|
* @param detour 替换函数指针
|
||||||
|
*/
|
||||||
|
BaseHook::BaseHook(void *origin, void *detour)
|
||||||
|
: origin_(origin), detour_(detour)
|
||||||
|
{
|
||||||
|
wxhelper::wxutils::print_utf8_to_console("BaseHook created with origin"); // 调试打印
|
||||||
|
// 调试打印
|
||||||
|
SPDLOG_INFO("BaseHook created with origin: {} and detour: {}", origin, detour); // 调试打印
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 安装钩子函数
|
||||||
|
* @return 返回安装钩子的结果
|
||||||
|
*/
|
||||||
|
int BaseHook::Hook()
|
||||||
|
{
|
||||||
|
wxhelper::wxutils::print_utf8_to_console("Hook called"); // 调试打印
|
||||||
|
SPDLOG_INFO("Hook called"); // 调试打印
|
||||||
|
if (hook_flag_)
|
||||||
|
{
|
||||||
|
// std::cout << "hook already called" << std::endl; // 调试打印
|
||||||
|
wxhelper::wxutils::print_utf8_to_console("hook already called"); // 调试打印
|
||||||
|
|
||||||
BaseHook::BaseHook(void *origin, void *detour)
|
|
||||||
: origin_(origin), detour_(detour) {}
|
|
||||||
|
|
||||||
int BaseHook::Hook() {
|
|
||||||
if (hook_flag_) {
|
|
||||||
SPDLOG_INFO("hook already called");
|
SPDLOG_INFO("hook already called");
|
||||||
|
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
DetourTransactionBegin();
|
DetourTransactionBegin();
|
||||||
DetourUpdateThread(GetCurrentThread());
|
DetourUpdateThread(GetCurrentThread());
|
||||||
DetourAttach((PVOID *)origin_, detour_);
|
DetourAttach((PVOID *)origin_, detour_);
|
||||||
LONG ret = DetourTransactionCommit();
|
LONG ret = DetourTransactionCommit();
|
||||||
if (ret == NO_ERROR) {
|
if (ret == NO_ERROR)
|
||||||
|
{
|
||||||
hook_flag_ = true;
|
hook_flag_ = true;
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int BaseHook::Unhook() {
|
/**
|
||||||
if (!hook_flag_) {
|
* @brief 卸载钩子函数
|
||||||
|
* @return 返回卸载钩子的结果
|
||||||
|
*/
|
||||||
|
int BaseHook::Unhook()
|
||||||
|
{
|
||||||
|
wxhelper::wxutils::print_utf8_to_console("Unhook called"); // 调试打印
|
||||||
|
|
||||||
|
SPDLOG_INFO("Unhook called"); // 调试打印
|
||||||
|
if (!hook_flag_)
|
||||||
|
{
|
||||||
|
wxhelper::wxutils::print_utf8_to_console("hook already called"); // 调试打印
|
||||||
SPDLOG_INFO("hook already called");
|
SPDLOG_INFO("hook already called");
|
||||||
return NO_ERROR;
|
return NO_ERROR;
|
||||||
}
|
}
|
||||||
@ -83,10 +150,11 @@ int BaseHook::Unhook() {
|
|||||||
DetourUpdateThread(GetCurrentThread());
|
DetourUpdateThread(GetCurrentThread());
|
||||||
DetourDetach((PVOID *)origin_, detour_);
|
DetourDetach((PVOID *)origin_, detour_);
|
||||||
LONG ret = DetourTransactionCommit();
|
LONG ret = DetourTransactionCommit();
|
||||||
if (ret == NO_ERROR) {
|
if (ret == NO_ERROR)
|
||||||
|
{
|
||||||
hook_flag_ = false;
|
hook_flag_ = false;
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace hook
|
} // namespace hook
|
@ -4,35 +4,61 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
namespace hook {
|
namespace hook {
|
||||||
struct InnerMessageStruct {
|
/**
|
||||||
|
* @struct InnerMessageStruct 定义了一个名为 InnerMessageStruct 的结构体,用于在内部消息传递中存储缓冲区和长度信息
|
||||||
|
*/
|
||||||
|
struct InnerMessageStruct {
|
||||||
char* buffer;
|
char* buffer;
|
||||||
int64_t length;
|
int64_t length;
|
||||||
|
// 析构函数,用于释放缓冲区内存
|
||||||
~InnerMessageStruct() {
|
~InnerMessageStruct() {
|
||||||
if (this->buffer != NULL) {
|
if (this->buffer!= NULL) {
|
||||||
delete[] this->buffer;
|
delete[] this->buffer;
|
||||||
this->buffer = NULL;
|
this->buffer = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
void SendHttpMsgCallback(PTP_CALLBACK_INSTANCE instance, PVOID context,
|
/**
|
||||||
PTP_WORK Work);
|
* @brief 发送 HTTP 消息的回调函数
|
||||||
|
* @param instance 回调实例
|
||||||
|
* @param context 上下文
|
||||||
|
* @param Work 工作项
|
||||||
|
*/
|
||||||
|
void SendHttpMsgCallback(PTP_CALLBACK_INSTANCE instance, PVOID context, PTP_WORK Work);
|
||||||
|
|
||||||
void SendTcpMsgCallback(PTP_CALLBACK_INSTANCE instance, PVOID context,
|
/**
|
||||||
PTP_WORK Work);
|
* @brief 发送 TCP 消息的回调函数
|
||||||
class BaseHook {
|
* @param instance 回调实例
|
||||||
|
* @param context 上下文
|
||||||
|
* @param Work 工作项
|
||||||
|
*/
|
||||||
|
void SendTcpMsgCallback(PTP_CALLBACK_INSTANCE instance, PVOID context, PTP_WORK Work);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @class BaseHook 定义了一个名为 BaseHook 的类,作为钩子的基类
|
||||||
|
*/
|
||||||
|
class BaseHook {
|
||||||
public:
|
public:
|
||||||
|
// 构造函数
|
||||||
BaseHook():hook_flag_(false),origin_(nullptr),detour_(nullptr){}
|
BaseHook():hook_flag_(false),origin_(nullptr),detour_(nullptr){}
|
||||||
|
// 构造函数,接收原始函数和替换函数的指针
|
||||||
BaseHook(void* origin, void* detour);
|
BaseHook(void* origin, void* detour);
|
||||||
|
// 钩子函数,用于安装钩子
|
||||||
int Hook();
|
int Hook();
|
||||||
|
// 卸载钩子函数
|
||||||
int Unhook();
|
int Unhook();
|
||||||
|
// 虚析构函数
|
||||||
virtual ~BaseHook() {}
|
virtual ~BaseHook() {}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
// 钩子标志
|
||||||
bool hook_flag_;
|
bool hook_flag_;
|
||||||
|
// 原始函数指针
|
||||||
void* origin_;
|
void* origin_;
|
||||||
|
// 替换函数指针
|
||||||
void* detour_;
|
void* detour_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace hook
|
} // namespace hook
|
||||||
#endif
|
#endif
|
Loading…
Reference in New Issue
Block a user