forked from VangBanLaNhat/fca-unofficial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
652 lines (587 loc) · 23 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
"use strict";
var utils = require("./utils");
var cheerio = require("cheerio");
var log = require("npmlog");
var checkVerified = null;
var defaultLogRecordSize = 100;
log.maxRecordSize = defaultLogRecordSize;
function setOptions(globalOptions, options) {
Object.keys(options).map(function (key) {
switch (key) {
case 'online':
globalOptions.online = Boolean(options.online);
break;
case 'logLevel':
log.level = options.logLevel;
globalOptions.logLevel = options.logLevel;
break;
case 'logRecordSize':
log.maxRecordSize = options.logRecordSize;
globalOptions.logRecordSize = options.logRecordSize;
break;
case 'selfListen':
globalOptions.selfListen = Boolean(options.selfListen);
break;
case 'listenEvents':
globalOptions.listenEvents = Boolean(options.listenEvents);
break;
case 'pageID':
globalOptions.pageID = options.pageID.toString();
break;
case 'updatePresence':
globalOptions.updatePresence = Boolean(options.updatePresence);
break;
case 'forceLogin':
globalOptions.forceLogin = Boolean(options.forceLogin);
break;
case 'userAgent':
globalOptions.userAgent = options.userAgent;
break;
case 'autoMarkDelivery':
globalOptions.autoMarkDelivery = Boolean(options.autoMarkDelivery);
break;
case 'autoMarkRead':
globalOptions.autoMarkRead = Boolean(options.autoMarkRead);
break;
case 'listenTyping':
globalOptions.listenTyping = Boolean(options.listenTyping);
break;
case 'proxy':
if (typeof options.proxy != "string") {
delete globalOptions.proxy;
utils.setProxy();
} else {
globalOptions.proxy = options.proxy;
utils.setProxy(globalOptions.proxy);
}
break;
case 'autoReconnect':
globalOptions.autoReconnect = Boolean(options.autoReconnect);
break;
case 'emitReady':
globalOptions.emitReady = Boolean(options.emitReady);
break;
default:
log.warn("setOptions", "Unrecognized option given to setOptions: " + key);
break;
}
});
}
function buildAPI(globalOptions, html, jar) {
var maybeCookie = jar.getCookies("https://www.facebook.com").filter(function (val) {
return val.cookieString().split("=")[0] === "c_user";
});
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." };
}
if (html.indexOf("/checkpoint/block/?next") > -1) {
log.warn("login", "Checkpoint detected. Please log in with a browser to verify.");
}
var userID = maybeCookie[0].cookieString().split("=")[1].toString();
log.info("login", `Logged in as ${userID}`);
try {
clearInterval(checkVerified);
} catch (_) { }
var clientID = (Math.random() * 2147483648 | 0).toString(16);
let oldFBMQTTMatch = html.match(/irisSeqID:"(.+?)",appID:219994525426954,endpoint:"(.+?)"/);
let mqttEndpoint = null;
let region = null;
let irisSeqID = null;
var noMqttData = null;
if (oldFBMQTTMatch) {
irisSeqID = oldFBMQTTMatch[1];
mqttEndpoint = oldFBMQTTMatch[2];
region = new URL(mqttEndpoint).searchParams.get("region").toUpperCase();
log.info("login", `Got this account's message region: ${region}`);
} else {
let newFBMQTTMatch = html.match(/{"app_id":"219994525426954","endpoint":"(.+?)","iris_seq_id":"(.+?)"}/);
if (newFBMQTTMatch) {
irisSeqID = newFBMQTTMatch[2];
mqttEndpoint = newFBMQTTMatch[1].replace(/\\\//g, "/");
region = new URL(mqttEndpoint).searchParams.get("region").toUpperCase();
log.info("login", `Got this account's message region: ${region}`);
} else {
let legacyFBMQTTMatch = html.match(/(\["MqttWebConfig",\[\],{fbid:")(.+?)(",appID:219994525426954,endpoint:")(.+?)(",pollingEndpoint:")(.+?)(3790])/);
if (legacyFBMQTTMatch) {
mqttEndpoint = legacyFBMQTTMatch[4];
region = new URL(mqttEndpoint).searchParams.get("region").toUpperCase();
log.warn("login", `Cannot get sequence ID with new RegExp. Fallback to old RegExp (without seqID)...`);
log.info("login", `Got this account's message region: ${region}`);
log.info("login", `[Unused] Polling endpoint: ${legacyFBMQTTMatch[6]}`);
} else {
log.warn("login", "Cannot get MQTT region & sequence ID.");
noMqttData = html;
}
}
}
// All data available to api functions
var ctx = {
userID: userID,
jar: jar,
clientID: clientID,
globalOptions: globalOptions,
loggedIn: true,
access_token: 'NONE',
clientMutationId: 0,
mqttClient: undefined,
lastSeqId: irisSeqID,
syncToken: undefined,
mqttEndpoint,
region,
firstListen: true,
wsReqNumber: 0,
wsTaskNumber: 0
};
var api = {
setOptions: setOptions.bind(null, globalOptions),
getAppState: function getAppState() {
return utils.getAppState(jar);
}
};
if (noMqttData) {
api["htmlData"] = noMqttData;
}
const apiFuncNames = [
'addExternalModule',
'addUserToGroup',
'changeAdminStatus',
'changeArchivedStatus',
'changeBio',
'changeBlockedStatus',
'changeGroupImage',
'changeNickname',
'changeThreadColor',
'changeThreadEmoji',
'createNewGroup',
'createPoll',
'deleteMessage',
'deleteThread',
'editMessage',
'forwardAttachment',
'getCurrentUserID',
'getEmojiUrl',
'getFriendsList',
'getThreadHistory',
'getThreadInfo',
'getThreadList',
'getThreadPictures',
'getUserID',
'getUserInfo',
'handleMessageRequest',
'listenMqtt',
'logout',
'markAsDelivered',
'markAsRead',
'markAsReadAll',
'markAsSeen',
'muteThread',
'removeUserFromGroup',
'resolvePhotoUrl',
'searchForThread',
'sendMessage',
'sendTypingIndicator',
'setMessageReaction',
'setTitle',
'threadColors',
'unsendMessage',
// HTTP
'httpGet',
'httpPost',
// Deprecated features
"getThreadListDeprecated",
'getThreadHistoryDeprecated',
'getThreadInfoDeprecated',
];
var defaultFuncs = utils.makeDefaults(html, userID, ctx);
// Load all api functions in a loop
apiFuncNames.map(function (v) {
api[v] = require('./src/' + v)(defaultFuncs, api, ctx);
});
//Removing original `listen` that uses pull.
//Map it to listenMqtt instead for backward compatibly.
api.listen = api.listenMqtt;
return [ctx, defaultFuncs, api];
}
function makeLogin(jar, email, password, loginOptions, callback, prCallback) {
return function (res) {
var html = res.body;
var $ = cheerio.load(html);
var arr = [];
// This will be empty, but just to be sure we leave it
$("#login_form input").map(function (i, v) {
arr.push({ val: $(v).val(), name: $(v).attr("name") });
});
arr = arr.filter(function (v) {
return v.val && v.val.length;
});
var form = utils.arrToForm(arr);
form.lsd = utils.getFrom(html, "[\"LSD\",[],{\"token\":\"", "\"}");
form.lgndim = Buffer.from("{\"w\":1440,\"h\":900,\"aw\":1440,\"ah\":834,\"c\":24}").toString('base64');
form.email = email;
form.pass = password;
form.default_persistent = '0';
form.lgnrnd = utils.getFrom(html, "name=\"lgnrnd\" value=\"", "\"");
form.locale = 'en_US';
form.timezone = '240';
form.lgnjs = ~~(Date.now() / 1000);
// Getting cookies from the HTML page... (kill me now plz)
// we used to get a bunch of cookies in the headers of the response of the
// request, but FB changed and they now send those cookies inside the JS.
// They run the JS which then injects the cookies in the page.
// The "solution" is to parse through the html and find those cookies
// which happen to be conveniently indicated with a _js_ in front of their
// variable name.
//
// ---------- Very Hacky Part Starts -----------------
var willBeCookies = html.split("\"_js_");
willBeCookies.slice(1).map(function (val) {
var cookieData = JSON.parse("[\"" + utils.getFrom(val, "", "]") + "]");
jar.setCookie(utils.formatCookie(cookieData, "facebook"), "https://www.facebook.com");
});
// ---------- Very Hacky Part Ends -----------------
log.info("login", "Logging in...");
return utils
.post("https://www.facebook.com/login/device-based/regular/login/?login_attempt=1&lwv=110", jar, form, loginOptions)
.then(utils.saveCookies(jar))
.then(function (res) {
var headers = res.headers;
if (!headers.location) {
throw { error: "Wrong username/password." };
}
// This means the account has login approvals turned on.
if (headers.location.indexOf('https://www.facebook.com/checkpoint/') > -1) {
log.info("login", "You have login approvals turned on.");
var nextURL = 'https://www.facebook.com/checkpoint/?next=https%3A%2F%2Fwww.facebook.com%2Fhome.php';
return utils
.get(headers.location, jar, null, loginOptions)
.then(utils.saveCookies(jar))
.then(function (res) {
var html = res.body;
// Make the form in advance which will contain the fb_dtsg and nh
var $ = cheerio.load(html);
var arr = [];
$("form input").map(function (i, v) {
arr.push({ val: $(v).val(), name: $(v).attr("name") });
});
arr = arr.filter(function (v) {
return v.val && v.val.length;
});
var form = utils.arrToForm(arr);
if (html.indexOf("checkpoint/?next") > -1) {
setTimeout(() => {
checkVerified = setInterval((_form) => {
/* utils
.post("https://www.facebook.com/login/approvals/approved_machine_check/", jar, form, loginOptions, null, {
"Referer": "https://www.facebook.com/checkpoint/?next"
})
.then(utils.saveCookies(jar))
.then(res => {
try {
JSON.parse(res.body.replace(/for\s*\(\s*;\s*;\s*\)\s*;\s*()/, ""));
} catch (ex) {
clearInterval(checkVerified);
log.info("login", "Verified from browser. Logging in...");
return loginHelper(utils.getAppState(jar), email, password, loginOptions, callback);
}
})
.catch(ex => {
log.error("login", ex);
}); */
}, 5000, {
fb_dtsg: form.fb_dtsg,
jazoest: form.jazoest,
dpr: 1
});
}, 2500);
throw {
error: 'login-approval',
continue: function submit2FA(code) {
form.approvals_code = code;
form['submit[Continue]'] = $("#checkpointSubmitButton").html(); //'Continue';
var prResolve = null;
var prReject = null;
var rtPromise = new Promise(function (resolve, reject) {
prResolve = resolve;
prReject = reject;
});
if (typeof code == "string") {
utils
.post(nextURL, jar, form, loginOptions)
.then(utils.saveCookies(jar))
.then(function (res) {
var $ = cheerio.load(res.body);
var error = $("#approvals_code").parent().attr("data-xui-error");
if (error) {
throw {
error: 'login-approval',
errordesc: "Invalid 2FA code.",
lerror: error,
continue: submit2FA
};
}
})
.then(function () {
// Use the same form (safe I hope)
delete form.no_fido;
delete form.approvals_code;
form.name_action_selected = 'dont_save'; //'save_device';
return utils
.post(nextURL, jar, form, loginOptions)
.then(utils.saveCookies(jar));
})
.then(function (res) {
var headers = res.headers;
if (!headers.location && res.body.indexOf('Review Recent Login') > -1) {
throw { error: "Something went wrong with login approvals." };
}
var appState = utils.getAppState(jar);
if (callback === prCallback) {
callback = function (err, api) {
if (err) {
return prReject(err);
}
return prResolve(api);
};
}
// Simply call loginHelper because all it needs is the jar
// and will then complete the login process
return loginHelper(appState, email, password, loginOptions, callback);
})
.catch(function (err) {
// Check if using Promise instead of callback
if (callback === prCallback) {
prReject(err);
} else {
callback(err);
}
});
} else {
utils
.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"
})
.then(utils.saveCookies(jar))
.then(res => {
try {
JSON.parse(res.body.replace(/for\s*\(\s*;\s*;\s*\)\s*;\s*/, ""));
} catch (ex) {
clearInterval(checkVerified);
log.info("login", "Verified from browser. Logging in...");
if (callback === prCallback) {
callback = function (err, api) {
if (err) {
return prReject(err);
}
return prResolve(api);
};
}
return loginHelper(utils.getAppState(jar), email, password, loginOptions, callback);
}
})
.catch(ex => {
log.error("login", ex);
if (callback === prCallback) {
prReject(ex);
} else {
callback(ex);
}
});
}
return rtPromise;
}
};
} else {
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." };
}
if (html.indexOf("Suspicious Login Attempt") > -1) {
form['submit[This was me]'] = "This was me";
} else {
form['submit[This Is Okay]'] = "This Is Okay";
}
return utils
.post(nextURL, jar, form, loginOptions)
.then(utils.saveCookies(jar))
.then(function () {
// Use the same form (safe I hope)
form.name_action_selected = 'save_device';
return utils
.post(nextURL, jar, form, loginOptions)
.then(utils.saveCookies(jar));
})
.then(function (res) {
var headers = res.headers;
if (!headers.location && res.body.indexOf('Review Recent Login') > -1) {
throw { error: "Something went wrong with review recent login." };
}
var appState = utils.getAppState(jar);
// Simply call loginHelper because all it needs is the jar
// and will then complete the login process
return loginHelper(appState, email, password, loginOptions, callback);
})
.catch(function (e) {
callback(e);
});
}
});
}
return utils
.get('https://www.facebook.com/', jar, null, loginOptions)
.then(utils.saveCookies(jar));
});
};
}
// Helps the login
function loginHelper(appState, email, password, globalOptions, callback, prCallback) {
var mainPromise = null;
var jar = utils.getJar();
// If we're given an appState we loop through it and save each cookie
// back into the jar.
if (appState) {
appState.map(function (c) {
var str = c.key + "=" + c.value + "; expires=" + c.expires + "; domain=" + c.domain + "; path=" + c.path + ";";
jar.setCookie(str, "http://" + c.domain);
});
/** TODO:
* Check websocket traffic
* Check what graphql returns through the extension
* */
// Load the main page.
console.log("HERE's THE PROBLEM")
// TODO HERE: Rewrite this using node-fetch might solve it?
const tfunc = async () => {
console.log("STARTING LOADING")
const fetch = require('node-fetch')
const { CookieJar } = require('tough-cookie')
const { URL } = require('url')
const toughJar = new CookieJar()
// Assuming appState is provided and contains cookies
appState.forEach(c => {
const str = `${c.key}=${c.value}; expires=${c.expires}; domain=${c.domain}; path=${c.path};`
toughJar.setCookieSync(str, `http://${c.domain}`)
})
const url = 'https://www.facebook.com/'
const headers = {
// 'User-Agent': no user agent. It kicks us out if we add it
'Cookie': toughJar.getCookieStringSync(url)
}
const response = await fetch(url, {
method: 'GET',
headers: headers,
redirect: 'manual' // To handle redirections manually
})
const body = await response.text()
console.log("BODYBODYBODY")
console.log(body)
// console.log(response)
return { ...response, body }
}
tfunc()
mainPromise = tfunc()
// mainPromise = utils
// .get('https://www.facebook.com/', jar, null, globalOptions, { noRef: true })
// .then(utils.saveCookies(jar));
} else {
// TODO: Try a different request library. If that doesn't work, try requests from puppeteer
// Open the main page, then we login with the given credentials and finally
// load the main page again (it'll give us some IDs that we need)
mainPromise = utils
.get("https://www.facebook.com/", null, null, globalOptions, { noRef: true })
.then(utils.saveCookies(jar))
.then(makeLogin(jar, email, password, globalOptions, callback, prCallback))
.then(function () {
return utils
.get('https://www.facebook.com/', jar, null, globalOptions)
.then(utils.saveCookies(jar));
});
}
var ctx = null;
var _defaultFuncs = null;
var api = null;
// -> Here res.body is null. I should rewrite it.
mainPromise = mainPromise
.then(function (res) {
// Hacky check for the redirection that happens on some ISPs, which doesn't return statusCode 3xx
var reg = /<meta http-equiv="refresh" content="0;url=([^"]+)[^>]+>/;
var redirect = reg.exec(res.body);
if (redirect && redirect[1]) {
return utils
.get(redirect[1], jar, null, globalOptions)
.then(utils.saveCookies(jar));
}
return res;
})
.then(function (res) {
var html = res.body;
console.log(html)
var stuff = buildAPI(globalOptions, html, jar);
ctx = stuff[0];
_defaultFuncs = stuff[1];
api = stuff[2];
return res;
});
// given a pageID we log in as a page
if (globalOptions.pageID) {
mainPromise = mainPromise
.then(function () {
return utils
.get('https://www.facebook.com/' + ctx.globalOptions.pageID + '/messages/?section=messages&subsection=inbox', ctx.jar, null, globalOptions);
})
.then(function (resData) {
var url = utils.getFrom(resData.body, 'window.location.replace("https:\\/\\/www.facebook.com\\', '");').split('\\').join('');
url = url.substring(0, url.length - 1);
return utils
.get('https://www.facebook.com' + url, ctx.jar, null, globalOptions);
});
}
// At the end we call the callback or catch an exception
mainPromise
.then(function () {
log.info("login", 'Done logging in.');
return callback(null, api);
})
.catch(function (e) {
log.error("login", e.error || e);
callback(e);
});
}
function login(loginData, options, callback) {
if (utils.getType(options) === 'Function' || utils.getType(options) === 'AsyncFunction') {
callback = options;
options = {};
}
var globalOptions = {
selfListen: false,
listenEvents: false,
listenTyping: false,
updatePresence: false,
forceLogin: false,
autoMarkDelivery: true,
autoMarkRead: false,
autoReconnect: true,
logRecordSize: defaultLogRecordSize,
online: true,
emitReady: false,
// userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10.1.2 Safari/603.3.8"
};
setOptions(globalOptions, options);
var prCallback = null;
if (utils.getType(callback) !== "Function" && utils.getType(callback) !== "AsyncFunction") {
var rejectFunc = null;
var resolveFunc = null;
var returnPromise = new Promise(function (resolve, reject) {
resolveFunc = resolve;
rejectFunc = reject;
});
prCallback = function (error, api) {
if (error) {
return rejectFunc(error);
}
return resolveFunc(api);
};
callback = prCallback;
}
loginHelper(loginData.appState, loginData.email, loginData.password, globalOptions, callback, prCallback);
return returnPromise;
}
module.exports = login;