2022-10-15 20:25:42 +08:00
|
|
|
|
#pragma warning(disable : 4251)
|
|
|
|
|
|
2023-02-26 23:07:04 +08:00
|
|
|
|
#include <chrono>
|
|
|
|
|
#include <condition_variable>
|
2022-10-15 20:25:42 +08:00
|
|
|
|
#include <memory>
|
2023-02-26 23:07:04 +08:00
|
|
|
|
#include <mutex>
|
2022-10-15 20:25:42 +08:00
|
|
|
|
#include <queue>
|
|
|
|
|
#include <random>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <thread>
|
|
|
|
|
|
2023-02-16 06:09:37 +08:00
|
|
|
|
#include <nng/nng.h>
|
|
|
|
|
#include <nng/protocol/pair1/pair.h>
|
|
|
|
|
#include <nng/supplemental/util/platform.h>
|
2022-10-15 20:25:42 +08:00
|
|
|
|
|
2023-02-16 06:09:37 +08:00
|
|
|
|
#include "wcf.pb.h"
|
2021-02-12 23:21:57 +08:00
|
|
|
|
|
2022-08-21 08:45:16 +08:00
|
|
|
|
#include "accept_new_friend.h"
|
2023-02-28 20:14:22 +08:00
|
|
|
|
#include "add_chatroom_member.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-10-15 20:25:42 +08:00
|
|
|
|
#include "log.h"
|
2023-02-19 05:34:10 +08:00
|
|
|
|
#include "pb_types.h"
|
2023-02-16 22:04:31 +08:00
|
|
|
|
#include "pb_util.h"
|
2022-08-13 21:55:08 +08:00
|
|
|
|
#include "receive_msg.h"
|
2021-02-12 23:21:57 +08:00
|
|
|
|
#include "rpc_server.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"
|
|
|
|
|
|
2023-02-17 22:06:40 +08:00
|
|
|
|
#define G_BUF_SIZE (16 * 1024 * 1024)
|
2023-02-20 00:58:12 +08:00
|
|
|
|
#define CMD_URL "tcp://0.0.0.0:10086"
|
|
|
|
|
#define MSG_URL "tcp://0.0.0.0:10087"
|
2023-02-16 22:04:31 +08:00
|
|
|
|
|
2023-02-19 05:34:10 +08:00
|
|
|
|
extern int IsLogin(void); // Defined in spy.cpp
|
|
|
|
|
extern string GetSelfWxid(); // Defined in spy.cpp
|
2022-08-13 21:55:08 +08:00
|
|
|
|
|
2022-10-15 20:25:42 +08:00
|
|
|
|
bool gIsListening;
|
2023-02-26 23:07:04 +08:00
|
|
|
|
mutex gMutex;
|
|
|
|
|
condition_variable gCV;
|
2023-02-19 05:34:10 +08:00
|
|
|
|
queue<WxMsg_t> gMsgQueue;
|
2022-10-15 20:25:42 +08:00
|
|
|
|
|
2023-02-16 06:09:37 +08:00
|
|
|
|
static DWORD lThreadId = 0;
|
|
|
|
|
static bool lIsRunning = false;
|
|
|
|
|
static nng_socket sock;
|
2023-02-16 22:04:31 +08:00
|
|
|
|
static uint8_t gBuffer[G_BUF_SIZE] = { 0 };
|
2022-08-20 22:10:11 +08:00
|
|
|
|
|
2023-02-16 06:09:37 +08:00
|
|
|
|
bool func_is_login(uint8_t *out, size_t *len)
|
|
|
|
|
{
|
|
|
|
|
Response rsp = Response_init_default;
|
|
|
|
|
rsp.func = Functions_FUNC_IS_LOGIN;
|
|
|
|
|
rsp.which_msg = Response_status_tag;
|
|
|
|
|
rsp.msg.status = IsLogin();
|
2022-09-25 11:22:24 +08:00
|
|
|
|
|
2023-02-16 06:09:37 +08:00
|
|
|
|
pb_ostream_t stream = pb_ostream_from_buffer(out, *len);
|
|
|
|
|
if (!pb_encode(&stream, Response_fields, &rsp)) {
|
2023-02-17 22:06:40 +08:00
|
|
|
|
LOG_ERROR("Encoding failed: {}", PB_GET_ERROR(&stream));
|
2023-02-16 06:09:37 +08:00
|
|
|
|
return false;
|
2021-02-12 23:21:57 +08:00
|
|
|
|
}
|
2023-02-16 22:04:31 +08:00
|
|
|
|
*len = stream.bytes_written;
|
2022-10-15 20:25:42 +08:00
|
|
|
|
|
2023-02-16 06:09:37 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2022-10-15 20:25:42 +08:00
|
|
|
|
|
2023-02-16 06:09:37 +08:00
|
|
|
|
bool func_get_self_wxid(uint8_t *out, size_t *len)
|
|
|
|
|
{
|
|
|
|
|
Response rsp = Response_init_default;
|
|
|
|
|
rsp.func = Functions_FUNC_IS_LOGIN;
|
|
|
|
|
rsp.which_msg = Response_str_tag;
|
|
|
|
|
rsp.msg.str = (char *)GetSelfWxid().c_str();
|
2023-02-16 22:04:31 +08:00
|
|
|
|
|
|
|
|
|
pb_ostream_t stream = pb_ostream_from_buffer(out, *len);
|
|
|
|
|
if (!pb_encode(&stream, Response_fields, &rsp)) {
|
2023-02-17 22:06:40 +08:00
|
|
|
|
LOG_ERROR("Encoding failed: {}", PB_GET_ERROR(&stream));
|
2023-02-16 06:09:37 +08:00
|
|
|
|
return false;
|
2021-02-12 23:21:57 +08:00
|
|
|
|
}
|
2023-02-16 22:04:31 +08:00
|
|
|
|
*len = stream.bytes_written;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool func_get_msg_types(uint8_t *out, size_t *len)
|
|
|
|
|
{
|
|
|
|
|
Response rsp = Response_init_default;
|
|
|
|
|
rsp.func = Functions_FUNC_GET_MSG_TYPES;
|
|
|
|
|
rsp.which_msg = Response_types_tag;
|
|
|
|
|
|
|
|
|
|
MsgTypes_t types = GetMsgTypes();
|
|
|
|
|
rsp.msg.types.types.funcs.encode = encode_types;
|
|
|
|
|
rsp.msg.types.types.arg = &types;
|
2022-08-20 15:15:04 +08:00
|
|
|
|
|
2023-02-16 06:09:37 +08:00
|
|
|
|
pb_ostream_t stream = pb_ostream_from_buffer(out, *len);
|
|
|
|
|
if (!pb_encode(&stream, Response_fields, &rsp)) {
|
2023-02-17 22:06:40 +08:00
|
|
|
|
LOG_ERROR("Encoding failed: {}", PB_GET_ERROR(&stream));
|
2023-02-16 06:09:37 +08:00
|
|
|
|
return false;
|
2022-10-15 20:25:42 +08:00
|
|
|
|
}
|
2023-02-16 22:04:31 +08:00
|
|
|
|
*len = stream.bytes_written;
|
2021-02-12 23:21:57 +08:00
|
|
|
|
|
2023-02-16 06:09:37 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-02-12 23:21:57 +08:00
|
|
|
|
|
2023-02-16 22:50:18 +08:00
|
|
|
|
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;
|
|
|
|
|
|
2023-02-17 22:06:40 +08:00
|
|
|
|
vector<RpcContact_t> contacts = GetContacts();
|
|
|
|
|
rsp.msg.contacts.contacts.funcs.encode = encode_contacts;
|
|
|
|
|
rsp.msg.contacts.contacts.arg = &contacts;
|
2023-02-16 22:50:18 +08:00
|
|
|
|
|
|
|
|
|
pb_ostream_t stream = pb_ostream_from_buffer(out, *len);
|
|
|
|
|
if (!pb_encode(&stream, Response_fields, &rsp)) {
|
2023-02-17 22:06:40 +08:00
|
|
|
|
LOG_ERROR("Encoding failed: {}", PB_GET_ERROR(&stream));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
*len = stream.bytes_written;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool func_get_db_names(uint8_t *out, size_t *len)
|
|
|
|
|
{
|
|
|
|
|
Response rsp = Response_init_default;
|
|
|
|
|
rsp.func = Functions_FUNC_GET_DB_NAMES;
|
|
|
|
|
rsp.which_msg = Response_dbs_tag;
|
|
|
|
|
|
|
|
|
|
DbNames_t dbnames = GetDbNames();
|
|
|
|
|
rsp.msg.dbs.names.funcs.encode = encode_dbnames;
|
|
|
|
|
rsp.msg.dbs.names.arg = &dbnames;
|
|
|
|
|
|
|
|
|
|
pb_ostream_t stream = pb_ostream_from_buffer(out, *len);
|
|
|
|
|
if (!pb_encode(&stream, Response_fields, &rsp)) {
|
|
|
|
|
LOG_ERROR("Encoding failed: {}", PB_GET_ERROR(&stream));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
*len = stream.bytes_written;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool func_get_db_tables(char *db, uint8_t *out, size_t *len)
|
|
|
|
|
{
|
|
|
|
|
Response rsp = Response_init_default;
|
|
|
|
|
rsp.func = Functions_FUNC_GET_DB_TABLES;
|
|
|
|
|
rsp.which_msg = Response_tables_tag;
|
|
|
|
|
|
|
|
|
|
DbTables_t tables = GetDbTables(db);
|
|
|
|
|
rsp.msg.tables.tables.funcs.encode = encode_tables;
|
|
|
|
|
rsp.msg.tables.tables.arg = &tables;
|
|
|
|
|
|
|
|
|
|
pb_ostream_t stream = pb_ostream_from_buffer(out, *len);
|
|
|
|
|
if (!pb_encode(&stream, Response_fields, &rsp)) {
|
|
|
|
|
LOG_ERROR("Encoding failed: {}", PB_GET_ERROR(&stream));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
*len = stream.bytes_written;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-18 09:08:27 +08:00
|
|
|
|
bool func_send_txt(TextMsg txt, uint8_t *out, size_t *len)
|
|
|
|
|
{
|
|
|
|
|
Response rsp = Response_init_default;
|
|
|
|
|
rsp.func = Functions_FUNC_SEND_TXT;
|
|
|
|
|
rsp.which_msg = Response_status_tag;
|
|
|
|
|
rsp.msg.status = 0;
|
|
|
|
|
|
|
|
|
|
if ((txt.msg == NULL) || (txt.receiver == NULL)) {
|
|
|
|
|
rsp.msg.status = -1; // Empty message or empty receiver
|
|
|
|
|
} else {
|
|
|
|
|
string msg(txt.msg);
|
|
|
|
|
string receiver(txt.receiver);
|
|
|
|
|
string aters(txt.aters ? txt.aters : "");
|
|
|
|
|
|
2023-02-18 19:45:36 +08:00
|
|
|
|
SendTextMessage(receiver, msg, aters);
|
2023-02-18 09:08:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pb_ostream_t stream = pb_ostream_from_buffer(out, *len);
|
|
|
|
|
if (!pb_encode(&stream, Response_fields, &rsp)) {
|
|
|
|
|
LOG_ERROR("Encoding failed: {}", PB_GET_ERROR(&stream));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
*len = stream.bytes_written;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool func_send_img(char *path, char *receiver, uint8_t *out, size_t *len)
|
|
|
|
|
{
|
|
|
|
|
Response rsp = Response_init_default;
|
|
|
|
|
rsp.func = Functions_FUNC_SEND_IMG;
|
|
|
|
|
rsp.which_msg = Response_status_tag;
|
|
|
|
|
rsp.msg.status = 0;
|
|
|
|
|
|
|
|
|
|
if ((path == NULL) || (receiver == NULL)) {
|
|
|
|
|
rsp.msg.status = -1;
|
|
|
|
|
} else {
|
2023-02-18 19:45:36 +08:00
|
|
|
|
SendImageMessage(receiver, path);
|
2023-02-18 09:08:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pb_ostream_t stream = pb_ostream_from_buffer(out, *len);
|
|
|
|
|
if (!pb_encode(&stream, Response_fields, &rsp)) {
|
|
|
|
|
LOG_ERROR("Encoding failed: {}", PB_GET_ERROR(&stream));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
*len = stream.bytes_written;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-23 07:42:58 +08:00
|
|
|
|
bool func_send_file(char *path, char *receiver, uint8_t *out, size_t *len)
|
|
|
|
|
{
|
|
|
|
|
Response rsp = Response_init_default;
|
|
|
|
|
rsp.func = Functions_FUNC_SEND_IMG;
|
|
|
|
|
rsp.which_msg = Response_status_tag;
|
|
|
|
|
rsp.msg.status = 0;
|
|
|
|
|
|
|
|
|
|
if ((path == NULL) || (receiver == NULL)) {
|
|
|
|
|
rsp.msg.status = -1;
|
|
|
|
|
} else {
|
|
|
|
|
SendImageMessage(receiver, path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pb_ostream_t stream = pb_ostream_from_buffer(out, *len);
|
|
|
|
|
if (!pb_encode(&stream, Response_fields, &rsp)) {
|
|
|
|
|
LOG_ERROR("Encoding failed: {}", PB_GET_ERROR(&stream));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
*len = stream.bytes_written;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 04:03:53 +08:00
|
|
|
|
bool func_send_xml(XmlMsg xml, uint8_t *out, size_t *len)
|
|
|
|
|
{
|
|
|
|
|
Response rsp = Response_init_default;
|
|
|
|
|
rsp.func = Functions_FUNC_SEND_XML;
|
|
|
|
|
rsp.which_msg = Response_status_tag;
|
|
|
|
|
rsp.msg.status = 0;
|
|
|
|
|
|
|
|
|
|
if ((xml.content == NULL) || (xml.receiver == NULL)) {
|
|
|
|
|
rsp.msg.status = -1;
|
|
|
|
|
} else {
|
|
|
|
|
string receiver(xml.receiver);
|
|
|
|
|
string content(xml.content);
|
|
|
|
|
string path(xml.path ? xml.path : "");
|
|
|
|
|
uint32_t type = (uint32_t)xml.type;
|
|
|
|
|
SendXmlMessage(receiver, content, path, type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pb_ostream_t stream = pb_ostream_from_buffer(out, *len);
|
|
|
|
|
if (!pb_encode(&stream, Response_fields, &rsp)) {
|
|
|
|
|
LOG_ERROR("Encoding failed: {}", PB_GET_ERROR(&stream));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
*len = stream.bytes_written;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-19 05:34:10 +08:00
|
|
|
|
static void PushMessage()
|
|
|
|
|
{
|
2023-02-20 00:58:12 +08:00
|
|
|
|
static nng_socket msg_sock;
|
|
|
|
|
static uint8_t buffer[G_BUF_SIZE] = { 0 };
|
2023-02-19 05:34:10 +08:00
|
|
|
|
|
|
|
|
|
int rv;
|
|
|
|
|
Response rsp = Response_init_default;
|
|
|
|
|
rsp.func = Functions_FUNC_ENABLE_RECV_TXT;
|
|
|
|
|
rsp.which_msg = Response_wxmsg_tag;
|
|
|
|
|
|
2023-02-20 00:58:12 +08:00
|
|
|
|
pb_ostream_t stream = pb_ostream_from_buffer(buffer, G_BUF_SIZE);
|
|
|
|
|
|
|
|
|
|
char *url = (char *)MSG_URL;
|
|
|
|
|
if ((rv = nng_pair1_open(&msg_sock)) != 0) {
|
2023-03-04 22:43:54 +08:00
|
|
|
|
LOG_ERROR("nng_pair0_open error {}", nng_strerror(rv));
|
2023-02-20 00:58:12 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((rv = nng_listen(msg_sock, url, NULL, 0)) != 0) {
|
2023-03-04 22:43:54 +08:00
|
|
|
|
LOG_ERROR("nng_listen error {}", nng_strerror(rv));
|
2023-02-20 00:58:12 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-04 22:43:54 +08:00
|
|
|
|
LOG_INFO("MSG Server listening on {}", url);
|
2023-02-20 00:58:12 +08:00
|
|
|
|
if ((rv = nng_setopt_ms(msg_sock, NNG_OPT_SENDTIMEO, 2000)) != 0) {
|
2023-03-04 22:43:54 +08:00
|
|
|
|
LOG_ERROR("nng_setopt_ms: {}", nng_strerror(rv));
|
2023-02-20 00:58:12 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2023-02-19 05:34:10 +08:00
|
|
|
|
|
|
|
|
|
while (gIsListening) {
|
2023-02-26 23:07:04 +08:00
|
|
|
|
unique_lock<mutex> lock(gMutex);
|
|
|
|
|
if (gCV.wait_for(lock, chrono::milliseconds(1000), []() { return !gMsgQueue.empty(); })) {
|
|
|
|
|
while (!gMsgQueue.empty()) {
|
2023-02-26 23:07:50 +08:00
|
|
|
|
auto wxmsg = gMsgQueue.front();
|
|
|
|
|
rsp.msg.wxmsg.is_self = wxmsg.is_self;
|
|
|
|
|
rsp.msg.wxmsg.is_group = wxmsg.is_group;
|
|
|
|
|
rsp.msg.wxmsg.type = wxmsg.type;
|
|
|
|
|
rsp.msg.wxmsg.id = (char *)wxmsg.id.c_str();
|
|
|
|
|
rsp.msg.wxmsg.xml = (char *)wxmsg.xml.c_str();
|
|
|
|
|
rsp.msg.wxmsg.sender = (char *)wxmsg.sender.c_str();
|
|
|
|
|
rsp.msg.wxmsg.roomid = (char *)wxmsg.roomid.c_str();
|
|
|
|
|
rsp.msg.wxmsg.content = (char *)wxmsg.content.c_str();
|
2023-02-26 23:07:04 +08:00
|
|
|
|
gMsgQueue.pop();
|
2023-02-26 23:07:50 +08:00
|
|
|
|
LOG_DEBUG("Recv msg: {}", wxmsg.content);
|
2023-02-26 23:07:04 +08:00
|
|
|
|
if (!pb_encode(&stream, Response_fields, &rsp)) {
|
|
|
|
|
LOG_ERROR("Encoding failed: {}", PB_GET_ERROR(&stream));
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rv = nng_send(msg_sock, buffer, stream.bytes_written, 0);
|
|
|
|
|
if (rv != 0) {
|
2023-03-04 22:43:54 +08:00
|
|
|
|
LOG_ERROR("nng_send: {}", nng_strerror(rv));
|
2023-02-26 23:07:04 +08:00
|
|
|
|
}
|
2023-02-27 23:56:46 +08:00
|
|
|
|
LOG_DEBUG("Send data length {}", stream.bytes_written);
|
2023-02-19 05:34:10 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-20 00:58:12 +08:00
|
|
|
|
nng_close(msg_sock);
|
2023-02-19 05:34:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool func_enable_recv_txt(uint8_t *out, size_t *len)
|
|
|
|
|
{
|
|
|
|
|
Response rsp = Response_init_default;
|
|
|
|
|
rsp.func = Functions_FUNC_ENABLE_RECV_TXT;
|
|
|
|
|
rsp.which_msg = Response_status_tag;
|
|
|
|
|
rsp.msg.status = -1;
|
|
|
|
|
|
|
|
|
|
ListenMessage();
|
|
|
|
|
HANDLE msgThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)PushMessage, NULL, NULL, NULL);
|
|
|
|
|
if (msgThread != 0) {
|
|
|
|
|
CloseHandle(msgThread);
|
|
|
|
|
rsp.msg.status = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pb_ostream_t stream = pb_ostream_from_buffer(out, *len);
|
|
|
|
|
if (!pb_encode(&stream, Response_fields, &rsp)) {
|
|
|
|
|
LOG_ERROR("Encoding failed: {}", PB_GET_ERROR(&stream));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
*len = stream.bytes_written;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool func_disable_recv_txt(uint8_t *out, size_t *len)
|
|
|
|
|
{
|
|
|
|
|
Response rsp = Response_init_default;
|
|
|
|
|
rsp.func = Functions_FUNC_DISABLE_RECV_TXT;
|
|
|
|
|
rsp.which_msg = Response_status_tag;
|
|
|
|
|
rsp.msg.status = 0;
|
|
|
|
|
|
|
|
|
|
UnListenMessage(); // 可能需要1秒之后才能退出,见 PushMessage
|
|
|
|
|
|
|
|
|
|
pb_ostream_t stream = pb_ostream_from_buffer(out, *len);
|
|
|
|
|
if (!pb_encode(&stream, Response_fields, &rsp)) {
|
|
|
|
|
LOG_ERROR("Encoding failed: {}", PB_GET_ERROR(&stream));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
*len = stream.bytes_written;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-17 22:06:40 +08:00
|
|
|
|
bool func_exec_db_query(char *db, char *sql, uint8_t *out, size_t *len)
|
|
|
|
|
{
|
|
|
|
|
Response rsp = Response_init_default;
|
|
|
|
|
rsp.func = Functions_FUNC_GET_DB_TABLES;
|
|
|
|
|
rsp.which_msg = Response_rows_tag;
|
|
|
|
|
|
|
|
|
|
DbRows_t rows = ExecDbQuery(db, sql);
|
|
|
|
|
rsp.msg.rows.rows.arg = &rows;
|
|
|
|
|
rsp.msg.rows.rows.funcs.encode = encode_rows;
|
|
|
|
|
|
|
|
|
|
pb_ostream_t stream = pb_ostream_from_buffer(out, *len);
|
|
|
|
|
if (!pb_encode(&stream, Response_fields, &rsp)) {
|
|
|
|
|
LOG_ERROR("Encoding failed: {}", PB_GET_ERROR(&stream));
|
2023-02-16 22:50:18 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
*len = stream.bytes_written;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-21 21:15:37 +08:00
|
|
|
|
bool func_accept_friend(char *v3, char *v4, uint8_t *out, size_t *len)
|
|
|
|
|
{
|
|
|
|
|
Response rsp = Response_init_default;
|
|
|
|
|
rsp.func = Functions_FUNC_SEND_IMG;
|
|
|
|
|
rsp.which_msg = Response_status_tag;
|
|
|
|
|
rsp.msg.status = 0;
|
|
|
|
|
|
|
|
|
|
if ((v3 == NULL) || (v4 == NULL)) {
|
|
|
|
|
rsp.msg.status = -1;
|
|
|
|
|
LOG_ERROR("Empty V3 or V4.");
|
|
|
|
|
} else {
|
|
|
|
|
rsp.msg.status = AcceptNewFriend(v3, v4);
|
|
|
|
|
if (rsp.msg.status != 1) {
|
|
|
|
|
LOG_ERROR("AcceptNewFriend failed: {}", rsp.msg.status);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pb_ostream_t stream = pb_ostream_from_buffer(out, *len);
|
|
|
|
|
if (!pb_encode(&stream, Response_fields, &rsp)) {
|
|
|
|
|
LOG_ERROR("Encoding failed: {}", PB_GET_ERROR(&stream));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
*len = stream.bytes_written;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-28 20:14:22 +08:00
|
|
|
|
bool func_add_room_members(char *roomid, char *wxids, uint8_t *out, size_t *len)
|
|
|
|
|
{
|
|
|
|
|
Response rsp = Response_init_default;
|
|
|
|
|
rsp.func = Functions_FUNC_ADD_ROOM_MEMBERS;
|
|
|
|
|
rsp.which_msg = Response_status_tag;
|
|
|
|
|
rsp.msg.status = 0;
|
|
|
|
|
|
|
|
|
|
rsp.msg.status = AddChatroomMember(roomid, wxids);
|
|
|
|
|
if (rsp.msg.status != 1) {
|
|
|
|
|
LOG_ERROR("AddChatroomMember failed: {}", rsp.msg.status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pb_ostream_t stream = pb_ostream_from_buffer(out, *len);
|
|
|
|
|
if (!pb_encode(&stream, Response_fields, &rsp)) {
|
|
|
|
|
LOG_ERROR("Encoding failed: {}", PB_GET_ERROR(&stream));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
*len = stream.bytes_written;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-16 06:09:37 +08:00
|
|
|
|
static bool dispatcher(uint8_t *in, size_t in_len, uint8_t *out, size_t *out_len)
|
|
|
|
|
{
|
|
|
|
|
bool ret = false;
|
|
|
|
|
Request req = Request_init_default;
|
|
|
|
|
pb_istream_t stream = pb_istream_from_buffer(in, in_len);
|
|
|
|
|
if (!pb_decode(&stream, Request_fields, &req)) {
|
|
|
|
|
LOG_ERROR("Decoding failed: {}", PB_GET_ERROR(&stream));
|
|
|
|
|
pb_release(Request_fields, &req);
|
|
|
|
|
return false;
|
2022-10-15 20:25:42 +08:00
|
|
|
|
}
|
2021-08-22 21:57:16 +08:00
|
|
|
|
|
2023-03-01 04:03:53 +08:00
|
|
|
|
LOG_DEBUG("Func: {:#x} Data: {}", (uint8_t)req.func, in_len);
|
2023-02-16 06:09:37 +08:00
|
|
|
|
switch (req.func) {
|
|
|
|
|
case Functions_FUNC_IS_LOGIN: {
|
2023-02-23 00:01:14 +08:00
|
|
|
|
LOG_DEBUG("[Functions_FUNC_IS_LOGIN]");
|
2023-02-16 06:09:37 +08:00
|
|
|
|
ret = func_is_login(out, out_len);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Functions_FUNC_GET_SELF_WXID: {
|
2023-02-23 00:01:14 +08:00
|
|
|
|
LOG_DEBUG("[Functions_FUNC_GET_SELF_WXID]");
|
2023-02-16 06:09:37 +08:00
|
|
|
|
ret = func_get_self_wxid(out, out_len);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-02-16 22:04:31 +08:00
|
|
|
|
case Functions_FUNC_GET_MSG_TYPES: {
|
2023-02-23 00:01:14 +08:00
|
|
|
|
LOG_DEBUG("[Functions_FUNC_GET_MSG_TYPES]");
|
2023-02-16 22:04:31 +08:00
|
|
|
|
ret = func_get_msg_types(out, out_len);
|
2023-02-16 22:50:18 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Functions_FUNC_GET_CONTACTS: {
|
2023-02-23 00:01:14 +08:00
|
|
|
|
LOG_DEBUG("[Functions_FUNC_GET_CONTACTS]");
|
2023-02-16 22:50:18 +08:00
|
|
|
|
ret = func_get_contacts(out, out_len);
|
2023-02-16 22:04:31 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
2023-02-17 22:06:40 +08:00
|
|
|
|
case Functions_FUNC_GET_DB_NAMES: {
|
2023-02-23 00:01:14 +08:00
|
|
|
|
LOG_DEBUG("[Functions_FUNC_GET_DB_NAMES]");
|
2023-02-17 22:06:40 +08:00
|
|
|
|
ret = func_get_db_names(out, out_len);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Functions_FUNC_GET_DB_TABLES: {
|
2023-02-23 00:01:14 +08:00
|
|
|
|
LOG_DEBUG("[Functions_FUNC_GET_DB_TABLES]");
|
2023-02-17 22:06:40 +08:00
|
|
|
|
ret = func_get_db_tables(req.msg.str, out, out_len);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-02-18 09:08:27 +08:00
|
|
|
|
case Functions_FUNC_SEND_TXT: {
|
2023-02-23 00:01:14 +08:00
|
|
|
|
LOG_DEBUG("[Functions_FUNC_SEND_TXT]");
|
2023-02-18 09:08:27 +08:00
|
|
|
|
ret = func_send_txt(req.msg.txt, out, out_len);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Functions_FUNC_SEND_IMG: {
|
2023-02-23 00:01:14 +08:00
|
|
|
|
LOG_DEBUG("[Functions_FUNC_SEND_IMG]");
|
2023-02-20 01:10:40 +08:00
|
|
|
|
ret = func_send_img(req.msg.file.path, req.msg.file.receiver, out, out_len);
|
2023-02-18 09:08:27 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
2023-02-23 07:42:58 +08:00
|
|
|
|
case Functions_FUNC_SEND_FILE: {
|
|
|
|
|
LOG_DEBUG("[Functions_FUNC_SEND_FILE]");
|
|
|
|
|
ret = func_send_file(req.msg.file.path, req.msg.file.receiver, out, out_len);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-03-01 04:03:53 +08:00
|
|
|
|
case Functions_FUNC_SEND_XML: {
|
|
|
|
|
LOG_DEBUG("[Functions_FUNC_SEND_XML]");
|
|
|
|
|
ret = func_send_xml(req.msg.xml, out, out_len);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-02-19 05:34:10 +08:00
|
|
|
|
case Functions_FUNC_ENABLE_RECV_TXT: {
|
2023-02-23 00:01:14 +08:00
|
|
|
|
LOG_DEBUG("[Functions_FUNC_ENABLE_RECV_TXT]");
|
2023-02-19 05:34:10 +08:00
|
|
|
|
ret = func_enable_recv_txt(out, out_len);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Functions_FUNC_DISABLE_RECV_TXT: {
|
2023-02-23 00:01:14 +08:00
|
|
|
|
LOG_DEBUG("[Functions_FUNC_DISABLE_RECV_TXT]");
|
2023-02-19 05:34:10 +08:00
|
|
|
|
ret = func_disable_recv_txt(out, out_len);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-02-17 22:06:40 +08:00
|
|
|
|
case Functions_FUNC_EXEC_DB_QUERY: {
|
2023-02-23 00:01:14 +08:00
|
|
|
|
LOG_DEBUG("[Functions_FUNC_EXEC_DB_QUERY]");
|
2023-02-17 22:06:40 +08:00
|
|
|
|
ret = func_exec_db_query(req.msg.query.db, req.msg.query.sql, out, out_len);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-02-21 21:15:37 +08:00
|
|
|
|
case Functions_FUNC_ACCEPT_FRIEND: {
|
2023-02-23 00:01:14 +08:00
|
|
|
|
LOG_DEBUG("[Functions_FUNC_ACCEPT_FRIEND]");
|
2023-02-21 21:15:37 +08:00
|
|
|
|
ret = func_accept_friend(req.msg.v.v3, req.msg.v.v4, out, out_len);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-02-28 20:14:22 +08:00
|
|
|
|
case Functions_FUNC_ADD_ROOM_MEMBERS: {
|
|
|
|
|
LOG_DEBUG("[Functions_FUNC_ADD_ROOM_MEMBERS]");
|
|
|
|
|
ret = func_add_room_members(req.msg.m.roomid, req.msg.m.wxids, out, out_len);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-02-16 06:09:37 +08:00
|
|
|
|
default: {
|
|
|
|
|
LOG_ERROR("[UNKNOW FUNCTION]");
|
|
|
|
|
break;
|
2022-10-15 20:25:42 +08:00
|
|
|
|
}
|
2022-08-07 20:50:49 +08:00
|
|
|
|
}
|
2023-02-16 06:09:37 +08:00
|
|
|
|
pb_release(Request_fields, &req);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2022-08-07 20:08:54 +08:00
|
|
|
|
|
2023-02-16 06:09:37 +08:00
|
|
|
|
static int RunServer()
|
|
|
|
|
{
|
|
|
|
|
int rv = 0;
|
2023-02-20 00:58:12 +08:00
|
|
|
|
char *url = (char *)CMD_URL;
|
2023-02-16 06:09:37 +08:00
|
|
|
|
if ((rv = nng_pair1_open(&sock)) != 0) {
|
2023-03-04 22:43:54 +08:00
|
|
|
|
LOG_ERROR("nng_pair0_open error {}", nng_strerror(rv));
|
2023-02-16 06:09:37 +08:00
|
|
|
|
return rv;
|
2022-08-07 20:50:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-16 06:09:37 +08:00
|
|
|
|
if ((rv = nng_listen(sock, url, NULL, 0)) != 0) {
|
2023-03-04 22:43:54 +08:00
|
|
|
|
LOG_ERROR("nng_listen error {}", nng_strerror(rv));
|
2023-02-16 06:09:37 +08:00
|
|
|
|
return rv;
|
2022-10-15 20:25:42 +08:00
|
|
|
|
}
|
2022-08-07 20:50:49 +08:00
|
|
|
|
|
2023-03-04 22:43:54 +08:00
|
|
|
|
LOG_INFO("CMD Server listening on {}", url);
|
2023-02-16 06:09:37 +08:00
|
|
|
|
if ((rv = nng_setopt_ms(sock, NNG_OPT_SENDTIMEO, 1000)) != 0) {
|
2023-03-04 22:43:54 +08:00
|
|
|
|
LOG_ERROR("nng_setopt_ms error: {}", nng_strerror(rv));
|
2023-02-16 06:09:37 +08:00
|
|
|
|
return rv;
|
2022-08-07 20:50:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-16 06:09:37 +08:00
|
|
|
|
lIsRunning = true;
|
|
|
|
|
while (lIsRunning) {
|
|
|
|
|
uint8_t *in = NULL;
|
2023-02-16 22:04:31 +08:00
|
|
|
|
size_t in_len, out_len = G_BUF_SIZE;
|
2023-02-16 06:09:37 +08:00
|
|
|
|
if ((rv = nng_recv(sock, &in, &in_len, NNG_FLAG_ALLOC)) != 0) {
|
2023-03-04 22:43:54 +08:00
|
|
|
|
LOG_ERROR("nng_recv error: {}", nng_strerror(rv));
|
2023-02-16 06:09:37 +08:00
|
|
|
|
break;
|
2022-08-07 20:50:49 +08:00
|
|
|
|
}
|
2023-02-16 22:04:31 +08:00
|
|
|
|
|
2023-03-01 04:03:53 +08:00
|
|
|
|
// LOG_BUFFER(in, in_len);
|
2023-02-16 06:09:37 +08:00
|
|
|
|
if (dispatcher(in, in_len, gBuffer, &out_len)) {
|
2023-02-23 00:01:14 +08:00
|
|
|
|
LOG_DEBUG("Send data length {}", out_len);
|
|
|
|
|
// LOG_BUFFER(gBuffer, out_len);
|
2023-02-16 06:09:37 +08:00
|
|
|
|
rv = nng_send(sock, gBuffer, out_len, 0);
|
|
|
|
|
if (rv != 0) {
|
2023-03-04 22:43:54 +08:00
|
|
|
|
LOG_ERROR("nng_send: {}", nng_strerror(rv));
|
2023-02-16 06:09:37 +08:00
|
|
|
|
}
|
2022-08-07 20:50:49 +08:00
|
|
|
|
|
2023-02-16 06:09:37 +08:00
|
|
|
|
} else {
|
|
|
|
|
// Error
|
|
|
|
|
LOG_ERROR("Dispatcher failed...");
|
|
|
|
|
rv = nng_send(sock, gBuffer, 0, 0);
|
2023-03-04 22:43:54 +08:00
|
|
|
|
if (rv != 0) {
|
|
|
|
|
LOG_ERROR("nng_send: {}", nng_strerror(rv));
|
|
|
|
|
}
|
2023-02-17 22:06:40 +08:00
|
|
|
|
// break;
|
2023-02-16 06:09:37 +08:00
|
|
|
|
}
|
|
|
|
|
nng_free(in, in_len);
|
2022-08-07 20:50:49 +08:00
|
|
|
|
}
|
2023-02-23 00:01:14 +08:00
|
|
|
|
LOG_DEBUG("Leave RunServer");
|
2023-02-16 06:09:37 +08:00
|
|
|
|
return rv;
|
2022-08-07 23:32:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-15 20:25:42 +08:00
|
|
|
|
int RpcStartServer()
|
2023-02-16 06:09:37 +08:00
|
|
|
|
{
|
|
|
|
|
if (lIsRunning) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2023-01-20 11:17:48 +08:00
|
|
|
|
|
2022-10-16 09:54:26 +08:00
|
|
|
|
HANDLE rpcThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)RunServer, NULL, NULL, &lThreadId);
|
2022-10-15 20:25:42 +08:00
|
|
|
|
if (rpcThread != 0) {
|
|
|
|
|
CloseHandle(rpcThread);
|
2022-08-07 23:49:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-15 20:25:42 +08:00
|
|
|
|
int RpcStopServer()
|
2021-02-12 23:21:57 +08:00
|
|
|
|
{
|
2023-02-16 06:09:37 +08:00
|
|
|
|
if (lIsRunning) {
|
|
|
|
|
nng_close(sock);
|
2023-02-19 05:34:10 +08:00
|
|
|
|
UnListenMessage();
|
2023-01-20 11:17:48 +08:00
|
|
|
|
lIsRunning = false;
|
2023-02-19 05:34:10 +08:00
|
|
|
|
Sleep(1000);
|
|
|
|
|
LOG_INFO("Server stoped.");
|
2022-08-20 22:10:11 +08:00
|
|
|
|
}
|
|
|
|
|
return 0;
|
2021-02-12 23:21:57 +08:00
|
|
|
|
}
|