WeChatFerry/Spy/rpc_server.cpp

286 lines
8.7 KiB
C++
Raw Normal View History

2022-08-07 20:50:49 +08:00
#include <stdio.h>
2021-02-12 23:21:57 +08:00
#include <stdlib.h>
2022-08-21 08:45:16 +08:00
#include "accept_new_friend.h"
2022-08-07 23:32:25 +08:00
#include "exec_sql.h"
2022-08-07 20:50:49 +08:00
#include "get_contacts.h"
2022-08-13 21:55:08 +08:00
#include "receive_msg.h"
2022-08-07 21:17:04 +08:00
#include "rpc_h.h"
2021-02-12 23:21:57 +08:00
#include "rpc_server.h"
2022-08-07 20:08:54 +08:00
#include "sdk.h"
2022-08-07 20:50:49 +08:00
#include "send_msg.h"
2022-08-13 21:55:08 +08:00
#include "spy.h"
2022-08-07 20:50:49 +08:00
#include "spy_types.h"
2022-08-07 23:32:25 +08:00
#include "util.h"
using namespace std;
2021-02-12 23:21:57 +08:00
2022-08-13 21:55:08 +08:00
extern int IsLogin(void); // Defined in spy.cpp
2022-09-25 11:22:24 +08:00
extern wstring GetSelfWxid(); // Defined in spy.cpp
2022-08-13 21:55:08 +08:00
extern HANDLE g_hEvent; // New message signal
extern BOOL g_rpcKeepAlive; // Keep RPC server thread running
extern MsgQueue_t g_MsgQueue; // Queue for message
extern const MsgTypesMap_t g_WxMsgTypes; // Map of WeChat Message types
static BOOL listenMsgFlag = false;
2021-02-12 23:21:57 +08:00
2022-08-20 22:10:11 +08:00
RPC_STATUS CALLBACK SecurityCallback(RPC_IF_HANDLE /*hInterface*/, void * /*pBindingHandle*/)
{
return RPC_S_OK; // Always allow anyone.
}
int RpcStartServer()
{
RPC_STATUS status;
// Uses the protocol combined with the endpoint for receiving
// remote procedure calls.
status = RpcServerUseProtseqEp(reinterpret_cast<RPC_WSTR>((RPC_WSTR)L"ncalrpc"), // Use TCP/IP protocol
RPC_C_LISTEN_MAX_CALLS_DEFAULT, // Backlog queue length for TCP/IP.
reinterpret_cast<RPC_WSTR>((RPC_WSTR)L"wcferry"), // TCP/IP port to use
NULL // No security
);
if (status)
return status;
// Registers the interface and auto listen
// Equal to RpcServerRegisterIf + RpcServerListen
status = RpcServerRegisterIf2(server_ISpy_v1_0_s_ifspec, // Interface to register.
NULL, // Use the MIDL generated entry-point vector.
NULL, // Use the MIDL generated entry-point vector.
RPC_IF_ALLOW_LOCAL_ONLY | RPC_IF_AUTOLISTEN, // Forces use of security callback.
RPC_C_LISTEN_MAX_CALLS_DEFAULT, // Use default number of concurrent calls.
(unsigned)-1, // Infinite max size of incoming data blocks.
SecurityCallback); // Naive security callback.
while (g_rpcKeepAlive) {
Sleep(1000); // 休眠释放CPU
}
return 0;
}
int RpcStopServer()
{
RPC_STATUS status;
UnListenMessage();
listenMsgFlag = false;
g_rpcKeepAlive = false;
status = RpcMgmtStopServerListening(NULL);
if (status)
return status;
status = RpcServerUnregisterIf(server_ISpy_v1_0_s_ifspec, NULL, 0);
return status;
}
2021-02-12 23:21:57 +08:00
int server_IsLogin() { return IsLogin(); }
2022-09-25 11:22:24 +08:00
int server_GetSelfWxId(wchar_t wxid[20]) { return wcscpy_s(wxid, 20, GetSelfWxid().c_str()); }
2021-02-12 23:21:57 +08:00
void server_EnableReceiveMsg()
{
unsigned long ulCode = 0;
2022-08-13 21:55:08 +08:00
ListenMessage();
2022-08-20 15:15:04 +08:00
listenMsgFlag = true;
2021-02-12 23:21:57 +08:00
RpcTryExcept
{
// 调用客户端的回调函数
2022-08-13 21:55:08 +08:00
while (listenMsgFlag) {
2021-02-12 23:21:57 +08:00
// 中断式兼顾及时性和CPU使用率
WaitForSingleObject(g_hEvent, INFINITE); // 等待消息
while (!g_MsgQueue.empty()) {
2022-08-07 15:03:17 +08:00
client_ReceiveMsg(g_MsgQueue.front()); // 调用接收消息回调
2021-02-12 23:21:57 +08:00
g_MsgQueue.pop();
}
ResetEvent(g_hEvent);
}
}
RpcExcept(1)
{
ulCode = RpcExceptionCode();
2022-08-07 15:03:17 +08:00
printf("server_EnableReceiveMsg exception 0x%lx = %ld\n", ulCode, ulCode);
2021-02-12 23:21:57 +08:00
}
RpcEndExcept
}
2022-08-20 15:15:04 +08:00
void server_DisableReceiveMsg()
{
UnListenMessage();
listenMsgFlag = false;
}
2022-08-20 17:39:21 +08:00
int server_SendTextMsg(const wchar_t *wxid, const wchar_t *msg, const wchar_t *atWxids)
2021-02-12 23:21:57 +08:00
{
2022-08-20 17:39:21 +08:00
SendTextMessage(wxid, msg, atWxids);
2021-02-12 23:21:57 +08:00
return 0;
}
2021-08-22 21:57:16 +08:00
int server_SendImageMsg(const wchar_t *wxid, const wchar_t *path)
{
SendImageMessage(wxid, path);
return 0;
}
2022-08-07 20:50:49 +08:00
int server_GetMsgTypes(int *pNum, PPRpcIntBstrPair *msgTypes)
2022-08-07 20:08:54 +08:00
{
2022-08-07 20:50:49 +08:00
*pNum = g_WxMsgTypes.size();
2022-08-20 22:15:58 +08:00
PPRpcIntBstrPair pp = (PPRpcIntBstrPair)midl_user_allocate(*pNum * sizeof(PRpcIntBstrPair));
2022-08-07 20:08:54 +08:00
if (pp == NULL) {
printf("server_GetMsgTypes midl_user_allocate Failed for pp\n");
return -2;
2022-08-07 20:50:49 +08:00
}
int index = 0;
for (auto it = g_WxMsgTypes.begin(); it != g_WxMsgTypes.end(); it++) {
PRpcIntBstrPair p = (PRpcIntBstrPair)midl_user_allocate(sizeof(RpcIntBstrPair_t));
2022-08-07 20:08:54 +08:00
if (p == NULL) {
printf("server_GetMsgTypes midl_user_allocate Failed for p\n");
return -3;
2022-08-07 20:50:49 +08:00
}
2022-08-07 20:08:54 +08:00
2022-08-07 20:50:49 +08:00
p->key = it->first;
p->value = SysAllocString(it->second.c_str());
2022-08-07 20:08:54 +08:00
pp[index++] = p;
2022-08-07 20:50:49 +08:00
}
*msgTypes = pp;
2022-08-07 20:08:54 +08:00
return 0;
2022-08-07 20:50:49 +08:00
}
int server_GetContacts(int *pNum, PPRpcContact *contacts)
{
2022-08-07 23:32:25 +08:00
vector<RpcContact_t> vContacts = GetContacts();
2022-08-07 20:50:49 +08:00
*pNum = vContacts.size();
2022-08-20 22:15:58 +08:00
PPRpcContact pp = (PPRpcContact)midl_user_allocate(*pNum * sizeof(PRpcContact));
2022-08-07 20:50:49 +08:00
if (pp == NULL) {
printf("server_GetMsgTypes midl_user_allocate Failed for pp\n");
return -2;
}
int index = 0;
for (auto it = vContacts.begin(); it != vContacts.end(); it++) {
PRpcContact p = (PRpcContact)midl_user_allocate(sizeof(RpcContact_t));
if (p == NULL) {
printf("server_GetMsgTypes midl_user_allocate Failed for p\n");
return -3;
}
p->wxId = it->wxId;
p->wxCode = it->wxCode;
p->wxName = it->wxName;
p->wxCountry = it->wxCountry;
p->wxProvince = it->wxProvince;
p->wxCity = it->wxCity;
p->wxGender = it->wxGender;
pp[index++] = p;
}
*contacts = pp;
return 0;
}
2021-08-22 21:57:16 +08:00
2022-08-07 23:32:25 +08:00
int server_GetDbNames(int *pNum, BSTR **dbs)
{
vector<wstring> vDbs = GetDbNames();
*pNum = vDbs.size();
BSTR *pp = (BSTR *)midl_user_allocate(*pNum * sizeof(BSTR));
if (pp == NULL) {
printf("server_GetMsgTypes midl_user_allocate Failed for pp\n");
return -2;
}
int index = 0;
for (auto it = vDbs.begin(); it != vDbs.end(); it++) {
pp[index++] = GetBstrFromWstring(*it);
}
*dbs = pp;
return 0;
}
2022-08-07 23:49:37 +08:00
int server_GetDbTables(const wchar_t *db, int *pNum, PPRpcTables *tbls)
{
vector<RpcTables_t> tables = GetDbTables(db);
*pNum = tables.size();
2022-08-20 22:15:58 +08:00
PPRpcTables pp = (PPRpcTables)midl_user_allocate(*pNum * sizeof(PRpcTables));
2022-08-07 23:49:37 +08:00
if (pp == NULL) {
printf("server_GetMsgTypes midl_user_allocate Failed for pp\n");
return -2;
}
int index = 0;
for (auto it = tables.begin(); it != tables.end(); it++) {
2022-08-20 17:39:21 +08:00
PRpcTables p = (PRpcTables)midl_user_allocate(sizeof(RpcTables_t));
2022-08-13 21:57:23 +08:00
if (p == NULL) {
printf("server_GetDbTables midl_user_allocate Failed for p\n");
return -3;
2022-08-20 17:39:21 +08:00
}
2022-08-13 21:57:23 +08:00
2022-08-13 21:55:08 +08:00
p->table = it->table;
p->sql = it->sql;
pp[index++] = p;
2022-08-07 23:49:37 +08:00
}
*tbls = pp;
return 0;
}
2022-08-20 22:10:11 +08:00
int server_ExecDbQuery(const wchar_t *db, const wchar_t *sql, int *pRow, int *pCol, PPPRpcSqlResult *ret)
2021-02-12 23:21:57 +08:00
{
2022-08-20 22:10:11 +08:00
vector<vector<RpcSqlResult_t>> vvSqlResult = ExecDbQuery(db, sql);
if (vvSqlResult.empty()) {
*pRow = *pCol = 0;
ret = NULL;
return -1;
}
*pRow = vvSqlResult.size();
*pCol = vvSqlResult[0].size();
PPPRpcSqlResult ppp = (PPPRpcSqlResult)midl_user_allocate(*pRow * sizeof(PPRpcSqlResult));
if (ppp == NULL) {
printf("server_ExecDbQuery midl_user_allocate Failed for ppp\n");
return -2;
}
2021-02-12 23:21:57 +08:00
2022-08-20 22:10:11 +08:00
for (int r = 0; r < *pRow; r++) {
PPRpcSqlResult pp = (PPRpcSqlResult)midl_user_allocate(*pCol * sizeof(PRpcSqlResult));
if (pp == NULL) {
midl_user_free(ppp);
printf("server_ExecDbQuery midl_user_allocate Failed for pp\n");
return -2;
}
2021-02-12 23:21:57 +08:00
2022-08-20 22:10:11 +08:00
for (int c = 0; c < *pCol; c++) {
PRpcSqlResult p = (PRpcSqlResult)midl_user_allocate(sizeof(RpcSqlResult_t));
if (p == NULL) {
midl_user_free(pp);
printf("server_ExecDbQuery midl_user_allocate Failed for p\n");
return -2;
}
2021-02-12 23:21:57 +08:00
2022-08-20 22:10:11 +08:00
p->type = vvSqlResult[r][c].type;
p->column = vvSqlResult[r][c].column;
p->content = vvSqlResult[r][c].content;
2021-02-12 23:21:57 +08:00
2022-08-20 22:10:11 +08:00
pp[c] = p;
}
2022-08-13 21:55:08 +08:00
2022-08-20 22:10:11 +08:00
ppp[r] = pp;
}
2022-08-13 21:55:08 +08:00
2022-08-20 22:10:11 +08:00
*ret = ppp;
2021-02-12 23:21:57 +08:00
2022-08-20 22:10:11 +08:00
return 0;
2021-02-12 23:21:57 +08:00
}
2022-08-21 08:45:16 +08:00
BOOL server_AcceptNewFriend(const wchar_t *v3, const wchar_t *v4) { return AcceptNewFriend(v3, v4); }