@dongdev/fca-unofficial 3.0.30 → 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 +33 -22
- package/test/fca.test.cjs +533 -0
- package/CHANGELOG.md +0 -293
- 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 -52
- package/src/api/messaging/sendMessage.js +0 -270
- package/src/api/messaging/sendTypingIndicator.js +0 -74
- package/src/api/messaging/setMessageReaction.js +0 -91
- 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 -295
- 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 -399
- package/src/api/users/getUserInfoV2.js +0 -134
- package/src/core/sendReqMqtt.js +0 -96
- package/src/database/models/index.js +0 -87
- package/src/database/models/thread.js +0 -50
- package/src/database/models/user.js +0 -46
- package/src/database/threadData.js +0 -98
- package/src/database/userData.js +0 -89
- 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.js +0 -1174
- package/src/utils/headers.js +0 -115
- package/src/utils/loginParser.js +0 -365
- package/src/utils/messageFormat.js +0 -1173
- package/src/utils/request.js +0 -332
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 };
|
package/src/utils/loginParser.js
DELETED
|
@@ -1,365 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const logger = require("../../func/logger");
|
|
4
|
-
|
|
5
|
-
function cleanXssi(t) {
|
|
6
|
-
if (t == null) return "";
|
|
7
|
-
let s = String(t);
|
|
8
|
-
s = s.replace(/^[\uFEFF\xEF\xBB\xBF]+/, "");
|
|
9
|
-
s = s.replace(/^\)\]\}',?\s*/, "");
|
|
10
|
-
s = s.replace(/^\s*for\s*\(;;\);\s*/i, "");
|
|
11
|
-
return s;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function makeParsable(html) {
|
|
15
|
-
const raw = cleanXssi(String(html || ""));
|
|
16
|
-
const split = raw.split(/\}\r?\n\s*\{/);
|
|
17
|
-
if (split.length === 1) return raw;
|
|
18
|
-
return "[" + split.join("},{") + "]";
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function parseAndCheckLogin(ctx, http, retryCount = 0) {
|
|
22
|
-
const delay = ms => new Promise(r => setTimeout(r, ms));
|
|
23
|
-
const emit = (event, payload) => {
|
|
24
|
-
try {
|
|
25
|
-
if (ctx && ctx._emitter && typeof ctx._emitter.emit === "function") {
|
|
26
|
-
ctx._emitter.emit(event, payload);
|
|
27
|
-
}
|
|
28
|
-
} catch { }
|
|
29
|
-
};
|
|
30
|
-
const headerOf = (headers, name) => {
|
|
31
|
-
if (!headers) return;
|
|
32
|
-
const k = Object.keys(headers).find(k => k.toLowerCase() === name.toLowerCase());
|
|
33
|
-
return k ? headers[k] : undefined;
|
|
34
|
-
};
|
|
35
|
-
const buildUrl = cfg => {
|
|
36
|
-
try {
|
|
37
|
-
return cfg?.baseURL
|
|
38
|
-
? new URL(cfg.url || "/", cfg.baseURL).toString()
|
|
39
|
-
: cfg?.url || "";
|
|
40
|
-
} catch {
|
|
41
|
-
return cfg?.url || "";
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
const formatCookie = (arr, service) => {
|
|
46
|
-
const n = String(arr?.[0] || "");
|
|
47
|
-
const v = String(arr?.[1] || "");
|
|
48
|
-
return `${n}=${v}; Domain=.${service}.com; Path=/; Secure`;
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
const maybeAutoLogin = async (resData, resConfig) => {
|
|
52
|
-
// Prevent infinite loop if auto login is already in progress
|
|
53
|
-
if (ctx.auto_login) {
|
|
54
|
-
const e = new Error("Not logged in. Auto login already in progress.");
|
|
55
|
-
e.error = "Not logged in.";
|
|
56
|
-
e.res = resData;
|
|
57
|
-
throw e;
|
|
58
|
-
}
|
|
59
|
-
// Check if performAutoLogin function exists
|
|
60
|
-
if (typeof ctx.performAutoLogin !== "function") {
|
|
61
|
-
const e = new Error("Not logged in. Auto login function not available.");
|
|
62
|
-
e.error = "Not logged in.";
|
|
63
|
-
e.res = resData;
|
|
64
|
-
throw e;
|
|
65
|
-
}
|
|
66
|
-
// Set flag to prevent concurrent auto login attempts
|
|
67
|
-
ctx.auto_login = true;
|
|
68
|
-
logger("Login session expired, attempting auto login...", "warn");
|
|
69
|
-
emit("sessionExpired", { res: resData });
|
|
70
|
-
|
|
71
|
-
try {
|
|
72
|
-
const ok = await ctx.performAutoLogin();
|
|
73
|
-
if (ok) {
|
|
74
|
-
logger("Auto login successful! Retrying request...", "info");
|
|
75
|
-
emit("autoLoginSuccess", { res: resData });
|
|
76
|
-
ctx.auto_login = false;
|
|
77
|
-
|
|
78
|
-
// After successful auto login, retry the original request
|
|
79
|
-
if (resConfig) {
|
|
80
|
-
const url = buildUrl(resConfig);
|
|
81
|
-
const method = String(resConfig?.method || "GET").toUpperCase();
|
|
82
|
-
const ctype = String(headerOf(resConfig?.headers, "content-type") || "").toLowerCase();
|
|
83
|
-
const isMultipart = ctype.includes("multipart/form-data");
|
|
84
|
-
const payload = resConfig?.data;
|
|
85
|
-
const params = resConfig?.params;
|
|
86
|
-
|
|
87
|
-
try {
|
|
88
|
-
let newData;
|
|
89
|
-
if (method === "GET") {
|
|
90
|
-
newData = await http.get(url, ctx.jar, params || null, ctx.globalOptions, ctx);
|
|
91
|
-
} else if (isMultipart) {
|
|
92
|
-
newData = await http.postFormData(url, ctx.jar, payload, params, ctx.globalOptions, ctx);
|
|
93
|
-
} else {
|
|
94
|
-
newData = await http.post(url, ctx.jar, payload, ctx.globalOptions, ctx);
|
|
95
|
-
}
|
|
96
|
-
// Retry parsing with the new response
|
|
97
|
-
return await parseAndCheckLogin(ctx, http, retryCount)(newData);
|
|
98
|
-
} catch (retryErr) {
|
|
99
|
-
// Handle ERR_INVALID_CHAR - don't retry, return error immediately
|
|
100
|
-
if (
|
|
101
|
-
retryErr?.code === "ERR_INVALID_CHAR" ||
|
|
102
|
-
(retryErr?.message && retryErr.message.includes("Invalid character in header"))
|
|
103
|
-
) {
|
|
104
|
-
logger(
|
|
105
|
-
`Auto login retry failed: Invalid header detected. Error: ${retryErr.message}`,
|
|
106
|
-
"error"
|
|
107
|
-
);
|
|
108
|
-
const e = new Error("Not logged in. Auto login retry failed due to invalid header.");
|
|
109
|
-
e.error = "Not logged in.";
|
|
110
|
-
e.res = resData;
|
|
111
|
-
e.originalError = retryErr;
|
|
112
|
-
throw e;
|
|
113
|
-
}
|
|
114
|
-
logger(
|
|
115
|
-
`Auto login retry failed: ${
|
|
116
|
-
retryErr && retryErr.message ? retryErr.message : String(retryErr)
|
|
117
|
-
}`,
|
|
118
|
-
"error"
|
|
119
|
-
);
|
|
120
|
-
const e = new Error("Not logged in. Auto login retry failed.");
|
|
121
|
-
e.error = "Not logged in.";
|
|
122
|
-
e.res = resData;
|
|
123
|
-
e.originalError = retryErr;
|
|
124
|
-
throw e;
|
|
125
|
-
}
|
|
126
|
-
} else {
|
|
127
|
-
// No config available, can't retry
|
|
128
|
-
const e = new Error(
|
|
129
|
-
"Not logged in. Auto login successful but cannot retry request."
|
|
130
|
-
);
|
|
131
|
-
e.error = "Not logged in.";
|
|
132
|
-
e.res = resData;
|
|
133
|
-
throw e;
|
|
134
|
-
}
|
|
135
|
-
} else {
|
|
136
|
-
ctx.auto_login = false;
|
|
137
|
-
const e = new Error("Not logged in. Auto login failed.");
|
|
138
|
-
e.error = "Not logged in.";
|
|
139
|
-
e.res = resData;
|
|
140
|
-
emit("autoLoginFailed", { error: e, res: resData });
|
|
141
|
-
throw e;
|
|
142
|
-
}
|
|
143
|
-
} catch (autoLoginErr) {
|
|
144
|
-
ctx.auto_login = false;
|
|
145
|
-
// If error already has the right format, rethrow it
|
|
146
|
-
if (autoLoginErr.error === "Not logged in.") {
|
|
147
|
-
throw autoLoginErr;
|
|
148
|
-
}
|
|
149
|
-
// Otherwise, wrap it
|
|
150
|
-
logger(
|
|
151
|
-
`Auto login error: ${
|
|
152
|
-
autoLoginErr && autoLoginErr.message ? autoLoginErr.message : String(autoLoginErr)
|
|
153
|
-
}`,
|
|
154
|
-
"error"
|
|
155
|
-
);
|
|
156
|
-
const e = new Error("Not logged in. Auto login error.");
|
|
157
|
-
e.error = "Not logged in.";
|
|
158
|
-
e.res = resData;
|
|
159
|
-
e.originalError = autoLoginErr;
|
|
160
|
-
emit("autoLoginFailed", { error: e, res: resData });
|
|
161
|
-
throw e;
|
|
162
|
-
}
|
|
163
|
-
};
|
|
164
|
-
|
|
165
|
-
return async res => {
|
|
166
|
-
const status = res?.status ?? 0;
|
|
167
|
-
if (status >= 500 && status < 600) {
|
|
168
|
-
if (retryCount >= 5) {
|
|
169
|
-
const err = new Error(
|
|
170
|
-
"Request retry failed. Check the `res` and `statusCode` property on this error."
|
|
171
|
-
);
|
|
172
|
-
err.statusCode = status;
|
|
173
|
-
err.res = res?.data;
|
|
174
|
-
err.error =
|
|
175
|
-
"Request retry failed. Check the `res` and `statusCode` property on this error.";
|
|
176
|
-
logger(`parseAndCheckLogin: Max retries (5) reached for status ${status}`, "error");
|
|
177
|
-
throw err;
|
|
178
|
-
}
|
|
179
|
-
const baseDelay = retryCount === 0 ? 1500 : 1000 * Math.pow(2, retryCount);
|
|
180
|
-
const jitter = Math.floor(Math.random() * 200); // 0-199ms jitter
|
|
181
|
-
const retryTime = Math.min(
|
|
182
|
-
baseDelay + jitter,
|
|
183
|
-
10000 // Max 10 seconds
|
|
184
|
-
);
|
|
185
|
-
const method = String(res?.config?.method || "GET").toUpperCase();
|
|
186
|
-
const url = buildUrl(res?.config);
|
|
187
|
-
logger(
|
|
188
|
-
`parseAndCheckLogin: [${method}] ${url || "(no url)"} -> Retrying request (attempt ${
|
|
189
|
-
retryCount + 1
|
|
190
|
-
}/5) after ${retryTime}ms for status ${status}`,
|
|
191
|
-
"warn"
|
|
192
|
-
);
|
|
193
|
-
await delay(retryTime);
|
|
194
|
-
const ctype = String(
|
|
195
|
-
headerOf(res?.config?.headers, "content-type") || ""
|
|
196
|
-
).toLowerCase();
|
|
197
|
-
const isMultipart = ctype.includes("multipart/form-data");
|
|
198
|
-
const payload = res?.config?.data;
|
|
199
|
-
const params = res?.config?.params;
|
|
200
|
-
retryCount += 1;
|
|
201
|
-
try {
|
|
202
|
-
if (method === "GET") {
|
|
203
|
-
const newData = await http.get(url, ctx.jar, params || null, ctx.globalOptions, ctx);
|
|
204
|
-
return await parseAndCheckLogin(ctx, http, retryCount)(newData);
|
|
205
|
-
}
|
|
206
|
-
if (isMultipart) {
|
|
207
|
-
const newData = await http.postFormData(
|
|
208
|
-
url,
|
|
209
|
-
ctx.jar,
|
|
210
|
-
payload,
|
|
211
|
-
params,
|
|
212
|
-
ctx.globalOptions,
|
|
213
|
-
ctx
|
|
214
|
-
);
|
|
215
|
-
return await parseAndCheckLogin(ctx, http, retryCount)(newData);
|
|
216
|
-
} else {
|
|
217
|
-
const newData = await http.post(url, ctx.jar, payload, ctx.globalOptions, ctx);
|
|
218
|
-
return await parseAndCheckLogin(ctx, http, retryCount)(newData);
|
|
219
|
-
}
|
|
220
|
-
} catch (retryErr) {
|
|
221
|
-
// Handle ERR_INVALID_CHAR - don't retry, return error immediately
|
|
222
|
-
if (
|
|
223
|
-
retryErr?.code === "ERR_INVALID_CHAR" ||
|
|
224
|
-
(retryErr?.message && retryErr.message.includes("Invalid character in header"))
|
|
225
|
-
) {
|
|
226
|
-
logger(
|
|
227
|
-
`parseAndCheckLogin: Invalid header detected, aborting retry. Error: ${retryErr.message}`,
|
|
228
|
-
"error"
|
|
229
|
-
);
|
|
230
|
-
const err = new Error(
|
|
231
|
-
"Invalid header content detected. Request aborted to prevent crash."
|
|
232
|
-
);
|
|
233
|
-
err.error = "Invalid header content";
|
|
234
|
-
err.statusCode = status;
|
|
235
|
-
err.res = res?.data;
|
|
236
|
-
err.originalError = retryErr;
|
|
237
|
-
throw err;
|
|
238
|
-
}
|
|
239
|
-
// If max retries reached, return error instead of throwing to prevent crash
|
|
240
|
-
if (retryCount >= 5) {
|
|
241
|
-
logger(
|
|
242
|
-
`parseAndCheckLogin: Max retries reached, returning error instead of crashing`,
|
|
243
|
-
"error"
|
|
244
|
-
);
|
|
245
|
-
const err = new Error(
|
|
246
|
-
"Request retry failed after 5 attempts. Check the `res` and `statusCode` property on this error."
|
|
247
|
-
);
|
|
248
|
-
err.statusCode = status;
|
|
249
|
-
err.res = res?.data;
|
|
250
|
-
err.error = "Request retry failed after 5 attempts";
|
|
251
|
-
err.originalError = retryErr;
|
|
252
|
-
throw err;
|
|
253
|
-
}
|
|
254
|
-
// Continue retry loop
|
|
255
|
-
return await parseAndCheckLogin(ctx, http, retryCount)(res);
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
if (status === 404) return;
|
|
259
|
-
if (status !== 200) {
|
|
260
|
-
const err = new Error(
|
|
261
|
-
"parseAndCheckLogin got status code: " +
|
|
262
|
-
status +
|
|
263
|
-
". Bailing out of trying to parse response."
|
|
264
|
-
);
|
|
265
|
-
err.statusCode = status;
|
|
266
|
-
err.res = res?.data;
|
|
267
|
-
throw err;
|
|
268
|
-
}
|
|
269
|
-
const resBodyRaw = res?.data;
|
|
270
|
-
const body = typeof resBodyRaw === "string" ? makeParsable(resBodyRaw) : resBodyRaw;
|
|
271
|
-
let parsed;
|
|
272
|
-
try {
|
|
273
|
-
parsed = typeof body === "object" && body !== null ? body : JSON.parse(body);
|
|
274
|
-
} catch (e) {
|
|
275
|
-
const err = new Error("JSON.parse error. Check the `detail` property on this error.");
|
|
276
|
-
err.error = "JSON.parse error. Check the `detail` property on this error.";
|
|
277
|
-
err.detail = e;
|
|
278
|
-
err.res = resBodyRaw;
|
|
279
|
-
throw err;
|
|
280
|
-
}
|
|
281
|
-
const method = String(res?.config?.method || "GET").toUpperCase();
|
|
282
|
-
if (parsed?.redirect && method === "GET") {
|
|
283
|
-
const redirectRes = await http.get(parsed.redirect, ctx.jar, null, ctx.globalOptions, ctx);
|
|
284
|
-
return await parseAndCheckLogin(ctx, http)(redirectRes);
|
|
285
|
-
}
|
|
286
|
-
if (
|
|
287
|
-
parsed?.jsmods &&
|
|
288
|
-
parsed.jsmods.require &&
|
|
289
|
-
Array.isArray(parsed.jsmods.require[0]) &&
|
|
290
|
-
parsed.jsmods.require[0][0] === "Cookie"
|
|
291
|
-
) {
|
|
292
|
-
parsed.jsmods.require[0][3][0] = String(parsed.jsmods.require[0][3][0] || "").replace(
|
|
293
|
-
"_js_",
|
|
294
|
-
""
|
|
295
|
-
);
|
|
296
|
-
const requireCookie = parsed.jsmods.require[0][3];
|
|
297
|
-
await ctx.jar.setCookie(
|
|
298
|
-
formatCookie(requireCookie, "facebook"),
|
|
299
|
-
"https://www.facebook.com"
|
|
300
|
-
);
|
|
301
|
-
await ctx.jar.setCookie(
|
|
302
|
-
formatCookie(requireCookie, "messenger"),
|
|
303
|
-
"https://www.messenger.com"
|
|
304
|
-
);
|
|
305
|
-
}
|
|
306
|
-
if (parsed?.jsmods && Array.isArray(parsed.jsmods.require)) {
|
|
307
|
-
for (const item of parsed.jsmods.require) {
|
|
308
|
-
if (item[0] === "DTSG" && item[1] === "setToken") {
|
|
309
|
-
ctx.fb_dtsg = item[3][0];
|
|
310
|
-
ctx.ttstamp = "2";
|
|
311
|
-
for (let j = 0; j < ctx.fb_dtsg.length; j++) ctx.ttstamp += ctx.fb_dtsg.charCodeAt(j);
|
|
312
|
-
break;
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
if (parsed?.error === 1357001) {
|
|
317
|
-
const err = new Error("Facebook blocked the login");
|
|
318
|
-
err.error = "login_blocked";
|
|
319
|
-
err.res = parsed;
|
|
320
|
-
emit("loginBlocked", { res: parsed });
|
|
321
|
-
throw err;
|
|
322
|
-
}
|
|
323
|
-
const resData = parsed;
|
|
324
|
-
const resStr = JSON.stringify(resData);
|
|
325
|
-
if (
|
|
326
|
-
resStr.includes("XCheckpointFBScrapingWarningController") ||
|
|
327
|
-
resStr.includes("601051028565049")
|
|
328
|
-
) {
|
|
329
|
-
emit("checkpoint", { type: "scraping_warning", res: resData });
|
|
330
|
-
return await maybeAutoLogin(resData, res?.config);
|
|
331
|
-
}
|
|
332
|
-
if (
|
|
333
|
-
resStr.includes("https://www.facebook.com/login.php?") ||
|
|
334
|
-
String(parsed?.redirect || "").includes("login.php?")
|
|
335
|
-
) {
|
|
336
|
-
return await maybeAutoLogin(resData, res?.config);
|
|
337
|
-
}
|
|
338
|
-
if (resStr.includes("1501092823525282")) {
|
|
339
|
-
logger("Bot checkpoint 282 detected, please check the account!", "error");
|
|
340
|
-
const err = new Error("Checkpoint 282 detected");
|
|
341
|
-
err.error = "checkpoint_282";
|
|
342
|
-
err.res = resData;
|
|
343
|
-
emit("checkpoint", { type: "282", res: resData });
|
|
344
|
-
emit("checkpoint_282", { res: resData });
|
|
345
|
-
throw err;
|
|
346
|
-
}
|
|
347
|
-
if (resStr.includes("828281030927956")) {
|
|
348
|
-
logger("Bot checkpoint 956 detected, please check the account!", "error");
|
|
349
|
-
const err = new Error("Checkpoint 956 detected");
|
|
350
|
-
err.error = "checkpoint_956";
|
|
351
|
-
err.res = resData;
|
|
352
|
-
emit("checkpoint", { type: "956", res: resData });
|
|
353
|
-
emit("checkpoint_956", { res: resData });
|
|
354
|
-
throw err;
|
|
355
|
-
}
|
|
356
|
-
return parsed;
|
|
357
|
-
};
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
module.exports = {
|
|
361
|
-
parseAndCheckLogin,
|
|
362
|
-
cleanXssi,
|
|
363
|
-
makeParsable
|
|
364
|
-
};
|
|
365
|
-
|