forked from Garfield-yin/wework-chat-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wework.cc
543 lines (461 loc) · 18.3 KB
/
wework.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include <limits>
#include <thread>
#include <mutex>
#include <chrono>
#include <stdexcept>
#include <cctype>
#include "openssl/md5.h"
#include "openssl/sha.h"
#include "openssl/des.h"
#include "openssl/rsa.h"
#include "openssl/pem.h"
#include <openssl/hmac.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/bn.h>
#include <openssl/pem.h>
#include "include/wework/wework.h"
#include "include/rapidjson/document.h"
#include "include/rapidjson/stringbuffer.h"
#include "include/rapidjson/writer.h"
#include "include/rapidjson/error/en.h"
using namespace std;
using namespace rapidjson;
//WeWorkFinanceSdk_t *WeWorkChat::sdk_;
//std::string WeWorkChat::private_key_;
//int64_t WeWorkChat::seq_;
//bool WeWorkChat::end_;
//std::mutex WeWorkChat::mtx_;
std::string ERROR_PREFIX = "WEWORK_CHAT_NODE::";
//// thread/////
void FinalizerCallback(Napi::Env env, void *finalizeData, TsfnContext *context);
// The thread-safe function finalizer callback. This callback executes
// at destruction of thread-safe function, taking as arguments the finalizer
// data and threadsafe-function context.
void FinalizerCallback(Napi::Env env, void *finalizeData,
TsfnContext *context) {
// Join the thread
context->nativeThread.join();
// Resolve the Promise previously returned to JS via the CreateTSFN method.
context->deferred.Resolve(Napi::Boolean::New(env, true));
delete context;
}
void MediaDataFinalizerCallback(Napi::Env env,void *finalizeData,::MediaData_t* finalizeHint);
void MediaDataFinalizerCallback(Napi::Env env,void *finalizeData,::MediaData_t* media) {
// 释放数据
::FreeMediaData(media);
}
static const char reverse_table[128] = {
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64
};
const int max_results = 1000;
Napi::Object WeWorkChat::Init(Napi::Env env, Napi::Object exports) {
Napi::Function func =
DefineClass(env,
"WeWorkChat",
{
InstanceMethod("getMediaData", &WeWorkChat::GetMediaData),
InstanceMethod("fetchData", &WeWorkChat::StartFetchData),
InstanceMethod("getChatData", &WeWorkChat::GetChat),
InstanceMethod("stopFetch", &WeWorkChat::EndFetchData)});
Napi::FunctionReference* constructor = new Napi::FunctionReference();
*constructor = Napi::Persistent(func);
env.SetInstanceData(constructor);
exports.Set("WeWorkChat", func);
return exports;
}
int WeWorkChat::initSdk(const Napi::CallbackInfo& info){
int ret = 0;
// new sdk api
this->sdk_ = ::NewSdk();
ret = ::Init(this->sdk_, this->corpid_.c_str(), this->secret_.c_str());
if (ret != 0)
{
printf("%sinit sdk err ret:%d\n", ERROR_PREFIX.c_str(), ret);
Napi::TypeError::New(info.Env(), "Init WeWorkFinance sdk error.")
.ThrowAsJavaScriptException();
return -1;
}
return 0;
}
WeWorkChat::WeWorkChat(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<WeWorkChat>(info) {
Napi::Env env = info.Env();
int length = info.Length();
if (length <= 0 || !info[0].IsObject()) {
Napi::TypeError::New(env, "Expected one object argument").ThrowAsJavaScriptException();
return;
}
Napi::Object obj = info[0].As<Napi::Object>();
//value.ToObject()
this->corpid_ = obj.Get("corpid").ToString();
this->secret_ = obj.Get("secret").ToString();
this->private_key_ = obj.Get("private_key").ToString();
this->seq_ = obj.Get("seq").ToNumber();
//cout <<"corpid:"<< this->corpid_<<",secret:"<< this->secret_<<",seq:"<<this->seq_ << endl;
this->end_ = false;
this->initSdk(info);
}
Napi::Value WeWorkChat::EndFetchData(const Napi::CallbackInfo& info) {
this->end_ = true;
// sleep 一下,确保正在执行的线程执行完毕
std::this_thread::sleep_for(std::chrono::milliseconds(800));
// sdk 由用户手动释放
DestroySdk(this->sdk_);
return Napi::Number::New(info.Env(), this->seq_);
}
Napi::Value WeWorkChat::GetChat(const Napi::CallbackInfo& info){
Napi::Env env = info.Env();
int length = info.Length();
if (length <= 0 || !info[0].IsObject()) {
Napi::TypeError::New(env, "Expected one object argument").ThrowAsJavaScriptException();
}
Napi::Object obj = info[0].As<Napi::Object>();
std::string sdk_fileid = obj.Get("max_results").ToString();
std::int64_t seq = obj.Get("seq").ToNumber();
std::int64_t timeout = obj.Get("timeout").ToNumber();
if (!seq) seq= 0;
Slice_t *chatDatas = NewSlice();
// getchatdata api
const int numOfRetries = 3;
int cnt = 1;
int ret = 0;
do {
ret = GetChatData(this->sdk_, seq, max_results, "", "", timeout, chatDatas);
if (ret >= 10001 && ret <= 10003){
//cout << "\t try number#" << cnt <<" fail \n";
std::this_thread::sleep_for(std::chrono::seconds(1));
++cnt;
} else{
break;
}
}while (cnt <= numOfRetries);
if (ret != 0)
{
printf("%sGetChatData err ret:%d\n",ERROR_PREFIX.c_str(), ret);
char errMsg[256];
sprintf(errMsg, "%sGetChatData err ret:%d\n",ERROR_PREFIX.c_str(), ret);
Napi::Error::New(env, errMsg).ThrowAsJavaScriptException();
return env.Null();
}
char *data = GetContentFromSlice(chatDatas);
// parse data
rapidjson::Document doc;
if (doc.Parse(data).HasParseError())
{
printf("%sparse json data error,data:%s\n",ERROR_PREFIX.c_str(), data);
printf("%sparse error: (%d:%zu)%s\n", ERROR_PREFIX.c_str(), doc.GetParseError(), doc.GetErrorOffset(), rapidjson::GetParseError_En(doc.GetParseError()));
Napi::Error::New(env, "parse json data error").ThrowAsJavaScriptException();
return env.Null();
}
if (doc.HasMember("errcode"))
{
int errcode = doc["errcode"].GetInt();
if (errcode != 0)
{
string errMsg = doc["errmsg"].GetString();
printf("%sget chat message error:%s.\n",ERROR_PREFIX.c_str(), errMsg.c_str());
Napi::Error::New(env, "get chat message error").ThrowAsJavaScriptException();
return env.Null();
}
}
const rapidjson::Value &chatData = doc["chatdata"];
Napi::Array data_array = Napi::Array::New(info.Env(), chatData.Size());
Napi::Object retObj = Napi::Object::New(env);
unsigned int dataSize =chatData.Size();
int64_t last_seq = 0;
if (dataSize >0) {
last_seq =chatData[dataSize-1]["seq"].GetInt64();
}
for (SizeType i = 0; i < dataSize; ++i)
{
//cout << "current seq:" <<chatData[i]["seq"].GetInt64()<<endl;
string encryptRandomKey = chatData[i]["encrypt_random_key"].GetString();
//cout << "encrypt_random_key: " << encryptRandomKey << endl;
string encryptChatMsg = chatData[i]["encrypt_chat_msg"].GetString();
//cout << "encrypt_chat_msg: " << encryptChatMsg << endl;
string encrypt_key = rsa_pri_decrypt(encryptRandomKey, this->private_key_.c_str());
//cout << "encrypt_key: " << encrypt_key << endl;
if (encrypt_key.length()==0) {
cout <<"random_key:"<<encryptRandomKey<<endl;
cout <<"encrypt_chat_msg:"<<encryptChatMsg<<endl;
cout <<"private_key_:"<<this->private_key_<<endl;
continue;
}
Slice_t *slice_msg = NewSlice();
int ret = DecryptData(encrypt_key.c_str(), encryptChatMsg.c_str(), slice_msg);
if (ret != 0){
cout << ERROR_PREFIX <<"Decrypt Data error:"<<ret<< endl;
continue;
}
char *msg_data = GetContentFromSlice(slice_msg);
data_array[i] = Napi::String::New(info.Env(), msg_data);
FreeSlice(slice_msg);
}
retObj.Set("last_seq", Napi::Number::New(env,last_seq));
retObj.Set("data", data_array);
FreeSlice(chatDatas);
return retObj;
}
void* WeWorkChat::fetchData(TsfnContext *context, void *this__) {
WeWorkChat * this_ = static_cast<WeWorkChat*>(this__);
while (true) {
if (this_->end_){
cout << "End fetch data:"<<this_->seq_<< endl;
break;
}
// 微信限制频率为最高100ms/每次
std::this_thread::sleep_for(std::chrono::milliseconds(150));
Slice_t *chatDatas = NewSlice();
// getchatdata api
const int numOfRetries = 3;
int cnt = 1;
int ret = 0;
do {
ret = GetChatData(this_->sdk_, this_->seq_, max_results, "", "", 30, chatDatas);
if (ret >= 10001 && ret <= 10003){
//cout << "\t try number#" << cnt <<" fail \n";
std::this_thread::sleep_for(std::chrono::seconds(1));
++cnt;
} else{
break;
}
}while (cnt <= numOfRetries);
if (ret != 0)
{
printf("%sGetChatData err ret:%d\n",ERROR_PREFIX.c_str(), ret);
continue;
}
char *data = GetContentFromSlice(chatDatas);
int64_t seq = this_->parseJsonData(context,data);
if (seq <0) {
continue;
}
FreeSlice(chatDatas);
}
context->tsfn.Release();
return 0;
}
Napi::Value WeWorkChat::StartFetchData(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
auto tsContext = new TsfnContext(env);
// Create a new ThreadSafeFunction.
tsContext->tsfn =
Napi::ThreadSafeFunction::New(env, // Environment
info[0].As<Napi::Function>(), // JS function from caller
"TSFN_FETCHDATA", // Resource name
0, // Max queue size (0 = unlimited).
1, // Initial thread count
tsContext, // Context,
FinalizerCallback, // Finalizer
(void *)nullptr // Finalizer data
);
tsContext->nativeThread = std::thread(fetchData, tsContext, this);
return tsContext->deferred.Promise();
}
int64_t WeWorkChat::parseJsonData(TsfnContext *context,const char *data){
auto callback = [](Napi::Env env, Napi::Function jsCallback, MsgData *msg) {
jsCallback.Call({Napi::String::New(env, msg->msg_data)});
FreeSlice(msg->slice_msg);
};
rapidjson::Document doc;
if (doc.Parse(data).HasParseError())
{
printf("%sparse json data error,data:%s\n",ERROR_PREFIX.c_str(), data);
printf("%sparse error: (%d:%zu)%s\n", ERROR_PREFIX.c_str(), doc.GetParseError(), doc.GetErrorOffset(), rapidjson::GetParseError_En(doc.GetParseError()));
return -1;
}
if (doc.HasMember("errcode"))
{
int errcode = doc["errcode"].GetInt();
if (errcode != 0)
{
string errMsg = doc["errmsg"].GetString();
printf("%sget chat message error:%s.\n",ERROR_PREFIX.c_str(), errMsg.c_str());
return -1;
}
}
const rapidjson::Value &chatData = doc["chatdata"];
for (SizeType i = 0; i < chatData.Size(); ++i)
{
int64_t seq = chatData[i]["seq"].GetInt64();
this->mtx_.lock();
this->seq_ = seq;
// cout << "data seq: " << seq << endl;
this->mtx_.unlock();
string encryptRandomKey = chatData[i]["encrypt_random_key"].GetString();
//cout << "encrypt_random_key: " << encryptRandomKey << endl;
string encryptChatMsg = chatData[i]["encrypt_chat_msg"].GetString();
//cout << "encrypt_chat_msg: " << encryptChatMsg << endl;
string encrypt_key = rsa_pri_decrypt(encryptRandomKey, this->private_key_.c_str());
//cout << "encrypt_key: " << encrypt_key << endl;
if (encrypt_key.length()==0) {
cout <<"random_key:"<<encryptRandomKey<<endl;
cout <<"encrypt_chat_msg:"<<encryptChatMsg<<endl;
cout <<"private_key_:"<<this->private_key_<<endl;
continue;
}
Slice_t *slice_msg = NewSlice();
int ret = DecryptData(encrypt_key.c_str(), encryptChatMsg.c_str(), slice_msg);
if (ret != 0){
cout << ERROR_PREFIX <<"Decrypt Data error:"<<ret<< endl;
continue;
}
//cout << "DecryptData ret: " << ret << endl;
//int64_t msg_len = GetSliceLen(slice_msg);
//cout << "msg_len: " << msg_len << endl;
char *msg_data = GetContentFromSlice(slice_msg);
MsgData *theData = new MsgData();
theData->msg_data =msg_data;
theData->slice_msg =slice_msg;
// 确保执行完 callback 再释放 slice_msg
napi_status status =
context->tsfn.BlockingCall(theData, callback);
if (status != napi_ok) {
FreeSlice(slice_msg);
Napi::Error::Fatal("parseJsonData", "Napi::ThreadSafeNapi::Function.BlockingCall() failed");
}
std::this_thread::sleep_for(std::chrono::milliseconds(80));
}
return this->seq_;
}
Napi::Value WeWorkChat::GetMediaData(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
int length = info.Length();
if (length <= 0 || !info[0].IsObject()) {
Napi::TypeError::New(env, "Expected one object argument").ThrowAsJavaScriptException();
}
Napi::Object obj = info[0].As<Napi::Object>();
std::string sdk_fileid = obj.Get("sdk_fileid").ToString();
std::string index_buf = obj.Get("index_buf").ToString();
Napi::Function cb;
bool isCbFunction = false;
if (info[1].IsFunction()){
cb = info[1].As<Napi::Function>();
isCbFunction = true;
}
// 记得释放
MediaData_t* media = NewMediaData();
const int numOfRetries = 3;
int cnt = 1;
int ret = 0;
do {
ret = ::GetMediaData(this->sdk_, index_buf.c_str(),sdk_fileid.c_str(), "", "",30, media);
if (ret >= 10001 && ret <= 10003){
cout << "\t try number#" << cnt <<" fail \n";
std::this_thread::sleep_for(std::chrono::seconds(1));
++cnt;
} else{
break;
}
}while (cnt <= numOfRetries);
if (ret != 0) {
printf("%sGetMediaData err ret:%d\n",ERROR_PREFIX.c_str(), ret);
::FreeMediaData(media);
if (isCbFunction){
cb.Call(env.Global(), {Napi::String::New(env, "GetMediaData err")});
return Napi::Number::New(env,-1);
} else{
char errMsg[128];
sprintf(errMsg, "Get media data error,errorcode:%d", ret);
Napi::Error::New(env, errMsg).ThrowAsJavaScriptException();
return env.Null();
}
}
//int media_data_len = ::GetDataLen(media);
//int index_len = ::GetIndexLen(media);
//printf("content size:%d isfin:%d outindex:%s index_len:%d\n",media_data_len, is_finish,out_index_buf,index_len);
//char* media_data = ::GetData(media);
std:: string out_index_buf = "";
Napi::ArrayBuffer buf_data = Napi::ArrayBuffer::New(env, GetData(media), GetDataLen(media),MediaDataFinalizerCallback,media);
bool is_finish;
if(IsMediaDataFinish(media)==1)
{
is_finish = true;
out_index_buf = "";
//break;
} else {
out_index_buf = ::GetOutIndexBuf(media);
is_finish = false;
}
Napi::Object retObj = Napi::Object::New(env);
retObj.Set("is_finished", Napi::Boolean::New(env,is_finish));
retObj.Set("buf_index",Napi::String::New(env,out_index_buf));
retObj.Set("data", buf_data);
if (isCbFunction){
cb.Call(env.Global(), {env.Null(),retObj});
return env.Null();
} else{
return retObj;
}
}
std::string rsa_pri_decrypt(const std::string &cipherText,const char *priKey)
{
std::string random_key = decode64(cipherText);
//printf("randkey decodebase64:%s\n", random_key.c_str());
std::string strRet;
RSA *rsa = RSA_new();
BIO *keybio;
keybio = BIO_new_mem_buf(priKey, -1);
// 此处有三种方法
// 1, 读取内存里生成的密钥对,再从内存生成rsa
// 2, 读取磁盘里生成的密钥对文本文件,在从内存生成rsa
// 3,直接从读取文件指针生成rsa
rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa, NULL, NULL);
if(rsa == NULL)
{
printf("%sFailed to create RSA.\n",ERROR_PREFIX.c_str());
return "";
}
int len = RSA_size(rsa);
char *decryptedText = (char *)malloc(len + 1);
memset(decryptedText, 0, len + 1);
// 解密函数
int ret = RSA_private_decrypt(random_key.length(), (const unsigned char *)random_key.c_str(), (unsigned char *)decryptedText, rsa, RSA_PKCS1_PADDING);
if (ret >= 0)
strRet = std::string(decryptedText, ret);
// 释放内存
free(decryptedText);
BIO_free_all(keybio);
RSA_free(rsa);
return strRet;
}
std::string decode64(const std::string &ascdata)
{
using std::string;
string retval;
const string::const_iterator last = ascdata.end();
int bits_collected = 0;
unsigned int accumulator = 0;
for (string::const_iterator i = ascdata.begin(); i != last; ++i) {
const int c = *i;
if (std::isspace(c) || c == '=') {
continue;
}
if ((c > 127) || (c < 0) || (reverse_table[c] > 63)) {
printf("%sThis contains characters not legal in a base64 encoded string.\n",ERROR_PREFIX.c_str());
return "";
}
accumulator = (accumulator << 6) | reverse_table[c];
bits_collected += 6;
if (bits_collected >= 8) {
bits_collected -= 8;
retval += static_cast<char>((accumulator >> bits_collected) & 0xffu);
}
}
return retval;
}