Refatoring

This commit is contained in:
Changhua 2025-01-31 09:55:44 +08:00
parent bc40b6e922
commit f2c017de10
4 changed files with 36 additions and 32 deletions

View File

@ -49,10 +49,10 @@ QWORD MessageHandler::DispatchMsg(QWORD arg1, QWORD arg2)
if (wxMsg.roomid.find("@chatroom") != std::string::npos) { if (wxMsg.roomid.find("@chatroom") != std::string::npos) {
wxMsg.is_group = true; wxMsg.is_group = true;
wxMsg.sender = wxMsg.is_self ? GetSelfWxid() : GetStringByWstrAddr(arg2 + OS_RECV_MSG_WXID); wxMsg.sender = wxMsg.is_self ? user_info::get_self_wxid() : GetStringByWstrAddr(arg2 + OS_RECV_MSG_WXID);
} else { } else {
wxMsg.is_group = false; wxMsg.is_group = false;
wxMsg.sender = wxMsg.is_self ? GetSelfWxid() : wxMsg.roomid; wxMsg.sender = wxMsg.is_self ? user_info::get_self_wxid() : wxMsg.roomid;
} }
} catch (const std::exception &e) { } catch (const std::exception &e) {
LOG_ERROR(GB2312ToUtf8(e.what())); LOG_ERROR(GB2312ToUtf8(e.what()));

View File

@ -7,11 +7,11 @@
#include "log.hpp" #include "log.hpp"
#include "send_msg.h" #include "send_msg.h"
#include "spy_types.h" #include "spy_types.h"
#include "user_info.h"
#include "util.h" #include "util.h"
extern HANDLE g_hEvent; extern HANDLE g_hEvent;
extern QWORD g_WeChatWinDllAddr; extern QWORD g_WeChatWinDllAddr;
extern string GetSelfWxid(); // Defined in spy.cpp
#define SRTM_SIZE 0x3F0 #define SRTM_SIZE 0x3F0
@ -263,7 +263,7 @@ void SendXmlMessage(string receiver, string xml, string path, QWORD type)
WxString *pReceiver = NewWxStringFromStr(receiver); WxString *pReceiver = NewWxStringFromStr(receiver);
WxString *pXml = NewWxStringFromStr(xml); WxString *pXml = NewWxStringFromStr(xml);
WxString *pPath = NewWxStringFromStr(path); WxString *pPath = NewWxStringFromStr(path);
WxString *pSender = NewWxStringFromStr(GetSelfWxid()); WxString *pSender = NewWxStringFromStr(user_info::get_self_wxid());
sendXmlMsg(pBuf, (QWORD)pSender, (QWORD)pReceiver, (QWORD)pXml, (QWORD)pPath, (QWORD)(&nullBuf), type, 0x4, sign, sendXmlMsg(pBuf, (QWORD)pSender, (QWORD)pReceiver, (QWORD)pXml, (QWORD)pPath, (QWORD)(&nullBuf), type, 0x4, sign,
pBuf2); pBuf2);

View File

@ -6,17 +6,17 @@
#include "user_info.h" #include "user_info.h"
#include "util.h" #include "util.h"
namespace user_info
{
extern UINT64 g_WeChatWinDllAddr; extern UINT64 g_WeChatWinDllAddr;
namespace user_info
{
#define OS_USER_HOME 0x5932770 #define OS_USER_HOME 0x5932770
#define OS_USER_WXID 0x595C270 #define OS_USER_WXID 0x595C270
#define OS_USER_NAME 0x595C3D8 #define OS_USER_NAME 0x595C3D8
#define OS_USER_MOBILE 0x595C318 #define OS_USER_MOBILE 0x595C318
std::string get_home_path() std::string
get_home_path()
{ {
static std::once_flag flag; static std::once_flag flag;
static std::string home_path; static std::string home_path;
@ -29,28 +29,34 @@ std::string get_home_path()
return home_path; return home_path;
} }
std::optional<std::string> get_self_wxid() std::string get_self_wxid()
{ {
static std::once_flag flag;
static std::string wxid;
std::call_once(flag, [] {
UINT64 wxid_type = 0; UINT64 wxid_type = 0;
try { try {
wxid_type = GET_UINT64(g_WeChatWinDllAddr + OS_USER_WXID + 0x18); wxid_type = GET_UINT64(g_WeChatWinDllAddr + OS_USER_WXID + 0x18);
if (wxid_type == 0xF) { if (wxid_type == 0xF) {
return GET_STRING_FROM_P(g_WeChatWinDllAddr + OS_USER_WXID); wxid = GET_STRING_FROM_P(g_WeChatWinDllAddr + OS_USER_WXID);
} else { } else {
return GET_STRING(g_WeChatWinDllAddr + OS_USER_WXID); wxid = GET_STRING(g_WeChatWinDllAddr + OS_USER_WXID);
} }
} catch (...) { } catch (...) {
LOG_ERROR("Failed to get wxid, type: {:#x}", wxid_type); LOG_ERROR("Failed to get wxid, type: {:#x}", wxid_type);
LOG_BUFFER(reinterpret_cast<uint8_t *>(g_WeChatWinDllAddr + OS_USER_WXID), 20); LOG_BUFFER(reinterpret_cast<uint8_t *>(g_WeChatWinDllAddr + OS_USER_WXID), 20);
return std::nullopt; wxid = "获取wxid失败";
} }
});
return wxid;
} }
UserInfo_t get_user_info() UserInfo_t get_user_info()
{ {
UserInfo_t ui; UserInfo_t ui;
auto wxid = get_self_wxid(); ui.wxid = get_self_wxid();
ui.wxid = wxid.value_or("unknown_wxid");
UINT64 name_type = GET_UINT64(g_WeChatWinDllAddr + OS_USER_NAME + 0x18); UINT64 name_type = GET_UINT64(g_WeChatWinDllAddr + OS_USER_NAME + 0x18);
ui.name = (name_type == 0xF) ? GET_STRING_FROM_P(g_WeChatWinDllAddr + OS_USER_NAME) ui.name = (name_type == 0xF) ? GET_STRING_FROM_P(g_WeChatWinDllAddr + OS_USER_NAME)
@ -64,20 +70,18 @@ UserInfo_t get_user_info()
bool rpc_get_self_wxid(uint8_t *out, size_t *len) bool rpc_get_self_wxid(uint8_t *out, size_t *len)
{ {
return fill_response<Functions_FUNC_GET_SELF_WXID>(out, len, [](Response &rsp) { return fill_response<Functions_FUNC_GET_SELF_WXID>(
auto wxid = get_self_wxid(); out, len, [](Response &rsp) { rsp.msg.str = (char *)get_self_wxid().c_str(); });
rsp.msg.str = wxid ? wxid->c_str() : "error";
});
} }
bool rpc_get_user_info(uint8_t *out, size_t *len) bool rpc_get_user_info(uint8_t *out, size_t *len)
{ {
return fill_response<Functions_FUNC_GET_USER_INFO>(out, len, [](Response &rsp) { return fill_response<Functions_FUNC_GET_USER_INFO>(out, len, [](Response &rsp) {
UserInfo_t ui = get_user_info(); UserInfo_t ui = get_user_info();
rsp.msg.ui.wxid = ui.wxid.c_str(); rsp.msg.ui.wxid = (char *)ui.wxid.c_str();
rsp.msg.ui.name = ui.name.c_str(); rsp.msg.ui.name = (char *)ui.name.c_str();
rsp.msg.ui.mobile = ui.mobile.c_str(); rsp.msg.ui.mobile = (char *)ui.mobile.c_str();
rsp.msg.ui.home = ui.home.c_str(); rsp.msg.ui.home = (char *)ui.home.c_str();
}); });
} }

View File

@ -12,7 +12,7 @@ namespace user_info
std::string get_home_path(); std::string get_home_path();
// 获取自身 wxid // 获取自身 wxid
std::optional<std::string> get_self_wxid(); std::string get_self_wxid();
// 获取用户信息 // 获取用户信息
UserInfo_t get_user_info(); UserInfo_t get_user_info();