refactoring

This commit is contained in:
Changhua 2025-01-31 01:08:55 +08:00
parent bfe3336753
commit 86a55cf00c
3 changed files with 70 additions and 32 deletions

View File

@ -75,7 +75,7 @@ static bool func_is_login(uint8_t *out, size_t *len)
static bool func_get_self_wxid(uint8_t *out, size_t *len)
{
return FillResponse<Functions_FUNC_GET_SELF_WXID>(Response_str_tag, out, len, [](Response &rsp) {
std::string wxid = GetSelfWxid();
std::string wxid = user_info::get_self_wxid();
rsp.msg.str = wxid.empty() ? nullptr : (char *)wxid.c_str();
});
}
@ -83,7 +83,7 @@ static bool func_get_self_wxid(uint8_t *out, size_t *len)
static bool func_get_user_info(uint8_t *out, size_t *len)
{
return FillResponse<Functions_FUNC_GET_USER_INFO>(Response_ui_tag, out, len, [](Response &rsp) {
UserInfo_t ui = GetUserInfo();
UserInfo_t ui = user_info::get_user_info();
rsp.msg.ui.wxid = (char *)ui.wxid.c_str();
rsp.msg.ui.name = (char *)ui.name.c_str();
rsp.msg.ui.mobile = (char *)ui.mobile.c_str();

View File

@ -1,7 +1,14 @@
#include "user_info.h"
#include <filesystem>
#include <mutex>
#include "fill_response.h"
#include "log.hpp"
#include "user_info.h"
#include "util.h"
namespace user_info
{
extern UINT64 g_WeChatWinDllAddr;
#define OS_USER_HOME 0x5932770
@ -9,50 +16,69 @@ extern UINT64 g_WeChatWinDllAddr;
#define OS_USER_NAME 0x595C3D8
#define OS_USER_MOBILE 0x595C318
static char home[MAX_PATH] = { 0 };
string GetHomePath()
std::string get_home_path()
{
if (home[0] == 0) {
string path = Wstring2String(GET_WSTRING(g_WeChatWinDllAddr + OS_USER_HOME)) + "\\WeChat Files\\";
strncpy_s(home, path.c_str(), path.size());
static std::once_flag flag;
static std::string home_path;
std::call_once(flag, [] {
std::string path = Wstring2String(GET_WSTRING(g_WeChatWinDllAddr + OS_USER_HOME)) + "\\WeChat Files\\";
home_path = std::filesystem::absolute(path).string();
});
return home_path;
}
return string(home);
}
string GetSelfWxid()
std::optional<std::string> get_self_wxid()
{
UINT64 wxidType = 0;
UINT64 wxid_type = 0;
try {
wxidType = GET_UINT64(g_WeChatWinDllAddr + OS_USER_WXID + 0x18);
if (wxidType == 0xF) {
wxid_type = GET_UINT64(g_WeChatWinDllAddr + OS_USER_WXID + 0x18);
if (wxid_type == 0xF) {
return GET_STRING_FROM_P(g_WeChatWinDllAddr + OS_USER_WXID);
} else {
return GET_STRING(g_WeChatWinDllAddr + OS_USER_WXID);
}
} catch (...) {
LOG_ERROR("wxid type: {:#x}", wxidType);
LOG_BUFFER((uint8_t *)(g_WeChatWinDllAddr + OS_USER_WXID), 20);
return "empty_wxid";
LOG_ERROR("Failed to get wxid, type: {:#x}", wxid_type);
LOG_BUFFER(reinterpret_cast<uint8_t *>(g_WeChatWinDllAddr + OS_USER_WXID), 20);
return std::nullopt;
}
}
UserInfo_t GetUserInfo()
UserInfo_t get_user_info()
{
UserInfo_t ui;
auto wxid = get_self_wxid();
ui.wxid = wxid.value_or("unknown_wxid");
ui.wxid = GetSelfWxid();
UINT64 nameType = GET_UINT64(g_WeChatWinDllAddr + OS_USER_NAME + 0x18);
if (nameType == 0xF) {
ui.name = GET_STRING_FROM_P(g_WeChatWinDllAddr + OS_USER_NAME);
} else { // 0x1F
ui.name = GET_STRING(g_WeChatWinDllAddr + OS_USER_NAME);
}
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)
: GET_STRING(g_WeChatWinDllAddr + OS_USER_NAME);
ui.mobile = GET_STRING_FROM_P(g_WeChatWinDllAddr + OS_USER_MOBILE);
ui.home = GetHomePath();
ui.home = get_home_path();
return ui;
}
bool rpc_get_self_wxid(uint8_t *out, size_t *len)
{
return fill_response<Functions_FUNC_GET_SELF_WXID>(out, len, [](Response &rsp) {
auto wxid = get_self_wxid();
rsp.msg.str = wxid ? wxid->c_str() : "error";
});
}
bool rpc_get_user_info(uint8_t *out, size_t *len)
{
return fill_response<Functions_FUNC_GET_USER_INFO>(out, len, [](Response &rsp) {
UserInfo_t ui = get_user_info();
rsp.msg.ui.wxid = ui.wxid.c_str();
rsp.msg.ui.name = ui.name.c_str();
rsp.msg.ui.mobile = ui.mobile.c_str();
rsp.msg.ui.home = ui.home.c_str();
});
}
} // namespace user_info

View File

@ -1,12 +1,24 @@
#pragma once
#include <optional>
#include <string>
#include "pb_types.h"
using namespace std;
namespace user_info
{
string GetHomePath();
string GetSelfWxid();
// 获取 WeChat 数据存储路径
std::string get_home_path();
UserInfo_t GetUserInfo();
// 获取自身 wxid
std::optional<std::string> get_self_wxid();
// 获取用户信息
UserInfo_t get_user_info();
// RPC 方法
bool rpc_get_self_wxid(uint8_t *out, size_t *len);
bool rpc_get_user_info(uint8_t *out, size_t *len);
} // namespace user_info