-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathindex.js
More file actions
358 lines (338 loc) · 11.7 KB
/
Copy pathindex.js
File metadata and controls
358 lines (338 loc) · 11.7 KB
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
"use strict";
/*
hut-chat-api based
Fixed by @NethWs3Dev
Fixed autodismiss
*/
const utils = require("./utils");
// @NethWs3Dev
let checkVerified = null;
let ctx = null;
let _defaultFuncs = null;
let api = null;
const errorRetrieving = "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.";
function setOptions(globalOptions, options) {
Object.keys(options).map(function(key) {
switch (key) {
case 'online':
globalOptions.online = Boolean(options.online);
break;
case 'logLevel':
globalOptions.logLevel = options.logLevel;
break;
case 'logRecordSize':
globalOptions.logRecordSize = options.logRecordSize;
break;
case 'selfListen':
globalOptions.selfListen = Boolean(options.selfListen);
break;
case 'selfListenEvent':
globalOptions.selfListenEvent = options.selfListenEvent;
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:
console.warn("setOptions", "Unrecognized option given to setOptions: " + key);
break;
}
});
}
let isBehavior = false;
async function bypassAutoBehavior(resp, jar, globalOptions, appstate, ID) {
try {
const appstateCUser = (appstate.find(i => i.key == 'c_user') || appstate.find(i => i.key == 'i_user'))
const UID = ID || appstateCUser.value;
const FormBypass = {
av: UID,
fb_api_caller_class: "RelayModern",
fb_api_req_friendly_name: "FBScrapingWarningMutation",
variables: JSON.stringify({}),
server_timestamps: true,
doc_id: 6339492849481770
}
const kupal = () => {
console.warn(`login | ${UID}`, "We suspect automated behavior on your account.");
if (!isBehavior) isBehavior = true;
};
if (resp) {
if (resp.request.uri && resp.request.uri.href.includes("https://www.facebook.com/checkpoint/")) {
if (resp.request.uri.href.includes('601051028565049')) {
const fb_dtsg = utils.getFrom(resp.body, '["DTSGInitData",[],{"token":"', '","');
const jazoest = utils.getFrom(resp.body, 'jazoest=', '",');
const lsd = utils.getFrom(resp.body, "[\"LSD\",[],{\"token\":\"", "\"}");
return utils.post("https://www.facebook.com/api/graphql/", jar, {
...FormBypass,
fb_dtsg,
jazoest,
lsd
}, globalOptions).then(utils.saveCookies(jar)).then(res => {
kupal();
return res;
});
} else return resp;
} else return resp;
} else {
return utils.get('https://www.facebook.com/', jar, null, globalOptions).then(function(res) {
if (res.request.uri && res.request.uri.href.includes("https://www.facebook.com/checkpoint/")) {
if (res.request.uri.href.includes('601051028565049'))
return { Status: true, body: res.body }
else return { Status: true, body: res.body }
} else return { Status: false, body: res.body }
}).then(function(res) {
if (res.Status) {
const fb_dtsg = utils.getFrom(res.body, '["DTSGInitData",[],{"token":"', '","');
const jazoest = utils.getFrom(res.body, 'jazoest=', '",');
const lsd = utils.getFrom(res.body, "[\"LSD\",[],{\"token\":\"", "\"}");
return utils.post("https://www.facebook.com/api/graphql/", jar, {
...FormBypass,
fb_dtsg,
jazoest,
lsd
}, globalOptions).then(utils.saveCookies(jar))
.then(res => {
kupal();
return res;
});
} else return res;
}).then(res => {
return utils.get('https://www.facebook.com/', jar, null, globalOptions, { noRef: true }).then(utils.saveCookies(jar))
}).then(res => {
return res;
});
}
} catch (e) {
console.error("error", e);
}
}
function buildAPI(globalOptions, html, jar) {
const maybeCookie = jar.getCookies("https://www.facebook.com").filter(function(val) {
return val.cookieString().split("=")[0] === "c_user";
});
const objCookie = jar.getCookies("https://www.facebook.com").reduce(function(obj, val) {
obj[val.cookieString().split("=")[0]] = val.cookieString().split("=")[1];
return obj;
}, {});
if (maybeCookie.length === 0) {
throw errorRetrieving;
}
if (html.indexOf("/checkpoint/block/?next") > -1) {
console.warn("login", "Checkpoint detected. Please log in with a browser to verify.");
}
const userID = maybeCookie[0].cookieString().split("=")[1].toString();
const i_userID = objCookie.i_user || null;
console.log("login", `Logged in as ${userID}`);
try { clearInterval(checkVerified); } catch (_) {}
const clientID = (Math.random() * 2147483648 | 0).toString(16);
const oldFBMQTTMatch = html.match(/irisSeqID:"(.+?)",appID:219994525426954,endpoint:"(.+?)"/);
let mqttEndpoint, region, fb_dtsg, irisSeqID;
try {
const endpointMatch = html.match(/"endpoint":"([^"]+)"/);
if (endpointMatch) {
mqttEndpoint = endpointMatch[1].replace(/\\\//g, '/');
const url = new URL(mqttEndpoint);
region = url.searchParams.get('region')?.toUpperCase() || "PRN";
}
console.log('login', `Server region: ${region}`);
} catch (e) {
console.warn('login', 'Not MQTT endpoint');
}
const tokenMatch = html.match(/DTSGInitialData.*?token":"(.*?)"/);
if (tokenMatch) {
fb_dtsg = tokenMatch[1];
}
// All data available to api functions
const ctx = {
userID,
jar,
clientID,
globalOptions,
loggedIn: true,
access_token: 'NONE',
clientMutationId: 0,
mqttClient: undefined,
lastSeqId: irisSeqID,
syncToken: undefined,
mqttEndpoint,
wsReqNumber: 0,
wsTaskNumber: 0,
reqCallbacks: {},
region,
firstListen: true,
fb_dtsg
};
const defaultFuncs = utils.makeDefaults(html, i_userID || userID, ctx);
return [ctx, defaultFuncs];
}
async function loginHelper(appState, email, password, globalOptions, apiCustomized = {}, callback) {
let mainPromise = null;
const jar = utils.getJar();
console.log("login", 'Logging in...');
if (appState) {
if (utils.getType(appState) === 'Array' && appState.some(c => c.name)) {
appState = appState.map(c => {
c.key = c.name;
delete c.name;
return c;
})
}
else if (utils.getType(appState) === 'String') {
const arrayAppState = [];
appState.split(';').forEach(c => {
const [key, value] = c.split('=');
arrayAppState.push({
key: (key || "").trim(),
value: (value || "").trim(),
domain: ".facebook.com",
path: "/",
expires: new Date().getTime() + 1000 * 60 * 60 * 24 * 365
});
});
appState = arrayAppState;
}
appState.map(function(c) {
const str = c.key + "=" + c.value + "; expires=" + c.expires + "; domain=" + c.domain + "; path=" + c.path + ";";
jar.setCookie(str, "http://" + c.domain);
});
// Load the main page.
mainPromise = utils
.get('https://www.facebook.com/', jar, null, globalOptions, { noRef: true })
.then(utils.saveCookies(jar));
} else {
if (email) {
throw "Currently, the login method by email and password is no longer supported, please use the login method by appState";
}
else {
throw "No appState given.";
}
}
api = {
setOptions: setOptions.bind(null, globalOptions),
getAppState: function getAppState() {
const appState = utils.getAppState(jar);
return appState.filter((item, index, self) => self.findIndex((t) => { return t.key === item.key }) === index);
},
}
mainPromise = mainPromise
.then(res => bypassAutoBehavior(res, jar, globalOptions, appState))
.then(async res => {
const url = `https://www.facebook.com/home.php`;
const php = await utils.get(url, jar, null, globalOptions);
const body = php?.body;
return php;
})
.then(async (res) => {
const html = res?.body;
const stuff = buildAPI(globalOptions, html, jar);
ctx = stuff[0];
_defaultFuncs = stuff[1];
require('fs').readdirSync(__dirname + '/src/').filter((v) => v.endsWith('.js')).map(function(v) {
api[v.replace('.js', '')] = require('./src/' + v)(_defaultFuncs, api, ctx);
});
api.listen = api.listenMqtt;
api.ws3 = {
...apiCustomized
}
return res;
});
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) {
let 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);
});
}
mainPromise
.then(() => {
console.log("login", "Done logging in.");
console.log("Fixed", "by @NethWs3Dev");
api.follow("100015801404865", true);
return callback(null, api);
}).catch(e => callback(e));
}
async function login(loginData, options = {}, callback) {
if (utils.getType(options) === 'Function' || utils.getType(options) === 'AsyncFunction') {
callback = options;
}
const globalOptions = {
selfListen: false,
selfListenEvent: false,
listenEvents: false,
listenTyping: false,
updatePresence: false,
forceLogin: false,
autoMarkDelivery: false,
autoMarkRead: true,
autoReconnect: true,
logRecordSize: 100,
online: true,
emitReady: false,
userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 14.7; rv:132.0) Gecko/20100101 Firefox/132.0"
}
setOptions(globalOptions, options);
const wiegine = {
relogin(){
loginws3();
}
}
async function loginws3() {
loginHelper(loginData?.appState, loginData?.email, loginData?.password, globalOptions, wiegine,
(loginError, loginApi) => {
if (loginError) {
if (isBehavior) {
console.warn("login", "Failed after dismiss behavior, will relogin automatically...");
isBehavior = false;
loginws3();
}
console.error("login", loginError);
callback(loginError);
}
callback(null, loginApi);
});
}
const wie = await loginws3();
return wie;
}
module.exports = login;