From d6d650335f5990eb33cabb9a06512075a29ae3c7 Mon Sep 17 00:00:00 2001 From: hugy <504650082@qq.com> Date: Mon, 5 Jun 2023 14:10:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20hook=E6=B6=88=E6=81=AF=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0http=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/3.9.2.26.md | 8 +++++++- src/hooks.cc | 50 ++++++++++++++++++++++++++++++++++++++++----- src/hooks.h | 2 +- src/http_handler.cc | 11 +++++++++- 4 files changed, 63 insertions(+), 8 deletions(-) diff --git a/doc/3.9.2.26.md b/doc/3.9.2.26.md index 394dbe8..53bca43 100644 --- a/doc/3.9.2.26.md +++ b/doc/3.9.2.26.md @@ -284,7 +284,10 @@ #### 9.hook消息** ###### 接口功能 -> hook接收文本消息,图片消息,群消息.该接口将hook的消息通过tcp回传给本地的端口 +> hook接收文本消息,图片消息,群消息.该接口将hook的消息通过tcp回传给本地的端口。 +enableHttp=1时,使用url,timeout参数配置服务端的接收地址。请求为post,Content-Type 为json。 +enableHttp=0时,使用ip,port的tcp服务回传消息。 + ###### 接口地址 > [/api/?type=9](/api/?type=9) @@ -297,6 +300,9 @@ |---|---|---|---| |port |true |string| 本地服务端端口,用来接收消息内容 | |ip |true |string| 服务端ip地址,用来接收消息内容,可以是任意ip,即tcp客户端连接的服务端的ip (3.8.1.26版本)| +|url |true |string| http的请求地址,enableHttp=1时,不能为空 | +|timeout |true |string| 超时时间,单位ms| +|enableHttp |true |number| 0/1 :1.启用http 0.不启用http| ###### 返回字段 |返回字段|字段类型|说明 | diff --git a/src/hooks.cc b/src/hooks.cc index cc546a6..f9f4eb0 100644 --- a/src/hooks.cc +++ b/src/hooks.cc @@ -5,8 +5,10 @@ #include #include "thread_pool.h" #include "wechat_function.h" +#include "http_client.h" using namespace nlohmann; using namespace std; + namespace wxhelper { namespace hooks { static int server_port_ = 0; @@ -43,6 +45,8 @@ static DWORD user_info_next_addr_ = 0; UserInfo userinfo = {}; +static bool enable_http_ = false; + VOID CALLBACK SendMsgCallback(PTP_CALLBACK_INSTANCE instance, PVOID context, PTP_WORK Work) { InnerMessageStruct *msg = (InnerMessageStruct *)context; @@ -107,6 +111,25 @@ clean: return; } +VOID CALLBACK SendHttpMsgCallback(PTP_CALLBACK_INSTANCE instance, PVOID context, + PTP_WORK Work) { + InnerMessageStruct *msg = (InnerMessageStruct *)context; + if (msg == NULL) { + SPDLOG_INFO("http msg is null"); + return; + } + + unique_ptr sms(msg); + json j_msg = + json::parse(msg->buffer, msg->buffer + msg->length, nullptr, false); + if (j_msg.is_discarded() == true) { + return; + } + string jstr = j_msg.dump() + "\n"; + HttpClient::GetInstance().SendRequest(jstr); +} + + void __cdecl OnRecvMsg(DWORD msg_addr) { json j_msg; unsigned long long msgid = *(unsigned long long *)(msg_addr + 0x30); @@ -154,8 +177,14 @@ void __cdecl OnRecvMsg(DWORD msg_addr) { inner_msg->buffer = new char[jstr.size() + 1]; memcpy(inner_msg->buffer, jstr.c_str(), jstr.size() + 1); inner_msg->length = jstr.size(); - bool add = ThreadPool::GetInstance().AddWork(SendMsgCallback,inner_msg); - SPDLOG_INFO("add msg work:{}",add); + if(enable_http_){ + bool add = ThreadPool::GetInstance().AddWork(SendHttpMsgCallback,inner_msg); + SPDLOG_INFO("add http msg work:{}",add); + }else{ + bool add = ThreadPool::GetInstance().AddWork(SendMsgCallback,inner_msg); + SPDLOG_INFO("add msg work:{}",add); + } + } @@ -202,8 +231,13 @@ void __cdecl OnSnsTimeLineMsg(DWORD msg_addr) { inner_msg->buffer = new char[jstr.size() + 1]; memcpy(inner_msg->buffer, jstr.c_str(), jstr.size() + 1); inner_msg->length = jstr.size(); - bool add = ThreadPool::GetInstance().AddWork(SendMsgCallback,inner_msg); - SPDLOG_INFO("add sns work:{}",add); + if(enable_http_){ + bool add = ThreadPool::GetInstance().AddWork(SendHttpMsgCallback,inner_msg); + SPDLOG_INFO("add http msg work:{}",add); + }else{ + bool add = ThreadPool::GetInstance().AddWork(SendMsgCallback,inner_msg); + SPDLOG_INFO("add msg work:{}",add); + } } /// @brief hook sns msg implement @@ -221,7 +255,12 @@ _declspec(naked) void HandleSNSMsg() { } } -int HookRecvMsg(char *client_ip, int port) { +int HookRecvMsg(char *client_ip, int port,char* url,uint64_t timeout,bool enable) { + + enable_http_ = enable; + if(enable_http_){ + HttpClient::GetInstance().SetConfig(url,timeout); + } server_port_ = port; strcpy_s(server_ip_, client_ip); DWORD base = Utils::GetWeChatWinBase(); @@ -253,6 +292,7 @@ int HookRecvMsg(char *client_ip, int port) { int UnHookRecvMsg() { server_port_ = 0; + enable_http_ = false; if (!msg_hook_flag_) { SPDLOG_INFO("recv msg hook already called"); return 2; diff --git a/src/hooks.h b/src/hooks.h index 960712e..c16d233 100644 --- a/src/hooks.h +++ b/src/hooks.h @@ -7,7 +7,7 @@ namespace hooks { extern UserInfo userinfo; extern bool user_info_flag_ ; -int HookRecvMsg(char* client_ip, int port); +int HookRecvMsg(char* client_ip, int port,char* url,uint64_t timeout,bool enable); int UnHookRecvMsg(); diff --git a/src/http_handler.cc b/src/http_handler.cc index 11b9250..216f79b 100644 --- a/src/http_handler.cc +++ b/src/http_handler.cc @@ -171,12 +171,21 @@ string Dispatch(struct mg_connection *c, struct mg_http_message *hm) { break; } case WECHAT_MSG_START_HOOK: { + int port = GetIntParam(j_param, "port"); wstring ip = GetWStringParam(j_param, "ip"); string client_ip = Utils::WstringToUTF8(ip); + int enable = GetIntParam(j_param, "enableHttp"); + string s_url =""; + int timeout = 0; + if(enable){ + wstring url = GetWStringParam(j_param, "url"); + timeout = GetIntParam(j_param, "timeout"); + s_url = Utils::WstringToUTF8(url); + } char ip_cstr[16]; strcpy_s(ip_cstr, client_ip.c_str()); - int success = hooks::HookRecvMsg(ip_cstr, port); + int success = hooks::HookRecvMsg(ip_cstr, port,(char *)s_url.c_str(),timeout,enable); json ret_data = {{"code", success}, {"result", "OK"}}; ret = ret_data.dump(); break;