-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapi.js
491 lines (415 loc) · 14.6 KB
/
api.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
const puppeteer = require('puppeteer')
const atob = require('atob')
const Queue = require('queue')
const mqttParser = require('mqtt-packet').parser
const Order = Symbol('Order')
module.exports = class {
constructor (options) {
this.options = {
session: null,
selfListen: false,
workerLimit: 3,
debug: false,
...(options || {})
}
this._browser = null // Puppeteer instance
this._masterPage = null // Holds the master page
this._workerPages = [] // Holds the worker pages
this._listenFnIsSetUp = false
this._listenFns = []
this._listenRawFns = null // Begin as null, changes to [] when primed
this._aliasMap = {} // Maps user handles to IDs
this.uid = null // Holds the user's ID when authenticated
// Handle new messages sequentially
this._messageQueueIncoming = Queue({
autostart: true,
concurrency: 1,
timeout: 1000
})
// Worker thread queue
this._actionQueueOutgoing = {
[Order]: []
}
}
threadHandleToID (handle) {
// FIXME: Should this be ID to Handle???
// Received messages contain the ID
// Outgoing messages get changed to the handle
// But if a user changes their username, the cache will be wrong
return this._aliasMap[handle] || handle
}
async _delegate (thread, fn) {
this.options.debug && console.debug('Received function ', fn, thread)
if (!thread) throw new Error('No thread target')
thread = thread.toString()
let _resolve
const promise = new Promise(resolve => {
_resolve = resolve
})
const pushQueue = (workerObj, fn) => {
this.options.debug &&
console.debug('Pushing function to worker thread', workerObj.id)
workerObj.queue.push(async finish => {
this.options.debug && console.debug('Executing function (finally)')
workerObj.active = true
workerObj.lastActivity = new Date()
_resolve(await fn.apply(workerObj.page))
finish()
})
}
const replaceWorker = async (workerObj, newThread, hookFn) => {
this.options.debug &&
console.debug('Replacing worker thread queue', workerObj.id)
workerObj.thread = null
workerObj.queue.autostart = false
hookFn && (await hookFn())
await this._setTarget(workerObj.page, newThread)
workerObj.thread = newThread
workerObj.queue.start()
workerObj.queue.autostart = true
}
const target = this._workerPages.find(
workerObj => this.threadHandleToID(thread) === workerObj.thread
)
if (target) {
this.options.debug &&
console.debug('Existing worker thread found, pushing')
// Push new action to target worker queue
pushQueue(target, fn)
} else {
this.options.debug && console.debug('Target worker thread not found')
// Queue new action if there are no free workers
if (this._workerPages.length >= this.options.workerLimit) {
const freeTarget = this._workerPages
.filter(workerObj => !workerObj.active)
.sort((a, b) => a.lastActivity > b.lastActivity)
.shift()
if (freeTarget) {
replaceWorker(freeTarget, thread, async () =>
pushQueue(freeTarget, fn)
)
} else {
this.options.debug && console.debug('Reached worker thread capacity')
if (thread in this._actionQueueOutgoing) {
this.options.debug &&
console.debug('Adding function to existing queue')
this._actionQueueOutgoing[thread].push(fn)
} else {
this.options.debug && console.debug('Creating new function queue')
this._actionQueueOutgoing[thread] = [fn]
this._actionQueueOutgoing[Order].push(thread)
}
}
} else {
this.options.debug && console.debug('Spawning new worker')
// Create a new worker if there is an empty worker slot
const target = {
thread,
active: true,
lastActivity: new Date(),
queue: Queue({
autostart: false, // Do not start queue until the new page is ready
concurrency: 1,
timeout: 2000
}),
id: this._workerPages.length
}
pushQueue(target, fn)
this._workerPages.push(target)
// Attach page
const page = await this._browser.newPage()
await this._setTarget(page, thread)
target.page = page
// Handle worker replacement
target.queue.on('end', async () => {
this.options.debug && console.debug('Worker finished tasks')
target.active = false
const next = this._actionQueueOutgoing[Order].shift()
if (!next) return
await replaceWorker(target, next, async () => {
const outgoingQueue = this._actionQueueOutgoing[next]
delete this._actionQueueOutgoing[next]
outgoingQueue.forEach(fn => pushQueue(target, fn))
})
})
// Enable queue
target.queue.start()
target.queue.autostart = true
}
}
return promise
}
async getSession () {
return this._masterPage.cookies()
}
async login (email, password) {
return new Promise(async (resolve, reject) => {
this.options.debug && console.log('Logging in...')
const browser = (this._browser = await puppeteer.launch({
headless: !this.options.debug
}))
const page = (this._masterPage = (await browser.pages())[0]) // await browser.newPage())
if (this.options.session) {
await page.setCookie(...this.options.session)
}
// await page.setUserAgent("Mozilla/5.0 (Android 7.0; Mobile; rv:54.0) Gecko/54.0 Firefox/54.0")
// Go to the login page
await page.goto('https://m.facebook.com/login.php', {
waitUntil: 'networkidle2'
})
// If there's a session (from cookie), then skip login
let authFail = false
if (page.url().startsWith('https://m.facebook.com/login.php')) {
await (async (cb, ...items) =>
Promise.all(items.map(q => page.$(q))).then(r => cb(...r)))(
async (emailField, passwordField, submitButton) => {
// Looks like we're unauthenticated
await emailField.type(email)
await passwordField.type(password)
let navigationPromise = page.waitForNavigation()
page.$eval('button[name=login]', elem => elem.click())
setTimeout(async () => {
if (
page.url().startsWith('https://m.facebook.com/login.php') &&
(await Promise.all(
[
'//div[contains(text(), "find account")]',
'//div[contains(text(), "Need help with finding your account?")]',
'//div[contains(text(), "The password that you entered is incorrect")]',
'//div[contains(text(), "Incorrect password")]'
].map(xPath => page.$x(xPath))
).then(r => r.flat().length > 0))
) {
authFail = true
await this.close()
reject(new Error('Bad credentials'))
}
}, 3000)
await navigationPromise.catch(() => {})
},
'input[name=email]',
'input[name=pass]',
'button[name=login]'
)
}
if (!authFail) {
await page.goto('https://m.facebook.com/messages', {
waitUntil: 'networkidle2'
})
// String
this.uid = (await this.getSession()).find(
cookie => cookie.name === 'c_user'
).value
this.options.debug && console.log(`Logged in as ${this.uid}`)
resolve(this)
}
})
}
getCurrentUserID () {
/* String */
return this.uid
}
async _setTarget (page, target) {
target = target.toString()
const threadPrefix = 'https://m.facebook.com/messages/read/?tid='
let slug = page.url().substr(threadPrefix.length)
if (target === this.threadHandleToID(slug)) {
return null
}
const response = await page.goto(`${threadPrefix}${target}`, {
waitUntil: 'networkidle2'
})
slug = page.url().substr(threadPrefix.length)
this._aliasMap[slug] = target
return response
}
async sendMessage (target, data) {
if (typeof data === 'number') {
data = data.toString()
} else if (typeof data === 'function') {
data = await data()
}
this._delegate(target, async function () {
const inputElem = await this.$('[placeholder="Write a message..."]')
await inputElem.type(data)
await this.$eval('button[name=send]', elem => elem.click())
})
}
_stopListen (optionalCallback) {
const client = this._masterPage._client
if (typeof optionalCallback === 'function') {
client.off('Network.webSocketFrameReceived', optionalCallback)
this._listenRawFns = this._listenRawFns.filter(
callback => callback !== optionalCallback
)
} else {
for (const callback of this._listenRawFns) {
client.off('Network.webSocketFrameReceived', callback)
}
this._listenRawFns = []
}
}
listen (callback) {
// When first called, create the data transformer
if (!this._listenFnIsSetUp) {
this._listenFnIsSetUp = true
this.listenRaw(async rawData => {
let ret = {}
switch (rawData.class) {
case 'ReadReceipt':
case 'MarkFolderSeen':
case 'NoOp':
case 'FolderCount':
case 'ThreadFolder':
return
case 'AdminTextMessage':
// Theme, emoji, nickname
// TODO: Group add remove?
// .type: string
// .untypedData: any
// .messageMetadata: any
// ignore if type === 'change_thread_theme
return
case 'MessageDelete':
// Local message removal, don't care as much
// ret = {
// type: 'message_unsend',
// messageId: rawData.messageIds[0]
// }
/*
{
actorFbId: '...',
attachments: [],
irisSeqId: '...',
messageIds: [ 'mid.....' ],
requestContext: { apiArgs: {} },
threadKey: { otherUserFbId: '...' },
class: 'MessageDelete'
}
*/
return
case 'NewMessage':
if (
rawData.messageMetadata.actorFbId === this.uid &&
!this.options.selfListen
) {
return
}
ret = {
type: 'message',
body: rawData.body || '',
thread: Number(
Object.values(rawData.messageMetadata.threadKey)[0]
),
sender: Number(rawData.messageMetadata.actorFbId),
timestamp: rawData.messageMetadata.timestamp,
messageId: rawData.messageMetadata.messageId,
attachments: rawData.attachments
}
break
case 'ClientPayload':
let clientPayload = JSON.parse(
Buffer.from(rawData.payload).toString()
)
// FIXME: DEBUG ONLY
if (
Object.keys(clientPayload).filter(v => v != 'deltas').length > 0
) {
this.options.debug &&
console.debug(
'Extra keys',
Object.keys(clientPayload),
'Extra keys'
)
}
if (clientPayload.deltas && clientPayload.deltas.length > 1) {
this.options.debug &&
console.debug(
'Several deltas',
clientPayload.deltas,
'Several deltas'
)
}
let deltaType = Object.keys(clientPayload.deltas[0])[0]
let delta = clientPayload.deltas[0][deltaType]
this.options.debug && console.debug(deltaType, delta)
switch (deltaType) {
case 'deltaRecallMessageData':
ret = {
type: 'message_unsend',
thread: Object.values(delta.threadKey)[0],
messageId: delta.messageID,
timestamp: delta.deletionTimestamp
}
break
default:
this.options.debug && console.debug(deltaType, delta)
}
break
// { deltas: [ { deltaMessageReply: [Object] } ] }
// { deltas: [ { deltaMessageReaction: [Object] } ] }
// { deltas: [ { deltaUpdateThreadTheme: [Object] } ] }
// { deltas: [ { deltaRecallMessageData: [Object] } ] }
default:
this.options.debug &&
console.log(rawData.class, rawData, rawData.class, '\n')
return
}
for (let callback of this._listenFns) {
callback(ret)
}
})
}
this._listenFns.push(callback)
}
listenRaw (callback) {
if (this._listenRawFns === null) {
this._listenRawFns = []
let parser = mqttParser({ protocolVersion: 4 })
parser.on('packet', ({ topic, payload }) => {
if (topic !== '/t_ms') return
let json = JSON.parse(payload)
if (!json.deltas) return
for (let delta of json.deltas) {
for (const callback of this._listenRawFns) {
this._messageQueueIncoming.push(async finish => {
await callback(delta)
finish()
})
}
}
})
this._masterPage._client.on(
'Network.webSocketFrameReceived',
async ({ timestamp, response: { payloadData } }) => {
// FIXME: Only parse if longer than ???
payloadData.length > 8 &&
parser.parse(Buffer.from(payloadData, 'base64'))
}
)
}
if (this._listenRawFns.indexOf(callback) === -1) {
this._listenRawFns.push(callback)
}
return () => this._stopListen(callback)
}
async sendImage (target, imagePathOrImagePaths) {
if (!imagePathOrImagePaths) return
const images = Array.isArray(imagePathOrImagePaths)
? imagePathOrImagePaths
: Array(imagePathOrImagePaths)
return this._delegate(target, async function () {
for (const imagePath of images) {
let uploadBtn = await this.$(
'input[type=file][data-sigil="m-raw-file-input"]'
)
await uploadBtn.uploadFile(imagePath)
}
await this.waitForSelector('button[name=send]:not([disabled])')
await this.$eval('button[name=send]', elem => elem.click())
})
}
async close () {
return this._browser.close()
}
}