sagor-fca 0.0.14 → 0.0.16
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/index.js +597 -0
- package/package.json +41 -77
- package/src/addExternalModule.js +15 -0
- package/src/{controllers/addUserToGroup.js → addUserToGroup.js} +15 -51
- package/src/changeAdminStatus.js +47 -0
- package/src/changeArchivedStatus.js +41 -0
- package/src/{controllers/changeBio.js → changeBio.js} +6 -19
- package/src/{controllers/changeBlockedStatus.js → changeBlockedStatus.js} +3 -14
- package/src/{controllers/changeGroupImage.js → changeGroupImage.js} +16 -40
- package/src/changeNickname.js +43 -0
- package/src/{controllers/changeThreadColor.js → changeThreadColor.js} +10 -20
- package/src/changeThreadEmoji.js +41 -0
- package/src/{controllers/createNewGroup.js → createNewGroup.js} +12 -28
- package/src/{controllers/createPoll.js → createPoll.js} +13 -25
- package/src/deleteMessage.js +44 -0
- package/src/deleteThread.js +42 -0
- package/src/forwardAttachment.js +47 -0
- package/src/getCurrentUserID.js +7 -0
- package/src/{controllers/getEmojiUrl.js → getEmojiUrl.js} +4 -14
- package/src/{controllers/getFriendsList.js → getFriendsList.js} +10 -21
- package/src/{controllers/getThreadHistory.js → getThreadHistory.js} +72 -237
- package/src/{controllers/getThreadHistoryDeprecated.js → getThreadHistoryDeprecated.js} +20 -42
- package/src/getThreadInfo.js +285 -0
- package/src/getThreadInfoDeprecated.js +56 -0
- package/src/{controllers/getThreadList.js → getThreadList.js} +50 -111
- package/src/getThreadListDeprecated.js +46 -0
- package/src/getThreadPictures.js +59 -0
- package/src/{controllers/getUserID.js → getUserID.js} +9 -14
- package/src/{controllers/getUserInfo.js → getUserInfo.js} +12 -38
- package/src/{controllers/handleFriendRequest.js → handleFriendRequest.js} +11 -26
- package/src/handleMessageRequest.js +47 -0
- package/src/httpGet.js +47 -0
- package/src/httpPost.js +47 -0
- package/src/listenMqtt.js +723 -0
- package/src/logout.js +68 -0
- package/src/{controllers/markAsDelivered.js → markAsDelivered.js} +11 -22
- package/src/{controllers/markAsRead.js → markAsRead.js} +18 -40
- package/src/{controllers/markAsReadAll.js → markAsReadAll.js} +10 -20
- package/src/{controllers/markAsSeen.js → markAsSeen.js} +7 -18
- package/src/{controllers/muteThread.js → muteThread.js} +11 -18
- package/src/removeUserFromGroup.js +45 -0
- package/src/{controllers/resolvePhotoUrl.js → resolvePhotoUrl.js} +8 -17
- package/src/{controllers/searchForThread.js → searchForThread.js} +10 -21
- package/src/sendMessage.js +538 -0
- package/src/sendTypingIndicator.js +70 -0
- package/src/setMessageReaction.js +103 -0
- package/src/{controllers/setPostReaction.js → setPostReaction.js} +13 -26
- package/src/{controllers/setTitle.js → setTitle.js} +13 -29
- package/src/threadColors.js +41 -0
- package/src/{controllers/unfriend.js → unfriend.js} +9 -19
- package/src/unsendMessage.js +39 -0
- package/utils.js +1194 -0
- package/README.md +0 -282
- package/jest.config.js +0 -10
- package/pnpm-workspace.yaml +0 -3
- package/scripts/discover-e2ee-jid.js +0 -266
- package/scripts/e2ee-send-debug.js +0 -218
- package/scripts/e2ee-send-smoke.js +0 -148
- package/scripts/listen-echo-all.js +0 -311
- package/src/controllers/addExternalModule.js +0 -34
- package/src/controllers/changeAdminStatus.js +0 -79
- package/src/controllers/changeArchivedStatus.js +0 -55
- package/src/controllers/changeNickname.js +0 -59
- package/src/controllers/changeThreadEmoji.js +0 -55
- package/src/controllers/connectE2EE.js +0 -47
- package/src/controllers/deleteMessage.js +0 -56
- package/src/controllers/deleteThread.js +0 -56
- package/src/controllers/disconnectE2EE.js +0 -47
- package/src/controllers/downloadE2EEMedia.js +0 -52
- package/src/controllers/editMessage.js +0 -81
- package/src/controllers/forwardAttachment.js +0 -60
- package/src/controllers/getAvatarUser.js +0 -77
- package/src/controllers/getCurrentUserID.js +0 -11
- package/src/controllers/getE2EEDeviceData.js +0 -53
- package/src/controllers/getMessage.js +0 -833
- package/src/controllers/getRegion.js +0 -11
- package/src/controllers/getThreadInfo.js +0 -265
- package/src/controllers/getThreadInfoDeprecated.js +0 -80
- package/src/controllers/getThreadListDeprecated.js +0 -91
- package/src/controllers/getThreadPictures.js +0 -79
- package/src/controllers/handleMessageRequest.js +0 -65
- package/src/controllers/httpGet.js +0 -70
- package/src/controllers/httpPost.js +0 -70
- package/src/controllers/index.js +0 -75
- package/src/controllers/listenMqtt.js +0 -870
- package/src/controllers/logout.js +0 -144
- package/src/controllers/pinMessage.js +0 -86
- package/src/controllers/refreshFb_dtsg.js +0 -89
- package/src/controllers/removeUserFromGroup.js +0 -79
- package/src/controllers/searchStickers.js +0 -53
- package/src/controllers/sendMediaE2EE.js +0 -105
- package/src/controllers/sendMessage.js +0 -566
- package/src/controllers/sendMessageE2EE.js +0 -77
- package/src/controllers/sendMessageMqtt.js +0 -235
- package/src/controllers/sendReactionE2EE.js +0 -65
- package/src/controllers/sendTypingE2EE.js +0 -71
- package/src/controllers/sendTypingIndicator.js +0 -59
- package/src/controllers/setMessageReaction.js +0 -161
- package/src/controllers/setMessageReactionMqtt.js +0 -72
- package/src/controllers/stopListenMqtt.js +0 -79
- package/src/controllers/threadColors.js +0 -57
- package/src/controllers/unsendMessage.js +0 -65
- package/src/controllers/unsendMessageE2EE.js +0 -60
- package/src/e2ee/bridge.js +0 -488
- package/src/e2ee/thread.js +0 -12
- package/src/index.js +0 -768
- package/src/service/uploadAttachment.js +0 -318
- package/src/utils/base-parts/auth.js +0 -286
- package/src/utils/base-parts/formatters.js +0 -751
- package/src/utils/base-parts/identity.js +0 -169
- package/src/utils/base-parts/network.js +0 -159
- package/src/utils/base-parts/parsing.js +0 -53
- package/src/utils/base-parts/type.js +0 -9
- package/src/utils/base.js +0 -18
- package/src/utils/defaults.js +0 -10
- package/src/utils/format-attachments.js +0 -7
- package/src/utils/format-core.js +0 -8
- package/src/utils/format-events.js +0 -11
- package/src/utils/format-messages.js +0 -10
- package/src/utils/format-presence.js +0 -9
- package/src/utils/format-threads.js +0 -8
- package/src/utils/formatters.js +0 -18
- package/src/utils/identity.js +0 -12
- package/src/utils/index.js +0 -15
- package/src/utils/network.js +0 -12
- package/src/utils/parsing.js +0 -11
- package/src/utils.js +0 -3
- package/test/config.env.example +0 -30
- package/test/data/shareAttach.js +0 -146
- package/test/data/something.mov +0 -0
- package/test/data/test.png +0 -0
- package/test/data/test.txt +0 -7
- package/test/example-appstate.txt +0 -11
- package/test/integration/api.integration.test.js +0 -387
- package/test/integration/controllers.appstate.integration.test.js +0 -233
- package/test/integration/e2ee.appstate.integration.test.js +0 -290
- package/test/integration/env-test-config.js +0 -46
- package/test/integration/login.appstate.integration.test.js +0 -47
- package/test/integration/page.integration.test.js +0 -139
- package/test/jest.setup.js +0 -54
- package/test/unit/controllers/controllers.basic.test.js +0 -116
- package/test/unit/controllers/controllers.registry.test.js +0 -36
- package/test/unit/controllers/e2ee.controllers.test.js +0 -299
- package/test/unit/controllers/sendMessage.e2ee-auto.test.js +0 -111
- package/test/unit/controllers/sendMessage.transport-fallback.test.js +0 -177
- package/test/unit/index/index.test.js +0 -110
- package/test/unit/utils/base-parts/auth.test.js +0 -93
- package/test/unit/utils/base-parts/formatters.test.js +0 -53
- package/test/unit/utils/base-parts/identity.test.js +0 -43
- package/test/unit/utils/base-parts/parsing.test.js +0 -38
- package/test/unit/utils/base-parts/type.test.js +0 -23
- package/test/unit/utils/base.test.js +0 -35
package/index.js
ADDED
|
@@ -0,0 +1,597 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var utils = require("./utils");
|
|
4
|
+
var cheerio = require("cheerio");
|
|
5
|
+
var log = require("npmlog");
|
|
6
|
+
|
|
7
|
+
var checkVerified = null;
|
|
8
|
+
|
|
9
|
+
var defaultLogRecordSize = 100;
|
|
10
|
+
log.maxRecordSize = defaultLogRecordSize;
|
|
11
|
+
|
|
12
|
+
function setOptions(globalOptions, options) {
|
|
13
|
+
Object.keys(options).map(function (key) {
|
|
14
|
+
switch (key) {
|
|
15
|
+
case 'pauseLog':
|
|
16
|
+
if (options.pauseLog) log.pause();
|
|
17
|
+
break;
|
|
18
|
+
case 'online':
|
|
19
|
+
globalOptions.online = Boolean(options.online);
|
|
20
|
+
break;
|
|
21
|
+
case 'logLevel':
|
|
22
|
+
log.level = options.logLevel;
|
|
23
|
+
globalOptions.logLevel = options.logLevel;
|
|
24
|
+
break;
|
|
25
|
+
case 'logRecordSize':
|
|
26
|
+
log.maxRecordSize = options.logRecordSize;
|
|
27
|
+
globalOptions.logRecordSize = options.logRecordSize;
|
|
28
|
+
break;
|
|
29
|
+
case 'selfListen':
|
|
30
|
+
globalOptions.selfListen = Boolean(options.selfListen);
|
|
31
|
+
break;
|
|
32
|
+
case 'listenEvents':
|
|
33
|
+
globalOptions.listenEvents = Boolean(options.listenEvents);
|
|
34
|
+
break;
|
|
35
|
+
case 'pageID':
|
|
36
|
+
globalOptions.pageID = options.pageID.toString();
|
|
37
|
+
break;
|
|
38
|
+
case 'updatePresence':
|
|
39
|
+
globalOptions.updatePresence = Boolean(options.updatePresence);
|
|
40
|
+
break;
|
|
41
|
+
case 'forceLogin':
|
|
42
|
+
globalOptions.forceLogin = Boolean(options.forceLogin);
|
|
43
|
+
break;
|
|
44
|
+
case 'userAgent':
|
|
45
|
+
globalOptions.userAgent = options.userAgent;
|
|
46
|
+
break;
|
|
47
|
+
case 'autoMarkDelivery':
|
|
48
|
+
globalOptions.autoMarkDelivery = Boolean(options.autoMarkDelivery);
|
|
49
|
+
break;
|
|
50
|
+
case 'autoMarkRead':
|
|
51
|
+
globalOptions.autoMarkRead = Boolean(options.autoMarkRead);
|
|
52
|
+
break;
|
|
53
|
+
case 'listenTyping':
|
|
54
|
+
globalOptions.listenTyping = Boolean(options.listenTyping);
|
|
55
|
+
break;
|
|
56
|
+
case 'proxy':
|
|
57
|
+
if (typeof options.proxy != "string") {
|
|
58
|
+
delete globalOptions.proxy;
|
|
59
|
+
utils.setProxy();
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
globalOptions.proxy = options.proxy;
|
|
63
|
+
utils.setProxy(globalOptions.proxy);
|
|
64
|
+
}
|
|
65
|
+
break;
|
|
66
|
+
case 'autoReconnect':
|
|
67
|
+
globalOptions.autoReconnect = Boolean(options.autoReconnect);
|
|
68
|
+
break;
|
|
69
|
+
case 'emitReady':
|
|
70
|
+
globalOptions.emitReady = Boolean(options.emitReady);
|
|
71
|
+
break;
|
|
72
|
+
default:
|
|
73
|
+
log.warn("setOptions", "Unrecognized option given to setOptions: " + key);
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function buildAPI(globalOptions, html, jar) {
|
|
80
|
+
var maybeCookie = jar.getCookies("https://www.facebook.com").filter(function (val) {
|
|
81
|
+
return val.cookieString().split("=")[0] === "c_user";
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
var objCookie = jar.getCookies("https://www.facebook.com").reduce(function (obj, val) {
|
|
85
|
+
obj[val.cookieString().split("=")[0]] = val.cookieString().split("=")[1];
|
|
86
|
+
return obj;
|
|
87
|
+
}, {});
|
|
88
|
+
|
|
89
|
+
if (maybeCookie.length === 0) throw { error: "Error retrieving userID. This can be caused by a lot of things, including getting blocked by Facebook for logging in from an unknown location. Try logging in with a browser to verify." };
|
|
90
|
+
|
|
91
|
+
if (html.indexOf("/checkpoint/block/?next") > -1) log.warn("login", "Checkpoint detected. Please log in with a browser to verify.");
|
|
92
|
+
|
|
93
|
+
var userID = maybeCookie[0].cookieString().split("=")[1].toString();
|
|
94
|
+
// var i_userID = objCookie.i_user || null;
|
|
95
|
+
log.info("login", `Logged in as ${userID}`);
|
|
96
|
+
|
|
97
|
+
try {
|
|
98
|
+
clearInterval(checkVerified);
|
|
99
|
+
}
|
|
100
|
+
catch (_) { }
|
|
101
|
+
|
|
102
|
+
var clientID = (Math.random() * 2147483648 | 0).toString(16);
|
|
103
|
+
|
|
104
|
+
let oldFBMQTTMatch = html.match(/irisSeqID:"(.+?)",appID:219994525426954,endpoint:"(.+?)"/);
|
|
105
|
+
let mqttEndpoint = null;
|
|
106
|
+
let region = null;
|
|
107
|
+
let irisSeqID = null;
|
|
108
|
+
var noMqttData = null;
|
|
109
|
+
|
|
110
|
+
if (oldFBMQTTMatch) {
|
|
111
|
+
irisSeqID = oldFBMQTTMatch[1];
|
|
112
|
+
mqttEndpoint = oldFBMQTTMatch[2];
|
|
113
|
+
region = new URL(mqttEndpoint).searchParams.get("region").toUpperCase();
|
|
114
|
+
log.info("login", `Got this account's message region: ${region}`);
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
let newFBMQTTMatch = html.match(/{"app_id":"219994525426954","endpoint":"(.+?)","iris_seq_id":"(.+?)"}/);
|
|
118
|
+
// html.match(/"appID":219994525426954,"endpoint":"(.+?)","pollingEndpoint":"(.+?)","subscribedTopics":\[\]/);
|
|
119
|
+
if (newFBMQTTMatch) {
|
|
120
|
+
irisSeqID = newFBMQTTMatch[2];
|
|
121
|
+
mqttEndpoint = newFBMQTTMatch[1].replace(/\\\//g, "/");
|
|
122
|
+
region = new URL(mqttEndpoint).searchParams.get("region").toUpperCase();
|
|
123
|
+
log.info("login", `Got this account's message region: ${region}`);
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
let legacyFBMQTTMatch = html.match(/(\["MqttWebConfig",\[\],{fbid:")(.+?)(",appID:219994525426954,endpoint:")(.+?)(",pollingEndpoint:")(.+?)(3790])/);
|
|
127
|
+
if (legacyFBMQTTMatch) {
|
|
128
|
+
mqttEndpoint = legacyFBMQTTMatch[4];
|
|
129
|
+
region = new URL(mqttEndpoint).searchParams.get("region").toUpperCase();
|
|
130
|
+
log.warn("login", `Cannot get sequence ID with new RegExp. Fallback to old RegExp (without seqID)...`);
|
|
131
|
+
log.info("login", `Got this account's message region: ${region}`);
|
|
132
|
+
log.info("login", `[Unused] Polling endpoint: ${legacyFBMQTTMatch[6]}`);
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
log.warn("login", "Cannot get MQTT region & sequence ID.");
|
|
136
|
+
noMqttData = html;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// All data available to api functions
|
|
142
|
+
var ctx = {
|
|
143
|
+
userID: userID,
|
|
144
|
+
jar: jar,
|
|
145
|
+
clientID: clientID,
|
|
146
|
+
globalOptions: globalOptions,
|
|
147
|
+
loggedIn: true,
|
|
148
|
+
access_token: 'NONE',
|
|
149
|
+
clientMutationId: 0,
|
|
150
|
+
mqttClient: undefined,
|
|
151
|
+
lastSeqId: irisSeqID,
|
|
152
|
+
syncToken: undefined,
|
|
153
|
+
mqttEndpoint,
|
|
154
|
+
region,
|
|
155
|
+
firstListen: true
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
var api = {
|
|
159
|
+
setOptions: setOptions.bind(null, globalOptions),
|
|
160
|
+
getAppState: function getAppState() {
|
|
161
|
+
return utils.getAppState(jar);
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
if (noMqttData) api["htmlData"] = noMqttData;
|
|
166
|
+
|
|
167
|
+
const apiFuncNames = [
|
|
168
|
+
'addExternalModule',
|
|
169
|
+
'addUserToGroup',
|
|
170
|
+
'changeAdminStatus',
|
|
171
|
+
'changeArchivedStatus',
|
|
172
|
+
'changeBio',
|
|
173
|
+
'changeBlockedStatus',
|
|
174
|
+
'changeGroupImage',
|
|
175
|
+
'changeNickname',
|
|
176
|
+
'changeThreadColor',
|
|
177
|
+
'changeThreadEmoji',
|
|
178
|
+
'createNewGroup',
|
|
179
|
+
'createPoll',
|
|
180
|
+
'deleteMessage',
|
|
181
|
+
'deleteThread',
|
|
182
|
+
'forwardAttachment',
|
|
183
|
+
'getCurrentUserID',
|
|
184
|
+
'getEmojiUrl',
|
|
185
|
+
'getFriendsList',
|
|
186
|
+
'getThreadHistory',
|
|
187
|
+
'getThreadInfo',
|
|
188
|
+
'getThreadList',
|
|
189
|
+
'getThreadPictures',
|
|
190
|
+
'getUserID',
|
|
191
|
+
'getUserInfo',
|
|
192
|
+
'handleMessageRequest',
|
|
193
|
+
'listenMqtt',
|
|
194
|
+
'logout',
|
|
195
|
+
'markAsDelivered',
|
|
196
|
+
'markAsRead',
|
|
197
|
+
'markAsReadAll',
|
|
198
|
+
'markAsSeen',
|
|
199
|
+
'muteThread',
|
|
200
|
+
'removeUserFromGroup',
|
|
201
|
+
'resolvePhotoUrl',
|
|
202
|
+
'searchForThread',
|
|
203
|
+
'sendMessage',
|
|
204
|
+
'sendTypingIndicator',
|
|
205
|
+
'setMessageReaction',
|
|
206
|
+
'setTitle',
|
|
207
|
+
'threadColors',
|
|
208
|
+
'unsendMessage',
|
|
209
|
+
'unfriend',
|
|
210
|
+
|
|
211
|
+
// HTTP
|
|
212
|
+
'httpGet',
|
|
213
|
+
'httpPost',
|
|
214
|
+
|
|
215
|
+
// Deprecated features
|
|
216
|
+
"getThreadListDeprecated",
|
|
217
|
+
'getThreadHistoryDeprecated',
|
|
218
|
+
'getThreadInfoDeprecated',
|
|
219
|
+
];
|
|
220
|
+
|
|
221
|
+
var defaultFuncs = utils.makeDefaults(html, userID, ctx);
|
|
222
|
+
|
|
223
|
+
// Load all api functions in a loop
|
|
224
|
+
apiFuncNames.map(v => api[v] = require('./src/' + v)(defaultFuncs, api, ctx));
|
|
225
|
+
|
|
226
|
+
return [ctx, defaultFuncs, api];
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function makeLogin(jar, email, password, loginOptions, callback, prCallback) {
|
|
230
|
+
return function (res) {
|
|
231
|
+
var html = res.body;
|
|
232
|
+
var $ = cheerio.load(html);
|
|
233
|
+
var arr = [];
|
|
234
|
+
|
|
235
|
+
// This will be empty, but just to be sure we leave it
|
|
236
|
+
$("#login_form input").map((i, v) => arr.push({ val: $(v).val(), name: $(v).attr("name") }));
|
|
237
|
+
|
|
238
|
+
arr = arr.filter(function (v) {
|
|
239
|
+
return v.val && v.val.length;
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
var form = utils.arrToForm(arr);
|
|
243
|
+
form.lsd = utils.getFrom(html, "[\"LSD\",[],{\"token\":\"", "\"}");
|
|
244
|
+
form.lgndim = Buffer.from("{\"w\":1440,\"h\":900,\"aw\":1440,\"ah\":834,\"c\":24}").toString('base64');
|
|
245
|
+
form.email = email;
|
|
246
|
+
form.pass = password;
|
|
247
|
+
form.default_persistent = '0';
|
|
248
|
+
form.lgnrnd = utils.getFrom(html, "name=\"lgnrnd\" value=\"", "\"");
|
|
249
|
+
form.locale = 'en_US';
|
|
250
|
+
form.timezone = '240';
|
|
251
|
+
form.lgnjs = ~~(Date.now() / 1000);
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
// Getting cookies from the HTML page... (kill me now plz)
|
|
255
|
+
// we used to get a bunch of cookies in the headers of the response of the
|
|
256
|
+
// request, but FB changed and they now send those cookies inside the JS.
|
|
257
|
+
// They run the JS which then injects the cookies in the page.
|
|
258
|
+
// The "solution" is to parse through the html and find those cookies
|
|
259
|
+
// which happen to be conveniently indicated with a _js_ in front of their
|
|
260
|
+
// variable name.
|
|
261
|
+
//
|
|
262
|
+
// ---------- Very Hacky Part Starts -----------------
|
|
263
|
+
var willBeCookies = html.split("\"_js_");
|
|
264
|
+
willBeCookies.slice(1).map(function (val) {
|
|
265
|
+
var cookieData = JSON.parse("[\"" + utils.getFrom(val, "", "]") + "]");
|
|
266
|
+
jar.setCookie(utils.formatCookie(cookieData, "facebook"), "https://www.facebook.com");
|
|
267
|
+
});
|
|
268
|
+
// ---------- Very Hacky Part Ends -----------------
|
|
269
|
+
|
|
270
|
+
log.info("login", "Logging in...");
|
|
271
|
+
return utils
|
|
272
|
+
.post("https://www.facebook.com/login/device-based/regular/login/?login_attempt=1&lwv=110", jar, form, loginOptions)
|
|
273
|
+
.then(utils.saveCookies(jar))
|
|
274
|
+
.then(function (res) {
|
|
275
|
+
var headers = res.headers;
|
|
276
|
+
if (!headers.location) throw { error: "Wrong username/password." };
|
|
277
|
+
|
|
278
|
+
// This means the account has login approvals turned on.
|
|
279
|
+
if (headers.location.indexOf('https://www.facebook.com/checkpoint/') > -1) {
|
|
280
|
+
log.info("login", "You have login approvals turned on.");
|
|
281
|
+
var nextURL = 'https://www.facebook.com/checkpoint/?next=https%3A%2F%2Fwww.facebook.com%2Fhome.php';
|
|
282
|
+
|
|
283
|
+
return utils
|
|
284
|
+
.get(headers.location, jar, null, loginOptions)
|
|
285
|
+
.then(utils.saveCookies(jar))
|
|
286
|
+
.then(function (res) {
|
|
287
|
+
var html = res.body;
|
|
288
|
+
// Make the form in advance which will contain the fb_dtsg and nh
|
|
289
|
+
var $ = cheerio.load(html);
|
|
290
|
+
var arr = [];
|
|
291
|
+
$("form input").map((i, v) => arr.push({ val: $(v).val(), name: $(v).attr("name") }));
|
|
292
|
+
|
|
293
|
+
arr = arr.filter(function (v) {
|
|
294
|
+
return v.val && v.val.length;
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
var form = utils.arrToForm(arr);
|
|
298
|
+
if (html.indexOf("checkpoint/?next") > -1) {
|
|
299
|
+
setTimeout(() => {
|
|
300
|
+
checkVerified = setInterval((_form) => { }, 5000, {
|
|
301
|
+
fb_dtsg: form.fb_dtsg,
|
|
302
|
+
jazoest: form.jazoest,
|
|
303
|
+
dpr: 1
|
|
304
|
+
});
|
|
305
|
+
}, 2500);
|
|
306
|
+
throw {
|
|
307
|
+
error: 'login-approval',
|
|
308
|
+
continue: function submit2FA(code) {
|
|
309
|
+
form.approvals_code = code;
|
|
310
|
+
form['submit[Continue]'] = $("#checkpointSubmitButton").html(); //'Continue';
|
|
311
|
+
var prResolve = null;
|
|
312
|
+
var prReject = null;
|
|
313
|
+
var rtPromise = new Promise(function (resolve, reject) {
|
|
314
|
+
prResolve = resolve;
|
|
315
|
+
prReject = reject;
|
|
316
|
+
});
|
|
317
|
+
if (typeof code == "string") {
|
|
318
|
+
utils
|
|
319
|
+
.post(nextURL, jar, form, loginOptions)
|
|
320
|
+
.then(utils.saveCookies(jar))
|
|
321
|
+
.then(function (res) {
|
|
322
|
+
var $ = cheerio.load(res.body);
|
|
323
|
+
var error = $("#approvals_code").parent().attr("data-xui-error");
|
|
324
|
+
if (error) {
|
|
325
|
+
throw {
|
|
326
|
+
error: 'login-approval',
|
|
327
|
+
errordesc: "Invalid 2FA code.",
|
|
328
|
+
lerror: error,
|
|
329
|
+
continue: submit2FA
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
})
|
|
333
|
+
.then(function () {
|
|
334
|
+
// Use the same form (safe I hope)
|
|
335
|
+
delete form.no_fido;
|
|
336
|
+
delete form.approvals_code;
|
|
337
|
+
form.name_action_selected = 'dont_save'; //'save_device';
|
|
338
|
+
|
|
339
|
+
return utils.post(nextURL, jar, form, loginOptions).then(utils.saveCookies(jar));
|
|
340
|
+
})
|
|
341
|
+
.then(function (res) {
|
|
342
|
+
var headers = res.headers;
|
|
343
|
+
if (!headers.location && res.body.indexOf('Review Recent Login') > -1) throw { error: "Something went wrong with login approvals." };
|
|
344
|
+
|
|
345
|
+
var appState = utils.getAppState(jar);
|
|
346
|
+
|
|
347
|
+
if (callback === prCallback) {
|
|
348
|
+
callback = function (err, api) {
|
|
349
|
+
if (err) return prReject(err);
|
|
350
|
+
return prResolve(api);
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// Simply call loginHelper because all it needs is the jar
|
|
355
|
+
// and will then complete the login process
|
|
356
|
+
return loginHelper(appState, email, password, loginOptions, callback);
|
|
357
|
+
})
|
|
358
|
+
.catch(function (err) {
|
|
359
|
+
// Check if using Promise instead of callback
|
|
360
|
+
if (callback === prCallback) prReject(err);
|
|
361
|
+
else callback(err);
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
else {
|
|
365
|
+
utils
|
|
366
|
+
.post("https://www.facebook.com/checkpoint/?next=https%3A%2F%2Fwww.facebook.com%2Fhome.php", jar, form, loginOptions, null, { "Referer": "https://www.facebook.com/checkpoint/?next" })
|
|
367
|
+
.then(utils.saveCookies(jar))
|
|
368
|
+
.then(res => {
|
|
369
|
+
try {
|
|
370
|
+
JSON.parse(res.body.replace(/for\s*\(\s*;\s*;\s*\)\s*;\s*/, ""));
|
|
371
|
+
}
|
|
372
|
+
catch (ex) {
|
|
373
|
+
clearInterval(checkVerified);
|
|
374
|
+
log.info("login", "Verified from browser. Logging in...");
|
|
375
|
+
if (callback === prCallback) {
|
|
376
|
+
callback = function (err, api) {
|
|
377
|
+
if (err) return prReject(err);
|
|
378
|
+
return prResolve(api);
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
return loginHelper(utils.getAppState(jar), email, password, loginOptions, callback);
|
|
382
|
+
}
|
|
383
|
+
})
|
|
384
|
+
.catch(ex => {
|
|
385
|
+
log.error("login", ex);
|
|
386
|
+
if (callback === prCallback) prReject(ex);
|
|
387
|
+
else callback(ex);
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
return rtPromise;
|
|
391
|
+
}
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
else {
|
|
395
|
+
if (!loginOptions.forceLogin) throw { error: "Couldn't login. Facebook might have blocked this account. Please login with a browser or enable the option 'forceLogin' and try again." };
|
|
396
|
+
|
|
397
|
+
if (html.indexOf("Suspicious Login Attempt") > -1) form['submit[This was me]'] = "This was me";
|
|
398
|
+
else form['submit[This Is Okay]'] = "This Is Okay";
|
|
399
|
+
|
|
400
|
+
return utils
|
|
401
|
+
.post(nextURL, jar, form, loginOptions)
|
|
402
|
+
.then(utils.saveCookies(jar))
|
|
403
|
+
.then(function () {
|
|
404
|
+
// Use the same form (safe I hope)
|
|
405
|
+
form.name_action_selected = 'save_device';
|
|
406
|
+
|
|
407
|
+
return utils.post(nextURL, jar, form, loginOptions).then(utils.saveCookies(jar));
|
|
408
|
+
})
|
|
409
|
+
.then(function (res) {
|
|
410
|
+
var headers = res.headers;
|
|
411
|
+
|
|
412
|
+
if (!headers.location && res.body.indexOf('Review Recent Login') > -1) throw { error: "Something went wrong with review recent login." };
|
|
413
|
+
|
|
414
|
+
var appState = utils.getAppState(jar);
|
|
415
|
+
|
|
416
|
+
// Simply call loginHelper because all it needs is the jar
|
|
417
|
+
// and will then complete the login process
|
|
418
|
+
return loginHelper(appState, email, password, loginOptions, callback);
|
|
419
|
+
})
|
|
420
|
+
.catch(e => callback(e));
|
|
421
|
+
}
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
return utils.get('https://www.facebook.com/', jar, null, loginOptions).then(utils.saveCookies(jar));
|
|
426
|
+
});
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
// Helps the login
|
|
431
|
+
function loginHelper(appState, email, password, globalOptions, callback, prCallback) {
|
|
432
|
+
var mainPromise = null;
|
|
433
|
+
var jar = utils.getJar();
|
|
434
|
+
|
|
435
|
+
// If we're given an appState we loop through it and save each cookie
|
|
436
|
+
// back into the jar.
|
|
437
|
+
if (appState) {
|
|
438
|
+
// check and convert cookie to appState
|
|
439
|
+
if (utils.getType(appState) === 'Array' && appState.some(c => c.name)) {
|
|
440
|
+
appState = appState.map(c => {
|
|
441
|
+
c.key = c.name;
|
|
442
|
+
delete c.name;
|
|
443
|
+
return c;
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
else if (utils.getType(appState) === 'String') {
|
|
447
|
+
const arrayAppState = [];
|
|
448
|
+
appState.split(';').forEach(c => {
|
|
449
|
+
const [key, value] = c.split('=');
|
|
450
|
+
|
|
451
|
+
arrayAppState.push({
|
|
452
|
+
key: (key || "").trim(),
|
|
453
|
+
value: (value || "").trim(),
|
|
454
|
+
domain: "facebook.com",
|
|
455
|
+
path: "/",
|
|
456
|
+
expires: new Date().getTime() + 1000 * 60 * 60 * 24 * 365
|
|
457
|
+
});
|
|
458
|
+
});
|
|
459
|
+
appState = arrayAppState;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
appState.map(function (c) {
|
|
463
|
+
const str = c.key + "=" + c.value + "; expires=" + c.expires + "; domain=" + c.domain + "; path=" + c.path + ";";
|
|
464
|
+
jar.setCookie(str, "http://" + c.domain);
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
// Load the main page.
|
|
468
|
+
mainPromise = utils.get('https://mbasic.facebook.com/a/', jar, null, globalOptions, { noRef: true }).then(utils.saveCookies(jar));
|
|
469
|
+
} else {
|
|
470
|
+
// Open the main page, then we login with the given credentials and finally
|
|
471
|
+
// load the main page again (it'll give us some IDs that we need)
|
|
472
|
+
mainPromise = utils
|
|
473
|
+
.get("https://mbasic.facebook.com/a/", null, null, globalOptions, { noRef: true })
|
|
474
|
+
.then(utils.saveCookies(jar))
|
|
475
|
+
.then(makeLogin(jar, email, password, globalOptions, callback, prCallback))
|
|
476
|
+
.then(function () {
|
|
477
|
+
return utils.get('https://mbasic.facebook.com/a/', jar, null, globalOptions).then(utils.saveCookies(jar));
|
|
478
|
+
});
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
let redirectArr = [1, "https://m.facebook.com/"];
|
|
483
|
+
let ctx;
|
|
484
|
+
let api;
|
|
485
|
+
function checkAndFixErr(res) {
|
|
486
|
+
const reg_antierr = /This browser is not supported/gs;
|
|
487
|
+
if (reg_antierr.test(res.body)) {
|
|
488
|
+
const Data = JSON.stringify(res.body);
|
|
489
|
+
const Dt_Check = Data.split('2Fhome.php&gfid=')[1];
|
|
490
|
+
if (Dt_Check == undefined) return res;
|
|
491
|
+
const fid = Dt_Check.split("\\\\")[0];
|
|
492
|
+
if (Dt_Check == undefined || Dt_Check == "") return res;
|
|
493
|
+
const final_fid = fid.split(`\\`)[0];
|
|
494
|
+
if (final_fid == undefined || final_fid == '') return res;
|
|
495
|
+
const redirectlink = redirectArr[1] + "a/preferences.php?basic_site_devices=m_basic&uri=" + encodeURIComponent("https://m.facebook.com/home.php") + "&gfid=" + final_fid;
|
|
496
|
+
return utils.get(redirectlink, jar, null, globalOptions).then(utils.saveCookies(jar));
|
|
497
|
+
}
|
|
498
|
+
else return res;
|
|
499
|
+
}
|
|
500
|
+
function redirect(res) {
|
|
501
|
+
const reg = /<meta http-equiv="refresh" content="0;url=([^"]+)[^>]+>/;
|
|
502
|
+
redirectArr = reg.exec(res.body);
|
|
503
|
+
if (redirectArr && redirectArr[1]) return utils.get(redirectArr[1], jar, null, globalOptions).then(utils.saveCookies(jar));
|
|
504
|
+
return res;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
// At the end we call the callback or catch an exception
|
|
508
|
+
mainPromise = mainPromise
|
|
509
|
+
.then(res => redirect(res))
|
|
510
|
+
.then(res => checkAndFixErr(res))
|
|
511
|
+
//fix via login with defaut UA return WWW.facebook.com not m.facebook.com
|
|
512
|
+
.then(function (res) {
|
|
513
|
+
const Regex_Via = /MPageLoadClientMetrics/gs; //default for normal account, can easily get region, without this u can't get region in some case but u can run normal
|
|
514
|
+
if (!Regex_Via.test(res.body)) {
|
|
515
|
+
//www.facebook.com
|
|
516
|
+
globalOptions.userAgent = "Mozilla/5.0 (Linux; Android 14; SM-A055F Build/UP1A.231005.007; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/130.0.6723.86 Mobile Safari/537.36 [FB_IAB/FB4A;FBAV/461.0.0.47.85;] ";
|
|
517
|
+
return utils.get('https://www.facebook.com/', jar, null, globalOptions, { noRef: true }).then(utils.saveCookies(jar));
|
|
518
|
+
}
|
|
519
|
+
else return res;
|
|
520
|
+
})
|
|
521
|
+
.then(res => redirect(res))
|
|
522
|
+
.then(res => checkAndFixErr(res))
|
|
523
|
+
.then(function (res) {
|
|
524
|
+
const html = res.body;
|
|
525
|
+
const stuff = buildAPI(globalOptions, html, jar);
|
|
526
|
+
ctx = stuff[0];
|
|
527
|
+
api = stuff[2];
|
|
528
|
+
return res;
|
|
529
|
+
});
|
|
530
|
+
|
|
531
|
+
// given a pageID we log in as a page
|
|
532
|
+
if (globalOptions.pageID) {
|
|
533
|
+
mainPromise = mainPromise
|
|
534
|
+
.then(function () {
|
|
535
|
+
return utils.get('https://www.facebook.com/' + ctx.globalOptions.pageID + '/messages/?section=messages&subsection=inbox', ctx.jar, null, globalOptions);
|
|
536
|
+
})
|
|
537
|
+
.then(function (resData) {
|
|
538
|
+
let url = utils.getFrom(resData.body, 'window.location.replace("https:\\/\\/www.facebook.com\\', '");').split('\\').join('');
|
|
539
|
+
url = url.substring(0, url.length - 1);
|
|
540
|
+
return utils.get('https://www.facebook.com' + url, ctx.jar, null, globalOptions);
|
|
541
|
+
});
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
// At the end we call the callback or catch an exception
|
|
545
|
+
mainPromise
|
|
546
|
+
.then(function () {
|
|
547
|
+
log.info("login", 'Done logging in.');
|
|
548
|
+
return callback(null, api);
|
|
549
|
+
})
|
|
550
|
+
.catch(function (e) {
|
|
551
|
+
log.error("login", e.error || e);
|
|
552
|
+
callback(e);
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
function login(loginData, options, callback) {
|
|
556
|
+
if (utils.getType(options) === 'Function' || utils.getType(options) === 'AsyncFunction') {
|
|
557
|
+
callback = options;
|
|
558
|
+
options = {};
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
var globalOptions = {
|
|
562
|
+
pauseLog: false,
|
|
563
|
+
selfListen: false,
|
|
564
|
+
listenEvents: true,
|
|
565
|
+
listenTyping: false,
|
|
566
|
+
updatePresence: false,
|
|
567
|
+
forceLogin: false,
|
|
568
|
+
autoMarkDelivery: false,
|
|
569
|
+
autoMarkRead: false,
|
|
570
|
+
autoReconnect: true,
|
|
571
|
+
logRecordSize: defaultLogRecordSize,
|
|
572
|
+
online: false,
|
|
573
|
+
emitReady: false,
|
|
574
|
+
userAgent: "Mozilla/5.0 (Linux; Android 14; SM-A055F Build/UP1A.231005.007; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/130.0.6723.86 Mobile Safari/537.36 [FB_IAB/FB4A;FBAV/461.0.0.47.85;] "
|
|
575
|
+
};
|
|
576
|
+
|
|
577
|
+
setOptions(globalOptions, options);
|
|
578
|
+
|
|
579
|
+
var prCallback = null;
|
|
580
|
+
if (utils.getType(callback) !== "Function" && utils.getType(callback) !== "AsyncFunction") {
|
|
581
|
+
var rejectFunc = null;
|
|
582
|
+
var resolveFunc = null;
|
|
583
|
+
var returnPromise = new Promise(function (resolve, reject) {
|
|
584
|
+
resolveFunc = resolve;
|
|
585
|
+
rejectFunc = reject;
|
|
586
|
+
});
|
|
587
|
+
prCallback = function (error, api) {
|
|
588
|
+
if (error) return rejectFunc(error);
|
|
589
|
+
return resolveFunc(api);
|
|
590
|
+
};
|
|
591
|
+
callback = prCallback;
|
|
592
|
+
}
|
|
593
|
+
loginHelper(loginData.appState, loginData.email, loginData.password, globalOptions, callback, prCallback);
|
|
594
|
+
return returnPromise;
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
module.exports = login;
|