-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathindex.ts
563 lines (491 loc) · 18 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
import { Telegraf, Context } from 'telegraf';
import { telegrafThrottler } from 'telegraf-throttler';
import config from './config';
import { v4 as v4uuid } from 'uuid';
import axios from 'axios';
import fs from 'fs/promises';
import path from 'path';
import sharp from 'sharp';
import cluster from 'cluster';
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"');
}
const QQ_MODE = config.mode ?? (config.proxyUrl ? 'CHINA' : 'WORLD');
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 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 (imgData: string) => {
const obj = {
busiId: QQ_MODE === 'WORLD' ? 'different_dimension_me_img_entry' : 'ai_painting_anime_entry',
extra: JSON.stringify({
face_rects: [],
version: 2,
platform: 'web',
data_report: {
parent_trace_id: v4uuid(),
root_channel: '',
level: 0,
},
}),
images: [imgData],
};
const sign = signV1(obj);
let extra;
try {
extra = await asyncRetry(
async (bail) => {
const response = await axios.request({
httpsAgent,
method: 'POST',
url: 'https://ai.tu.qq.com/trpc.shadow_cv.ai_processor_cgi.AIProcessorCgi/Process',
data: obj,
headers: {
'Content-Type': 'application/json',
'Origin': 'https://h5.tu.qq.com',
'Referer': 'https://h5.tu.qq.com/',
'User-Agent':
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.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 === 'IMG_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;
}
if (data.code === -2100) { // request image is invalid
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 JSON.parse(data.extra as string);
},
{
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 {
videoUrl: QQ_MODE === 'CHINA' ? (extra.video_urls[0] as string) : undefined,
comparedImgUrl: extra.img_urls[1] as string,
singleImgUrl: QQ_MODE === 'CHINA' ? (extra.img_urls[2] as string) : undefined,
};
};
const qqDownload = async (url: string): Promise<Buffer> => {
let data;
try {
data = await asyncRetry(
async () => {
const response = await axios.request({
url,
timeout: 7000,
responseType: 'arraybuffer',
});
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 userSessions: Array<UserSession> = [];
const cropImage = async (imgData: Buffer, type: 'COMPARED' | 'SINGLE'): 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;
if (type === 'COMPARED') {
cropLeft = 0;
cropTop = 0;
cropWidth = width;
cropHeight = height - (width > height ? 177 : 182);
} else {
cropLeft = (width > height ? 19 : 27);
cropTop = (width > height ? 19 : 29);
cropWidth = width - cropLeft - (width > height ? 22 : 30);
cropHeight = height - cropTop - (width > height ? 202 : 213);
}
return img.extract({
left: cropLeft,
top: cropTop,
width: cropWidth,
height: cropHeight,
})
.toBuffer();
};
const processUserSession = async ({ ctx, userId, photoId, replyMessageId }: UserSession) => {
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;
},
{
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);
const urls = await qqRequest(telegramFileData.toString('base64'));
console.log('QQ responded successfully for ' + userId);
console.log('Downloading from QQ for ' + userId);
const [comparedImgData, singleImgData, videoData] = await Promise.all([
config.sendMedia.compared ?
qqDownload(urls.comparedImgUrl).then((data) => cropImage(data, 'COMPARED')) : null,
(config.sendMedia.single && QQ_MODE === 'CHINA') ?
qqDownload(urls.singleImgUrl || '').then((data) => cropImage(data, 'SINGLE')) : null,
(config.sendMedia.video && QQ_MODE === 'CHINA') ?
qqDownload(urls.videoUrl || '') : 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,
}, {
caption: config.messages.media,
reply_to_message_id: replyMessageId,
parse_mode: 'MarkdownV2',
});
}));
}
if (singleImgData) {
mediaPromises.push(sendMedia(async () => {
await ctx.replyWithPhoto({
source: singleImgData,
}, {
caption: config.messages.media,
reply_to_message_id: replyMessageId,
parse_mode: 'MarkdownV2',
});
}));
}
if (videoData) {
mediaPromises.push(sendMedia(async () => {
await ctx.replyWithVideo({
source: videoData,
}, {
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) {
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());
} 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()}`);
}
}
const currentSessionIndex = userSessions.findIndex((session) => session.userId === userId);
userSessions.splice(currentSessionIndex, 1);
console.log('Sessions length decreased: ' + userSessions.length);
if (shuttingDown) {
tryToShutDown();
}
};
const addUserSession = async (ctx: Context, userId: number, photoId: string, replyMessageId: number) => {
const currentSession = (userSessions.find((session) => session.userId === userId));
if (currentSession) {
await ctx.reply('You are already in the queue, please wait', {
reply_to_message_id: replyMessageId,
});
return;
}
const session = {
ctx,
userId,
photoId,
replyMessageId,
};
userSessions.push(session);
console.log('Sessions length increased: ' + userSessions.length);
await processUserSession(session);
};
let bot: Telegraf;
const startBot = () => {
bot = new Telegraf(config.botToken);
const throttler = telegrafThrottler();
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;
console.log('Received photo from ' + userId);
const photoId = [...ctx.update.message.photo].pop()?.file_id || '';
addUserSession(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) {
//
}
};
let shuttingDown = false;
let tryToShutDown: () => void;
if (cluster.isPrimary) {
console.log('Current mode:', QQ_MODE);
let hasWorker = false;
tryToShutDown = (): void => {
shuttingDown = true;
if (!hasWorker) {
process.exit();
}
};
const addWorker = (): void => {
if (!shuttingDown) {
const worker = cluster.fork();
console.log(`Worker #${worker.process.pid} started`);
hasWorker = true;
}
};
addWorker();
cluster.on('exit', (worker, code, signal) => {
hasWorker = false;
console.warn(`Worker #${worker.process.pid} is dead`, 'code:', code, 'signal:', signal);
if (shuttingDown) {
tryToShutDown();
} else {
setTimeout(() => {
addWorker();
}, 100);
}
});
} else {
startBot();
tryToShutDown = () => {
if (!shuttingDown) {
stopBot();
}
shuttingDown = true;
if (!userSessions.length) {
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();
});