-
Notifications
You must be signed in to change notification settings - Fork 806
Expand file tree
/
Copy pathqq-vip-api.js
More file actions
786 lines (753 loc) · 26.9 KB
/
Copy pathqq-vip-api.js
File metadata and controls
786 lines (753 loc) · 26.9 KB
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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
'use strict';
// Strict, account-scoped QQ Music membership resolution.
const crypto = require('crypto');
const VIP_TYPE_KEYS = new Set([
'viptype',
'viplevel',
'musicviptype',
'musicviplevel',
'greenviptype',
'greenviplevel',
'greenlevel',
'associatortype',
'associatorlevel',
]);
const SVIP_TYPE_KEYS = new Set([
'sviptype',
'sviplevel',
'superviptype',
'superviplevel',
'luxuryviptype',
'luxuryviplevel',
'greensvip',
]);
const VIP_FLAG_KEYS = new Set([
'isvip',
// VipQueryServer_V2 still returns these legacy desktop field names.
// They are account-scoped, current-state flags (not track capability hints).
'ivipflag',
'inewvip',
'vip',
'vipflag',
'isgreenvip',
'greenvip',
'ismember',
'member',
'isassociator',
'associator',
]);
const SVIP_FLAG_KEYS = new Set([
'issvip',
'isupervip',
'inewsupervip',
'svip',
'issupervip',
'supervip',
'isluxuryvip',
'luxuryvip',
]);
const MEMBERSHIP_STATUS_KEYS = new Set([
'status',
'state',
'active',
'opened',
'valid',
'vipstatus',
'memberstatus',
]);
const MEMBERSHIP_LEVEL_KEYS = new Set([
'level',
'viplevel',
'memberlevel',
]);
const MEMBERSHIP_EXPIRY_KEYS = new Set([
'expire',
'expires',
'expireat',
'expiretime',
'expiry',
'expiryat',
'expirytime',
'endtime',
'validuntil',
'validtime',
'deadline',
'duetime',
'vipexpireat',
'vipexpiretime',
'vipexpirytime',
'vipendtime',
'musicvipexpiretime',
'musicvipendtime',
'greenvipexpiretime',
'greenvipendtime',
'associatorexpiretime',
'associatorendtime',
'svipexpiretime',
'svipendtime',
'supervipexpiretime',
'supervipendtime',
'superendtime',
'luxuryvipexpiretime',
'luxuryvipendtime',
]);
const VIP_MEMBERSHIP_EXPIRY_KEYS = new Set([
'vipexpireat',
'vipexpiretime',
'vipexpirytime',
'vipendtime',
'musicvipexpiretime',
'musicvipendtime',
'greenvipexpiretime',
'greenvipendtime',
'associatorexpiretime',
'associatorendtime',
]);
const SVIP_MEMBERSHIP_EXPIRY_KEYS = new Set([
'svipexpiretime',
'svipendtime',
'supervipexpiretime',
'supervipendtime',
'superendtime',
'luxuryvipexpiretime',
'luxuryvipendtime',
]);
const VIP_MEMBERSHIP_STATUS_KEYS = new Set([
'vipstatus',
'musicvipstatus',
'greenvipstatus',
'associatorstatus',
]);
const SVIP_MEMBERSHIP_STATUS_KEYS = new Set([
'svipstatus',
'supervipstatus',
'luxuryvipstatus',
]);
const MEMBERSHIP_CONTEXT_RE = /vip|member|membership|green|luxury|associator/;
const SVIP_CONTEXT_RE = /svip|supervip|luxuryvip|luxury/;
const MAX_WALK_DEPTH = 8;
const QQ_VIP_STALE_POSITIVE_GRACE_MS = 10 * 60 * 1000;
function canonicalQQVipKey(value) {
return String(value || '').replace(/[^a-z0-9]/gi, '').toLowerCase();
}
function primitiveMembershipSignal(value) {
if (value === true) return 1;
if (value === false) return 0;
if (typeof value === 'number') {
if (!Number.isFinite(value)) return null;
return value > 0 ? 1 : 0;
}
if (typeof value !== 'string') return null;
const text = value.trim().toLowerCase();
if (!text) return null;
if (/^-?\d+(?:\.\d+)?$/.test(text)) return Number(text) > 0 ? 1 : 0;
if (/^(?:true|yes|active|valid|opened|open|vip|svip|premium|member)$/.test(text)) return 1;
if (/^(?:false|no|none|normal|ordinary|expired|inactive|closed|invalid)$/.test(text)) return 0;
if (/^(?:已开通|有效|会员|绿钻|豪华绿钻)$/.test(text)) return 1;
if (/^(?:未开通|已过期|过期|普通用户|普通账号|非会员)$/.test(text)) return 0;
return null;
}
function normalizedExpiryMs(value) {
if (value == null || value === '') return 0;
let numeric = Number(value);
if (Number.isFinite(numeric) && numeric > 0) {
if (numeric < 10000000000) numeric *= 1000;
return numeric >= 946684800000 ? numeric : 0;
}
if (typeof value === 'string') {
numeric = Date.parse(value);
return Number.isFinite(numeric) && numeric >= 946684800000 ? numeric : 0;
}
return 0;
}
function qqVipObjectExpiry(obj) {
const values = [];
if (!objectHasDirectMembershipKey(obj)) return { present: false, expired: false, expiresAt: 0 };
Object.keys(obj || {}).forEach(key => {
if (!isQQMembershipExpiryKey(key)) return;
const value = normalizedExpiryMs(obj[key]);
if (value > 0) values.push(value);
});
if (!values.length) return { present: false, expired: false, expiresAt: 0 };
const expiresAt = Math.max(...values);
return {
present: true,
expired: expiresAt <= Date.now(),
expiresAt,
};
}
function qqVipObjectLooksExpired(obj, now) {
const expiry = qqVipObjectExpiry(obj);
return !!(expiry.present && expiry.expiresAt <= (Number(now) || Date.now()));
}
function pathMembershipContext(pathParts) {
const pathText = (pathParts || []).map(canonicalQQVipKey).join(' ');
return {
membership: MEMBERSHIP_CONTEXT_RE.test(pathText),
svip: SVIP_CONTEXT_RE.test(pathText),
};
}
function objectHasDirectMembershipKey(obj) {
return Object.keys(obj || {}).some(key => {
const normalized = canonicalQQVipKey(key);
return VIP_TYPE_KEYS.has(normalized) ||
SVIP_TYPE_KEYS.has(normalized) ||
VIP_FLAG_KEYS.has(normalized) ||
SVIP_FLAG_KEYS.has(normalized) ||
VIP_MEMBERSHIP_STATUS_KEYS.has(normalized) ||
SVIP_MEMBERSHIP_STATUS_KEYS.has(normalized);
});
}
function isQQMembershipExpiryKey(key) {
return MEMBERSHIP_EXPIRY_KEYS.has(canonicalQQVipKey(key));
}
function qqMembershipExpiryTier(key, context, hasVipSignal, hasSvipSignal) {
const normalized = canonicalQQVipKey(key);
if (SVIP_MEMBERSHIP_EXPIRY_KEYS.has(normalized)) return 'svip';
if (VIP_MEMBERSHIP_EXPIRY_KEYS.has(normalized)) return 'vip';
if (context && context.svip) return 'svip';
if (hasVipSignal && hasSvipSignal) return 'both';
if (hasSvipSignal) return 'svip';
return 'vip';
}
function assessQQVipObject(obj, context, now) {
let vipPositive = false;
let svipPositive = false;
let vipNegative = false;
let svipNegative = false;
let vipType = 0;
let svipType = 0;
let evidence = false;
const vipExpiryValues = [];
const svipExpiryValues = [];
const keys = Object.keys(obj || {});
const hasDirectMembershipKey = objectHasDirectMembershipKey(obj);
const hasDirectVipSignal = keys.some(key => {
const normalized = canonicalQQVipKey(key);
return VIP_TYPE_KEYS.has(normalized) || VIP_FLAG_KEYS.has(normalized) || VIP_MEMBERSHIP_STATUS_KEYS.has(normalized);
});
const hasDirectSvipSignal = keys.some(key => {
const normalized = canonicalQQVipKey(key);
return SVIP_TYPE_KEYS.has(normalized) || SVIP_FLAG_KEYS.has(normalized) || SVIP_MEMBERSHIP_STATUS_KEYS.has(normalized);
});
const hasPairedMembershipSignal = hasDirectMembershipKey || !!(
context
&& context.membership
&& keys.some(key => {
const normalized = canonicalQQVipKey(key);
return MEMBERSHIP_STATUS_KEYS.has(normalized) ||
VIP_MEMBERSHIP_STATUS_KEYS.has(normalized) ||
SVIP_MEMBERSHIP_STATUS_KEYS.has(normalized) ||
MEMBERSHIP_LEVEL_KEYS.has(normalized);
})
);
keys.forEach(key => {
const normalized = canonicalQQVipKey(key);
const value = obj[key];
const signal = primitiveMembershipSignal(value);
const numeric = Number(value);
const isVipType = VIP_TYPE_KEYS.has(normalized);
const isSvipType = SVIP_TYPE_KEYS.has(normalized);
const isVipFlag = VIP_FLAG_KEYS.has(normalized);
const isSvipFlag = SVIP_FLAG_KEYS.has(normalized);
const inMembershipContext = !!(context && context.membership);
const inSvipContext = !!(context && context.svip);
const isContextStatus = inMembershipContext && MEMBERSHIP_STATUS_KEYS.has(normalized);
const isContextLevel = inMembershipContext && MEMBERSHIP_LEVEL_KEYS.has(normalized);
if (hasPairedMembershipSignal && isQQMembershipExpiryKey(normalized)) {
const expiry = normalizedExpiryMs(value);
if (expiry > 0) {
const tier = qqMembershipExpiryTier(normalized, context, hasDirectVipSignal, hasDirectSvipSignal);
if (tier === 'vip' || tier === 'both') vipExpiryValues.push(expiry);
if (tier === 'svip' || tier === 'both') svipExpiryValues.push(expiry);
}
return;
}
if (isVipType) {
evidence = true;
if (Number.isFinite(numeric) && numeric > 0) {
vipType = Math.max(vipType, numeric);
vipPositive = true;
} else if (signal === 0) {
vipNegative = true;
}
return;
}
if (isSvipType) {
evidence = true;
if (Number.isFinite(numeric) && numeric > 0) {
svipType = Math.max(svipType, numeric);
svipPositive = true;
} else if (signal === 0) {
svipNegative = true;
}
return;
}
if (isVipFlag) {
if (signal == null) return;
evidence = true;
if (signal > 0) vipPositive = true;
else vipNegative = true;
return;
}
if (isSvipFlag) {
if (signal == null) return;
evidence = true;
if (signal > 0) svipPositive = true;
else svipNegative = true;
return;
}
if (VIP_MEMBERSHIP_STATUS_KEYS.has(normalized) || SVIP_MEMBERSHIP_STATUS_KEYS.has(normalized)) {
if (signal == null) return;
evidence = true;
if (SVIP_MEMBERSHIP_STATUS_KEYS.has(normalized)) {
if (signal > 0) svipPositive = true;
else svipNegative = true;
} else {
if (signal > 0) vipPositive = true;
else vipNegative = true;
}
return;
}
if (isContextStatus || isContextLevel) {
if (signal == null) return;
evidence = true;
if (inSvipContext) {
if (signal > 0) svipPositive = true;
else svipNegative = true;
} else {
if (signal > 0) vipPositive = true;
else vipNegative = true;
}
}
});
const vipExpiryPresent = vipExpiryValues.length > 0;
const svipExpiryPresent = svipExpiryValues.length > 0;
const vipExpiresAt = vipExpiryPresent ? Math.max(...vipExpiryValues) : 0;
const svipExpiresAt = svipExpiryPresent ? Math.max(...svipExpiryValues) : 0;
if (vipExpiryPresent && vipExpiresAt <= now) {
vipNegative = true;
vipPositive = false;
vipType = 0;
}
if (svipExpiryPresent && svipExpiresAt <= now) {
svipNegative = true;
svipPositive = false;
svipType = 0;
}
const activeExpiresAt = svipPositive
? (svipExpiresAt > now ? svipExpiresAt : (vipExpiresAt > now ? vipExpiresAt : 0))
: (vipPositive && vipExpiresAt > now ? vipExpiresAt : 0);
return {
evidence,
vipPositive,
svipPositive,
vipNegative,
svipNegative,
vipType,
svipType,
vipExpiresAt,
svipExpiresAt,
expiresAt: activeExpiresAt,
};
}
function collectQQVipAssessments(value, out, pathParts, depth, now, expectedUin) {
if (depth > MAX_WALK_DEPTH || value == null) return out;
if (Array.isArray(value)) {
value.forEach((item, index) => collectQQVipAssessments(item, out, pathParts.concat('#' + String(index)), depth + 1, now, expectedUin));
return out;
}
if (typeof value !== 'object') return out;
if (expectedUin) {
const lastPathPart = String(pathParts[pathParts.length - 1] || '');
if (/^(?:o0*)?\d+$/i.test(lastPathPart)) {
const pathUin = lastPathPart.replace(/\D/g, '').replace(/^0+/, '');
if (pathUin && pathUin !== expectedUin) return out;
}
const directUins = Object.keys(value).filter(key => {
return /^(?:uin|useruin|userid|qq|accountuin)$/.test(canonicalQQVipKey(key));
}).map(key => String(value[key] == null ? '' : value[key]).replace(/\D/g, '').replace(/^0+/, '')).filter(Boolean);
if (directUins.length && !directUins.includes(expectedUin)) return out;
}
const context = pathMembershipContext(pathParts);
if (context.membership || objectHasDirectMembershipKey(value)) {
const assessment = assessQQVipObject(value, context, now);
if (assessment.evidence) out.push(assessment);
}
Object.keys(value).forEach(key => {
collectQQVipAssessments(value[key], out, pathParts.concat(key), depth + 1, now, expectedUin);
});
return out;
}
function emptyQQVipStatus() {
return {
vipType: 0,
svipType: 0,
vipLevel: 'none',
isVip: false,
isSvip: false,
vipLabel: '无VIP',
membershipKnown: false,
resolved: false,
decision: 'unknown',
vipExpiresAt: 0,
svipExpiresAt: 0,
expiresAt: 0,
};
}
function fallbackQQVipStatus(fallback) {
fallback = fallback || {};
const isSvip = !!fallback.isSvip || fallback.vipLevel === 'svip' || Number(fallback.svipType || fallback.svip_type || 0) > 0;
const isVip = isSvip || !!fallback.isVip || fallback.vipLevel === 'vip' || Number(fallback.vipType || fallback.vip_type || 0) > 0;
const known = isVip || fallback.membershipKnown === true || fallback.resolved === true;
if (!known) return emptyQQVipStatus();
return {
vipType: isVip ? Math.max(1, Number(fallback.vipType || fallback.vip_type || 0) || 0) : 0,
svipType: isSvip ? Math.max(1, Number(fallback.svipType || fallback.svip_type || 0) || 0) : 0,
vipLevel: isSvip ? 'svip' : (isVip ? 'vip' : 'none'),
isVip,
isSvip,
vipLabel: isSvip ? 'SVIP' : (isVip ? 'VIP' : '无VIP'),
membershipKnown: true,
resolved: true,
decision: isVip ? 'positive' : 'negative',
vipExpiresAt: !isSvip ? (Number(fallback.expiresAt || fallback.expireAt || 0) || 0) : 0,
svipExpiresAt: isSvip ? (Number(fallback.expiresAt || fallback.expireAt || 0) || 0) : 0,
expiresAt: Number(fallback.expiresAt || fallback.expireAt || 0) || 0,
};
}
function normalizeQQVipPayload(payload, fallback, options) {
options = options || {};
const now = Number(options.now) || Date.now();
const expectedUin = String(options.expectedUin || '').replace(/\D/g, '').replace(/^0+/, '');
const assessments = collectQQVipAssessments(payload, [], [], 0, now, expectedUin);
const positive = assessments.filter(item => item.vipPositive || item.svipPositive);
const explicitVipNegative = assessments.some(item => item.vipNegative);
if (!positive.length) {
if (explicitVipNegative) {
return {
...emptyQQVipStatus(),
membershipKnown: true,
resolved: true,
decision: 'negative',
};
}
return fallbackQQVipStatus(fallback);
}
const vipPositive = positive.filter(item => item.vipPositive);
const svipPositive = positive.filter(item => item.svipPositive);
const isSvip = svipPositive.length > 0;
const vipExpiries = vipPositive.map(item => Number(item.vipExpiresAt) || 0).filter(value => value > now);
const svipExpiries = svipPositive.map(item => Number(item.svipExpiresAt) || 0).filter(value => value > now);
const vipExpiresAt = vipExpiries.length ? Math.min(...vipExpiries) : 0;
const svipExpiresAt = svipExpiries.length ? Math.min(...svipExpiries) : 0;
const expiresAt = isSvip ? svipExpiresAt : vipExpiresAt;
const vipType = Math.max(1, ...vipPositive.map(item => Number(item.vipType) || 0));
const svipType = isSvip ? Math.max(1, ...svipPositive.map(item => Number(item.svipType) || 0)) : 0;
return {
vipType,
svipType,
vipLevel: isSvip ? 'svip' : 'vip',
isVip: true,
isSvip,
vipLabel: isSvip ? 'SVIP' : 'VIP',
membershipKnown: true,
resolved: true,
decision: 'positive',
vipExpiresAt,
svipExpiresAt,
expiresAt,
};
}
function combineQQVipResults(results) {
const valid = (Array.isArray(results) ? results : []).filter(Boolean);
const positives = valid.filter(item => item.decision === 'positive' || item.isVip || item.isSvip);
if (positives.length) {
const isSvip = positives.some(item => item.isSvip || item.vipLevel === 'svip');
const now = Date.now();
const vipExpiries = positives
.map(item => Number(item.vipExpiresAt) || (!item.isSvip && item.vipLevel !== 'svip' ? Number(item.expiresAt) || 0 : 0))
.filter(value => value > now);
const svipExpiries = positives
.map(item => Number(item.svipExpiresAt) || (item.isSvip || item.vipLevel === 'svip' ? Number(item.expiresAt) || 0 : 0))
.filter(value => value > Date.now());
const vipExpiresAt = vipExpiries.length ? Math.min(...vipExpiries) : 0;
const svipExpiresAt = svipExpiries.length ? Math.min(...svipExpiries) : 0;
const strongest = positives.find(item => isSvip && (item.isSvip || item.vipLevel === 'svip')) || positives[0];
return {
...emptyQQVipStatus(),
vipType: Math.max(1, ...positives.map(item => Number(item.vipType) || 0)),
svipType: isSvip ? Math.max(1, ...positives.map(item => Number(item.svipType) || 0)) : 0,
vipLevel: isSvip ? 'svip' : 'vip',
isVip: true,
isSvip,
vipLabel: isSvip ? 'SVIP' : 'VIP',
membershipKnown: true,
resolved: true,
decision: 'positive',
vipExpiresAt,
svipExpiresAt,
expiresAt: isSvip ? svipExpiresAt : vipExpiresAt,
vipSource: strongest.vipSource || '',
rawCode: Number(strongest.rawCode) || 0,
};
}
const negative = valid.find(item => item.decision === 'negative' && item.membershipKnown);
if (negative) {
return {
...emptyQQVipStatus(),
membershipKnown: true,
resolved: true,
decision: 'negative',
vipSource: negative.vipSource || '',
rawCode: Number(negative.rawCode) || 0,
};
}
return emptyQQVipStatus();
}
function qqVipProbeResponseSuccessful(body, probe) {
if (!body || typeof body !== 'object') return false;
const rootCodes = [body.code, body.result]
.filter(value => value != null && typeof value !== 'object')
.map(Number)
.filter(Number.isFinite);
if (rootCodes.some(code => code !== 0)) return false;
const responseKey = probe && probe.responseKey;
const block = responseKey && body[responseKey];
if (block && typeof block === 'object') {
const blockCodes = [block.code, block.result]
.filter(value => value != null && typeof value !== 'object')
.map(Number)
.filter(Number.isFinite);
if (blockCodes.some(code => code !== 0)) return false;
}
return true;
}
function qqVipPayloadBelongsToUin(payload, uin) {
const expected = String(uin || '').replace(/\D/g, '').replace(/^0+/, '');
if (!expected || !payload || typeof payload !== 'object') return false;
let matched = false;
function walk(value, depth) {
if (matched || depth > MAX_WALK_DEPTH || value == null || typeof value !== 'object') return;
if (Array.isArray(value)) {
value.forEach(item => walk(item, depth + 1));
return;
}
Object.keys(value).forEach(key => {
if (matched) return;
const normalizedKey = canonicalQQVipKey(key);
const normalizedValue = String(value[key] == null ? '' : value[key])
.replace(/\D/g, '')
.replace(/^0+/, '');
if (
(/^(?:uin|useruin|userid|qq|accountuin)$/.test(normalizedKey) && normalizedValue === expected) ||
(String(key).replace(/\D/g, '').replace(/^0+/, '') === expected && value[key] && typeof value[key] === 'object')
) {
matched = true;
return;
}
walk(value[key], depth + 1);
});
}
walk(payload, 0);
return matched;
}
function qqVipPayloadScopesForUin(payload, uin) {
const expected = String(uin || '').replace(/\D/g, '').replace(/^0+/, '');
if (!expected || !payload || typeof payload !== 'object') return [];
const scopes = [];
const seen = new Set();
function addScope(value) {
if (!value || typeof value !== 'object' || seen.has(value)) return;
seen.add(value);
scopes.push(value);
}
function walk(value, depth) {
if (depth > MAX_WALK_DEPTH || value == null || typeof value !== 'object') return;
if (Array.isArray(value)) {
value.forEach(item => walk(item, depth + 1));
return;
}
const keys = Object.keys(value);
const directIdentityMatch = keys.some(key => {
const normalizedKey = canonicalQQVipKey(key);
if (!/^(?:uin|useruin|userid|qq|accountuin)$/.test(normalizedKey)) return false;
const normalizedValue = String(value[key] == null ? '' : value[key])
.replace(/\D/g, '')
.replace(/^0+/, '');
return normalizedValue === expected;
});
if (directIdentityMatch) addScope(value);
keys.forEach(key => {
const child = value[key];
const mapKey = String(key).trim();
const mapKeyUin = /^(?:o0*)?\d+$/i.test(mapKey)
? mapKey.replace(/\D/g, '').replace(/^0+/, '')
: '';
if (mapKeyUin === expected && child && typeof child === 'object') {
addScope(child);
return;
}
walk(child, depth + 1);
});
}
walk(payload, 0);
return scopes;
}
async function resolveQQVipFromProbes(probes, requestProbe) {
if (!Array.isArray(probes) || typeof requestProbe !== 'function') {
return { ...emptyQQVipStatus(), errorCount: 0 };
}
const settled = await Promise.allSettled(probes.map(async probe => {
const body = await requestProbe(probe);
const accountScopes = qqVipPayloadScopesForUin(body, probe && probe.uin);
const scopedResults = accountScopes.map(scope => normalizeQQVipPayload(scope, {}, {
expectedUin: String(probe && probe.uin || '').replace(/\D/g, '').replace(/^0+/, ''),
}));
const normalized = accountScopes.length
? combineQQVipResults(scopedResults)
: emptyQQVipStatus();
return {
...normalized,
vipSource: probe && probe.source || '',
rawCode: Number(body && (body.code || body.result)) || 0,
probeSuccessful: qqVipProbeResponseSuccessful(body, probe),
accountMatched: accountScopes.length > 0,
};
}));
const values = settled
.filter(item => item.status === 'fulfilled')
.map(item => item.value);
const positiveValues = values.filter(item => item.decision === 'positive' && item.probeSuccessful);
const negativeValues = values.filter(item => item.decision === 'negative' && item.probeSuccessful);
const authoritativeNegativeValues = negativeValues.filter(item => item.accountMatched);
const probeCount = probes.length;
const completedProbeCount = values.filter(item => item.probeSuccessful).length;
const negativeQuorum = Math.min(2, Math.max(1, probeCount));
const authoritativeNegative = !positiveValues.length && authoritativeNegativeValues.length >= negativeQuorum;
let combined = emptyQQVipStatus();
if (positiveValues.length) {
combined = combineQQVipResults(positiveValues);
} else if (authoritativeNegative) {
combined = combineQQVipResults(authoritativeNegativeValues);
}
combined.errorCount = settled.filter(item => item.status === 'rejected').length;
combined.probeCount = probeCount;
combined.completedProbeCount = completedProbeCount;
combined.accountMatchedProbeCount = values.filter(item => item.accountMatched && item.probeSuccessful).length;
combined.positiveProbeCount = positiveValues.length;
combined.negativeProbeCount = authoritativeNegativeValues.length;
combined.negativeQuorum = negativeQuorum;
combined.authoritativeNegative = authoritativeNegative;
combined.authoritativePositive = positiveValues.length > 0;
combined.probeIncomplete = !positiveValues.length && !authoritativeNegative;
return combined;
}
function qqVipSessionCacheKey(uin, musicKey, cookieObj) {
const userId = String(uin || '').replace(/\D/g, '');
const ticket = String(musicKey || '');
if (!userId || !ticket) return '';
cookieObj = cookieObj || {};
const material = [
userId,
ticket,
String(cookieObj.login_type || ''),
String(cookieObj.qm_keyst || ''),
String(cookieObj.qqmusic_key || ''),
String(cookieObj.music_key || ''),
String(cookieObj.wxskey || ''),
String(cookieObj.psrf_qqaccess_token || ''),
String(cookieObj.psrf_qqrefresh_token || ''),
].join('\n');
const fingerprint = crypto.createHash('sha256').update(material).digest('hex').slice(0, 32);
return userId + ':' + fingerprint;
}
function qqVipCacheTtlMs(status, options) {
options = options || {};
const now = Number(options.now) || Date.now();
const positiveTtl = Math.max(0, Number(options.positiveTtlMs) || 2 * 60 * 1000);
const negativeTtl = Math.max(0, Number(options.negativeTtlMs) || 30 * 1000);
if (!status || !status.resolved || !status.membershipKnown) return 0;
if (!status.isVip) return negativeTtl;
const expiresAt = Number(status.expiresAt) || 0;
if (!expiresAt) return positiveTtl;
return Math.max(0, Math.min(positiveTtl, expiresAt - now));
}
function qqVipEntitlementRights(status) {
status = status || {};
const isSvip = !!status.isSvip || status.vipLevel === 'svip';
const isVip = isSvip || !!status.isVip || status.vipLevel === 'vip';
const membershipKnown = status.membershipKnown === true;
const membershipStale = !!status.membershipStale;
const verified = membershipKnown && !membershipStale;
return {
level: isSvip ? 'svip' : (isVip ? 'vip' : 'none'),
membershipKnown,
verified,
syncState: membershipStale ? 'stale_positive' : (membershipKnown ? 'checked' : 'unknown'),
canPlayVipTracks: verified && isVip,
canPlaySvipTracks: verified && isSvip,
maxQuality: verified ? (isSvip ? 'hires' : (isVip ? 'lossless' : 'standard')) : 'standard',
};
}
function preserveQQVipStalePositive(cachedEntry, nextStatus, options) {
options = options || {};
const now = Number(options.now) || Date.now();
const graceMs = Math.max(0, Number(options.graceMs) || QQ_VIP_STALE_POSITIVE_GRACE_MS);
const cachedStatus = cachedEntry && cachedEntry.value ? cachedEntry.value : cachedEntry;
if (!cachedStatus || !cachedStatus.isVip || !cachedStatus.resolved || cachedStatus.membershipKnown !== true) {
return nextStatus || null;
}
if (nextStatus && nextStatus.isVip && nextStatus.resolved) return nextStatus;
if (nextStatus && nextStatus.authoritativeNegative === true) return nextStatus;
const nextProbeIncomplete = !nextStatus
|| nextStatus.decision === 'unknown'
|| nextStatus.resolved !== true
|| nextStatus.probeIncomplete === true;
if (!nextProbeIncomplete) return nextStatus;
const entitlementExpiresAt = Number(cachedStatus.expiresAt) || 0;
if (entitlementExpiresAt > 0 && entitlementExpiresAt <= now) return nextStatus || null;
const cacheExpiresAt = Number(cachedEntry && cachedEntry.expiresAt) || now;
const staleUntil = Number(cachedEntry && cachedEntry.staleUntil) ||
Math.min(
cacheExpiresAt + graceMs,
entitlementExpiresAt > 0 ? entitlementExpiresAt : Number.MAX_SAFE_INTEGER
);
if (now >= staleUntil) return nextStatus || null;
const probeDecision = nextStatus && nextStatus.decision || 'unknown';
return {
...cachedStatus,
membershipKnown: true,
resolved: true,
decision: 'positive',
membershipStale: true,
vipSyncState: 'stale_positive',
vipEvidenceConflict: probeDecision === 'negative',
probeDecision,
vipSource: 'qq-vip-cache-stale-positive',
staleUntil,
};
}
module.exports = {
normalizeQQVipPayload,
combineQQVipResults,
resolveQQVipFromProbes,
qqVipSessionCacheKey,
qqVipCacheTtlMs,
qqVipEntitlementRights,
preserveQQVipStalePositive,
qqVipObjectLooksExpired,
_test: {
canonicalQQVipKey,
primitiveMembershipSignal,
normalizedExpiryMs,
assessQQVipObject,
collectQQVipAssessments,
emptyQQVipStatus,
qqVipProbeResponseSuccessful,
qqVipPayloadBelongsToUin,
qqVipPayloadScopesForUin,
qqVipEntitlementRights,
preserveQQVipStalePositive,
},
};