nayan-remake-api 0.0.1-security → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of nayan-remake-api might be problematic. Click here for more details.
- package/.cache/replit/__replit_disk_meta.json +1 -0
- package/.config/configstore/update-notifier-npm.json +4 -0
- package/.config/configstore/update-notifier-npm.json.1735545094 +4 -0
- package/.replit +80 -0
- package/CHANGELOG.md +2 -0
- package/DOCS.md +1947 -0
- package/LICENSE-MIT +21 -0
- package/README.md +198 -3
- package/index.js +641 -0
- package/package.json +69 -3
- package/replit.nix +8 -0
- package/src/addExternalModule.js +19 -0
- package/src/addUserToGroup.js +113 -0
- package/src/changeAdminStatus.js +79 -0
- package/src/changeArchivedStatus.js +55 -0
- package/src/changeAvatar.js +127 -0
- package/src/changeBio.js +77 -0
- package/src/changeBlockedStatus.js +47 -0
- package/src/changeGroupImage.js +133 -0
- package/src/changeNickname.js +59 -0
- package/src/changeThreadColor.js +65 -0
- package/src/changeThreadEmoji.js +55 -0
- package/src/createNewGroup.js +86 -0
- package/src/createPoll.js +71 -0
- package/src/deleteMessage.js +56 -0
- package/src/deleteThread.js +56 -0
- package/src/forwardAttachment.js +60 -0
- package/src/getCurrentUserID.js +7 -0
- package/src/getEmojiUrl.js +29 -0
- package/src/getFriendsList.js +84 -0
- package/src/getMessage.js +716 -0
- package/src/getThreadHistory.js +666 -0
- package/src/getThreadInfo.js +232 -0
- package/src/getThreadList.js +237 -0
- package/src/getThreadPictures.js +79 -0
- package/src/getUserID.js +66 -0
- package/src/getUserInfo.js +74 -0
- package/src/handleFriendRequest.js +61 -0
- package/src/handleMessageRequest.js +65 -0
- package/src/httpGet.js +57 -0
- package/src/httpPost.js +57 -0
- package/src/httpPostFormData.js +59 -0
- package/src/listenMqtt.js +862 -0
- package/src/logout.js +75 -0
- package/src/markAsDelivered.js +58 -0
- package/src/markAsRead.js +80 -0
- package/src/markAsReadAll.js +50 -0
- package/src/markAsSeen.js +59 -0
- package/src/muteThread.js +52 -0
- package/src/n +1 -0
- package/src/removeUserFromGroup.js +79 -0
- package/src/resolvePhotoUrl.js +45 -0
- package/src/searchForThread.js +53 -0
- package/src/sendMessage.js +449 -0
- package/src/sendTypingIndicator.js +103 -0
- package/src/setMessageReaction.js +117 -0
- package/src/setPostReaction.js +109 -0
- package/src/setTitle.js +86 -0
- package/src/threadColors.js +120 -0
- package/src/unfriend.js +52 -0
- package/src/unsendMessage.js +49 -0
- package/src/uploadAttachment.js +96 -0
- package/test/data/N +1 -0
- package/test/data/shareAttach.js +146 -0
- package/test/data/test.txt +7 -0
- package/test/example-config.json +18 -0
- package/test/n +1 -0
- package/test/test-page.js +140 -0
- package/test/test.js +387 -0
- package/utils.js +1451 -0
package/index.js
ADDED
|
@@ -0,0 +1,641 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const utils = require("./utils");
|
|
4
|
+
const cheerio = require("cheerio");
|
|
5
|
+
const log = require("npmlog");
|
|
6
|
+
let request = require("request");
|
|
7
|
+
|
|
8
|
+
let checkVerified = null;
|
|
9
|
+
|
|
10
|
+
const defaultLogRecordSize = 100;
|
|
11
|
+
log.maxRecordSize = defaultLogRecordSize;
|
|
12
|
+
|
|
13
|
+
function setOptions(globalOptions, options) {
|
|
14
|
+
Object.keys(options).map(function (key) {
|
|
15
|
+
switch (key) {
|
|
16
|
+
case 'online':
|
|
17
|
+
globalOptions.online = Boolean(options.online);
|
|
18
|
+
break;
|
|
19
|
+
case 'logLevel':
|
|
20
|
+
log.level = options.logLevel;
|
|
21
|
+
globalOptions.logLevel = options.logLevel;
|
|
22
|
+
break;
|
|
23
|
+
case 'logRecordSize':
|
|
24
|
+
log.maxRecordSize = options.logRecordSize;
|
|
25
|
+
globalOptions.logRecordSize = options.logRecordSize;
|
|
26
|
+
break;
|
|
27
|
+
case 'selfListen':
|
|
28
|
+
globalOptions.selfListen = Boolean(options.selfListen);
|
|
29
|
+
break;
|
|
30
|
+
case 'selfListenEvent':
|
|
31
|
+
globalOptions.selfListenEvent = options.selfListenEvent;
|
|
32
|
+
break;
|
|
33
|
+
case 'listenEvents':
|
|
34
|
+
globalOptions.listenEvents = Boolean(options.listenEvents);
|
|
35
|
+
break;
|
|
36
|
+
case 'pageID':
|
|
37
|
+
globalOptions.pageID = options.pageID.toString();
|
|
38
|
+
break;
|
|
39
|
+
case 'updatePresence':
|
|
40
|
+
globalOptions.updatePresence = Boolean(options.updatePresence);
|
|
41
|
+
break;
|
|
42
|
+
case 'forceLogin':
|
|
43
|
+
globalOptions.forceLogin = Boolean(options.forceLogin);
|
|
44
|
+
break;
|
|
45
|
+
case 'userAgent':
|
|
46
|
+
globalOptions.userAgent = options.userAgent;
|
|
47
|
+
break;
|
|
48
|
+
case 'autoMarkDelivery':
|
|
49
|
+
globalOptions.autoMarkDelivery = Boolean(options.autoMarkDelivery);
|
|
50
|
+
break;
|
|
51
|
+
case 'autoMarkRead':
|
|
52
|
+
globalOptions.autoMarkRead = Boolean(options.autoMarkRead);
|
|
53
|
+
break;
|
|
54
|
+
case 'listenTyping':
|
|
55
|
+
globalOptions.listenTyping = Boolean(options.listenTyping);
|
|
56
|
+
break;
|
|
57
|
+
case 'proxy':
|
|
58
|
+
if (typeof options.proxy != "string") {
|
|
59
|
+
delete globalOptions.proxy;
|
|
60
|
+
utils.setProxy();
|
|
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
|
+
const maybeCookie = jar.getCookies("https://www.facebook.com").filter(function (val) {
|
|
81
|
+
return val.cookieString().split("=")[0] === "c_user";
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
const 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) {
|
|
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
|
+
const userID = maybeCookie[0].cookieString().split("=")[1].toString();
|
|
98
|
+
const i_userID = objCookie.i_user || null;
|
|
99
|
+
log.info("login", `Logged in as ${userID}`);
|
|
100
|
+
|
|
101
|
+
try {
|
|
102
|
+
clearInterval(checkVerified);
|
|
103
|
+
} catch (_) { }
|
|
104
|
+
|
|
105
|
+
const clientID = (Math.random() * 2147483648 | 0).toString(16);
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
const oldFBMQTTMatch = html.match(/irisSeqID:"(.+?)",appID:219994525426954,endpoint:"(.+?)"/);
|
|
109
|
+
let mqttEndpoint = null;
|
|
110
|
+
let region = null;
|
|
111
|
+
let irisSeqID = null;
|
|
112
|
+
let noMqttData = null;
|
|
113
|
+
|
|
114
|
+
if (oldFBMQTTMatch) {
|
|
115
|
+
irisSeqID = oldFBMQTTMatch[1];
|
|
116
|
+
mqttEndpoint = oldFBMQTTMatch[2];
|
|
117
|
+
region = new URL(mqttEndpoint).searchParams.get("region").toUpperCase();
|
|
118
|
+
log.info("login", `Got this account's message region: ${region}`);
|
|
119
|
+
} else {
|
|
120
|
+
const newFBMQTTMatch = html.match(/{"app_id":"219994525426954","endpoint":"(.+?)","iris_seq_id":"(.+?)"}/);
|
|
121
|
+
if (newFBMQTTMatch) {
|
|
122
|
+
irisSeqID = newFBMQTTMatch[2];
|
|
123
|
+
mqttEndpoint = newFBMQTTMatch[1].replace(/\\\//g, "/");
|
|
124
|
+
region = new URL(mqttEndpoint).searchParams.get("region").toUpperCase();
|
|
125
|
+
log.info("login", `Got this account's message region: ${region}`);
|
|
126
|
+
} else {
|
|
127
|
+
const legacyFBMQTTMatch = html.match(/(\["MqttWebConfig",\[\],{fbid:")(.+?)(",appID:219994525426954,endpoint:")(.+?)(",pollingEndpoint:")(.+?)(3790])/);
|
|
128
|
+
if (legacyFBMQTTMatch) {
|
|
129
|
+
mqttEndpoint = legacyFBMQTTMatch[4];
|
|
130
|
+
region = new URL(mqttEndpoint).searchParams.get("region").toUpperCase();
|
|
131
|
+
log.warn("login", `Cannot get sequence ID with new RegExp. Fallback to old RegExp (without seqID)...`);
|
|
132
|
+
log.info("login", `Got this account's message region: ${region}`);
|
|
133
|
+
log.info("login", `[Unused] Polling endpoint: ${legacyFBMQTTMatch[6]}`);
|
|
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
|
+
const ctx = {
|
|
143
|
+
userID: userID,
|
|
144
|
+
i_userID: i_userID,
|
|
145
|
+
jar: jar,
|
|
146
|
+
clientID: clientID,
|
|
147
|
+
globalOptions: globalOptions,
|
|
148
|
+
loggedIn: true,
|
|
149
|
+
access_token: 'NONE',
|
|
150
|
+
clientMutationId: 0,
|
|
151
|
+
mqttClient: undefined,
|
|
152
|
+
lastSeqId: irisSeqID,
|
|
153
|
+
syncToken: undefined,
|
|
154
|
+
mqttEndpoint,
|
|
155
|
+
region,
|
|
156
|
+
firstListen: true
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
const api = {
|
|
160
|
+
setOptions: setOptions.bind(null, globalOptions),
|
|
161
|
+
getAppState: function getAppState() {
|
|
162
|
+
const appState = utils.getAppState(jar);
|
|
163
|
+
// filter duplicate
|
|
164
|
+
return appState.filter((item, index, self) => self.findIndex((t) => { return t.key === item.key }) === index);
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
if (noMqttData) {
|
|
169
|
+
api["htmlData"] = noMqttData;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const apiFuncNames = [
|
|
173
|
+
'addExternalModule',
|
|
174
|
+
'addUserToGroup',
|
|
175
|
+
'changeAdminStatus',
|
|
176
|
+
'changeArchivedStatus',
|
|
177
|
+
'changeAvatar',
|
|
178
|
+
'changeBio',
|
|
179
|
+
'changeBlockedStatus',
|
|
180
|
+
'changeGroupImage',
|
|
181
|
+
'changeNickname',
|
|
182
|
+
'changeThreadColor',
|
|
183
|
+
'changeThreadEmoji',
|
|
184
|
+
'createNewGroup',
|
|
185
|
+
'createPoll',
|
|
186
|
+
'deleteMessage',
|
|
187
|
+
'deleteThread',
|
|
188
|
+
'forwardAttachment',
|
|
189
|
+
'getCurrentUserID',
|
|
190
|
+
'getEmojiUrl',
|
|
191
|
+
'getFriendsList',
|
|
192
|
+
'getMessage',
|
|
193
|
+
'getThreadHistory',
|
|
194
|
+
'getThreadInfo',
|
|
195
|
+
'getThreadList',
|
|
196
|
+
'getThreadPictures',
|
|
197
|
+
'getUserID',
|
|
198
|
+
'getUserInfo',
|
|
199
|
+
'handleMessageRequest',
|
|
200
|
+
'listenMqtt',
|
|
201
|
+
'logout',
|
|
202
|
+
'markAsDelivered',
|
|
203
|
+
'markAsRead',
|
|
204
|
+
'markAsReadAll',
|
|
205
|
+
'markAsSeen',
|
|
206
|
+
'muteThread',
|
|
207
|
+
'removeUserFromGroup',
|
|
208
|
+
'resolvePhotoUrl',
|
|
209
|
+
'searchForThread',
|
|
210
|
+
'sendMessage',
|
|
211
|
+
'sendTypingIndicator',
|
|
212
|
+
'setMessageReaction',
|
|
213
|
+
'setPostReaction',
|
|
214
|
+
'setTitle',
|
|
215
|
+
'threadColors',
|
|
216
|
+
'unsendMessage',
|
|
217
|
+
|
|
218
|
+
// HTTP
|
|
219
|
+
'httpGet',
|
|
220
|
+
'httpPost',
|
|
221
|
+
'httpPostFormData',
|
|
222
|
+
|
|
223
|
+
'uploadAttachment'
|
|
224
|
+
];
|
|
225
|
+
|
|
226
|
+
const defaultFuncs = utils.makeDefaults(html, i_userID || userID, ctx);
|
|
227
|
+
|
|
228
|
+
// Load all api functions in a loop
|
|
229
|
+
apiFuncNames.map(function (v) {
|
|
230
|
+
api[v] = require('./src/' + v)(defaultFuncs, api, ctx);
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
//Removing original `listen` that uses pull.
|
|
234
|
+
//Map it to listenMqtt instead for backward compatibly.
|
|
235
|
+
api.listen = api.listenMqtt;
|
|
236
|
+
|
|
237
|
+
return [ctx, defaultFuncs, api];
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function requestWithPromise(options) {
|
|
241
|
+
return new Promise(function( resolve, reject ) {
|
|
242
|
+
request(options, function ( error, response, body ) {
|
|
243
|
+
if ( error ) return reject( error )
|
|
244
|
+
try {
|
|
245
|
+
resolve( response )
|
|
246
|
+
} catch ( error ) {
|
|
247
|
+
reject( error )
|
|
248
|
+
}})
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function makeLogin(jar, email, password, loginOptions, callback, prCallback) {
|
|
253
|
+
return function (res, err) {
|
|
254
|
+
const html = res.body;
|
|
255
|
+
const $ = cheerio.load(html);
|
|
256
|
+
let arr = [];
|
|
257
|
+
|
|
258
|
+
// This will be empty, but just to be sure we leave it
|
|
259
|
+
$("#login_form input").map(function (i, v) {
|
|
260
|
+
arr.push({ val: $(v).val(), name: $(v).attr("name") });
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
arr = arr.filter(function (v) {
|
|
264
|
+
return v.val && v.val.length;
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
const form = utils.arrToForm(arr);
|
|
268
|
+
form.lsd = $('input[name=lsd]').val();
|
|
269
|
+
form.email = email;
|
|
270
|
+
form.pass = password;
|
|
271
|
+
form.locale = 'en_US';
|
|
272
|
+
form.timezone = '240';
|
|
273
|
+
|
|
274
|
+
// Getting cookies from the HTML page... (kill me now plz)
|
|
275
|
+
// we used to get a bunch of cookies in the headers of the response of the
|
|
276
|
+
// request, but FB changed and they now send those cookies inside the JS.
|
|
277
|
+
// They run the JS which then injects the cookies in the page.
|
|
278
|
+
// The "solution" is to parse through the html and find those cookies
|
|
279
|
+
// which happen to be conveniently indicated with a _js_ in front of their
|
|
280
|
+
// variable name.
|
|
281
|
+
//
|
|
282
|
+
// ---------- Very Hacky Part Starts -----------------
|
|
283
|
+
const willBeCookies = html.split("\"_js_");
|
|
284
|
+
willBeCookies.slice(1).map(function (val) {
|
|
285
|
+
const cookieData = JSON.parse("[\"" + utils.getFrom(val, "", "]") + "]");
|
|
286
|
+
jar.setCookie(utils.formatCookie(cookieData, "facebook"), "https://www.facebook.com");
|
|
287
|
+
});
|
|
288
|
+
// ---------- Very Hacky Part Ends -----------------
|
|
289
|
+
|
|
290
|
+
log.info("login", "Logging in...");
|
|
291
|
+
let loginPromise = requestWithPromise({
|
|
292
|
+
url: "https://www.facebook.com/login/",
|
|
293
|
+
method: 'POST',
|
|
294
|
+
jar: jar,
|
|
295
|
+
headers: {
|
|
296
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
297
|
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36',
|
|
298
|
+
'sec-fetch-site': 'same-origin',
|
|
299
|
+
'origin': 'https://www.facebook.com',
|
|
300
|
+
'referer': 'https://www.facebook.com/',
|
|
301
|
+
'upgrade-insecure-requests': '1',
|
|
302
|
+
},
|
|
303
|
+
form: form
|
|
304
|
+
});
|
|
305
|
+
return loginPromise.then((res, err) => {
|
|
306
|
+
const headers = res.headers;
|
|
307
|
+
if (!headers.location) {
|
|
308
|
+
throw { error: "Wrong username/password." };
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// This means the account has login approvals turned on.
|
|
312
|
+
if (headers.location.indexOf('https://www.facebook.com/checkpoint/') > -1) {
|
|
313
|
+
log.info("login", "You have login approvals turned on.");
|
|
314
|
+
const nextURL = 'https://www.facebook.com/checkpoint/?next=https%3A%2F%2Fwww.facebook.com%2Fhome.php';
|
|
315
|
+
|
|
316
|
+
return utils
|
|
317
|
+
.get(headers.location, jar, null, loginOptions)
|
|
318
|
+
.then(utils.saveCookies(jar))
|
|
319
|
+
.then(function (res) {
|
|
320
|
+
const html = res.body;
|
|
321
|
+
// Make the form in advance which will contain the fb_dtsg and nh
|
|
322
|
+
const $ = cheerio.load(html);
|
|
323
|
+
let arr = [];
|
|
324
|
+
$("form input").map(function (i, v) {
|
|
325
|
+
arr.push({ val: $(v).val(), name: $(v).attr("name") });
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
arr = arr.filter(function (v) {
|
|
329
|
+
return v.val && v.val.length;
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
const form = utils.arrToForm(arr);
|
|
333
|
+
if (html.indexOf("checkpoint/?next") > -1) {
|
|
334
|
+
setTimeout(() => {
|
|
335
|
+
checkVerified = setInterval((_form) => {
|
|
336
|
+
/* utils
|
|
337
|
+
.post("https://www.facebook.com/login/approvals/approved_machine_check/", jar, form, loginOptions, null, {
|
|
338
|
+
"Referer": "https://www.facebook.com/checkpoint/?next"
|
|
339
|
+
})
|
|
340
|
+
.then(utils.saveCookies(jar))
|
|
341
|
+
.then(res => {
|
|
342
|
+
try {
|
|
343
|
+
JSON.parse(res.body.replace(/for\s*\(\s*;\s*;\s*\)\s*;\s*()/, ""));
|
|
344
|
+
} catch (ex) {
|
|
345
|
+
clearInterval(checkVerified);
|
|
346
|
+
log.info("login", "Verified from browser. Logging in...");
|
|
347
|
+
return loginHelper(utils.getAppState(jar), email, password, loginOptions, callback);
|
|
348
|
+
}
|
|
349
|
+
})
|
|
350
|
+
.catch(ex => {
|
|
351
|
+
log.error("login", ex);
|
|
352
|
+
}); */
|
|
353
|
+
}, 5000, {
|
|
354
|
+
fb_dtsg: form.fb_dtsg,
|
|
355
|
+
jazoest: form.jazoest,
|
|
356
|
+
dpr: 1
|
|
357
|
+
});
|
|
358
|
+
}, 2500);
|
|
359
|
+
throw {
|
|
360
|
+
error: 'login-approval',
|
|
361
|
+
continue: function submit2FA(code) {
|
|
362
|
+
form.approvals_code = code;
|
|
363
|
+
form['submit[Continue]'] = $("#checkpointSubmitButton").html(); //'Continue';
|
|
364
|
+
let prResolve = null;
|
|
365
|
+
let prReject = null;
|
|
366
|
+
const rtPromise = new Promise(function (resolve, reject) {
|
|
367
|
+
prResolve = resolve;
|
|
368
|
+
prReject = reject;
|
|
369
|
+
});
|
|
370
|
+
if (typeof code == "string") {
|
|
371
|
+
utils
|
|
372
|
+
.post(nextURL, jar, form, loginOptions)
|
|
373
|
+
.then(utils.saveCookies(jar))
|
|
374
|
+
.then(function (res) {
|
|
375
|
+
const $ = cheerio.load(res.body);
|
|
376
|
+
const error = $("#approvals_code").parent().attr("data-xui-error");
|
|
377
|
+
if (error) {
|
|
378
|
+
throw {
|
|
379
|
+
error: 'login-approval',
|
|
380
|
+
errordesc: "Invalid 2FA code.",
|
|
381
|
+
lerror: error,
|
|
382
|
+
continue: submit2FA
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
})
|
|
386
|
+
.then(function () {
|
|
387
|
+
// Use the same form (safe I hope)
|
|
388
|
+
delete form.no_fido;
|
|
389
|
+
delete form.approvals_code;
|
|
390
|
+
form.name_action_selected = 'dont_save'; //'save_device';
|
|
391
|
+
|
|
392
|
+
return utils
|
|
393
|
+
.post(nextURL, jar, form, loginOptions)
|
|
394
|
+
.then(utils.saveCookies(jar));
|
|
395
|
+
})
|
|
396
|
+
.then(function (res) {
|
|
397
|
+
const headers = res.headers;
|
|
398
|
+
if (!headers.location && res.body.indexOf('Review Recent Login') > -1) {
|
|
399
|
+
throw { error: "Something went wrong with login approvals." };
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
const appState = utils.getAppState(jar);
|
|
403
|
+
|
|
404
|
+
if (callback === prCallback) {
|
|
405
|
+
callback = function (err, api) {
|
|
406
|
+
if (err) {
|
|
407
|
+
return prReject(err);
|
|
408
|
+
}
|
|
409
|
+
return prResolve(api);
|
|
410
|
+
};
|
|
411
|
+
}
|
|
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(function (err) {
|
|
418
|
+
// Check if using Promise instead of callback
|
|
419
|
+
if (callback === prCallback) {
|
|
420
|
+
prReject(err);
|
|
421
|
+
} else {
|
|
422
|
+
callback(err);
|
|
423
|
+
}
|
|
424
|
+
});
|
|
425
|
+
} else {
|
|
426
|
+
utils
|
|
427
|
+
.post("https://www.facebook.com/checkpoint/?next=https%3A%2F%2Fwww.facebook.com%2Fhome.php", jar, form, loginOptions, null, {
|
|
428
|
+
"Referer": "https://www.facebook.com/checkpoint/?next"
|
|
429
|
+
})
|
|
430
|
+
.then(utils.saveCookies(jar))
|
|
431
|
+
.then(res => {
|
|
432
|
+
try {
|
|
433
|
+
JSON.parse(res.body.replace(/for\s*\(\s*;\s*;\s*\)\s*;\s*/, ""));
|
|
434
|
+
} catch (ex) {
|
|
435
|
+
clearInterval(checkVerified);
|
|
436
|
+
log.info("login", "Verified from browser. Logging in...");
|
|
437
|
+
if (callback === prCallback) {
|
|
438
|
+
callback = function (err, api) {
|
|
439
|
+
if (err) {
|
|
440
|
+
return prReject(err);
|
|
441
|
+
}
|
|
442
|
+
return prResolve(api);
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
return loginHelper(utils.getAppState(jar), email, password, loginOptions, callback);
|
|
446
|
+
}
|
|
447
|
+
})
|
|
448
|
+
.catch(ex => {
|
|
449
|
+
log.error("login", ex);
|
|
450
|
+
if (callback === prCallback) {
|
|
451
|
+
prReject(ex);
|
|
452
|
+
} else {
|
|
453
|
+
callback(ex);
|
|
454
|
+
}
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
return rtPromise;
|
|
458
|
+
}
|
|
459
|
+
};
|
|
460
|
+
} else {
|
|
461
|
+
if (!loginOptions.forceLogin) {
|
|
462
|
+
throw { error: "Couldn't login. Facebook might have blocked this account. Please login with a browser or enable the option 'forceLogin' and try again." };
|
|
463
|
+
}
|
|
464
|
+
if (html.indexOf("Suspicious Login Attempt") > -1) {
|
|
465
|
+
form['submit[This was me]'] = "This was me";
|
|
466
|
+
} else {
|
|
467
|
+
form['submit[This Is Okay]'] = "This Is Okay";
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
return utils
|
|
471
|
+
.post(nextURL, jar, form, loginOptions)
|
|
472
|
+
.then(utils.saveCookies(jar))
|
|
473
|
+
.then(function () {
|
|
474
|
+
// Use the same form (safe I hope)
|
|
475
|
+
form.name_action_selected = 'save_device';
|
|
476
|
+
|
|
477
|
+
return utils
|
|
478
|
+
.post(nextURL, jar, form, loginOptions)
|
|
479
|
+
.then(utils.saveCookies(jar));
|
|
480
|
+
})
|
|
481
|
+
.then(function (res) {
|
|
482
|
+
const headers = res.headers;
|
|
483
|
+
|
|
484
|
+
if (!headers.location && res.body.indexOf('Review Recent Login') > -1) {
|
|
485
|
+
throw { error: "Something went wrong with review recent login." };
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
const appState = utils.getAppState(jar);
|
|
489
|
+
|
|
490
|
+
// Simply call loginHelper because all it needs is the jar
|
|
491
|
+
// and will then complete the login process
|
|
492
|
+
return loginHelper(appState, email, password, loginOptions, callback);
|
|
493
|
+
})
|
|
494
|
+
.catch(function (e) {
|
|
495
|
+
callback(e);
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
return utils
|
|
502
|
+
.get('https://www.facebook.com/', jar, null, loginOptions)
|
|
503
|
+
.then(utils.saveCookies(jar));
|
|
504
|
+
});
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
// Helps the login
|
|
509
|
+
function loginHelper(appState, email, password, globalOptions, callback, prCallback) {
|
|
510
|
+
let mainPromise = null;
|
|
511
|
+
const jar = utils.getJar();
|
|
512
|
+
|
|
513
|
+
// If we're given an appState we loop through it and save each cookie
|
|
514
|
+
// back into the jar.
|
|
515
|
+
if (appState) {
|
|
516
|
+
appState.map(function (c) {
|
|
517
|
+
const str = c.key + "=" + c.value + "; expires=" + c.expires + "; domain=" + c.domain + "; path=" + c.path + ";";
|
|
518
|
+
jar.setCookie(str, "http://" + c.domain);
|
|
519
|
+
});
|
|
520
|
+
|
|
521
|
+
// Load the main page.
|
|
522
|
+
mainPromise = utils
|
|
523
|
+
.get('https://www.facebook.com/', jar, null, globalOptions, { noRef: true })
|
|
524
|
+
.then(utils.saveCookies(jar));
|
|
525
|
+
} else {
|
|
526
|
+
// Open the main page, then we login with the given credentials and finally
|
|
527
|
+
// load the main page again (it'll give us some IDs that we need)
|
|
528
|
+
mainPromise = requestWithPromise({
|
|
529
|
+
method: 'GET',
|
|
530
|
+
url: "https://www.facebook.com",
|
|
531
|
+
jar: true,
|
|
532
|
+
headers: {
|
|
533
|
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36'
|
|
534
|
+
}
|
|
535
|
+
})
|
|
536
|
+
.then(makeLogin(jar, email, password, globalOptions, callback, prCallback))
|
|
537
|
+
.then(function () {
|
|
538
|
+
return utils
|
|
539
|
+
.get('https://www.facebook.com/', jar, null, globalOptions)
|
|
540
|
+
.then(utils.saveCookies(jar));
|
|
541
|
+
});
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
let ctx = null;
|
|
545
|
+
let _defaultFuncs = null;
|
|
546
|
+
let api = null;
|
|
547
|
+
|
|
548
|
+
mainPromise = mainPromise
|
|
549
|
+
.then(function (res) {
|
|
550
|
+
// Hacky check for the redirection that happens on some ISPs, which doesn't return statusCode 3xx
|
|
551
|
+
const reg = /<meta http-equiv="refresh" content="0;url=([^"]+)[^>]+>/;
|
|
552
|
+
const redirect = reg.exec(res.body);
|
|
553
|
+
if (redirect && redirect[1]) {
|
|
554
|
+
return utils
|
|
555
|
+
.get(redirect[1], jar, null, globalOptions)
|
|
556
|
+
.then(utils.saveCookies(jar));
|
|
557
|
+
}
|
|
558
|
+
return res;
|
|
559
|
+
})
|
|
560
|
+
.then(function (res) {
|
|
561
|
+
const html = res.body;
|
|
562
|
+
const stuff = buildAPI(globalOptions, html, jar);
|
|
563
|
+
ctx = stuff[0];
|
|
564
|
+
_defaultFuncs = stuff[1];
|
|
565
|
+
api = stuff[2];
|
|
566
|
+
return res;
|
|
567
|
+
});
|
|
568
|
+
|
|
569
|
+
// given a pageID we log in as a page
|
|
570
|
+
if (globalOptions.pageID) {
|
|
571
|
+
mainPromise = mainPromise
|
|
572
|
+
.then(function () {
|
|
573
|
+
return utils
|
|
574
|
+
.get('https://www.facebook.com/' + ctx.globalOptions.pageID + '/messages/?section=messages&subsection=inbox', ctx.jar, null, globalOptions);
|
|
575
|
+
})
|
|
576
|
+
.then(function (resData) {
|
|
577
|
+
let url = utils.getFrom(resData.body, 'window.location.replace("https:\\/\\/www.facebook.com\\', '");').split('\\').join('');
|
|
578
|
+
url = url.substring(0, url.length - 1);
|
|
579
|
+
|
|
580
|
+
return utils
|
|
581
|
+
.get('https://www.facebook.com' + url, ctx.jar, null, globalOptions);
|
|
582
|
+
});
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
// At the end we call the callback or catch an exception
|
|
586
|
+
mainPromise
|
|
587
|
+
.then(function () {
|
|
588
|
+
log.info("login", 'Logging Done Brother/Sister Enjoy Mohammad Nayan Api.');
|
|
589
|
+
return callback(null, api);
|
|
590
|
+
})
|
|
591
|
+
.catch(function (e) {
|
|
592
|
+
log.error("login", e.error || e);
|
|
593
|
+
callback(e);
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
function login(loginData, options, callback) {
|
|
598
|
+
if (utils.getType(options) === 'Function' || utils.getType(options) === 'AsyncFunction') {
|
|
599
|
+
callback = options;
|
|
600
|
+
options = {};
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
const globalOptions = {
|
|
604
|
+
selfListen: false,
|
|
605
|
+
selfListenEvent: false,
|
|
606
|
+
listenEvents: false,
|
|
607
|
+
listenTyping: false,
|
|
608
|
+
updatePresence: false,
|
|
609
|
+
forceLogin: false,
|
|
610
|
+
autoMarkDelivery: true,
|
|
611
|
+
autoMarkRead: false,
|
|
612
|
+
autoReconnect: true,
|
|
613
|
+
logRecordSize: defaultLogRecordSize,
|
|
614
|
+
online: true,
|
|
615
|
+
emitReady: false,
|
|
616
|
+
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"
|
|
617
|
+
};
|
|
618
|
+
|
|
619
|
+
setOptions(globalOptions, options);
|
|
620
|
+
|
|
621
|
+
let prCallback = null;
|
|
622
|
+
if (utils.getType(callback) !== "Function" && utils.getType(callback) !== "AsyncFunction") {
|
|
623
|
+
let rejectFunc = null;
|
|
624
|
+
let resolveFunc = null;
|
|
625
|
+
var returnPromise = new Promise(function (resolve, reject) {
|
|
626
|
+
resolveFunc = resolve;
|
|
627
|
+
rejectFunc = reject;
|
|
628
|
+
});
|
|
629
|
+
prCallback = function (error, api) {
|
|
630
|
+
if (error) {
|
|
631
|
+
return rejectFunc(error);
|
|
632
|
+
}
|
|
633
|
+
return resolveFunc(api);
|
|
634
|
+
};
|
|
635
|
+
callback = prCallback;
|
|
636
|
+
}
|
|
637
|
+
loginHelper(loginData.appState, loginData.email, loginData.password, globalOptions, callback, prCallback);
|
|
638
|
+
return returnPromise;
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
module.exports = login;
|