feat: hook消息增加http支持

This commit is contained in:
hugy 2023-06-05 14:10:01 +08:00
parent d22aa9de2d
commit d6d650335f
4 changed files with 63 additions and 8 deletions

View File

@ -284,7 +284,10 @@
#### 9.hook消息**
###### 接口功能
> hook接收文本消息图片消息群消息.该接口将hook的消息通过tcp回传给本地的端口
> hook接收文本消息图片消息群消息.该接口将hook的消息通过tcp回传给本地的端口。
enableHttp=1时使用urltimeout参数配置服务端的接收地址。请求为postContent-Type 为json。
enableHttp=0时使用ipport的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|
###### 返回字段
|返回字段|字段类型|说明 |

View File

@ -5,8 +5,10 @@
#include <nlohmann/json.hpp>
#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<InnerMessageStruct> 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,10 +177,16 @@ 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();
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 msg implement
@ -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();
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 sns work:{}",add);
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;

View File

@ -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();

View File

@ -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;