-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathindex.ts
753 lines (659 loc) · 24.8 KB
/
index.ts
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
import { Telegraf, Context } from 'telegraf';
import { telegrafThrottler } from 'telegraf-throttler';
import config from './config';
import axios from 'axios';
import fs from 'fs/promises';
import path from 'path';
import sharp from 'sharp';
import { SocksProxyAgent } from 'socks-proxy-agent';
import { HttpsProxyAgent } from 'https-proxy-agent';
import md5 from 'md5';
import asyncRetry from 'async-retry';
if (!Object.values(config.sendMedia).some(((value) => value))) {
throw new Error('Set at least one of "sendMedia" options in your config to "true"');
}
let httpsAgent: HttpsProxyAgent | SocksProxyAgent | undefined;
if (config.proxyUrl) {
if (/^socks5/.test(config.proxyUrl)) {
httpsAgent = new SocksProxyAgent(config.proxyUrl);
} else {
httpsAgent = new HttpsProxyAgent(config.proxyUrl);
}
httpsAgent.timeout = 30000;
}
const FACE_HACK_SIZE = 170;
const FACE_HACK_SPACE = 200;
let faceHackBuffer: Buffer;
sharp(__dirname + '/face_hack.jpg')
.resize(FACE_HACK_SIZE, FACE_HACK_SIZE)
.toBuffer()
.then((buffer) => {
faceHackBuffer = buffer;
});
const faceHack = async (sourceImgBuffer: Buffer) => {
const sourceImg = sharp(sourceImgBuffer);
const sourceImgMeta = await sourceImg.metadata();
const sourceImgWidth = sourceImgMeta.width || 0;
const sourceImgHeight = sourceImgMeta.height || 0;
let imgWidth = sourceImgWidth;
let imgHeight = sourceImgHeight;
let img = sourceImg.clone();
if (sourceImgHeight > sourceImgWidth) {
const ratio = sourceImgHeight / sourceImgWidth;
if (ratio > 1.5) {
imgHeight = Math.floor(sourceImgWidth * 1.5);
} else {
imgWidth = Math.floor(sourceImgHeight / 1.5);
}
} else {
const ratio = sourceImgWidth / sourceImgHeight;
if (ratio > 1.5) {
imgWidth = Math.floor(sourceImgHeight * 1.5);
} else {
imgHeight = Math.floor(sourceImgWidth / 1.5);
}
}
imgWidth = Math.max(imgWidth, FACE_HACK_SIZE);
imgHeight = Math.max(imgHeight, FACE_HACK_SIZE);
img = img.resize({
fit: 'cover',
width: imgWidth,
height: imgHeight,
});
const imgBuffer = await img.toBuffer();
let resultImg;
if (imgHeight > imgWidth) {
resultImg = sharp({
create: {
width: imgWidth,
height: imgHeight + FACE_HACK_SIZE * 2 + FACE_HACK_SPACE * 2,
background: { r: 255, g: 255, b: 255 },
channels: 3,
},
})
.composite([
{
input: imgBuffer,
left: 0,
top: FACE_HACK_SIZE + FACE_HACK_SPACE,
},
{
input: faceHackBuffer,
left: Math.round(imgWidth / 2 - FACE_HACK_SIZE / 2),
top: 0,
},
{
input: faceHackBuffer,
left: Math.round(imgWidth / 2 - FACE_HACK_SIZE / 2),
top: FACE_HACK_SIZE + FACE_HACK_SPACE + imgHeight + FACE_HACK_SPACE,
},
]);
} else {
resultImg = sharp({
create: {
width: imgWidth + FACE_HACK_SIZE * 2 + FACE_HACK_SPACE * 2,
height: imgHeight,
background: { r: 255, g: 255, b: 255 },
channels: 3,
},
})
.composite([
{
input: imgBuffer,
left: FACE_HACK_SIZE + FACE_HACK_SPACE,
top: 0,
},
{
input: faceHackBuffer,
left: 0,
top: Math.round(imgHeight / 2 - FACE_HACK_SIZE / 2),
},
{
input: faceHackBuffer,
left: FACE_HACK_SIZE + FACE_HACK_SPACE + imgWidth + FACE_HACK_SPACE,
top: Math.round(imgHeight / 2 - FACE_HACK_SIZE / 2),
},
]);
}
return await resultImg.jpeg().toBuffer();
};
const signV1 = (obj: Record<string, unknown>) => {
const str = JSON.stringify(obj);
return md5(
'https://h5.tu.qq.com' +
(str.length + (encodeURIComponent(str).match(/%[89ABab]/g)?.length || 0)) +
'HQ31X02e',
);
};
const qqRequest = async (mode: typeof config.mode, imgBuffer: Buffer) => {
const request = async (obj: Record<string, unknown>) => {
const sign = signV1(obj);
let url = 'https://ai.tu.qq.com/overseas/trpc.shadow_cv.ai_processor_cgi.AIProcessorCgi/Process';
if (mode === 'AI_PAINTING_ANIME') {
url = 'https://ai.tu.qq.com/trpc.shadow_cv.ai_processor_cgi.AIProcessorCgi/Process';
}
let data;
try {
data = await asyncRetry(
async (bail) => {
const response = await axios.request({
httpsAgent,
method: 'POST',
url,
data: obj,
headers: {
'Content-Type': 'application/json',
'Origin': 'https://h5.tu.qq.com',
'Referer': 'https://h5.tu.qq.com/',
// eslint-disable-next-line max-len
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36',
'x-sign-value': sign,
'x-sign-version': 'v1',
},
timeout: 30000,
});
const data = response?.data as Record<string, unknown> | undefined;
if (!data) {
throw new Error('No data');
}
if (data.msg === 'VOLUMN_LIMIT') {
throw new Error('QQ rate limit caught');
}
if ((data.msg as string || '').includes('polaris limit')) {
throw new Error('QQ rate limit caught (polaris limit)');
}
if (
(data.msg === 'IMG_ILLEGAL') ||
(data.msg as string || '').includes('image illegal')
) {
bail(new Error('Couldn\'t pass the censorship. Try another photo.'));
return;
}
if (data.code === 1001) {
bail(new Error('Face not found. Try another photo.'));
return;
}
// "request image is invalid"
// or
// "the busi_id: <*> is not servered in this set group"
if (data.code === -2100) {
console.error('Invalid request', JSON.stringify(data));
bail(new Error('Try another photo.'));
return;
}
if (
data.code === 2119 || // user_ip_country | service upgrading
data.code === -2111 // AUTH_FAILED
) {
console.error('Blocked', JSON.stringify(data));
bail(new Error(config.messages.blocked));
return;
}
if (!data.extra) {
throw new Error('Got no data from QQ: ' + JSON.stringify(data));
}
return data;
},
{
onRetry(e, attempt) {
console.error(`QQ file upload error caught (attempt #${attempt}): ${e.toString()}`);
},
retries: 10,
factor: 1,
},
);
} catch (e) {
console.error(`QQ file upload error caught: ${(e as Error).toString()}`);
throw new Error(`Unable to upload the photo: ${(e as Error).toString()}`);
}
return data as Record<string, unknown> & { extra: string };
};
let comparedImgUrl: string | null = null;
let videoUrl: string | null = null;
let singleImgUrl: string | null = null;
switch (mode) {
case 'DIFFERENT_DIMENSION_ME': {
const data = await request({
busiId: 'different_dimension_me_img_entry',
extra: JSON.stringify({
face_rects: [],
version: 2,
platform: 'web',
}),
images: [imgBuffer.toString('base64')],
});
const extra = JSON.parse(data.extra);
if (config.sendMedia.single || config.sendMedia.compared) {
comparedImgUrl = extra.img_urls[1] as string;
}
break;
}
case 'AI_PAINTING_ANIME': {
const data = await request({
busiId: 'ai_painting_anime_img_entry',
extra: JSON.stringify({
face_rects: [],
version: 2,
platform: 'web',
}),
images: [imgBuffer.toString('base64')],
});
const extra = JSON.parse(data.extra);
if (config.sendMedia.compared) {
comparedImgUrl = extra.img_urls[1] as string;
}
if (config.sendMedia.single || config.sendMedia.video) {
const uuid = extra.uuid as string;
const videoData = await request({
busiId: 'ai_painting_anime_video_entry',
extra: JSON.stringify({
uuid,
face_rects: [],
version: 2,
platform: 'web',
}),
});
const videoExtra = JSON.parse(videoData.extra);
if (config.sendMedia.video) {
videoUrl = videoExtra.video_urls[0] as string;
}
if (config.sendMedia.single) {
singleImgUrl = videoExtra.img_urls[2] as string;
}
}
break;
}
}
return {
comparedImgUrl,
videoUrl,
singleImgUrl,
};
};
const qqDownload = async (url: string): Promise<Buffer> => {
let data;
try {
data = await asyncRetry(
async () => {
const response = await axios.request({
url,
timeout: 10000,
responseType: 'arraybuffer',
httpsAgent,
});
if (!response.data) {
throw new Error('No data');
}
return response.data;
},
{
onRetry(e, attempt) {
console.error(`QQ file download error caught (attempt #${attempt}): ${e.toString()}`);
},
retries: 10,
factor: 1,
},
);
} catch (e) {
console.error(`QQ file download error caught: ${(e as Error).toString()}`);
throw new Error(`Unable to download media: ${(e as Error).toString()}`);
}
return data;
};
const usersStatuses: Record<number, {
requestsCount: number;
requestsOverflow: boolean;
}> = {};
const getUsersRequestCount = () => {
return Object.values(usersStatuses).reduce((acc, s) => acc + s.requestsCount, 0);
};
const printUsersStatuses = () => {
console.log(`Users: ${Object.keys(usersStatuses).length} | Requests: ${getUsersRequestCount()}`);
};
const cropImage = async (imgData: Buffer, type: 'COMPARED' | 'SINGLE' | 'SINGLE_FROM_COMPARED'): Promise<Buffer> => {
const img = sharp(imgData);
const meta = await img.metadata();
const width = meta.width || 0;
const height = meta.height || 0;
let cropLeft;
let cropTop;
let cropWidth;
let cropHeight;
switch (type) {
case 'COMPARED':
cropLeft = 0;
cropTop = 0;
cropWidth = width;
cropHeight = height - (width > height ? 177 : 182);
break;
case 'SINGLE':
cropLeft = (width > height ? 19 : 27);
cropTop = (width > height ? 19 : 29);
cropWidth = width - cropLeft - (width > height ? 22 : 30);
cropHeight = height - cropTop - (width > height ? 202 : 213);
break;
case 'SINGLE_FROM_COMPARED':
cropLeft = (width > height ? 510 : 23);
cropTop = (width > height ? 25 : 547);
cropWidth = (width > height ? 467 : 753);
cropHeight = (width > height ? 701 : 497);
break;
}
return img.extract({
left: cropLeft,
top: cropTop,
width: cropWidth,
height: cropHeight,
})
.toBuffer();
};
const onPhotoReceived = async (ctx: Context, userId: number, photoId: string, replyMessageId: number) => {
usersStatuses[userId] ??= {
requestsCount: 0,
requestsOverflow: false,
};
const currentUserStatus = usersStatuses[userId];
if (currentUserStatus.requestsCount >= config.parallelRequests) {
console.log('Request rejected for ' + userId);
if (!currentUserStatus.requestsOverflow) {
currentUserStatus.requestsOverflow = true;
await ctx.reply('You send too many pictures, please wait', {
reply_to_message_id: replyMessageId,
});
}
return;
}
currentUserStatus.requestsCount++;
printUsersStatuses();
try {
let url: URL;
try {
url = await asyncRetry(
async () => {
return await ctx.telegram.getFileLink(photoId);
},
{
onRetry(e, attempt) {
console.error(`Telegram getFileLink error caught (attempt #${attempt}): ${e.toString()}`);
},
retries: 10,
factor: 1,
},
);
} catch (e) {
console.error(`Telegram getFileLink error caught: ${(e as Error).toString()}`);
throw new Error('Couldn\'t load the photo, please try again');
}
let telegramFileData;
try {
telegramFileData = await asyncRetry(
async () => {
const response = await axios.request({
url: url.href,
timeout: 5000,
responseType: 'arraybuffer',
});
if (!response?.data) {
throw new Error('No data');
}
return response.data as Buffer;
},
{
onRetry(e, attempt) {
console.error(`Telegram file download error caught (attempt #${attempt}): ${e.toString()}`);
},
retries: 10,
factor: 1,
},
);
} catch (e) {
console.error(`Telegram file download error caught: ${(e as Error).toString()}`);
throw new Error('Couldn\'t load the photo, please try again');
}
if (config.keepFiles.input) {
fs.writeFile(
path.join(__dirname, 'files', (new Date()).getTime() + '_' + userId + '_input.jpg'),
telegramFileData,
);
}
try {
await ctx.reply(config.messages.received, {
reply_to_message_id: replyMessageId,
parse_mode: 'MarkdownV2',
});
} catch (e) {
console.error('Unable to send "photo received" message for ' + userId, (e as Error).toString());
}
console.log('Uploading to QQ for ' + userId);
let imgData;
try {
imgData = await qqRequest(config.mode, telegramFileData);
} catch (e) {
if ((e as Error).toString().includes('Face not found')) { // TODO: it shouldn't rely on the text
console.log('Face not found, trying to hack for ' + userId);
imgData = await qqRequest(config.mode, (await faceHack(telegramFileData)));
} else {
throw e;
}
}
console.log('QQ responded successfully for ' + userId);
console.log('Downloading from QQ for ' + userId);
// eslint-disable-next-line prefer-const
let [comparedImgData, singleImgData, videoData] = await Promise.all([
imgData.comparedImgUrl ? qqDownload(imgData.comparedImgUrl).then((data) => cropImage(data, 'COMPARED')) : null,
imgData.singleImgUrl ?
qqDownload(imgData.singleImgUrl).then((data) => cropImage(data, 'SINGLE')) :
null,
imgData.videoUrl ? qqDownload(imgData.videoUrl) : null,
]);
if ((config.mode === 'DIFFERENT_DIMENSION_ME') && config.sendMedia.single && comparedImgData) {
singleImgData = await cropImage(comparedImgData, 'SINGLE_FROM_COMPARED');
if (!config.sendMedia.compared) {
comparedImgData = null;
}
}
const time = (new Date()).getTime();
if (config.keepFiles.compared && comparedImgData) {
fs.writeFile(
path.join(__dirname, 'files', time + '_' + userId + '_compared.jpg'),
comparedImgData,
);
}
if (config.keepFiles.single && singleImgData) {
fs.writeFile(
path.join(__dirname, 'files', time + '_' + userId + '_single.jpg'),
singleImgData,
);
}
if (config.keepFiles.video && videoData) {
fs.writeFile(
path.join(__dirname, 'files', time + '_' + userId + '_video.mp4'),
videoData,
);
}
const sendMedia = async (fn: () => Promise<void>) => {
return await asyncRetry(
async (bail) => {
try {
await fn();
} catch (e) {
const msg = (e as Error).toString();
if (msg.includes('replied message not found')) {
bail(new Error('Photo has been deleted'));
return;
}
if (msg.includes('was blocked by the user')) {
bail(new Error('Bot was blocked by the user'));
return;
}
throw e;
}
},
{
onRetry(e, attempt) {
console.error(`Unable to send media for ${userId} (attempt #${attempt}): ${e.toString()}`);
},
retries: 10,
factor: 1,
},
);
};
const mediaPromises: Array<Promise<unknown>> = [];
if (comparedImgData) {
mediaPromises.push(sendMedia(async () => {
await ctx.replyWithPhoto({
source: comparedImgData as Buffer,
}, {
caption: config.messages.media,
reply_to_message_id: replyMessageId,
parse_mode: 'MarkdownV2',
});
}));
}
if (singleImgData) {
mediaPromises.push(sendMedia(async () => {
await ctx.replyWithPhoto({
source: singleImgData as Buffer,
}, {
caption: config.messages.media,
reply_to_message_id: replyMessageId,
parse_mode: 'MarkdownV2',
});
}));
}
if (videoData) {
mediaPromises.push(sendMedia(async () => {
await ctx.replyWithVideo({
source: videoData as Buffer,
}, {
caption: config.messages.media,
reply_to_message_id: replyMessageId,
parse_mode: 'MarkdownV2',
});
}));
}
const settled = await Promise.allSettled(mediaPromises);
const errors = settled
.filter((item): item is PromiseRejectedResult => item.status === 'rejected')
.map((item) => item.reason as Error);
const sentCount = settled.filter((item) => item.status === 'fulfilled').length;
const errorsMgs = errors.map(e => e.toString()).join(' ');
if (errors.length) {
console.error(`Unable to send media for ${userId} (${sentCount}/${mediaPromises.length}): ${errorsMgs}`);
}
if (sentCount) {
console.log(`Files sent to ${userId} (${sentCount}/${mediaPromises.length})`);
} else {
throw new Error(`Unable to send media, please try again: ${errorsMgs}`);
}
if (config.messages.bye && currentUserStatus.requestsCount === 1) {
try {
await ctx.reply(config.messages.bye, {
disable_web_page_preview: true,
parse_mode: 'MarkdownV2',
});
} catch (e) {
console.error('Unable to send byeMessage for ' + userId, (e as Error).toString());
}
}
} catch (e) {
console.log('Error has occurred for ' + userId);
console.error(e);
try {
await asyncRetry(
async (bail) => {
try {
await ctx.reply(
'Some nasty error has occurred, please try again\n\n' + (e as Error).toString(),
{
reply_to_message_id: replyMessageId,
},
);
} catch (e) {
if ((e as Error).toString().includes('was blocked by the user')) {
bail(new Error('Bot was blocked by the user'));
return;
}
throw e;
}
},
{
onRetry(e, attempt) {
console.error(`Unable to send error message for ${userId} (attempt #${attempt}): ${e.toString()}`);
},
retries: 10,
factor: 1,
},
);
} catch (e) {
console.error(`Unable to send error message for ${userId}: ${(e as Error).toString()}`);
}
}
currentUserStatus.requestsCount--;
currentUserStatus.requestsOverflow = false;
if (currentUserStatus.requestsCount === 0) {
delete usersStatuses[userId];
}
printUsersStatuses();
if (shuttingDown) {
tryToShutDown();
}
};
let bot: Telegraf;
const startBot = () => {
bot = new Telegraf(config.botToken);
const throttler = telegrafThrottler({
in: {},
});
bot.use(throttler);
bot.start((ctx) => {
ctx.reply(config.messages.hello, {
disable_web_page_preview: true,
parse_mode: 'MarkdownV2',
})
.catch((e) => {
console.error('Unable to send helloMessage for ' + ctx.update.message.from.id, (e as Error).toString());
});
});
bot.on('photo', (ctx) => {
const userId = ctx.update.message.from.id;
const name = ((ctx.message.from.first_name || '') + ' ' + (ctx.message.from.last_name || '')).trim();
const username = ctx.message.from.username ? ('@' + ctx.message.from.username) : '<none>';
console.log(`Received photo from id: ${userId}, username: ${username}, name: ${name}`);
const photoId = [...ctx.update.message.photo].pop()?.file_id || '';
onPhotoReceived(ctx, userId, photoId, ctx.update.message.message_id).catch(e => e);
});
bot.catch((e) => {
console.error('Bot error has occurred ', e);
});
bot.launch();
};
const stopBot = () => {
try {
bot?.stop();
} catch (e) {
//
}
};
console.log('Current mode:', config.mode);
startBot();
let shuttingDown = false;
const tryToShutDown = () => {
if (!shuttingDown) {
stopBot();
}
shuttingDown = true;
if (!getUsersRequestCount()) {
process.exit();
}
};
process.on('SIGINT', () => tryToShutDown());
process.on('SIGTERM', () => tryToShutDown());
process.on('unhandledRejection', (promise, reason) => {
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
tryToShutDown();
});
process.on('uncaughtException', (err, origin) => {
console.error('Uncaught Exception:', err, 'origin:', origin);
tryToShutDown();
});