@pacoyang/wechaty-puppet-wechat 0.28.7 → 0.28.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/bridge.d.ts +41 -1
- package/dist/src/bridge.d.ts.map +1 -1
- package/dist/src/bridge.js +135 -0
- package/dist/src/bridge.js.map +1 -1
- package/dist/src/config.d.ts +0 -1
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +5 -5
- package/dist/src/config.js.map +1 -1
- package/dist/src/puppet-wechat.d.ts +32 -3
- package/dist/src/puppet-wechat.d.ts.map +1 -1
- package/dist/src/puppet-wechat.js +187 -7
- package/dist/src/puppet-wechat.js.map +1 -1
- package/dist/src/pure-function-helpers/is-type.js +2 -3
- package/dist/src/pure-function-helpers/is-type.js.map +1 -1
- package/dist/src/pure-function-helpers/message-extname.js +1 -2
- package/dist/src/pure-function-helpers/message-extname.js.map +1 -1
- package/dist/src/pure-function-helpers/message-filename.js +1 -2
- package/dist/src/pure-function-helpers/message-filename.js.map +1 -1
- package/dist/src/pure-function-helpers/message-raw-payload-parser.js +1 -2
- package/dist/src/pure-function-helpers/message-raw-payload-parser.js.map +1 -1
- package/dist/src/pure-function-helpers/normalize-scan-status.js +1 -2
- package/dist/src/pure-function-helpers/normalize-scan-status.js.map +1 -1
- package/dist/src/pure-function-helpers/web-message-type.js +1 -2
- package/dist/src/pure-function-helpers/web-message-type.js.map +1 -1
- package/dist/src/pure-function-helpers/xml.js +6 -7
- package/dist/src/pure-function-helpers/xml.js.map +1 -1
- package/dist/src/session-monitor.d.ts +16 -0
- package/dist/src/session-monitor.d.ts.map +1 -1
- package/dist/src/session-monitor.js +55 -4
- package/dist/src/session-monitor.js.map +1 -1
- package/dist/src/session-monitor.spec.d.ts +3 -0
- package/dist/src/session-monitor.spec.d.ts.map +1 -0
- package/dist/src/session-monitor.spec.js +65 -0
- package/dist/src/session-monitor.spec.js.map +1 -0
- package/package.json +1 -1
- package/src/bridge.ts +154 -0
- package/src/puppet-wechat.ts +212 -17
- package/src/session-monitor.spec.ts +80 -0
- package/src/session-monitor.ts +55 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"web-message-type.js","sourceRoot":"","sources":["../../../src/pure-function-helpers/web-message-type.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"web-message-type.js","sourceRoot":"","sources":["../../../src/pure-function-helpers/web-message-type.ts"],"names":[],"mappings":";;AAUA,wCA4DC;AAtED,gDAI+C;AAE/C,mDAEuC;AAEvC,SAAgB,cAAc,CAC5B,UAAgC;IAGhC,QAAQ,UAAU,CAAC,OAAO,EAAE,CAAC;QAC3B,KAAK,4BAAc,CAAC,IAAI;YACtB,QAAQ,UAAU,CAAC,UAAU,EAAE,CAAC;gBAC9B,KAAK,4BAAc,CAAC,QAAQ;oBAC1B,OAAO,4BAAW,CAAC,UAAU,CAAA;gBAE/B;oBACE,OAAO,4BAAW,CAAC,IAAI,CAAA;YAC3B,CAAC;QAEH,KAAK,4BAAc,CAAC,QAAQ,CAAC;QAC7B,KAAK,4BAAc,CAAC,KAAK;YACvB,OAAO,4BAAW,CAAC,KAAK,CAAA;QAE1B,KAAK,4BAAc,CAAC,KAAK;YACvB,OAAO,4BAAW,CAAC,KAAK,CAAA;QAE1B,KAAK,4BAAc,CAAC,UAAU,CAAC;QAC/B,KAAK,4BAAc,CAAC,KAAK;YACvB,OAAO,4BAAW,CAAC,KAAK,CAAA;QAE1B,KAAK,4BAAc,CAAC,GAAG;YACrB,QAAQ,UAAU,CAAC,UAAU,EAAE,CAAC;gBAC9B,KAAK,2BAAa,CAAC,MAAM,CAAC;gBAC1B,KAAK,2BAAa,CAAC,GAAG,CAAC;gBACvB,KAAK,2BAAa,CAAC,WAAW;oBAC5B,OAAO,4BAAW,CAAC,UAAU,CAAA;gBAE/B;oBACE,OAAO,4BAAW,CAAC,IAAI,CAAA;YAC3B,CAAC;QAEH;;;;;WAKG;QACH,KAAK,4BAAc,CAAC,GAAG;YACrB,OAAO,4BAAW,CAAC,IAAI,CAAA;QACzB,kBAAkB;QAClB,KAAK,4BAAc,CAAC,QAAQ;YAC1B,OAAO,4BAAW,CAAC,QAAQ,CAAA;QAC7B,4BAA4B;QAC5B,4BAA4B;QAC5B,4BAA4B;QAC5B,4BAA4B;QAC5B,4BAA4B;QAC5B,4BAA4B;QAC5B,4BAA4B;QAC5B,4BAA4B;QAC5B,8BAA8B;QAC9B,+BAA+B;QAC/B;YACE,OAAO,4BAAW,CAAC,IAAI,CAAA;IAC3B,CAAC;AACH,CAAC"}
|
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.stripHtml = stripHtml;
|
|
4
|
+
exports.unescapeHtml = unescapeHtml;
|
|
5
|
+
exports.digestEmoji = digestEmoji;
|
|
6
|
+
exports.unifyEmoji = unifyEmoji;
|
|
7
|
+
exports.stripEmoji = stripEmoji;
|
|
8
|
+
exports.plainText = plainText;
|
|
4
9
|
function stripHtml(html) {
|
|
5
10
|
if (!html) {
|
|
6
11
|
return '';
|
|
7
12
|
}
|
|
8
13
|
return html.replace(/(<([^>]+)>)/ig, '');
|
|
9
14
|
}
|
|
10
|
-
exports.stripHtml = stripHtml;
|
|
11
15
|
function unescapeHtml(str) {
|
|
12
16
|
if (!str) {
|
|
13
17
|
return '';
|
|
@@ -19,7 +23,6 @@ function unescapeHtml(str) {
|
|
|
19
23
|
.replace(/</g, '<')
|
|
20
24
|
.replace(/&/g, '&');
|
|
21
25
|
}
|
|
22
|
-
exports.unescapeHtml = unescapeHtml;
|
|
23
26
|
function digestEmoji(html) {
|
|
24
27
|
if (!html) {
|
|
25
28
|
return '';
|
|
@@ -28,7 +31,6 @@ function digestEmoji(html) {
|
|
|
28
31
|
.replace(/<img class="(\w*?emoji) (\w*?emoji[^"]+?)" text="(.*?)_web" src=[^>]+>/g, '$3') // <img class="emoji emoji1f4a4" text="[流汗]_web" src="/zh_CN/htmledition/v2/images/spacer.gif" />
|
|
29
32
|
.replace(/<span class="(\w*?emoji) (\w*?emoji[^"]+?)"><\/span>/g, '[$2]'); // '<span class="emoji emoji1f334"></span>'
|
|
30
33
|
}
|
|
31
|
-
exports.digestEmoji = digestEmoji;
|
|
32
34
|
/**
|
|
33
35
|
* unifyEmoji: the same emoji will be encoded as different xml code in browser. unify them.
|
|
34
36
|
*
|
|
@@ -44,7 +46,6 @@ function unifyEmoji(html) {
|
|
|
44
46
|
.replace(/<img class="(\w*?emoji) (\w*?emoji[^"]+?)" text="(.*?)_web" src=[^>]+>/g, '<emoji code="$2"/>') // <img class="emoji emoji1f4a4" text="[流汗]_web" src="/zh_CN/htmledition/v2/images/spacer.gif" />
|
|
45
47
|
.replace(/<span class="(\w*?emoji) (\w*?emoji[^"]+?)"><\/span>/g, '<emoji code="$2"/>'); // '<span class="emoji emoji1f334"></span>'
|
|
46
48
|
}
|
|
47
|
-
exports.unifyEmoji = unifyEmoji;
|
|
48
49
|
function stripEmoji(html) {
|
|
49
50
|
if (!html) {
|
|
50
51
|
return '';
|
|
@@ -53,12 +54,10 @@ function stripEmoji(html) {
|
|
|
53
54
|
.replace(/<img class="(\w*?emoji) (\w*?emoji[^"]+?)" text="(.*?)_web" src=[^>]+>/g, '') // <img class="emoji emoji1f4a4" text="[流汗]_web" src="/zh_CN/htmledition/v2/images/spacer.gif" />
|
|
54
55
|
.replace(/<span class="(\w*?emoji) (\w*?emoji[^"]+?)"><\/span>/g, ''); // '<span class="emoji emoji1f334"></span>'
|
|
55
56
|
}
|
|
56
|
-
exports.stripEmoji = stripEmoji;
|
|
57
57
|
function plainText(html) {
|
|
58
58
|
if (!html) {
|
|
59
59
|
return '';
|
|
60
60
|
}
|
|
61
61
|
return stripHtml(unescapeHtml(stripHtml(digestEmoji(html))));
|
|
62
62
|
}
|
|
63
|
-
exports.plainText = plainText;
|
|
64
63
|
//# sourceMappingURL=xml.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"xml.js","sourceRoot":"","sources":["../../../src/pure-function-helpers/xml.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"xml.js","sourceRoot":"","sources":["../../../src/pure-function-helpers/xml.ts"],"names":[],"mappings":";;AAAA,8BAKC;AAED,oCAUC;AAED,kCAWC;AASD,gCAWC;AAED,gCAWC;AAED,8BAaC;AA9ED,SAAgB,SAAS,CAAE,IAAa;IACtC,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,OAAO,EAAE,CAAA;IACX,CAAC;IACD,OAAO,IAAI,CAAC,OAAO,CAAC,eAAe,EAAE,EAAE,CAAC,CAAA;AAC1C,CAAC;AAED,SAAgB,YAAY,CAAE,GAAY;IACxC,IAAI,CAAC,GAAG,EAAE,CAAC;QACT,OAAO,EAAE,CAAA;IACX,CAAC;IACD,OAAO,GAAG;SACP,OAAO,CAAC,SAAS,EAAE,GAAG,CAAC;SACvB,OAAO,CAAC,SAAS,EAAE,GAAG,CAAC;SACvB,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC;SACrB,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC;SACrB,OAAO,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAA;AAC3B,CAAC;AAED,SAAgB,WAAW,CAAE,IAAa;IACxC,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,OAAO,EAAE,CAAA;IACX,CAAC;IACD,OAAO,IAAI;SACR,OAAO,CAAC,yEAAyE,EAChF,IAAI,CACL,CAAC,iGAAiG;SAClG,OAAO,CAAC,uDAAuD,EAC9D,MAAM,CACP,CAAA,CAAC,2CAA2C;AACjD,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,UAAU,CAAE,IAAa;IACvC,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,OAAO,EAAE,CAAA;IACX,CAAC;IACD,OAAO,IAAI;SACR,OAAO,CAAC,yEAAyE,EAChF,oBAAoB,CACrB,CAAC,iGAAiG;SAClG,OAAO,CAAC,uDAAuD,EAC9D,oBAAoB,CACrB,CAAA,CAAC,2CAA2C;AACjD,CAAC;AAED,SAAgB,UAAU,CAAE,IAAa;IACvC,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,OAAO,EAAE,CAAA;IACX,CAAC;IACD,OAAO,IAAI;SACR,OAAO,CAAC,yEAAyE,EAChF,EAAE,CACH,CAAC,iGAAiG;SAClG,OAAO,CAAC,uDAAuD,EAC9D,EAAE,CACH,CAAA,CAAC,2CAA2C;AACjD,CAAC;AAED,SAAgB,SAAS,CAAE,IAAa;IACtC,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,OAAO,EAAE,CAAA;IACX,CAAC;IACD,OAAO,SAAS,CACd,YAAY,CACV,SAAS,CACP,WAAW,CACT,IAAI,CACL,CACF,CACF,CACF,CAAA;AACH,CAAC"}
|
|
@@ -17,6 +17,7 @@ export interface SyncCheckAnswer {
|
|
|
17
17
|
export interface ApiAnswer {
|
|
18
18
|
api: string;
|
|
19
19
|
failures: number;
|
|
20
|
+
failingSince: null | number;
|
|
20
21
|
ret: string;
|
|
21
22
|
time: number;
|
|
22
23
|
}
|
|
@@ -38,6 +39,21 @@ export declare function parseSyncCheck(body: string): undefined | {
|
|
|
38
39
|
retcode: string;
|
|
39
40
|
selector: string;
|
|
40
41
|
};
|
|
42
|
+
export declare const REFUSED_AFTER: number;
|
|
43
|
+
export declare const REFUSED_SYNC_FAILURES = 10;
|
|
44
|
+
export declare const REFUSED_SEND_FAILURES = 5;
|
|
45
|
+
/**
|
|
46
|
+
* Whether an API failure looks like the server refusing the session (numeric
|
|
47
|
+
* BaseResponse.Ret other than 1205 rate limiting), rather than a transient
|
|
48
|
+
* network error.
|
|
49
|
+
*/
|
|
50
|
+
export declare function isRefusalCandidate(answer: null | ApiAnswer): boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Why the session counts as refused by the server, or undefined while it
|
|
53
|
+
* does not. Only answers from the server count: a request that failed on the
|
|
54
|
+
* way (`request failed: ...`) says nothing about the session.
|
|
55
|
+
*/
|
|
56
|
+
export declare function refusedSession(health: SessionHealth): undefined | string;
|
|
41
57
|
export declare class SessionMonitor {
|
|
42
58
|
private since;
|
|
43
59
|
private selfId?;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session-monitor.d.ts","sourceRoot":"","sources":["../../src/session-monitor.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,MAAM,WAAW,eAAe;IAC9B,OAAO,EAAI,MAAM,CAAC;IAClB,QAAQ,EAAG,MAAM,CAAC;IAClB,IAAI,EAAO,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,SAAS;IACxB,GAAG,EAAQ,MAAM,CAAC;IAElB,QAAQ,EAAG,MAAM,CAAC;IAElB,GAAG,EAAQ,MAAM,CAAC;IAClB,IAAI,EAAO,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,aAAa;IAC5B,GAAG,EAAa,MAAM,CAAC;IAEvB,KAAK,EAAW,MAAM,CAAC;IACvB,SAAS,EAAO,IAAI,GAAG,eAAe,CAAC;IACvC,QAAQ,EAAQ,IAAI,GAAG,SAAS,CAAC;IACjC,QAAQ,EAAQ,IAAI,GAAG,SAAS,CAAC;IAEjC,YAAY,EAAI,IAAI,GAAG,MAAM,CAAC;IAE9B,eAAe,EAAG,IAAI,GAAG,MAAM,CAAC;CACjC;AAUD,MAAM,MAAM,WAAW,GAAG,WAAW,GAAG,MAAM,CAAA;AAE9C;;GAEG;AACH,wBAAgB,WAAW,CAAE,GAAG,EAAE,MAAM,GAAG,SAAS,GAAG,WAAW,CAMjE;AAED,wBAAgB,cAAc,CAAE,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,CAG/F;AAED,qBAAa,cAAc;IAEzB,OAAO,CAAC,KAAK,CAAwB;IACrC,OAAO,CAAC,MAAM,CAAC,CAAkB;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jB,OAAO,EAAI,MAAM,EACjB,QAAQ,GAAG,KAAK,CAAC;QAAE,YAAY,CAAC,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,CAAA;KAAE,CAAM,GACjE,SAAS;IA0CL,MAAM,IAAK,aAAa;CAYhC;AAED,eAAe,cAAc,CAAA"}
|
|
@@ -11,7 +11,11 @@
|
|
|
11
11
|
* page for this.
|
|
12
12
|
*/
|
|
13
13
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
-
exports.SessionMonitor = exports.
|
|
14
|
+
exports.SessionMonitor = exports.REFUSED_SEND_FAILURES = exports.REFUSED_SYNC_FAILURES = exports.REFUSED_AFTER = void 0;
|
|
15
|
+
exports.watchedCall = watchedCall;
|
|
16
|
+
exports.parseSyncCheck = parseSyncCheck;
|
|
17
|
+
exports.isRefusalCandidate = isRefusalCandidate;
|
|
18
|
+
exports.refusedSession = refusedSession;
|
|
15
19
|
// webwxsync and the calls that send a message, by the last part of their path
|
|
16
20
|
const API_REGEX = /\/(webwxsync|webwxsendmsg|webwxsendappmsg|webwxsendmsgimg|webwxsendvideomsg|webwxsendemoticon)(\?|$)/;
|
|
17
21
|
const SYNCCHECK_REGEX = /\/synccheck(\?|$)/;
|
|
@@ -29,12 +33,55 @@ function watchedCall(url) {
|
|
|
29
33
|
const match = API_REGEX.exec(url);
|
|
30
34
|
return match ? match[1] : undefined;
|
|
31
35
|
}
|
|
32
|
-
exports.watchedCall = watchedCall;
|
|
33
36
|
function parseSyncCheck(body) {
|
|
34
37
|
const match = SYNCCHECK_BODY_REGEX.exec(body);
|
|
35
38
|
return match ? { retcode: match[1], selector: match[2] } : undefined;
|
|
36
39
|
}
|
|
37
|
-
|
|
40
|
+
// A session the server stopped serving keeps answering synccheck as usual,
|
|
41
|
+
// while webwxsync and every send answer an error (seen: BaseResponse.Ret 3,
|
|
42
|
+
// with the session's Sid still recognised: a wrong Sid answers 1101). The
|
|
43
|
+
// web app retries webwxsync a few times a second and never leaves, and a
|
|
44
|
+
// reload brings back the same refused session. Errors that last this long,
|
|
45
|
+
// with nothing succeeding in between, mean the session is gone.
|
|
46
|
+
exports.REFUSED_AFTER = 3 * 60 * 1000;
|
|
47
|
+
exports.REFUSED_SYNC_FAILURES = 10;
|
|
48
|
+
exports.REFUSED_SEND_FAILURES = 5;
|
|
49
|
+
// 1205 is the server asking us to send less; the web app logs out on it by
|
|
50
|
+
// itself.
|
|
51
|
+
const NOT_REFUSAL = ['1205'];
|
|
52
|
+
/**
|
|
53
|
+
* Whether an API failure looks like the server refusing the session (numeric
|
|
54
|
+
* BaseResponse.Ret other than 1205 rate limiting), rather than a transient
|
|
55
|
+
* network error.
|
|
56
|
+
*/
|
|
57
|
+
function isRefusalCandidate(answer) {
|
|
58
|
+
if (!answer || !answer.failingSince || answer.failures <= 0) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
return /^-?\d+$/.test(answer.ret) && !NOT_REFUSAL.includes(answer.ret);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Why the session counts as refused by the server, or undefined while it
|
|
65
|
+
* does not. Only answers from the server count: a request that failed on the
|
|
66
|
+
* way (`request failed: ...`) says nothing about the session.
|
|
67
|
+
*/
|
|
68
|
+
function refusedSession(health) {
|
|
69
|
+
const checks = [
|
|
70
|
+
[health.lastSync, exports.REFUSED_SYNC_FAILURES],
|
|
71
|
+
[health.lastSend, exports.REFUSED_SEND_FAILURES],
|
|
72
|
+
];
|
|
73
|
+
for (const [answer, minFailures] of checks) {
|
|
74
|
+
if (!answer || !isRefusalCandidate(answer) || answer.failures < minFailures) {
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
const failingSince = answer.failingSince;
|
|
78
|
+
const minutes = (health.now - failingSince) / 60000;
|
|
79
|
+
if (health.now - failingSince >= exports.REFUSED_AFTER) {
|
|
80
|
+
return `${answer.api} answered Ret ${answer.ret} ${answer.failures} times in a row for ${Math.round(minutes)} minutes`;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
38
85
|
class SessionMonitor {
|
|
39
86
|
constructor() {
|
|
40
87
|
this.since = Date.now();
|
|
@@ -75,8 +122,12 @@ class SessionMonitor {
|
|
|
75
122
|
onApiAnswer(api, ret, started, messages = []) {
|
|
76
123
|
const isSync = api === 'webwxsync';
|
|
77
124
|
const previous = isSync ? this.lastSync : this.lastSend;
|
|
125
|
+
const now = Date.now();
|
|
78
126
|
const failures = ret === '0' ? 0 : ((previous && previous.failures) || 0) + 1;
|
|
79
|
-
const
|
|
127
|
+
const failingSince = ret === '0'
|
|
128
|
+
? null
|
|
129
|
+
: (previous && previous.failures && previous.failingSince) || now;
|
|
130
|
+
const answer = { api, failingSince, failures, ret, time: now };
|
|
80
131
|
if (isSync) {
|
|
81
132
|
this.lastSync = answer;
|
|
82
133
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session-monitor.js","sourceRoot":"","sources":["../../src/session-monitor.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;GAUG;;;
|
|
1
|
+
{"version":3,"file":"session-monitor.js","sourceRoot":"","sources":["../../src/session-monitor.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;GAUG;;;AA6CH,kCAMC;AAED,wCAGC;AAoBD,gDAKC;AAOD,wCAgBC;AAxED,8EAA8E;AAC9E,MAAM,SAAS,GAAS,sGAAsG,CAAA;AAC9H,MAAM,eAAe,GAAG,mBAAmB,CAAA;AAC3C,8CAA8C;AAC9C,MAAM,oBAAoB,GAAG,wDAAwD,CAAA;AACrF,8DAA8D;AAC9D,MAAM,oBAAoB,GAAG,EAAE,CAAA;AAI/B;;GAEG;AACH,SAAgB,WAAW,CAAE,GAAW;IACtC,IAAI,eAAe,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;QAC9B,OAAO,WAAW,CAAA;IACpB,CAAC;IACD,MAAM,KAAK,GAAG,SAAS,CAAC,IAAI,CAAC,GA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../../src/session-monitor.spec.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
#!/usr/bin/env ts-node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
+
};
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
const blue_tape_1 = __importDefault(require("blue-tape"));
|
|
8
|
+
const session_monitor_1 = require("./session-monitor");
|
|
9
|
+
function health(lastSync, lastSend = null, now = Date.now()) {
|
|
10
|
+
return {
|
|
11
|
+
lastReceived: null,
|
|
12
|
+
lastSend,
|
|
13
|
+
lastSync,
|
|
14
|
+
now,
|
|
15
|
+
sentWithoutSync: null,
|
|
16
|
+
since: now - 3600 * 1000,
|
|
17
|
+
syncCheck: null,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function answer(api, ret, failures, failingFor, now = Date.now()) {
|
|
21
|
+
return { api, failingSince: failures ? now - failingFor : null, failures, ret, time: now };
|
|
22
|
+
}
|
|
23
|
+
(0, blue_tape_1.default)('SessionMonitor remembers when a failing streak started', async (t) => {
|
|
24
|
+
const monitor = new session_monitor_1.SessionMonitor();
|
|
25
|
+
const first = monitor.onApiAnswer('webwxsync', '3', Date.now());
|
|
26
|
+
t.equal(first.failures, 1, 'first failure counted');
|
|
27
|
+
t.ok(first.failingSince, 'streak start set');
|
|
28
|
+
const second = monitor.onApiAnswer('webwxsync', '3', Date.now());
|
|
29
|
+
t.equal(second.failures, 2, 'second failure counted');
|
|
30
|
+
t.equal(second.failingSince, first.failingSince, 'streak start kept');
|
|
31
|
+
const ok = monitor.onApiAnswer('webwxsync', '0', Date.now());
|
|
32
|
+
t.equal(ok.failingSince, null, 'a success ends the streak');
|
|
33
|
+
const again = monitor.onApiAnswer('webwxsync', '3', Date.now());
|
|
34
|
+
t.ok(again.failingSince && again.failingSince >= ok.time, 'a new streak starts after the success');
|
|
35
|
+
});
|
|
36
|
+
(0, blue_tape_1.default)('refusedSession() on the 2026-12-06 pattern: webwxsync Ret 3 for minutes', async (t) => {
|
|
37
|
+
const reason = (0, session_monitor_1.refusedSession)(health(answer('webwxsync', '3', 40000, 4 * 3600 * 1000)));
|
|
38
|
+
t.ok(reason, 'refused');
|
|
39
|
+
t.ok(reason && /webwxsync answered Ret 3/.test(reason), 'reason names the call and Ret: ' + reason);
|
|
40
|
+
});
|
|
41
|
+
(0, blue_tape_1.default)('refusedSession() waits for both enough failures and enough time', async (t) => {
|
|
42
|
+
t.notOk((0, session_monitor_1.refusedSession)(health(answer('webwxsync', '3', session_monitor_1.REFUSED_SYNC_FAILURES - 1, session_monitor_1.REFUSED_AFTER * 2))), 'too few failures');
|
|
43
|
+
t.notOk((0, session_monitor_1.refusedSession)(health(answer('webwxsync', '3', 500, session_monitor_1.REFUSED_AFTER - 1000))), 'not long enough');
|
|
44
|
+
t.ok((0, session_monitor_1.refusedSession)(health(answer('webwxsync', '3', session_monitor_1.REFUSED_SYNC_FAILURES, session_monitor_1.REFUSED_AFTER))), 'both reached');
|
|
45
|
+
});
|
|
46
|
+
(0, blue_tape_1.default)('refusedSession() ignores what does not come from the server, and rate limiting', async (t) => {
|
|
47
|
+
t.notOk((0, session_monitor_1.refusedSession)(health(answer('webwxsync', 'request failed: net::ERR_ABORTED', 500, session_monitor_1.REFUSED_AFTER * 2))), 'network failure');
|
|
48
|
+
t.notOk((0, session_monitor_1.refusedSession)(health(answer('webwxsync', '1205', 500, session_monitor_1.REFUSED_AFTER * 2))), '1205 is rate limiting');
|
|
49
|
+
t.notOk((0, session_monitor_1.refusedSession)(health(answer('webwxsync', '0', 0, 0))), 'working session');
|
|
50
|
+
t.notOk((0, session_monitor_1.refusedSession)(health(null)), 'nothing seen yet');
|
|
51
|
+
});
|
|
52
|
+
(0, blue_tape_1.default)('refusedSession() on sends alone', async (t) => {
|
|
53
|
+
const sync = answer('webwxsync', '0', 0, 0);
|
|
54
|
+
t.notOk((0, session_monitor_1.refusedSession)(health(sync, answer('webwxsendmsg', '3', session_monitor_1.REFUSED_SEND_FAILURES - 1, session_monitor_1.REFUSED_AFTER * 2))), 'too few sends');
|
|
55
|
+
t.ok((0, session_monitor_1.refusedSession)(health(sync, answer('webwxsendmsg', '3', session_monitor_1.REFUSED_SEND_FAILURES, session_monitor_1.REFUSED_AFTER))), 'sends refused for long enough');
|
|
56
|
+
});
|
|
57
|
+
(0, blue_tape_1.default)('isRefusalCandidate() distinguishes server refusals from rate limiting and network errors', async (t) => {
|
|
58
|
+
t.ok((0, session_monitor_1.isRefusalCandidate)(answer('webwxsync', '3', 3, 60 * 1000)), 'Ret 3 is a refusal candidate');
|
|
59
|
+
t.ok((0, session_monitor_1.isRefusalCandidate)(answer('webwxsendappmsg', '-1', 3, 60 * 1000)), 'Ret -1 is a refusal candidate');
|
|
60
|
+
t.notOk((0, session_monitor_1.isRefusalCandidate)(answer('webwxsendmsg', '1205', 3, 60 * 1000)), '1205 is rate limiting');
|
|
61
|
+
t.notOk((0, session_monitor_1.isRefusalCandidate)(answer('webwxsync', 'request failed: net::ERR_ABORTED', 3, 60 * 1000)), 'network error is not a refusal');
|
|
62
|
+
t.notOk((0, session_monitor_1.isRefusalCandidate)(answer('webwxsync', '0', 0, 0)), 'success is not a refusal');
|
|
63
|
+
t.notOk((0, session_monitor_1.isRefusalCandidate)(null), 'null is not a refusal');
|
|
64
|
+
});
|
|
65
|
+
//# sourceMappingURL=session-monitor.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session-monitor.spec.js","sourceRoot":"","sources":["../../src/session-monitor.spec.ts"],"names":[],"mappings":";;;;;;AAEA,0DAA6B;AAE7B,uDAS4C;AAE5C,SAAS,MAAM,CAAE,QAA0B,EAAE,WAA6B,IAAI,EAAE,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE;IAC9F,OAAO;QACL,YAAY,EAAM,IAAI;QACtB,QAAQ;QACR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|
|
1214
1214
|
await this.page.reload()
|
|
1215
1215
|
}
|
|
1216
1216
|
|
|
1217
|
+
/**
|
|
1218
|
+
* Leave a session the server refuses for the login page. Reloading alone
|
|
1219
|
+
* brings the refused session back: the server still renders the page as
|
|
1220
|
+
* logged in for as long as its session cookies are there. So those go,
|
|
1221
|
+
* while `webwx_auth_ticket` (valid for years) and `wxuin` stay: with them
|
|
1222
|
+
* the login page can ask the phone to confirm instead of showing a QR code.
|
|
1223
|
+
* When `preferQrcode` is true, `login_frequency` and `last_wxuin` are also
|
|
1224
|
+
* dropped so the page lands directly on a fresh QR code while still keeping
|
|
1225
|
+
* `webwx_auth_ticket` and `wxuin` for later phone confirmations.
|
|
1226
|
+
*/
|
|
1227
|
+
public async dropSession (preferQrcode = false): Promise<void> {
|
|
1228
|
+
log.verbose('PuppetWeChatBridge', 'dropSession(preferQrcode=%s)', preferQrcode)
|
|
1229
|
+
if (!this.page) {
|
|
1230
|
+
throw new Error('no page')
|
|
1231
|
+
}
|
|
1232
|
+
const page = this.page
|
|
1233
|
+
const url = page.url()
|
|
1234
|
+
const list = await page.cookies()
|
|
1235
|
+
const toRemove = preferQrcode
|
|
1236
|
+
? [...SESSION_COOKIES, 'login_frequency', 'last_wxuin']
|
|
1237
|
+
: SESSION_COOKIES
|
|
1238
|
+
if (!preferQrcode) {
|
|
1239
|
+
await this.offerConfirmOnPhone(list)
|
|
1240
|
+
}
|
|
1241
|
+
const drop = list.filter(c => toRemove.includes(c.name))
|
|
1242
|
+
if (drop.length) {
|
|
1243
|
+
await page.deleteCookie(...drop.map(c => ({ domain: c.domain, name: c.name, path: c.path, url })))
|
|
1244
|
+
}
|
|
1245
|
+
log.info('PuppetWeChatBridge', 'dropSession() removed %s, reloading', drop.map(c => c.name).join(', ') || 'no cookies')
|
|
1246
|
+
await page.reload()
|
|
1247
|
+
}
|
|
1248
|
+
|
|
1249
|
+
/**
|
|
1250
|
+
* Make the login page offer confirming the login on the phone. False when
|
|
1251
|
+
* the cookies needed for it are missing (no `webwx_auth_ticket` or
|
|
1252
|
+
* `wxuin`): then only a QR code can log in.
|
|
1253
|
+
*/
|
|
1254
|
+
public async offerConfirmOnPhone (list?: Cookie[]): Promise<boolean> {
|
|
1255
|
+
if (!this.page) {
|
|
1256
|
+
throw new Error('no page')
|
|
1257
|
+
}
|
|
1258
|
+
const page = this.page
|
|
1259
|
+
list = list || await page.cookies()
|
|
1260
|
+
const uin = list.find(c => c.name === 'wxuin' && !!c.value)
|
|
1261
|
+
const ticket = list.find(c => c.name === 'webwx_auth_ticket' && !!c.value)
|
|
1262
|
+
if (!uin || !ticket) {
|
|
1263
|
+
return false
|
|
1264
|
+
}
|
|
1265
|
+
const host = new URL(page.url()).hostname
|
|
1266
|
+
// the web app sets these for two days
|
|
1267
|
+
const expires = Math.floor(Date.now() / 1000) + 2 * 24 * 3600
|
|
1268
|
+
await page.setCookie(
|
|
1269
|
+
{ domain: host, expires, name: 'login_frequency', path: '/', value: '2' },
|
|
1270
|
+
{ domain: host, expires, name: 'last_wxuin', path: '/', value: uin.value },
|
|
1271
|
+
)
|
|
1272
|
+
return true
|
|
1273
|
+
}
|
|
1274
|
+
|
|
1275
|
+
/**
|
|
1276
|
+
* What the web app's login page is doing, or null when it is not showing
|
|
1277
|
+
* (logged in, or still loading).
|
|
1278
|
+
*/
|
|
1279
|
+
public async loginPageState (): Promise<null | LoginPageState> {
|
|
1280
|
+
if (!this.page) {
|
|
1281
|
+
throw new Error('no page')
|
|
1282
|
+
}
|
|
1283
|
+
return this.page.evaluate(() => {
|
|
1284
|
+
const w = window as any
|
|
1285
|
+
if (w.MMCgi && w.MMCgi.isLogin) {
|
|
1286
|
+
return null
|
|
1287
|
+
}
|
|
1288
|
+
const scope = w.angular && w.angular.element('[ng-controller="loginController"]').scope()
|
|
1289
|
+
if (!scope) {
|
|
1290
|
+
return null
|
|
1291
|
+
}
|
|
1292
|
+
return {
|
|
1293
|
+
canConfirmOnPhone : !!scope.isAssociationLogin,
|
|
1294
|
+
code : scope.code === undefined ? null : scope.code,
|
|
1295
|
+
hasQrcode : !!scope.qrcodeUrl,
|
|
1296
|
+
// scope.qrcodeUrl is the image URL (/qrcode/<uuid>); convert it to the
|
|
1297
|
+
// scannable QR payload (/l/<uuid>), matching Event.onScan.
|
|
1298
|
+
qrcodeUrl : scope.qrcodeUrl ? String(scope.qrcodeUrl).replace(/\/qrcode\//, '/l/') : null,
|
|
1299
|
+
waitingForPhone : !!scope.isWaitingAsConfirm,
|
|
1300
|
+
}
|
|
1301
|
+
})
|
|
1302
|
+
}
|
|
1303
|
+
|
|
1304
|
+
/**
|
|
1305
|
+
* Ask the phone to confirm the login, as the login page's "Log in" button
|
|
1306
|
+
* does. The web app then waits for the confirmation and logs in by itself;
|
|
1307
|
+
* when the server refuses to ask the phone, it shows a QR code instead.
|
|
1308
|
+
*/
|
|
1309
|
+
public async pushLoginConfirm (): Promise<void> {
|
|
1310
|
+
if (!this.page) {
|
|
1311
|
+
throw new Error('no page')
|
|
1312
|
+
}
|
|
1313
|
+
await this.page.evaluate(() => {
|
|
1314
|
+
const w = window as any
|
|
1315
|
+
const scope = w.angular && w.angular.element('[ng-controller="loginController"]').scope()
|
|
1316
|
+
if (!scope) {
|
|
1317
|
+
throw new Error('Login page is not loaded yet')
|
|
1318
|
+
}
|
|
1319
|
+
// a confirmation asked before is still being waited for
|
|
1320
|
+
if (w.checkLoginPromise) {
|
|
1321
|
+
w.checkLoginPromise.abort()
|
|
1322
|
+
w.checkLoginPromise = null
|
|
1323
|
+
}
|
|
1324
|
+
scope.$apply(() => scope.associationLogin())
|
|
1325
|
+
})
|
|
1326
|
+
}
|
|
1327
|
+
|
|
1328
|
+
/**
|
|
1329
|
+
* Stop waiting for the phone and show a fresh QR code. If the page is
|
|
1330
|
+
* already on the QR code view (which may have expired), reload straight
|
|
1331
|
+
* into a new QR code instead.
|
|
1332
|
+
*/
|
|
1333
|
+
public async showLoginQrcode (): Promise<void> {
|
|
1334
|
+
if (!this.page) {
|
|
1335
|
+
throw new Error('no page')
|
|
1336
|
+
}
|
|
1337
|
+
const switched = await this.page.evaluate(() => {
|
|
1338
|
+
const w = window as any
|
|
1339
|
+
const scope = w.angular && w.angular.element('[ng-controller="loginController"]').scope()
|
|
1340
|
+
if (!scope) {
|
|
1341
|
+
return false
|
|
1342
|
+
}
|
|
1343
|
+
if (w.checkLoginPromise) {
|
|
1344
|
+
w.checkLoginPromise.abort()
|
|
1345
|
+
w.checkLoginPromise = null
|
|
1346
|
+
}
|
|
1347
|
+
if (scope.isAssociationLogin || scope.isWaitingAsConfirm) {
|
|
1348
|
+
scope.$apply(() => scope.qrcodeLogin())
|
|
1349
|
+
return true
|
|
1350
|
+
}
|
|
1351
|
+
return false
|
|
1352
|
+
})
|
|
1353
|
+
if (!switched) {
|
|
1354
|
+
await this.dropSession(true)
|
|
1355
|
+
}
|
|
1356
|
+
}
|
|
1357
|
+
|
|
1217
1358
|
/**
|
|
1218
1359
|
* For the error of a send that just failed: what WeChat answered, when a
|
|
1219
1360
|
* failed answer came in during the last few seconds.
|
|
@@ -1290,5 +1431,18 @@ export interface BrowserHealth {
|
|
|
1290
1431
|
userName : null | string,
|
|
1291
1432
|
}
|
|
1292
1433
|
|
|
1434
|
+
export interface LoginPageState {
|
|
1435
|
+
// the page offers asking the phone to confirm, instead of a QR code
|
|
1436
|
+
canConfirmOnPhone : boolean,
|
|
1437
|
+
// the web app's login code: 0 QR code shown, 201 scanned, 200 logged in, 408 waiting
|
|
1438
|
+
code : null | number | string,
|
|
1439
|
+
hasQrcode : boolean,
|
|
1440
|
+
qrcodeUrl : null | string,
|
|
1441
|
+
waitingForPhone : boolean,
|
|
1442
|
+
}
|
|
1443
|
+
|
|
1444
|
+
// what makes the server render the page as logged in
|
|
1445
|
+
const SESSION_COOKIES = ['wxsid', 'webwx_data_ticket', 'wxpluginkey']
|
|
1446
|
+
|
|
1293
1447
|
export type Cookie = Protocol.Network.Cookie
|
|
1294
1448
|
export default Bridge
|