@dongdev/fca-unofficial 3.0.31 → 4.0.0
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/LICENSE +191 -0
- package/README.md +224 -406
- package/dist/index.d.mts +1241 -0
- package/dist/index.d.ts +1241 -0
- package/dist/index.js +27749 -0
- package/dist/index.mjs +27713 -0
- package/docs/ARCHITECTURE.md +467 -0
- package/docs/DOCS.md +686 -0
- package/fca-config.example.json +33 -0
- package/package.json +32 -22
- package/test/fca.test.cjs +533 -0
- package/CHANGELOG.md +0 -296
- package/DOCS.md +0 -2712
- package/func/checkUpdate.js +0 -222
- package/func/logAdapter.js +0 -33
- package/func/logger.js +0 -48
- package/index.d.ts +0 -751
- package/index.js +0 -8
- package/module/config.js +0 -40
- package/module/login.js +0 -133
- package/module/loginHelper.js +0 -1296
- package/module/options.js +0 -44
- package/src/api/action/addExternalModule.js +0 -25
- package/src/api/action/changeAvatar.js +0 -137
- package/src/api/action/changeBio.js +0 -75
- package/src/api/action/enableAutoSaveAppState.js +0 -73
- package/src/api/action/getCurrentUserID.js +0 -7
- package/src/api/action/handleFriendRequest.js +0 -57
- package/src/api/action/logout.js +0 -76
- package/src/api/action/refreshFb_dtsg.js +0 -48
- package/src/api/action/setPostReaction.js +0 -106
- package/src/api/action/unfriend.js +0 -54
- package/src/api/http/httpGet.js +0 -46
- package/src/api/http/httpPost.js +0 -52
- package/src/api/http/postFormData.js +0 -47
- package/src/api/messaging/addUserToGroup.js +0 -68
- package/src/api/messaging/changeAdminStatus.js +0 -126
- package/src/api/messaging/changeArchivedStatus.js +0 -55
- package/src/api/messaging/changeBlockedStatus.js +0 -48
- package/src/api/messaging/changeGroupImage.js +0 -91
- package/src/api/messaging/changeNickname.js +0 -70
- package/src/api/messaging/changeThreadColor.js +0 -79
- package/src/api/messaging/changeThreadEmoji.js +0 -111
- package/src/api/messaging/createNewGroup.js +0 -88
- package/src/api/messaging/createPoll.js +0 -46
- package/src/api/messaging/createThemeAI.js +0 -98
- package/src/api/messaging/deleteMessage.js +0 -136
- package/src/api/messaging/deleteThread.js +0 -56
- package/src/api/messaging/editMessage.js +0 -68
- package/src/api/messaging/forwardAttachment.js +0 -57
- package/src/api/messaging/getEmojiUrl.js +0 -29
- package/src/api/messaging/getFriendsList.js +0 -82
- package/src/api/messaging/getMessage.js +0 -829
- package/src/api/messaging/getThemePictures.js +0 -62
- package/src/api/messaging/handleMessageRequest.js +0 -65
- package/src/api/messaging/markAsDelivered.js +0 -57
- package/src/api/messaging/markAsRead.js +0 -88
- package/src/api/messaging/markAsReadAll.js +0 -49
- package/src/api/messaging/markAsSeen.js +0 -61
- package/src/api/messaging/muteThread.js +0 -50
- package/src/api/messaging/removeUserFromGroup.js +0 -62
- package/src/api/messaging/resolvePhotoUrl.js +0 -43
- package/src/api/messaging/scheduler.js +0 -264
- package/src/api/messaging/searchForThread.js +0 -53
- package/src/api/messaging/sendMessage.js +0 -270
- package/src/api/messaging/sendTypingIndicator.js +0 -74
- package/src/api/messaging/setMessageReaction.js +0 -90
- package/src/api/messaging/setTitle.js +0 -124
- package/src/api/messaging/shareContact.js +0 -49
- package/src/api/messaging/threadColors.js +0 -128
- package/src/api/messaging/unsendMessage.js +0 -81
- package/src/api/messaging/uploadAttachment.js +0 -492
- package/src/api/socket/core/connectMqtt.js +0 -258
- package/src/api/socket/core/emitAuth.js +0 -103
- package/src/api/socket/core/getSeqID.js +0 -320
- package/src/api/socket/core/getTaskResponseData.js +0 -25
- package/src/api/socket/core/parseDelta.js +0 -377
- package/src/api/socket/detail/buildStream.js +0 -215
- package/src/api/socket/detail/constants.js +0 -28
- package/src/api/socket/listenMqtt.js +0 -377
- package/src/api/socket/middleware/index.js +0 -216
- package/src/api/threads/getThreadHistory.js +0 -664
- package/src/api/threads/getThreadInfo.js +0 -296
- package/src/api/threads/getThreadList.js +0 -293
- package/src/api/threads/getThreadPictures.js +0 -78
- package/src/api/users/getUserID.js +0 -65
- package/src/api/users/getUserInfo.js +0 -402
- package/src/api/users/getUserInfoV2.js +0 -134
- package/src/core/sendReqMqtt.js +0 -96
- package/src/database/helpers.js +0 -53
- package/src/database/models/index.js +0 -88
- package/src/database/models/thread.js +0 -50
- package/src/database/models/user.js +0 -46
- package/src/database/threadData.js +0 -94
- package/src/database/userData.js +0 -98
- package/src/remote/remoteClient.js +0 -123
- package/src/utils/broadcast.js +0 -51
- package/src/utils/client.js +0 -10
- package/src/utils/constants.js +0 -23
- package/src/utils/cookies.js +0 -68
- package/src/utils/format/attachment.js +0 -357
- package/src/utils/format/cookie.js +0 -9
- package/src/utils/format/date.js +0 -50
- package/src/utils/format/decode.js +0 -44
- package/src/utils/format/delta.js +0 -194
- package/src/utils/format/ids.js +0 -64
- package/src/utils/format/index.js +0 -64
- package/src/utils/format/message.js +0 -88
- package/src/utils/format/presence.js +0 -132
- package/src/utils/format/readTyp.js +0 -44
- package/src/utils/format/thread.js +0 -42
- package/src/utils/format/utils.js +0 -141
- package/src/utils/headers.js +0 -115
- package/src/utils/loginParser/autoLogin.js +0 -125
- package/src/utils/loginParser/helpers.js +0 -43
- package/src/utils/loginParser/index.js +0 -10
- package/src/utils/loginParser/parseAndCheckLogin.js +0 -220
- package/src/utils/loginParser/textUtils.js +0 -28
- package/src/utils/request/client.js +0 -26
- package/src/utils/request/config.js +0 -23
- package/src/utils/request/defaults.js +0 -46
- package/src/utils/request/helpers.js +0 -46
- package/src/utils/request/index.js +0 -17
- package/src/utils/request/methods.js +0 -163
- package/src/utils/request/proxy.js +0 -21
- package/src/utils/request/retry.js +0 -77
- package/src/utils/request/sanitize.js +0 -49
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
function getType(obj) {
|
|
4
|
-
return Object.prototype.toString.call(obj).slice(8, -1);
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
function formatID(id) {
|
|
8
|
-
if (id != undefined && id != null) return id.replace(/(fb)?id[:.]/, "");
|
|
9
|
-
else return id;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
function padZeros(val, len) {
|
|
13
|
-
val = String(val);
|
|
14
|
-
len = len || 2;
|
|
15
|
-
while (val.length < len) val = "0" + val;
|
|
16
|
-
return val;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function arrayToObject(arr, getKey, getValue) {
|
|
20
|
-
return arr.reduce(function (acc, val) {
|
|
21
|
-
acc[getKey(val)] = getValue(val);
|
|
22
|
-
return acc;
|
|
23
|
-
}, {});
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function arrToForm(form) {
|
|
27
|
-
return arrayToObject(
|
|
28
|
-
form,
|
|
29
|
-
function (v) {
|
|
30
|
-
return v.name;
|
|
31
|
-
},
|
|
32
|
-
function (v) {
|
|
33
|
-
return v.val;
|
|
34
|
-
},
|
|
35
|
-
);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function getData_Path(Obj, Arr, Stt) {
|
|
39
|
-
if (Arr.length === 0 && Obj != undefined) {
|
|
40
|
-
return Obj;
|
|
41
|
-
} else if (Obj == undefined) {
|
|
42
|
-
return Stt;
|
|
43
|
-
}
|
|
44
|
-
const head = Arr[0];
|
|
45
|
-
if (head == undefined) {
|
|
46
|
-
return Stt;
|
|
47
|
-
}
|
|
48
|
-
const tail = Arr.slice(1);
|
|
49
|
-
return getData_Path(Obj[head], tail, Stt++);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function setData_Path(obj, path, value) {
|
|
53
|
-
if (!path.length) {
|
|
54
|
-
return obj;
|
|
55
|
-
}
|
|
56
|
-
const currentKey = path[0];
|
|
57
|
-
let currentObj = obj[currentKey];
|
|
58
|
-
|
|
59
|
-
if (!currentObj) {
|
|
60
|
-
obj[currentKey] = value;
|
|
61
|
-
currentObj = obj[currentKey];
|
|
62
|
-
}
|
|
63
|
-
path.shift();
|
|
64
|
-
if (!path.length) {
|
|
65
|
-
currentObj = value;
|
|
66
|
-
} else {
|
|
67
|
-
currentObj = setData_Path(currentObj, path, value);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
return obj;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function getPaths(obj, parentPath = []) {
|
|
74
|
-
let paths = [];
|
|
75
|
-
for (let prop in obj) {
|
|
76
|
-
if (typeof obj[prop] === "object") {
|
|
77
|
-
paths = paths.concat(getPaths(obj[prop], [...parentPath, prop]));
|
|
78
|
-
} else {
|
|
79
|
-
paths.push([...parentPath, prop]);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
return paths;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function cleanHTML(text) {
|
|
86
|
-
text = text.replace(
|
|
87
|
-
/(<br>)|(<\/?i>)|(<\/?em>)|(<\/?b>)|(!?~)|(&)|(')|(<)|(>)|(")/g,
|
|
88
|
-
(match) => {
|
|
89
|
-
switch (match) {
|
|
90
|
-
case "<br>":
|
|
91
|
-
return "\n";
|
|
92
|
-
case "<i>":
|
|
93
|
-
case "<em>":
|
|
94
|
-
case "</i>":
|
|
95
|
-
case "</em>":
|
|
96
|
-
return "*";
|
|
97
|
-
case "<b>":
|
|
98
|
-
case "</b>":
|
|
99
|
-
return "**";
|
|
100
|
-
case "~!":
|
|
101
|
-
case "!~":
|
|
102
|
-
return "||";
|
|
103
|
-
case "&":
|
|
104
|
-
return "&";
|
|
105
|
-
case "'":
|
|
106
|
-
return "'";
|
|
107
|
-
case "<":
|
|
108
|
-
return "<";
|
|
109
|
-
case ">":
|
|
110
|
-
return ">";
|
|
111
|
-
case """:
|
|
112
|
-
return '"';
|
|
113
|
-
}
|
|
114
|
-
},
|
|
115
|
-
);
|
|
116
|
-
return text;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
function getCurrentTimestamp() {
|
|
120
|
-
const date = new Date();
|
|
121
|
-
const unixTime = date.getTime();
|
|
122
|
-
return unixTime;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
function getSignatureID() {
|
|
126
|
-
return Math.floor(Math.random() * 2147483648).toString(16);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
module.exports = {
|
|
130
|
-
getType,
|
|
131
|
-
formatID,
|
|
132
|
-
padZeros,
|
|
133
|
-
arrayToObject,
|
|
134
|
-
arrToForm,
|
|
135
|
-
getData_Path,
|
|
136
|
-
setData_Path,
|
|
137
|
-
getPaths,
|
|
138
|
-
cleanHTML,
|
|
139
|
-
getCurrentTimestamp,
|
|
140
|
-
getSignatureID,
|
|
141
|
-
};
|
package/src/utils/headers.js
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
// Sanitize header value to remove invalid characters
|
|
4
|
-
function sanitizeHeaderValue(value) {
|
|
5
|
-
if (value === null || value === undefined) return "";
|
|
6
|
-
let str = String(value);
|
|
7
|
-
|
|
8
|
-
// Remove array-like strings (e.g., "["performAutoLogin"]")
|
|
9
|
-
// This handles cases where arrays were accidentally stringified
|
|
10
|
-
if (str.trim().startsWith("[") && str.trim().endsWith("]")) {
|
|
11
|
-
// Try to detect if it's a stringified array and remove it
|
|
12
|
-
try {
|
|
13
|
-
const parsed = JSON.parse(str);
|
|
14
|
-
if (Array.isArray(parsed)) {
|
|
15
|
-
// If it's an array, return empty string (invalid header value)
|
|
16
|
-
return "";
|
|
17
|
-
}
|
|
18
|
-
} catch {
|
|
19
|
-
// Not valid JSON, continue with normal sanitization
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// Remove invalid characters for HTTP headers:
|
|
24
|
-
// - Control characters (0x00-0x1F, except HTAB 0x09)
|
|
25
|
-
// - DEL character (0x7F)
|
|
26
|
-
// - Newlines and carriage returns
|
|
27
|
-
// - Square brackets (often indicate array stringification issues)
|
|
28
|
-
str = str.replace(/[\x00-\x08\x0B-\x0C\x0E-\x1F\x7F\r\n\[\]]/g, "").trim();
|
|
29
|
-
|
|
30
|
-
return str;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// Sanitize header name to ensure it's valid
|
|
34
|
-
function sanitizeHeaderName(name) {
|
|
35
|
-
if (!name || typeof name !== "string") return "";
|
|
36
|
-
// Remove invalid characters for HTTP header names
|
|
37
|
-
return name.replace(/[^\x21-\x7E]/g, "").trim();
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function getHeaders(url, options, ctx, customHeader) {
|
|
41
|
-
const u = new URL(url);
|
|
42
|
-
const ua = options?.userAgent || "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36";
|
|
43
|
-
const referer = options?.referer || "https://www.facebook.com/";
|
|
44
|
-
const origin = referer.replace(/\/+$/, "");
|
|
45
|
-
const contentType = options?.contentType || "application/x-www-form-urlencoded";
|
|
46
|
-
const acceptLang = options?.acceptLanguage || "en-US,en;q=0.9,vi;q=0.8";
|
|
47
|
-
const headers = {
|
|
48
|
-
Host: sanitizeHeaderValue(u.host),
|
|
49
|
-
Origin: sanitizeHeaderValue(origin),
|
|
50
|
-
Referer: sanitizeHeaderValue(referer),
|
|
51
|
-
"User-Agent": sanitizeHeaderValue(ua),
|
|
52
|
-
Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,application/json;q=0.8,*/*;q=0.7",
|
|
53
|
-
"Accept-Language": sanitizeHeaderValue(acceptLang),
|
|
54
|
-
"Accept-Encoding": "gzip, deflate, br",
|
|
55
|
-
"Content-Type": sanitizeHeaderValue(contentType),
|
|
56
|
-
Connection: "keep-alive",
|
|
57
|
-
DNT: "1",
|
|
58
|
-
"Upgrade-Insecure-Requests": "1",
|
|
59
|
-
"sec-ch-ua": "\"Chromium\";v=\"139\", \"Not;A=Brand\";v=\"24\", \"Google Chrome\";v=\"139\"",
|
|
60
|
-
"sec-ch-ua-mobile": "?0",
|
|
61
|
-
"sec-ch-ua-platform": "\"Windows\"",
|
|
62
|
-
"sec-ch-ua-arch": "\"x86\"",
|
|
63
|
-
"sec-ch-ua-bitness": "\"64\"",
|
|
64
|
-
"sec-ch-ua-full-version-list": "\"Chromium\";v=\"139.0.0.0\", \"Not;A=Brand\";v=\"24.0.0.0\", \"Google Chrome\";v=\"139.0.0.0\"",
|
|
65
|
-
"sec-ch-ua-platform-version": "\"15.0.0\"",
|
|
66
|
-
"Sec-Fetch-Site": "same-origin",
|
|
67
|
-
"Sec-Fetch-Mode": "cors",
|
|
68
|
-
"Sec-Fetch-Dest": "empty",
|
|
69
|
-
"X-Requested-With": "XMLHttpRequest",
|
|
70
|
-
Pragma: "no-cache",
|
|
71
|
-
"Cache-Control": "no-cache"
|
|
72
|
-
};
|
|
73
|
-
if (ctx?.region) {
|
|
74
|
-
const regionValue = sanitizeHeaderValue(ctx.region);
|
|
75
|
-
if (regionValue) headers["X-MSGR-Region"] = regionValue;
|
|
76
|
-
}
|
|
77
|
-
if (customHeader && typeof customHeader === "object") {
|
|
78
|
-
// Filter customHeader to only include valid HTTP header values (strings, numbers, booleans)
|
|
79
|
-
// Exclude functions, objects, arrays, and other non-serializable values
|
|
80
|
-
for (const [key, value] of Object.entries(customHeader)) {
|
|
81
|
-
// Skip null, undefined, functions, objects, and arrays
|
|
82
|
-
if (value === null || value === undefined || typeof value === "function") {
|
|
83
|
-
continue;
|
|
84
|
-
}
|
|
85
|
-
if (typeof value === "object") {
|
|
86
|
-
// Arrays are objects in JavaScript, so check for arrays explicitly
|
|
87
|
-
if (Array.isArray(value)) {
|
|
88
|
-
continue;
|
|
89
|
-
}
|
|
90
|
-
// Skip plain objects (but allow null which is already handled above)
|
|
91
|
-
continue;
|
|
92
|
-
}
|
|
93
|
-
// Only allow strings, numbers, and booleans - convert to string and sanitize
|
|
94
|
-
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
95
|
-
const sanitizedKey = sanitizeHeaderName(key);
|
|
96
|
-
const sanitizedValue = sanitizeHeaderValue(value);
|
|
97
|
-
if (sanitizedKey && sanitizedValue !== "") {
|
|
98
|
-
headers[sanitizedKey] = sanitizedValue;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
// Final pass: sanitize all header values to ensure no invalid characters
|
|
104
|
-
const sanitizedHeaders = {};
|
|
105
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
106
|
-
const sanitizedKey = sanitizeHeaderName(key);
|
|
107
|
-
const sanitizedValue = sanitizeHeaderValue(value);
|
|
108
|
-
if (sanitizedKey && sanitizedValue !== "") {
|
|
109
|
-
sanitizedHeaders[sanitizedKey] = sanitizedValue;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
return sanitizedHeaders;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
module.exports = { getHeaders };
|
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const logger = require("../../../func/logger");
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Tạo hàm maybeAutoLogin: khi session hết hạn thì thử đăng nhập lại và retry request.
|
|
7
|
-
* @param {Object} ctx - Context (jar, performAutoLogin, globalOptions, ...)
|
|
8
|
-
* @param {Object} http - HTTP client (get, post, postFormData)
|
|
9
|
-
* @param {Object} helpers - { buildUrl, headerOf, formatCookie }
|
|
10
|
-
* @param {Function} emit - createEmit(ctx)
|
|
11
|
-
* @param {Function} parseAndCheckLogin - Hàm parse chính để retry sau khi auto login thành công
|
|
12
|
-
*/
|
|
13
|
-
function createMaybeAutoLogin(ctx, http, helpers, emit, parseAndCheckLogin) {
|
|
14
|
-
const { buildUrl, headerOf, formatCookie } = helpers;
|
|
15
|
-
|
|
16
|
-
return async function maybeAutoLogin(resData, resConfig) {
|
|
17
|
-
if (ctx.auto_login) {
|
|
18
|
-
const e = new Error("Not logged in. Auto login already in progress.");
|
|
19
|
-
e.error = "Not logged in.";
|
|
20
|
-
e.res = resData;
|
|
21
|
-
throw e;
|
|
22
|
-
}
|
|
23
|
-
if (typeof ctx.performAutoLogin !== "function") {
|
|
24
|
-
const e = new Error("Not logged in. Auto login function not available.");
|
|
25
|
-
e.error = "Not logged in.";
|
|
26
|
-
e.res = resData;
|
|
27
|
-
throw e;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
ctx.auto_login = true;
|
|
31
|
-
logger("Login session expired, attempting auto login...", "warn");
|
|
32
|
-
emit("sessionExpired", { res: resData });
|
|
33
|
-
|
|
34
|
-
try {
|
|
35
|
-
const ok = await ctx.performAutoLogin();
|
|
36
|
-
if (ok) {
|
|
37
|
-
logger("Auto login successful! Retrying request...", "info");
|
|
38
|
-
emit("autoLoginSuccess", { res: resData });
|
|
39
|
-
ctx.auto_login = false;
|
|
40
|
-
|
|
41
|
-
if (resConfig) {
|
|
42
|
-
const url = buildUrl(resConfig);
|
|
43
|
-
const method = String(resConfig?.method || "GET").toUpperCase();
|
|
44
|
-
const ctype = String(headerOf(resConfig?.headers, "content-type") || "").toLowerCase();
|
|
45
|
-
const isMultipart = ctype.includes("multipart/form-data");
|
|
46
|
-
const payload = resConfig?.data;
|
|
47
|
-
const params = resConfig?.params;
|
|
48
|
-
|
|
49
|
-
try {
|
|
50
|
-
let newData;
|
|
51
|
-
if (method === "GET") {
|
|
52
|
-
newData = await http.get(url, ctx.jar, params || null, ctx.globalOptions, ctx);
|
|
53
|
-
} else if (isMultipart) {
|
|
54
|
-
newData = await http.postFormData(url, ctx.jar, payload, params, ctx.globalOptions, ctx);
|
|
55
|
-
} else {
|
|
56
|
-
newData = await http.post(url, ctx.jar, payload, ctx.globalOptions, ctx);
|
|
57
|
-
}
|
|
58
|
-
return await parseAndCheckLogin(ctx, http)(newData);
|
|
59
|
-
} catch (retryErr) {
|
|
60
|
-
if (
|
|
61
|
-
retryErr?.code === "ERR_INVALID_CHAR" ||
|
|
62
|
-
(retryErr?.message && retryErr.message.includes("Invalid character in header"))
|
|
63
|
-
) {
|
|
64
|
-
logger(
|
|
65
|
-
`Auto login retry failed: Invalid header detected. Error: ${retryErr.message}`,
|
|
66
|
-
"error"
|
|
67
|
-
);
|
|
68
|
-
const e = new Error("Not logged in. Auto login retry failed due to invalid header.");
|
|
69
|
-
e.error = "Not logged in.";
|
|
70
|
-
e.res = resData;
|
|
71
|
-
e.originalError = retryErr;
|
|
72
|
-
throw e;
|
|
73
|
-
}
|
|
74
|
-
logger(
|
|
75
|
-
`Auto login retry failed: ${
|
|
76
|
-
retryErr && retryErr.message ? retryErr.message : String(retryErr)
|
|
77
|
-
}`,
|
|
78
|
-
"error"
|
|
79
|
-
);
|
|
80
|
-
const e = new Error("Not logged in. Auto login retry failed.");
|
|
81
|
-
e.error = "Not logged in.";
|
|
82
|
-
e.res = resData;
|
|
83
|
-
e.originalError = retryErr;
|
|
84
|
-
throw e;
|
|
85
|
-
}
|
|
86
|
-
} else {
|
|
87
|
-
const e = new Error(
|
|
88
|
-
"Not logged in. Auto login successful but cannot retry request."
|
|
89
|
-
);
|
|
90
|
-
e.error = "Not logged in.";
|
|
91
|
-
e.res = resData;
|
|
92
|
-
throw e;
|
|
93
|
-
}
|
|
94
|
-
} else {
|
|
95
|
-
ctx.auto_login = false;
|
|
96
|
-
const e = new Error("Not logged in. Auto login failed.");
|
|
97
|
-
e.error = "Not logged in.";
|
|
98
|
-
e.res = resData;
|
|
99
|
-
emit("autoLoginFailed", { error: e, res: resData });
|
|
100
|
-
throw e;
|
|
101
|
-
}
|
|
102
|
-
} catch (autoLoginErr) {
|
|
103
|
-
ctx.auto_login = false;
|
|
104
|
-
if (autoLoginErr.error === "Not logged in.") {
|
|
105
|
-
throw autoLoginErr;
|
|
106
|
-
}
|
|
107
|
-
logger(
|
|
108
|
-
`Auto login error: ${
|
|
109
|
-
autoLoginErr && autoLoginErr.message ? autoLoginErr.message : String(autoLoginErr)
|
|
110
|
-
}`,
|
|
111
|
-
"error"
|
|
112
|
-
);
|
|
113
|
-
const e = new Error("Not logged in. Auto login error.");
|
|
114
|
-
e.error = "Not logged in.";
|
|
115
|
-
e.res = resData;
|
|
116
|
-
e.originalError = autoLoginErr;
|
|
117
|
-
emit("autoLoginFailed", { error: e, res: resData });
|
|
118
|
-
throw e;
|
|
119
|
-
}
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
module.exports = {
|
|
124
|
-
createMaybeAutoLogin
|
|
125
|
-
};
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const delay = ms => new Promise(r => setTimeout(r, ms));
|
|
4
|
-
|
|
5
|
-
function createEmit(ctx) {
|
|
6
|
-
return (event, payload) => {
|
|
7
|
-
try {
|
|
8
|
-
if (ctx && ctx._emitter && typeof ctx._emitter.emit === "function") {
|
|
9
|
-
ctx._emitter.emit(event, payload);
|
|
10
|
-
}
|
|
11
|
-
} catch { }
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function headerOf(headers, name) {
|
|
16
|
-
if (!headers) return;
|
|
17
|
-
const k = Object.keys(headers).find(k => k.toLowerCase() === name.toLowerCase());
|
|
18
|
-
return k ? headers[k] : undefined;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function buildUrl(cfg) {
|
|
22
|
-
try {
|
|
23
|
-
return cfg?.baseURL
|
|
24
|
-
? new URL(cfg.url || "/", cfg.baseURL).toString()
|
|
25
|
-
: cfg?.url || "";
|
|
26
|
-
} catch {
|
|
27
|
-
return cfg?.url || "";
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function formatCookie(arr, service) {
|
|
32
|
-
const n = String(arr?.[0] || "");
|
|
33
|
-
const v = String(arr?.[1] || "");
|
|
34
|
-
return `${n}=${v}; Domain=.${service}.com; Path=/; Secure`;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
module.exports = {
|
|
38
|
-
delay,
|
|
39
|
-
createEmit,
|
|
40
|
-
headerOf,
|
|
41
|
-
buildUrl,
|
|
42
|
-
formatCookie
|
|
43
|
-
};
|
|
@@ -1,220 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const logger = require("../../../func/logger");
|
|
4
|
-
const { makeParsable } = require("./textUtils");
|
|
5
|
-
const { delay, createEmit, headerOf, buildUrl, formatCookie } = require("./helpers");
|
|
6
|
-
const { createMaybeAutoLogin } = require("./autoLogin");
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Trả về hàm async (res) => parsed | throw.
|
|
10
|
-
* Xử lý: retry 5xx, parse JSON, redirect, cookie/DTSG, checkpoint và auto login.
|
|
11
|
-
*/
|
|
12
|
-
function parseAndCheckLogin(ctx, http, retryCount = 0) {
|
|
13
|
-
const emit = createEmit(ctx);
|
|
14
|
-
const helpers = { buildUrl, headerOf, formatCookie };
|
|
15
|
-
const maybeAutoLogin = createMaybeAutoLogin(ctx, http, helpers, emit, parseAndCheckLogin);
|
|
16
|
-
|
|
17
|
-
return async function handleResponse(res) {
|
|
18
|
-
const status = res?.status ?? 0;
|
|
19
|
-
|
|
20
|
-
// Retry khi 5xx
|
|
21
|
-
if (status >= 500 && status < 600) {
|
|
22
|
-
if (retryCount >= 5) {
|
|
23
|
-
const err = new Error(
|
|
24
|
-
"Request retry failed. Check the `res` and `statusCode` property on this error."
|
|
25
|
-
);
|
|
26
|
-
err.statusCode = status;
|
|
27
|
-
err.res = res?.data;
|
|
28
|
-
err.error =
|
|
29
|
-
"Request retry failed. Check the `res` and `statusCode` property on this error.";
|
|
30
|
-
logger(`parseAndCheckLogin: Max retries (5) reached for status ${status}`, "error");
|
|
31
|
-
throw err;
|
|
32
|
-
}
|
|
33
|
-
const baseDelay = retryCount === 0 ? 1500 : 1000 * Math.pow(2, retryCount);
|
|
34
|
-
const jitter = Math.floor(Math.random() * 200);
|
|
35
|
-
const retryTime = Math.min(baseDelay + jitter, 10000);
|
|
36
|
-
const method = String(res?.config?.method || "GET").toUpperCase();
|
|
37
|
-
const url = buildUrl(res?.config);
|
|
38
|
-
logger(
|
|
39
|
-
`parseAndCheckLogin: [${method}] ${url || "(no url)"} -> Retrying request (attempt ${
|
|
40
|
-
retryCount + 1
|
|
41
|
-
}/5) after ${retryTime}ms for status ${status}`,
|
|
42
|
-
"warn"
|
|
43
|
-
);
|
|
44
|
-
await delay(retryTime);
|
|
45
|
-
const ctype = String(
|
|
46
|
-
headerOf(res?.config?.headers, "content-type") || ""
|
|
47
|
-
).toLowerCase();
|
|
48
|
-
const isMultipart = ctype.includes("multipart/form-data");
|
|
49
|
-
const payload = res?.config?.data;
|
|
50
|
-
const params = res?.config?.params;
|
|
51
|
-
const nextRetry = retryCount + 1;
|
|
52
|
-
try {
|
|
53
|
-
if (method === "GET") {
|
|
54
|
-
const newData = await http.get(url, ctx.jar, params || null, ctx.globalOptions, ctx);
|
|
55
|
-
return await parseAndCheckLogin(ctx, http, nextRetry)(newData);
|
|
56
|
-
}
|
|
57
|
-
if (isMultipart) {
|
|
58
|
-
const newData = await http.postFormData(
|
|
59
|
-
url,
|
|
60
|
-
ctx.jar,
|
|
61
|
-
payload,
|
|
62
|
-
params,
|
|
63
|
-
ctx.globalOptions,
|
|
64
|
-
ctx
|
|
65
|
-
);
|
|
66
|
-
return await parseAndCheckLogin(ctx, http, nextRetry)(newData);
|
|
67
|
-
}
|
|
68
|
-
const newData = await http.post(url, ctx.jar, payload, ctx.globalOptions, ctx);
|
|
69
|
-
return await parseAndCheckLogin(ctx, http, nextRetry)(newData);
|
|
70
|
-
} catch (retryErr) {
|
|
71
|
-
if (
|
|
72
|
-
retryErr?.code === "ERR_INVALID_CHAR" ||
|
|
73
|
-
(retryErr?.message && retryErr.message.includes("Invalid character in header"))
|
|
74
|
-
) {
|
|
75
|
-
logger(
|
|
76
|
-
`parseAndCheckLogin: Invalid header detected, aborting retry. Error: ${retryErr.message}`,
|
|
77
|
-
"error"
|
|
78
|
-
);
|
|
79
|
-
const err = new Error(
|
|
80
|
-
"Invalid header content detected. Request aborted to prevent crash."
|
|
81
|
-
);
|
|
82
|
-
err.error = "Invalid header content";
|
|
83
|
-
err.statusCode = status;
|
|
84
|
-
err.res = res?.data;
|
|
85
|
-
err.originalError = retryErr;
|
|
86
|
-
throw err;
|
|
87
|
-
}
|
|
88
|
-
if (nextRetry >= 5) {
|
|
89
|
-
logger(
|
|
90
|
-
`parseAndCheckLogin: Max retries reached, returning error instead of crashing`,
|
|
91
|
-
"error"
|
|
92
|
-
);
|
|
93
|
-
const err = new Error(
|
|
94
|
-
"Request retry failed after 5 attempts. Check the `res` and `statusCode` property on this error."
|
|
95
|
-
);
|
|
96
|
-
err.statusCode = status;
|
|
97
|
-
err.res = res?.data;
|
|
98
|
-
err.error = "Request retry failed after 5 attempts";
|
|
99
|
-
err.originalError = retryErr;
|
|
100
|
-
throw err;
|
|
101
|
-
}
|
|
102
|
-
return await parseAndCheckLogin(ctx, http, nextRetry)(res);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
if (status === 404) return;
|
|
107
|
-
if (status !== 200) {
|
|
108
|
-
const err = new Error(
|
|
109
|
-
"parseAndCheckLogin got status code: " +
|
|
110
|
-
status +
|
|
111
|
-
". Bailing out of trying to parse response."
|
|
112
|
-
);
|
|
113
|
-
err.statusCode = status;
|
|
114
|
-
err.res = res?.data;
|
|
115
|
-
throw err;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const resBodyRaw = res?.data;
|
|
119
|
-
const body = typeof resBodyRaw === "string" ? makeParsable(resBodyRaw) : resBodyRaw;
|
|
120
|
-
let parsed;
|
|
121
|
-
try {
|
|
122
|
-
parsed = typeof body === "object" && body !== null ? body : JSON.parse(body);
|
|
123
|
-
} catch (e) {
|
|
124
|
-
const err = new Error("JSON.parse error. Check the `detail` property on this error.");
|
|
125
|
-
err.error = "JSON.parse error. Check the `detail` property on this error.";
|
|
126
|
-
err.detail = e;
|
|
127
|
-
err.res = resBodyRaw;
|
|
128
|
-
throw err;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
const method = String(res?.config?.method || "GET").toUpperCase();
|
|
132
|
-
if (parsed?.redirect && method === "GET") {
|
|
133
|
-
const redirectRes = await http.get(parsed.redirect, ctx.jar, null, ctx.globalOptions, ctx);
|
|
134
|
-
return await parseAndCheckLogin(ctx, http)(redirectRes);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
// Cookie từ jsmods
|
|
138
|
-
if (
|
|
139
|
-
parsed?.jsmods &&
|
|
140
|
-
parsed.jsmods.require &&
|
|
141
|
-
Array.isArray(parsed.jsmods.require[0]) &&
|
|
142
|
-
parsed.jsmods.require[0][0] === "Cookie"
|
|
143
|
-
) {
|
|
144
|
-
parsed.jsmods.require[0][3][0] = String(parsed.jsmods.require[0][3][0] || "").replace(
|
|
145
|
-
"_js_",
|
|
146
|
-
""
|
|
147
|
-
);
|
|
148
|
-
const requireCookie = parsed.jsmods.require[0][3];
|
|
149
|
-
await ctx.jar.setCookie(
|
|
150
|
-
formatCookie(requireCookie, "facebook"),
|
|
151
|
-
"https://www.facebook.com"
|
|
152
|
-
);
|
|
153
|
-
await ctx.jar.setCookie(
|
|
154
|
-
formatCookie(requireCookie, "messenger"),
|
|
155
|
-
"https://www.messenger.com"
|
|
156
|
-
);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// DTSG token
|
|
160
|
-
if (parsed?.jsmods && Array.isArray(parsed.jsmods.require)) {
|
|
161
|
-
for (const item of parsed.jsmods.require) {
|
|
162
|
-
if (item[0] === "DTSG" && item[1] === "setToken") {
|
|
163
|
-
ctx.fb_dtsg = item[3][0];
|
|
164
|
-
ctx.ttstamp = "2";
|
|
165
|
-
for (let j = 0; j < ctx.fb_dtsg.length; j++) ctx.ttstamp += ctx.fb_dtsg.charCodeAt(j);
|
|
166
|
-
break;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
if (parsed?.error === 1357001) {
|
|
172
|
-
const err = new Error("Facebook blocked the login");
|
|
173
|
-
err.error = "login_blocked";
|
|
174
|
-
err.res = parsed;
|
|
175
|
-
emit("loginBlocked", { res: parsed });
|
|
176
|
-
throw err;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
const resData = parsed;
|
|
180
|
-
const resStr = JSON.stringify(resData);
|
|
181
|
-
|
|
182
|
-
if (
|
|
183
|
-
resStr.includes("XCheckpointFBScrapingWarningController") ||
|
|
184
|
-
resStr.includes("601051028565049")
|
|
185
|
-
) {
|
|
186
|
-
emit("checkpoint", { type: "scraping_warning", res: resData });
|
|
187
|
-
return await maybeAutoLogin(resData, res?.config);
|
|
188
|
-
}
|
|
189
|
-
if (
|
|
190
|
-
resStr.includes("https://www.facebook.com/login.php?") ||
|
|
191
|
-
String(parsed?.redirect || "").includes("login.php?")
|
|
192
|
-
) {
|
|
193
|
-
return await maybeAutoLogin(resData, res?.config);
|
|
194
|
-
}
|
|
195
|
-
if (resStr.includes("1501092823525282")) {
|
|
196
|
-
logger("Bot checkpoint 282 detected, please check the account!", "error");
|
|
197
|
-
const err = new Error("Checkpoint 282 detected");
|
|
198
|
-
err.error = "checkpoint_282";
|
|
199
|
-
err.res = resData;
|
|
200
|
-
emit("checkpoint", { type: "282", res: resData });
|
|
201
|
-
emit("checkpoint_282", { res: resData });
|
|
202
|
-
throw err;
|
|
203
|
-
}
|
|
204
|
-
if (resStr.includes("828281030927956")) {
|
|
205
|
-
logger("Bot checkpoint 956 detected, please check the account!", "error");
|
|
206
|
-
const err = new Error("Checkpoint 956 detected");
|
|
207
|
-
err.error = "checkpoint_956";
|
|
208
|
-
err.res = resData;
|
|
209
|
-
emit("checkpoint", { type: "956", res: resData });
|
|
210
|
-
emit("checkpoint_956", { res: resData });
|
|
211
|
-
throw err;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
return parsed;
|
|
215
|
-
};
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
module.exports = {
|
|
219
|
-
parseAndCheckLogin
|
|
220
|
-
};
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Loại bỏ XSSI prefix và ký tự thừa từ chuỗi response.
|
|
5
|
-
*/
|
|
6
|
-
function cleanXssi(t) {
|
|
7
|
-
if (t == null) return "";
|
|
8
|
-
let s = String(t);
|
|
9
|
-
s = s.replace(/^[\uFEFF\xEF\xBB\xBF]+/, "");
|
|
10
|
-
s = s.replace(/^\)\]\}',?\s*/, "");
|
|
11
|
-
s = s.replace(/^\s*for\s*\(;;\);\s*/i, "");
|
|
12
|
-
return s;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Chuẩn hóa HTML/JSON response thành chuỗi có thể parse (nối nhiều object thành array).
|
|
17
|
-
*/
|
|
18
|
-
function makeParsable(html) {
|
|
19
|
-
const raw = cleanXssi(String(html || ""));
|
|
20
|
-
const split = raw.split(/\}\r?\n\s*\{/);
|
|
21
|
-
if (split.length === 1) return raw;
|
|
22
|
-
return "[" + split.join("},{") + "]";
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
module.exports = {
|
|
26
|
-
cleanXssi,
|
|
27
|
-
makeParsable
|
|
28
|
-
};
|