2022-10-15 20:25:42 +08:00
|
|
|
|
#pragma warning(disable : 4251)
|
|
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
#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"
|
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-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-16 22:04:31 +08:00
|
|
|
|
#define G_BUF_SIZE (1024 * 1024)
|
|
|
|
|
|
2022-10-15 20:25:42 +08:00
|
|
|
|
extern int IsLogin(void); // Defined in spy.cpp
|
|
|
|
|
extern std::string GetSelfWxid(); // Defined in spy.cpp
|
2022-08-13 21:55:08 +08:00
|
|
|
|
|
2022-10-15 20:25:42 +08:00
|
|
|
|
using namespace std;
|
2021-02-12 23:21:57 +08:00
|
|
|
|
|
2022-10-15 20:25:42 +08:00
|
|
|
|
mutex gMutex;
|
|
|
|
|
queue<WxMsg> gMsgQueue;
|
|
|
|
|
condition_variable gCv;
|
|
|
|
|
bool gIsListening;
|
|
|
|
|
|
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)) {
|
|
|
|
|
printf("Encoding failed: %s\n", PB_GET_ERROR(&stream));
|
|
|
|
|
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)) {
|
|
|
|
|
printf("Encoding failed: %s\n", 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)) {
|
|
|
|
|
printf("Encoding failed: %s\n", PB_GET_ERROR(&stream));
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
vector<RpcContact_t> 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;
|
|
|
|
|
}
|
|
|
|
|
|
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-02-16 06:09:37 +08:00
|
|
|
|
LOG_INFO("Func: {}", (uint8_t)req.func);
|
|
|
|
|
switch (req.func) {
|
|
|
|
|
case Functions_FUNC_IS_LOGIN: {
|
|
|
|
|
LOG_INFO("[Functions_FUNC_IS_LOGIN]");
|
|
|
|
|
ret = func_is_login(out, out_len);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Functions_FUNC_GET_SELF_WXID: {
|
|
|
|
|
LOG_INFO("[Functions_FUNC_GET_SELF_WXID]");
|
|
|
|
|
ret = func_get_self_wxid(out, out_len);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-02-16 22:04:31 +08:00
|
|
|
|
case Functions_FUNC_GET_MSG_TYPES: {
|
|
|
|
|
LOG_INFO("[Functions_FUNC_GET_MSG_TYPES]");
|
|
|
|
|
ret = func_get_msg_types(out, out_len);
|
2023-02-16 22:50:18 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Functions_FUNC_GET_CONTACTS: {
|
|
|
|
|
LOG_INFO("[Functions_FUNC_GET_CONTACTS]");
|
|
|
|
|
ret = func_get_contacts(out, out_len);
|
2023-02-16 22:04:31 +08:00
|
|
|
|
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;
|
|
|
|
|
char *url = (char *)"tcp://0.0.0.0:10086";
|
|
|
|
|
if ((rv = nng_pair1_open(&sock)) != 0) {
|
|
|
|
|
LOG_ERROR("nng_pair0_open error {}", rv);
|
|
|
|
|
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) {
|
|
|
|
|
LOG_ERROR("nng_listen error {}", rv);
|
|
|
|
|
return rv;
|
2022-10-15 20:25:42 +08:00
|
|
|
|
}
|
2022-08-07 20:50:49 +08:00
|
|
|
|
|
2023-02-16 06:09:37 +08:00
|
|
|
|
LOG_INFO("Server listening on {}", url);
|
|
|
|
|
if ((rv = nng_setopt_ms(sock, NNG_OPT_SENDTIMEO, 1000)) != 0) {
|
|
|
|
|
LOG_ERROR("nng_recv: {}", rv);
|
|
|
|
|
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) {
|
|
|
|
|
LOG_ERROR("nng_recv: {}", rv);
|
|
|
|
|
break;
|
2022-08-07 20:50:49 +08:00
|
|
|
|
}
|
2023-02-16 22:04:31 +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-16 22:04:31 +08:00
|
|
|
|
log_buffer(gBuffer, out_len);
|
2023-02-16 06:09:37 +08:00
|
|
|
|
rv = nng_send(sock, gBuffer, out_len, 0);
|
|
|
|
|
if (rv != 0) {
|
|
|
|
|
LOG_ERROR("nng_send: {}", rv);
|
|
|
|
|
}
|
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);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
nng_free(in, in_len);
|
2022-08-07 20:50:49 +08:00
|
|
|
|
}
|
2023-02-16 06:09:37 +08:00
|
|
|
|
LOG_INFO("Leave RunServer");
|
|
|
|
|
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);
|
|
|
|
|
// UnListenMessage(); // Do it in RpcDisableRecvMsg
|
|
|
|
|
LOG_INFO("Server stoped.");
|
2023-01-20 11:17:48 +08:00
|
|
|
|
lIsRunning = false;
|
2022-08-20 22:10:11 +08:00
|
|
|
|
}
|
|
|
|
|
return 0;
|
2021-02-12 23:21:57 +08:00
|
|
|
|
}
|