@dongdev/fca-unofficial 0.0.4 → 0.0.6
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/.travis.yml +6 -6
- package/CHANGELOG.md +1 -1
- package/DOCS.md +1738 -1738
- package/LICENSE-MIT +21 -21
- package/README.md +219 -219
- package/index.js +307 -571
- package/lib/login.js +0 -0
- package/package.json +6 -5
- package/src/addExternalModule.js +19 -15
- package/src/addUserToGroup.js +113 -77
- package/src/changeAdminStatus.js +79 -47
- package/src/changeArchivedStatus.js +55 -41
- package/src/changeAvatar.js +126 -0
- package/src/changeBio.js +66 -54
- package/src/changeBlockedStatus.js +40 -29
- package/src/changeGroupImage.js +127 -101
- package/src/changeNickname.js +50 -36
- package/src/changeThreadColor.js +65 -61
- package/src/changeThreadEmoji.js +55 -41
- package/src/createNewGroup.js +86 -70
- package/src/createPoll.js +71 -59
- package/src/deleteMessage.js +56 -44
- package/src/deleteThread.js +56 -42
- package/src/forwardAttachment.js +60 -47
- package/src/getCurrentUserID.js +7 -7
- package/src/getEmojiUrl.js +29 -27
- package/src/getFriendsList.js +83 -73
- package/src/getMessage.js +796 -0
- package/src/getThreadHistory.js +666 -537
- package/src/getThreadInfo.js +232 -171
- package/src/getThreadList.js +192 -213
- package/src/getThreadPictures.js +79 -59
- package/src/getUserID.js +66 -61
- package/src/getUserInfo.js +74 -66
- package/src/handleFriendRequest.js +61 -46
- package/src/handleMessageRequest.js +65 -47
- package/src/httpGet.js +52 -44
- package/src/httpPost.js +52 -43
- package/src/httpPostFormData.js +63 -0
- package/src/listenMqtt.js +969 -709
- package/src/logout.js +62 -55
- package/src/markAsDelivered.js +58 -47
- package/src/markAsRead.js +80 -70
- package/src/markAsReadAll.js +49 -39
- package/src/markAsSeen.js +59 -48
- package/src/muteThread.js +52 -45
- package/src/postFormData.js +46 -0
- package/src/refreshFb_dtsg.js +81 -0
- package/src/removeUserFromGroup.js +79 -45
- package/src/resolvePhotoUrl.js +45 -36
- package/src/searchForThread.js +53 -42
- package/src/sendMessage.js +328 -328
- package/src/sendMessageMqtt.js +316 -0
- package/src/sendTypingIndicator.js +103 -70
- package/src/setMessageReaction.js +106 -98
- package/src/setPostReaction.js +102 -95
- package/src/setTitle.js +86 -70
- package/src/threadColors.js +131 -41
- package/src/unfriend.js +52 -42
- package/src/unsendMessage.js +49 -39
- package/src/uploadAttachment.js +95 -0
- package/utils.js +1501 -1196
- package/.gitattributes +0 -2
- package/src/Screenshot.js +0 -83
- package/src/changeAvt.js +0 -85
- package/src/getThreadHistoryDeprecated.js +0 -71
- package/src/getThreadInfoDeprecated.js +0 -56
- package/src/getThreadListDeprecated.js +0 -46
- package/src/shareContact.js +0 -46
- 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-config.json +0 -18
- package/test/test-page.js +0 -140
- package/test/test.js +0 -385
package/index.js
CHANGED
@@ -1,572 +1,308 @@
|
|
1
|
-
"use strict";
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
function
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
}
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
error: 'login-approval',
|
309
|
-
continue: function submit2FA(code) {
|
310
|
-
form.approvals_code = code;
|
311
|
-
form['submit[Continue]'] = $("#checkpointSubmitButton").html(); //'Continue';
|
312
|
-
var prResolve = null;
|
313
|
-
var prReject = null;
|
314
|
-
var rtPromise = new Promise(function (resolve, reject) {
|
315
|
-
prResolve = resolve;
|
316
|
-
prReject = reject;
|
317
|
-
});
|
318
|
-
if (typeof code == "string") {
|
319
|
-
utils
|
320
|
-
.post(nextURL, jar, form, loginOptions)
|
321
|
-
.then(utils.saveCookies(jar))
|
322
|
-
.then(function (res) {
|
323
|
-
var $ = cheerio.load(res.body);
|
324
|
-
var error = $("#approvals_code").parent().attr("data-xui-error");
|
325
|
-
if (error) {
|
326
|
-
throw {
|
327
|
-
error: 'login-approval',
|
328
|
-
errordesc: "Invalid 2FA code.",
|
329
|
-
lerror: error,
|
330
|
-
continue: submit2FA
|
331
|
-
};
|
332
|
-
}
|
333
|
-
})
|
334
|
-
.then(function () {
|
335
|
-
// Use the same form (safe I hope)
|
336
|
-
delete form.no_fido;
|
337
|
-
delete form.approvals_code;
|
338
|
-
form.name_action_selected = 'dont_save'; //'save_device';
|
339
|
-
|
340
|
-
return utils.post(nextURL, jar, form, loginOptions).then(utils.saveCookies(jar));
|
341
|
-
})
|
342
|
-
.then(function (res) {
|
343
|
-
var headers = res.headers;
|
344
|
-
if (!headers.location && res.body.indexOf('Review Recent Login') > -1) throw { error: "Something went wrong with login approvals." };
|
345
|
-
|
346
|
-
var appState = utils.getAppState(jar);
|
347
|
-
|
348
|
-
if (callback === prCallback) {
|
349
|
-
callback = function (err, api) {
|
350
|
-
if (err) return prReject(err);
|
351
|
-
return prResolve(api);
|
352
|
-
};
|
353
|
-
}
|
354
|
-
|
355
|
-
// Simply call loginHelper because all it needs is the jar
|
356
|
-
// and will then complete the login process
|
357
|
-
return loginHelper(appState, email, password, loginOptions, callback);
|
358
|
-
})
|
359
|
-
.catch(function (err) {
|
360
|
-
// Check if using Promise instead of callback
|
361
|
-
if (callback === prCallback) prReject(err);
|
362
|
-
else callback(err);
|
363
|
-
});
|
364
|
-
}
|
365
|
-
else {
|
366
|
-
utils
|
367
|
-
.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" })
|
368
|
-
.then(utils.saveCookies(jar))
|
369
|
-
.then(res => {
|
370
|
-
try {
|
371
|
-
JSON.parse(res.body.replace(/for\s*\(\s*;\s*;\s*\)\s*;\s*/, ""));
|
372
|
-
}
|
373
|
-
catch (ex) {
|
374
|
-
clearInterval(checkVerified);
|
375
|
-
log.info("login", "Verified from browser. Logging in...");
|
376
|
-
if (callback === prCallback) {
|
377
|
-
callback = function (err, api) {
|
378
|
-
if (err) return prReject(err);
|
379
|
-
return prResolve(api);
|
380
|
-
};
|
381
|
-
}
|
382
|
-
return loginHelper(utils.getAppState(jar), email, password, loginOptions, callback);
|
383
|
-
}
|
384
|
-
})
|
385
|
-
.catch(ex => {
|
386
|
-
log.error("login", ex);
|
387
|
-
if (callback === prCallback) prReject(ex);
|
388
|
-
else callback(ex);
|
389
|
-
});
|
390
|
-
}
|
391
|
-
return rtPromise;
|
392
|
-
}
|
393
|
-
};
|
394
|
-
}
|
395
|
-
else {
|
396
|
-
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." };
|
397
|
-
|
398
|
-
if (html.indexOf("Suspicious Login Attempt") > -1) form['submit[This was me]'] = "This was me";
|
399
|
-
else form['submit[This Is Okay]'] = "This Is Okay";
|
400
|
-
|
401
|
-
return utils
|
402
|
-
.post(nextURL, jar, form, loginOptions)
|
403
|
-
.then(utils.saveCookies(jar))
|
404
|
-
.then(function () {
|
405
|
-
// Use the same form (safe I hope)
|
406
|
-
form.name_action_selected = 'save_device';
|
407
|
-
|
408
|
-
return utils.post(nextURL, jar, form, loginOptions).then(utils.saveCookies(jar));
|
409
|
-
})
|
410
|
-
.then(function (res) {
|
411
|
-
var headers = res.headers;
|
412
|
-
|
413
|
-
if (!headers.location && res.body.indexOf('Review Recent Login') > -1) throw { error: "Something went wrong with review recent login." };
|
414
|
-
|
415
|
-
var appState = utils.getAppState(jar);
|
416
|
-
|
417
|
-
// Simply call loginHelper because all it needs is the jar
|
418
|
-
// and will then complete the login process
|
419
|
-
return loginHelper(appState, email, password, loginOptions, callback);
|
420
|
-
})
|
421
|
-
.catch(e => callback(e));
|
422
|
-
}
|
423
|
-
});
|
424
|
-
}
|
425
|
-
|
426
|
-
return utils.get('https://www.facebook.com/', jar, null, loginOptions).then(utils.saveCookies(jar));
|
427
|
-
});
|
428
|
-
};
|
429
|
-
}
|
430
|
-
|
431
|
-
// Helps the login
|
432
|
-
function loginHelper(appState, email, password, globalOptions, callback, prCallback) {
|
433
|
-
var mainPromise = null;
|
434
|
-
var jar = utils.getJar();
|
435
|
-
|
436
|
-
// If we're given an appState we loop through it and save each cookie
|
437
|
-
// back into the jar.
|
438
|
-
if (appState) {
|
439
|
-
appState.map(function (c) {
|
440
|
-
var str = c.key + "=" + c.value + "; expires=" + c.expires + "; domain=" + c.domain + "; path=" + c.path + ";";
|
441
|
-
jar.setCookie(str, "http://" + c.domain);
|
442
|
-
});
|
443
|
-
|
444
|
-
// Load the main page.
|
445
|
-
mainPromise = utils.get('https://www.facebook.com/', jar, null, globalOptions, { noRef: true }).then(utils.saveCookies(jar));
|
446
|
-
}
|
447
|
-
else {
|
448
|
-
// Open the main page, then we login with the given credentials and finally
|
449
|
-
// load the main page again (it'll give us some IDs that we need)
|
450
|
-
mainPromise = utils
|
451
|
-
.get("https://www.facebook.com/", null, null, globalOptions, { noRef: true })
|
452
|
-
.then(utils.saveCookies(jar))
|
453
|
-
.then(makeLogin(jar, email, password, globalOptions, callback, prCallback))
|
454
|
-
.then(function () {
|
455
|
-
return utils.get('https://www.facebook.com/', jar, null, globalOptions).then(utils.saveCookies(jar));
|
456
|
-
});
|
457
|
-
}
|
458
|
-
|
459
|
-
var redirect = [1, "https://m.facebook.com/"], bypass_region_err = false, ctx, _defaultFuncs, api;
|
460
|
-
function CheckAndFixErr(res) {
|
461
|
-
let reg_antierr = /This browser is not supported/gs;
|
462
|
-
if (reg_antierr.test(res.body)) {
|
463
|
-
const Data = JSON.stringify(res.body);
|
464
|
-
const Dt_Check = Data.split('2Fhome.php&gfid=')[1];
|
465
|
-
if (Dt_Check == undefined) return res
|
466
|
-
const fid = Dt_Check.split("\\\\")[0];
|
467
|
-
if (Dt_Check == undefined || Dt_Check == "") return res
|
468
|
-
const final_fid = fid.split(`\\`)[0];
|
469
|
-
if (final_fid == undefined || final_fid == '') return res;
|
470
|
-
const redirectlink = redirect[1] + "a/preferences.php?basic_site_devices=m_basic&uri=" + encodeURIComponent("https://m.facebook.com/home.php") + "&gfid=" + final_fid;
|
471
|
-
bypass_region_err = true;
|
472
|
-
log.info('login', 'Bypass vùng tài khoản thành công');
|
473
|
-
return utils.get(redirectlink, jar, null, globalOptions).then(utils.saveCookies(jar));
|
474
|
-
}
|
475
|
-
else return res
|
476
|
-
}
|
477
|
-
function Redirect(res) {
|
478
|
-
var reg = /<meta http-equiv="refresh" content="0;url=([^"]+)[^>]+>/;
|
479
|
-
redirect = reg.exec(res.body);
|
480
|
-
if (redirect && redirect[1]) return utils.get(redirect[1], jar, null, globalOptions).then(utils.saveCookies(jar));
|
481
|
-
return res;
|
482
|
-
}
|
483
|
-
|
484
|
-
mainPromise = mainPromise
|
485
|
-
.then(res => Redirect(res))
|
486
|
-
.then(res => CheckAndFixErr(res))
|
487
|
-
.then(function(res) {
|
488
|
-
let Regex_Via = /MPageLoadClientMetrics/gs;
|
489
|
-
if (!Regex_Via.test(res.body)) {
|
490
|
-
globalOptions.userAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1";
|
491
|
-
return utils.get('https://www.facebook.com/', jar, null, globalOptions, { noRef: true }).then(utils.saveCookies(jar));
|
492
|
-
}
|
493
|
-
else return res
|
494
|
-
})
|
495
|
-
.then(res => Redirect(res))
|
496
|
-
.then(res => CheckAndFixErr(res))
|
497
|
-
.then(function (res) {
|
498
|
-
var html = res.body;
|
499
|
-
var stuff = buildAPI(globalOptions, html, jar, bypass_region_err);
|
500
|
-
ctx = stuff[0];
|
501
|
-
_defaultFuncs = stuff[1];
|
502
|
-
api = stuff[2];
|
503
|
-
return res;
|
504
|
-
});
|
505
|
-
|
506
|
-
// given a pageID we log in as a page
|
507
|
-
if (globalOptions.pageID) {
|
508
|
-
mainPromise = mainPromise
|
509
|
-
.then(function () {
|
510
|
-
return utils.get('https://www.facebook.com/' + ctx.globalOptions.pageID + '/messages/?section=messages&subsection=inbox', ctx.jar, null, globalOptions);
|
511
|
-
})
|
512
|
-
.then(function (resData) {
|
513
|
-
var url = utils.getFrom(resData.body, 'window.location.replace("https:\\/\\/www.facebook.com\\', '");').split('\\').join('');
|
514
|
-
url = url.substring(0, url.length - 1);
|
515
|
-
return utils.get('https://www.facebook.com' + url, ctx.jar, null, globalOptions);
|
516
|
-
});
|
517
|
-
}
|
518
|
-
|
519
|
-
// At the end we call the callback or catch an exception
|
520
|
-
mainPromise
|
521
|
-
.then(function () {
|
522
|
-
log.info("login", 'Done logging in.');
|
523
|
-
return callback(null, api);
|
524
|
-
})
|
525
|
-
.catch(function (e) {
|
526
|
-
log.error("login", e.error || e);
|
527
|
-
callback(e);
|
528
|
-
});
|
529
|
-
}
|
530
|
-
|
531
|
-
function login(loginData, options, callback) {
|
532
|
-
if (utils.getType(options) === 'Function' || utils.getType(options) === 'AsyncFunction') {
|
533
|
-
callback = options;
|
534
|
-
options = {};
|
535
|
-
}
|
536
|
-
|
537
|
-
var globalOptions = {
|
538
|
-
selfListen: false,
|
539
|
-
listenEvents: true,
|
540
|
-
listenTyping: true,
|
541
|
-
updatePresence: false,
|
542
|
-
forceLogin: false,
|
543
|
-
autoMarkDelivery: true,
|
544
|
-
autoMarkRead: false,
|
545
|
-
autoReconnect: true,
|
546
|
-
logRecordSize: defaultLogRecordSize,
|
547
|
-
online: true,
|
548
|
-
emitReady: false,
|
549
|
-
userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/600.3.18 (KHTML, like Gecko) Version/8.0.3 Safari/600.3.18"
|
550
|
-
};
|
551
|
-
|
552
|
-
setOptions(globalOptions, options);
|
553
|
-
|
554
|
-
var prCallback = null;
|
555
|
-
if (utils.getType(callback) !== "Function" && utils.getType(callback) !== "AsyncFunction") {
|
556
|
-
var rejectFunc = null;
|
557
|
-
var resolveFunc = null;
|
558
|
-
var returnPromise = new Promise(function (resolve, reject) {
|
559
|
-
resolveFunc = resolve;
|
560
|
-
rejectFunc = reject;
|
561
|
-
});
|
562
|
-
prCallback = function (error, api) {
|
563
|
-
if (error) return rejectFunc(error);
|
564
|
-
return resolveFunc(api);
|
565
|
-
};
|
566
|
-
callback = prCallback;
|
567
|
-
}
|
568
|
-
loginHelper(loginData.appState, loginData.email, loginData.password, globalOptions, callback, prCallback);
|
569
|
-
return returnPromise;
|
570
|
-
}
|
571
|
-
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
const utils = require("./utils");
|
4
|
+
const log = require("npmlog");
|
5
|
+
|
6
|
+
let checkVerified = null;
|
7
|
+
|
8
|
+
const defaultLogRecordSize = 100;
|
9
|
+
log.maxRecordSize = defaultLogRecordSize;
|
10
|
+
|
11
|
+
function setOptions(globalOptions, options) {
|
12
|
+
Object.keys(options).map(function (key) {
|
13
|
+
switch (key) {
|
14
|
+
case 'online':
|
15
|
+
globalOptions.online = Boolean(options.online);
|
16
|
+
break;
|
17
|
+
case 'logLevel':
|
18
|
+
log.level = options.logLevel;
|
19
|
+
globalOptions.logLevel = options.logLevel;
|
20
|
+
break;
|
21
|
+
case 'logRecordSize':
|
22
|
+
log.maxRecordSize = options.logRecordSize;
|
23
|
+
globalOptions.logRecordSize = options.logRecordSize;
|
24
|
+
break;
|
25
|
+
case 'selfListen':
|
26
|
+
globalOptions.selfListen = Boolean(options.selfListen);
|
27
|
+
break;
|
28
|
+
case 'selfListenEvent':
|
29
|
+
globalOptions.selfListenEvent = options.selfListenEvent;
|
30
|
+
break;
|
31
|
+
case 'listenEvents':
|
32
|
+
globalOptions.listenEvents = Boolean(options.listenEvents);
|
33
|
+
break;
|
34
|
+
case 'pageID':
|
35
|
+
globalOptions.pageID = options.pageID.toString();
|
36
|
+
break;
|
37
|
+
case 'updatePresence':
|
38
|
+
globalOptions.updatePresence = Boolean(options.updatePresence);
|
39
|
+
break;
|
40
|
+
case 'forceLogin':
|
41
|
+
globalOptions.forceLogin = Boolean(options.forceLogin);
|
42
|
+
break;
|
43
|
+
case 'userAgent':
|
44
|
+
globalOptions.userAgent = options.userAgent;
|
45
|
+
break;
|
46
|
+
case 'autoMarkDelivery':
|
47
|
+
globalOptions.autoMarkDelivery = Boolean(options.autoMarkDelivery);
|
48
|
+
break;
|
49
|
+
case 'autoMarkRead':
|
50
|
+
globalOptions.autoMarkRead = Boolean(options.autoMarkRead);
|
51
|
+
break;
|
52
|
+
case 'listenTyping':
|
53
|
+
globalOptions.listenTyping = Boolean(options.listenTyping);
|
54
|
+
break;
|
55
|
+
case 'proxy':
|
56
|
+
if (typeof options.proxy != "string") {
|
57
|
+
delete globalOptions.proxy;
|
58
|
+
utils.setProxy();
|
59
|
+
} else {
|
60
|
+
globalOptions.proxy = options.proxy;
|
61
|
+
utils.setProxy(globalOptions.proxy);
|
62
|
+
}
|
63
|
+
break;
|
64
|
+
case 'autoReconnect':
|
65
|
+
globalOptions.autoReconnect = Boolean(options.autoReconnect);
|
66
|
+
break;
|
67
|
+
case 'emitReady':
|
68
|
+
globalOptions.emitReady = Boolean(options.emitReady);
|
69
|
+
break;
|
70
|
+
default:
|
71
|
+
log.warn("setOptions", "Unrecognized option given to setOptions: " + key);
|
72
|
+
break;
|
73
|
+
}
|
74
|
+
});
|
75
|
+
}
|
76
|
+
|
77
|
+
function buildAPI(globalOptions, html, jar) {
|
78
|
+
const maybeCookie = jar.getCookies("https://www.facebook.com").filter(function (val) {
|
79
|
+
return val.cookieString().split("=")[0] === "c_user";
|
80
|
+
});
|
81
|
+
|
82
|
+
const objCookie = jar.getCookies("https://www.facebook.com").reduce(function (obj, val) {
|
83
|
+
obj[val.cookieString().split("=")[0]] = val.cookieString().split("=")[1];
|
84
|
+
return obj;
|
85
|
+
}, {});
|
86
|
+
|
87
|
+
if (maybeCookie.length === 0) {
|
88
|
+
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." };
|
89
|
+
}
|
90
|
+
|
91
|
+
if (html.indexOf("/checkpoint/block/?next") > -1) {
|
92
|
+
log.warn("login", "Checkpoint detected. Please log in with a browser to verify.");
|
93
|
+
}
|
94
|
+
|
95
|
+
const userID = maybeCookie[0].cookieString().split("=")[1].toString();
|
96
|
+
const i_userID = objCookie.i_user || null;
|
97
|
+
log.info("login", `Logged in as ${userID}`);
|
98
|
+
|
99
|
+
try {
|
100
|
+
clearInterval(checkVerified);
|
101
|
+
} catch (_) { }
|
102
|
+
|
103
|
+
const clientID = (Math.random() * 2147483648 | 0).toString(16);
|
104
|
+
|
105
|
+
|
106
|
+
const oldFBMQTTMatch = html.match(/irisSeqID:"(.+?)",appID:219994525426954,endpoint:"(.+?)"/);
|
107
|
+
let mqttEndpoint, region, fb_dtsg, irisSeqID;
|
108
|
+
try {
|
109
|
+
const endpointMatch = html.match(/"endpoint":"([^"]+)"/);
|
110
|
+
if (endpointMatch) {
|
111
|
+
mqttEndpoint = endpointMatch[1].replace(/\\\//g, '/');
|
112
|
+
const url = new URL(mqttEndpoint);
|
113
|
+
region = url.searchParams.get('region')?.toUpperCase() || "PRN";
|
114
|
+
}
|
115
|
+
log.info('login', `Sever region ${region}`);
|
116
|
+
} catch (e) {
|
117
|
+
log.warning('login', 'Not MQTT endpoint');
|
118
|
+
}
|
119
|
+
const tokenMatch = html.match(/DTSGInitialData.*?token":"(.*?)"/);
|
120
|
+
if (tokenMatch) {
|
121
|
+
fb_dtsg = tokenMatch[1];
|
122
|
+
}
|
123
|
+
|
124
|
+
// All data available to api functions
|
125
|
+
const ctx = {
|
126
|
+
userID: userID,
|
127
|
+
i_userID: i_userID,
|
128
|
+
jar: jar,
|
129
|
+
clientID: clientID,
|
130
|
+
globalOptions: globalOptions,
|
131
|
+
loggedIn: true,
|
132
|
+
access_token: 'NONE',
|
133
|
+
clientMutationId: 0,
|
134
|
+
mqttClient: undefined,
|
135
|
+
lastSeqId: irisSeqID,
|
136
|
+
syncToken: undefined,
|
137
|
+
mqttEndpoint,
|
138
|
+
region,
|
139
|
+
firstListen: true,
|
140
|
+
fb_dtsg
|
141
|
+
};
|
142
|
+
|
143
|
+
const api = {
|
144
|
+
setOptions: setOptions.bind(null, globalOptions),
|
145
|
+
getAppState: function getAppState() {
|
146
|
+
const appState = utils.getAppState(jar);
|
147
|
+
// filter duplicate
|
148
|
+
return appState.filter((item, index, self) => self.findIndex((t) => { return t.key === item.key }) === index);
|
149
|
+
}
|
150
|
+
};
|
151
|
+
|
152
|
+
const defaultFuncs = utils.makeDefaults(html, i_userID || userID, ctx);
|
153
|
+
require('fs').readdirSync(__dirname + '/src/').filter((v) => v.endsWith('.js')).map(function (v) {
|
154
|
+
api[v.replace('.js', '')] = require('./src/' + v)(defaultFuncs, api, ctx);
|
155
|
+
});
|
156
|
+
api.listen = api.listenMqtt;
|
157
|
+
|
158
|
+
return [ctx, defaultFuncs, api];
|
159
|
+
}
|
160
|
+
|
161
|
+
// Helps the login
|
162
|
+
function loginHelper(appState, email, password, globalOptions, callback, prCallback) {
|
163
|
+
let mainPromise = null;
|
164
|
+
const jar = utils.getJar();
|
165
|
+
|
166
|
+
// If we're given an appState we loop through it and save each cookie
|
167
|
+
// back into the jar.
|
168
|
+
if (appState) {
|
169
|
+
// check and convert cookie to appState
|
170
|
+
if (utils.getType(appState) === 'Array' && appState.some(c => c.name)) {
|
171
|
+
appState = appState.map(c => {
|
172
|
+
c.key = c.name;
|
173
|
+
delete c.name;
|
174
|
+
return c;
|
175
|
+
})
|
176
|
+
}
|
177
|
+
else if (utils.getType(appState) === 'String') {
|
178
|
+
const arrayAppState = [];
|
179
|
+
appState.split(';').forEach(c => {
|
180
|
+
const [key, value] = c.split('=');
|
181
|
+
|
182
|
+
arrayAppState.push({
|
183
|
+
key: (key || "").trim(),
|
184
|
+
value: (value || "").trim(),
|
185
|
+
domain: "facebook.com",
|
186
|
+
path: "/",
|
187
|
+
expires: new Date().getTime() + 1000 * 60 * 60 * 24 * 365
|
188
|
+
});
|
189
|
+
});
|
190
|
+
appState = arrayAppState;
|
191
|
+
}
|
192
|
+
|
193
|
+
appState.map(function (c) {
|
194
|
+
const str = c.key + "=" + c.value + "; expires=" + c.expires + "; domain=" + c.domain + "; path=" + c.path + ";";
|
195
|
+
jar.setCookie(str, "http://" + c.domain);
|
196
|
+
});
|
197
|
+
|
198
|
+
// Load the main page.
|
199
|
+
mainPromise = utils
|
200
|
+
.get('https://www.facebook.com/', jar, null, globalOptions, { noRef: true })
|
201
|
+
.then(utils.saveCookies(jar));
|
202
|
+
} else {
|
203
|
+
if (email) {
|
204
|
+
throw { error: "Currently, the login method by email and password is no longer supported, please use the login method by appState" };
|
205
|
+
}
|
206
|
+
else {
|
207
|
+
throw { error: "No appState given." };
|
208
|
+
}
|
209
|
+
}
|
210
|
+
|
211
|
+
let ctx = null;
|
212
|
+
let _defaultFuncs = null;
|
213
|
+
let api = null;
|
214
|
+
|
215
|
+
mainPromise = mainPromise
|
216
|
+
.then(function (res) {
|
217
|
+
// Hacky check for the redirection that happens on some ISPs, which doesn't return statusCode 3xx
|
218
|
+
const reg = /<meta http-equiv="refresh" content="0;url=([^"]+)[^>]+>/;
|
219
|
+
const redirect = reg.exec(res.body);
|
220
|
+
if (redirect && redirect[1]) {
|
221
|
+
return utils
|
222
|
+
.get(redirect[1], jar, null, globalOptions)
|
223
|
+
.then(utils.saveCookies(jar));
|
224
|
+
}
|
225
|
+
return res;
|
226
|
+
})
|
227
|
+
.then(function (res) {
|
228
|
+
const html = res.body;
|
229
|
+
const stuff = buildAPI(globalOptions, html, jar);
|
230
|
+
ctx = stuff[0];
|
231
|
+
_defaultFuncs = stuff[1];
|
232
|
+
api = stuff[2];
|
233
|
+
return res;
|
234
|
+
});
|
235
|
+
|
236
|
+
// given a pageID we log in as a page
|
237
|
+
if (globalOptions.pageID) {
|
238
|
+
mainPromise = mainPromise
|
239
|
+
.then(function () {
|
240
|
+
return utils
|
241
|
+
.get('https://www.facebook.com/' + ctx.globalOptions.pageID + '/messages/?section=messages&subsection=inbox', ctx.jar, null, globalOptions);
|
242
|
+
})
|
243
|
+
.then(function (resData) {
|
244
|
+
let url = utils.getFrom(resData.body, 'window.location.replace("https:\\/\\/www.facebook.com\\', '");').split('\\').join('');
|
245
|
+
url = url.substring(0, url.length - 1);
|
246
|
+
|
247
|
+
return utils
|
248
|
+
.get('https://www.facebook.com' + url, ctx.jar, null, globalOptions);
|
249
|
+
});
|
250
|
+
}
|
251
|
+
|
252
|
+
// At the end we call the callback or catch an exception
|
253
|
+
mainPromise
|
254
|
+
.then(function () {
|
255
|
+
log.info("login", 'Done logging in.');
|
256
|
+
return callback(null, api);
|
257
|
+
})
|
258
|
+
.catch(function (e) {
|
259
|
+
log.error("login", e.error || e);
|
260
|
+
callback(e);
|
261
|
+
});
|
262
|
+
}
|
263
|
+
|
264
|
+
function login(loginData, options, callback) {
|
265
|
+
if (utils.getType(options) === 'Function' || utils.getType(options) === 'AsyncFunction') {
|
266
|
+
callback = options;
|
267
|
+
options = {};
|
268
|
+
}
|
269
|
+
|
270
|
+
const globalOptions = {
|
271
|
+
selfListen: false,
|
272
|
+
selfListenEvent: false,
|
273
|
+
listenEvents: false,
|
274
|
+
listenTyping: false,
|
275
|
+
updatePresence: false,
|
276
|
+
forceLogin: false,
|
277
|
+
autoMarkDelivery: true,
|
278
|
+
autoMarkRead: false,
|
279
|
+
autoReconnect: true,
|
280
|
+
logRecordSize: defaultLogRecordSize,
|
281
|
+
online: true,
|
282
|
+
emitReady: false,
|
283
|
+
userAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36"
|
284
|
+
};
|
285
|
+
|
286
|
+
setOptions(globalOptions, options);
|
287
|
+
|
288
|
+
let prCallback = null;
|
289
|
+
if (utils.getType(callback) !== "Function" && utils.getType(callback) !== "AsyncFunction") {
|
290
|
+
let rejectFunc = null;
|
291
|
+
let resolveFunc = null;
|
292
|
+
var returnPromise = new Promise(function (resolve, reject) {
|
293
|
+
resolveFunc = resolve;
|
294
|
+
rejectFunc = reject;
|
295
|
+
});
|
296
|
+
prCallback = function (error, api) {
|
297
|
+
if (error) {
|
298
|
+
return rejectFunc(error);
|
299
|
+
}
|
300
|
+
return resolveFunc(api);
|
301
|
+
};
|
302
|
+
callback = prCallback;
|
303
|
+
}
|
304
|
+
loginHelper(loginData.appState, loginData.email, loginData.password, globalOptions, callback, prCallback);
|
305
|
+
return returnPromise;
|
306
|
+
}
|
307
|
+
|
572
308
|
module.exports = login;
|