From 33a5ed4033fecc222e9f580f88fae6d5309cdb4b Mon Sep 17 00:00:00 2001 From: Changhua Date: Thu, 16 Feb 2023 22:50:18 +0800 Subject: [PATCH] Impl GetContacts --- rpc/pb_types.h | 14 +++++++++++++- rpc/pb_util.cpp | 39 +++++++++++++++++++++++++++++++++++++++ rpc/pb_util.h | 3 ++- spy/Spy.vcxproj | 3 ++- spy/Spy.vcxproj.filters | 3 +++ spy/get_contacts.cpp | 26 +++++++++++++------------- spy/get_contacts.h | 9 +++++---- spy/rpc_server.cpp | 25 +++++++++++++++++++++++++ 8 files changed, 102 insertions(+), 20 deletions(-) diff --git a/rpc/pb_types.h b/rpc/pb_types.h index 97ebe58..ef5e9c8 100644 --- a/rpc/pb_types.h +++ b/rpc/pb_types.h @@ -3,4 +3,16 @@ #include #include -typedef std::map MsgTypes_t; \ No newline at end of file +using namespace std; + +typedef map MsgTypes_t; + +typedef struct { + int32_t gender; + string wxid; + string code; + string name; + string country; + string province; + string city; +} RpcContact_t; diff --git a/rpc/pb_util.cpp b/rpc/pb_util.cpp index 0ec3607..9a1347c 100644 --- a/rpc/pb_util.cpp +++ b/rpc/pb_util.cpp @@ -61,3 +61,42 @@ bool encode_types(pb_ostream_t *stream, const pb_field_t *field, void *const *ar return true; } + +bool encode_contacts(pb_ostream_t *stream, const pb_field_t *field, void *const *arg) +{ + vector *v = (vector *)*arg; + RpcContact message = RpcContact_init_default; + + LOG_INFO("encode_contacts[{}]", v->size()); + for (auto it = v->begin(); it != v->end(); it++) { + message.wxid.funcs.encode = &encode_string; + message.wxid.arg = (void *)(*it).wxid.c_str(); + + message.code.funcs.encode = &encode_string; + message.code.arg = (void *)(*it).code.c_str(); + + message.name.funcs.encode = &encode_string; + message.name.arg = (void *)(*it).name.c_str(); + + message.country.funcs.encode = &encode_string; + message.country.arg = (void *)(*it).country.c_str(); + + message.province.funcs.encode = &encode_string; + message.province.arg = (void *)(*it).province.c_str(); + + message.city.funcs.encode = &encode_string; + message.city.arg = (void *)(*it).city.c_str(); + + message.gender = (*it).gender; + + if (!pb_encode_tag_for_field(stream, field)) { + return false; + } + + if (!pb_encode_submessage(stream, RpcContact_fields, &message)) { + return false; + } + } + + return true; +} diff --git a/rpc/pb_util.h b/rpc/pb_util.h index 34ab1eb..ce5b5f9 100644 --- a/rpc/pb_util.h +++ b/rpc/pb_util.h @@ -6,4 +6,5 @@ void log_buffer(uint8_t *buffer, size_t len); bool encode_string(pb_ostream_t *stream, const pb_field_t *field, void *const *arg); bool decode_string(pb_istream_t *stream, const pb_field_t *field, void **arg); -bool encode_types(pb_ostream_t *stream, const pb_field_t *field, void *const *arg); \ No newline at end of file +bool encode_types(pb_ostream_t *stream, const pb_field_t *field, void *const *arg); +bool encode_contacts(pb_ostream_t *stream, const pb_field_t *field, void *const *arg); diff --git a/spy/Spy.vcxproj b/spy/Spy.vcxproj index a982ca0..f7c120a 100644 --- a/spy/Spy.vcxproj +++ b/spy/Spy.vcxproj @@ -77,7 +77,7 @@ false - true + false true @@ -194,6 +194,7 @@ C:\Tools\nanopb\protoc --nanopb_out=. wcf.proto + diff --git a/spy/Spy.vcxproj.filters b/spy/Spy.vcxproj.filters index 8adbac0..8a4b8e1 100644 --- a/spy/Spy.vcxproj.filters +++ b/spy/Spy.vcxproj.filters @@ -72,6 +72,9 @@ nnrpc + + nnrpc + diff --git a/spy/get_contacts.cpp b/spy/get_contacts.cpp index c55550d..05fcb2c 100644 --- a/spy/get_contacts.cpp +++ b/spy/get_contacts.cpp @@ -1,5 +1,5 @@ #pragma execution_character_set("utf-8") -#if 0 + #include "get_contacts.h" #include "load_calls.h" #include "util.h" @@ -7,26 +7,26 @@ extern WxCalls_t g_WxCalls; extern DWORD g_WeChatWinDllAddr; -bool GetContacts(wcf::Contacts *contacts) +vector GetContacts() { + vector contacts; DWORD baseAddr = g_WeChatWinDllAddr + g_WxCalls.contact.base; DWORD tempAddr = GET_DWORD(baseAddr); DWORD head = GET_DWORD(tempAddr + g_WxCalls.contact.head); DWORD node = GET_DWORD(head); while (node != head) { - wcf::Contact *cnt = contacts->add_contacts(); - cnt->set_wxid(GetStringByAddress(node + g_WxCalls.contact.wxId)); - cnt->set_code(GetStringByAddress(node + g_WxCalls.contact.wxCode)); - cnt->set_name(GetStringByAddress(node + g_WxCalls.contact.wxName)); - cnt->set_country(GetStringByAddress(node + g_WxCalls.contact.wxCountry)); - cnt->set_province(GetStringByAddress(node + g_WxCalls.contact.wxProvince)); - cnt->set_city(GetStringByAddress(node + g_WxCalls.contact.wxCity)); - cnt->set_gender(GET_DWORD(node + g_WxCalls.contact.wxGender)); - + RpcContact_t cnt; + cnt.wxid = GetStringByAddress(node + g_WxCalls.contact.wxId); + cnt.code = GetStringByAddress(node + g_WxCalls.contact.wxCode); + cnt.name = GetStringByAddress(node + g_WxCalls.contact.wxName); + cnt.country = GetStringByAddress(node + g_WxCalls.contact.wxCountry); + cnt.province = GetStringByAddress(node + g_WxCalls.contact.wxProvince); + cnt.city = GetStringByAddress(node + g_WxCalls.contact.wxCity); + cnt.gender = GET_DWORD(node + g_WxCalls.contact.wxGender); + contacts.push_back(cnt); node = GET_DWORD(node); } - return true; + return contacts; } -#endif \ No newline at end of file diff --git a/spy/get_contacts.h b/spy/get_contacts.h index 3c68ffe..a0ff70d 100644 --- a/spy/get_contacts.h +++ b/spy/get_contacts.h @@ -1,6 +1,7 @@ #pragma once -#if 0 -#include "../proto/wcf.grpc.pb.h" -bool GetContacts(wcf::Contacts *contacts); -#endif \ No newline at end of file +#include + +#include "pb_types.h" + +vector GetContacts(); diff --git a/spy/rpc_server.cpp b/spy/rpc_server.cpp index ed8ead6..b78e7bd 100644 --- a/spy/rpc_server.cpp +++ b/spy/rpc_server.cpp @@ -96,6 +96,26 @@ bool func_get_msg_types(uint8_t *out, size_t *len) return true; } +bool func_get_contacts(uint8_t *out, size_t *len) +{ + Response rsp = Response_init_default; + rsp.func = Functions_FUNC_GET_CONTACTS; + rsp.which_msg = Response_contacts_tag; + + vector contacts = GetContacts(); + rsp.msg.types.types.funcs.encode = encode_contacts; + rsp.msg.types.types.arg = &contacts; + + pb_ostream_t stream = pb_ostream_from_buffer(out, *len); + if (!pb_encode(&stream, Response_fields, &rsp)) { + printf("Encoding failed: %s\n", PB_GET_ERROR(&stream)); + return false; + } + *len = stream.bytes_written; + + return true; +} + static bool dispatcher(uint8_t *in, size_t in_len, uint8_t *out, size_t *out_len) { bool ret = false; @@ -124,6 +144,11 @@ static bool dispatcher(uint8_t *in, size_t in_len, uint8_t *out, size_t *out_len ret = func_get_msg_types(out, out_len); break; } + case Functions_FUNC_GET_CONTACTS: { + LOG_INFO("[Functions_FUNC_GET_CONTACTS]"); + ret = func_get_contacts(out, out_len); + break; + } default: { LOG_ERROR("[UNKNOW FUNCTION]"); break;