alicezetion 1.3.5 → 1.3.6

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