forked from paulallen87/chaturbate-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
581 lines (498 loc) · 12.8 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
'use strict';
const fs = require('fs');
const path = require('path');
const querystring = require('querystring');
const debug = require('debug')('chaturbate:browser');
const {EventEmitter} = require('events');
const chromeLauncher = require('lighthouse/chrome-launcher/chrome-launcher');
const chromeRemoteInterface = require('chrome-remote-interface');
const {Console} = require('console');
const logging = new Console(process.stdout, process.stderr);
const SERVER_URL = 'https://chaturbate.com';
const PROFILE_PATCH_PREFIX = '##PROFILE--';
/**
* Default browser config options.
*
* @type {Object}
*/
const DEFAULT_CONFIG = {
/** The port used by the chrome debugger. */
port: undefined,
/** The proxy server used by the browser. */
proxyServer: undefined,
/** The chaturbate server to connect to. */
server: SERVER_URL,
};
/**
* Default browser flags.
*
* @type {Array<string>}
*/
const CHROME_FLAGS = [
'--new',
'--args',
'--start-maximized',
'--disable-save-password-bubble',
'--disable-presentation-api',
'--disable-file-system',
'--disable-contextual-search',
'--disable-account-consistency',
'--disable-translate',
'--disable-background-mode',
'--disable-plugins-discovery',
'--disable-webgl',
'--disable-webgl-image-chromium',
'--disable-speech-api',
'--disable-smart-virtual-keyboard',
'--disable-print-preview',
'--disable-password-generation',
'--disable-overlay-scrollbar',
'--disable-offer-upload-credit-cards',
'--disable-ntp-popular-sites',
'--disable-cloud-import',
'--disable-component-cloud-policy',
'--disable-credit-card-scan',
'--disable-bundled-ppapi-flash',
'--disable-java',
'--disable-plugins',
'--disable-ipv6',
'--disable-people-search',
'--disable-default-apps',
'--incognito',
'--disable-sync',
'--disable-sync-backup',
'--disable-sync-rollback',
'--disable-sync-app-list',
'--disable-sync-types',
// '--enable-sandbox',
// '--enable-sandbox-logging',
'--isolate-extensions',
'--isolate-sites-for-testing=*',
// '--process-per-tab',
// '--process-per-site',
'--safe-plugins',
'--disable-gpu',
'--mute-audio',
'--no-zygote',
'--no-sandbox',
'--single-process',
'--headless',
];
/**
* Parses Chaturbate websocket message arguments into native
* javascript objects.
*
* @param {string} arg
* @return {*}
* @private
*/
const _parseArg = (arg) => {
if (arg === 'true' || arg === 'false') {
return arg === 'true';
}
if (!isNaN(arg) && arg !== '') {
return Number(arg);
}
if (arg.startsWith('{')) {
return JSON.parse(arg);
}
return arg;
};
/**
* Browser wrapper for interacting with Chaturbate.
*/
class ChaturbateBrowser extends EventEmitter {
/**
* Constructor.
*
* @param {Object} config
* @extends EventEmitter
* @constructor
*/
constructor(config = {}) {
super();
this.config = Object.assign({}, DEFAULT_CONFIG, config);
this.chrome = null;
this.protocol = null;
}
/**
* Launches the Chrome web browser.
*
* @return {Object} The chrome object that represents the browser process.
* @private
*/
_launchChrome() {
const flags = CHROME_FLAGS.slice();
if (this.config.proxyServer) {
flags.push(`--proxy-server=${this.config.proxyServer}`);
flags.push('--host-resolver-rules="MAP * 0.0.0.0, EXCLUDE localhost"');
}
return chromeLauncher.launch({
chromeFlags: flags,
port: this.config.port,
});
}
/**
* Launches the browser and connects the debugger.
*/
async start() {
debug('starting chrome...');
this.chrome = await this._launchChrome();
debug('starting remote debugging...');
this.protocol = await chromeRemoteInterface({port: this.chrome.port});
process.on('exit', () => this.stop());
process.on('SIGTERM', () => this.stop());
process.on('uncaughtException', (e) => this.stop(e));
debug('enabling debugging domains...');
await [
this.protocol.Page.enable(),
this.protocol.Runtime.enable(),
this.protocol.Network.enable(),
];
debug('adding event listeners...');
this.protocol.Page.loadEventFired(() => this._onPageLoad());
this.protocol.Runtime.consoleAPICalled((params) => {
return this._onConsoleAPICalled(params);
});
}
/**
* Executes a callback and waited for the page to load.
*
* @param {Function} cb
* @return {*}
*/
async wait(cb) {
const pagePromise = this._getPageLoadPromise();
const result = await cb.call(this);
await Promise.resolve(pagePromise);
return result;
}
/**
* Navigates to a URL.
*
* @param {string} url
* @return {*}
*/
goto(url) {
return this.wait(() => {
return this.protocol.Page.navigate({
url: `${this.config.server}/${url}`,
});
});
}
/**
* Gets a list of cookies.
*
* @return {Array<Object>}
*/
async cookies() {
const response = await this.protocol.Network.getCookies();
return response.cookies;
}
/**
* Checks the current login session.
*
* @return {boolean}
*/
async session() {
const cookies = await this.cookies();
const sessionCookie = cookies.find((cookie) => {
return cookie.name === 'sessionid' &&
cookie.session &&
Boolean(cookie.value);
});
return Boolean(sessionCookie);
}
/**
* Performs a login as a user.
*
* @param {string} username
* @param {string} password
* @return {boolean}
*/
async login(username, password) {
debug(`logging in as '${username}'...`);
await this.goto('auth/login/');
await this.wait(async() => {
await this._evaluateScript('login', {
'LOGIN_PASSWORD': password,
'LOGIN_USERNAME': username,
});
});
const session = await this.session();
if (session) {
debug(`logged in`);
this.emit('login', username);
return true;
}
logging.error('login failed');
return false;
}
/**
* Requests a token purchase with Bitcoin.
*
* @param {*} amount
* @return {Object}
*/
async purchase(amount) {
debug(`requesting ${amount} tokens...`);
await this.goto('tipping/purchase_tokens/');
await this.wait(async() => {
await this._evaluateScript('payment', {
'PAYMENT_AMOUNT': amount,
});
});
const results = await this._evaluateScript('bitcoin');
this.emit('purchase', results);
return results;
}
/**
* Navigates the browser to a user's profile.
*
* @param {string} username
*/
async profile(username) {
debug(`loading profile for '${username}'...`);
await this.goto(`${username}/`);
await this._evaluateScript('profile', {
'PATCH_PREFIX': PROFILE_PATCH_PREFIX,
});
this.emit('profile', username);
}
/**
* Stops the remote dubugger and kills the Chrome process.
*
* @param {Error=} e
*/
stop(e) {
if (e) {
debug('Stopping because of error');
logging.error(e);
}
if (this.protocol) {
debug('stopping remote debugging...');
this.protocol.close();
this.protocol = null;
}
if (this.chrome) {
debug('stopping chrome...');
this.chrome.kill();
this.chrome = null;
}
}
/**
* Creates a promise for the next page load.
*
* @return {Promise}
* @private
*/
_getPageLoadPromise() {
return new Promise((resolve, reject) => {
this.once('page_load', () => resolve());
});
}
/**
* Called when the browser successfully loads a page.
*
* @private
*/
_onPageLoad() {
debug('onPageLoad');
this.emit('page_load');
}
/**
* Called when a console message is intercepted.
*
* @param {Object} params
* @private
*/
_onConsoleAPICalled(params) {
const EXPECTED_LENGTH = 1;
const FIRST_ARG = 0;
debug(params);
if (params.type.toUpperCase() !== 'DEBUG') return;
if (params.args.length !== EXPECTED_LENGTH) return;
const arg = params.args[FIRST_ARG].value;
if (!arg.startsWith(PROFILE_PATCH_PREFIX)) return;
const json = arg.slice(PROFILE_PATCH_PREFIX.length);
const message = JSON.parse(json);
this._onProfileMessage(message);
}
/**
* Called when a message is received.
*
* @param {Object} message
* @private
*/
_onProfileMessage(message) {
debug(`received profile message: ${message.type}`);
switch (message.type) {
case 'init':
this._onProfileInit(message.payload);
return;
case 'websocket_hooked':
this._onProfileWebsocketHooked(message.payload);
return;
case 'websocket_open':
this._onProfileWebsocketOpen();
return;
case 'websocket_message':
this._onProfileWebsocketMessage(message.payload);
return;
case 'websocket_error':
this._onProfileWebsocketError(message.payload);
return;
case 'websocket_close':
this._onProfileWebsocketClose(message.payload);
return;
default:
debug('unknown profile message');
}
}
/**
* Called when the profile patch is initialized.
*
* @param {Object} payload
* @private
*/
_onProfileInit(payload) {
debug(`profile initialized`);
const result = {
chatSettings: JSON.parse(payload.chatSettings),
csrftoken: payload.csrftoken,
hasPlayer: payload.hasPlayer,
hasWebsocket: payload.hasWebsocket,
initializerSettings: JSON.parse(payload.initializerSettings),
room: payload.room,
settings: JSON.parse(payload.settings),
};
debug(result);
this.emit('init', result);
}
/**
* Called when the profile websocket is hooked.
*
* @param {Object} payload
* @private
*/
_onProfileWebsocketHooked(payload) {
debug(`websocket hook initialized`);
const result = {
chatSettings: JSON.parse(payload.chatSettings),
csrftoken: payload.csrftoken,
hasPlayer: payload.hasPlayer,
hasWebsocket: payload.hasWebsocket,
initializerSettings: JSON.parse(payload.initializerSettings),
room: payload.room,
settings: JSON.parse(payload.settings),
};
debug(result);
this.emit('hooked', result);
}
/**
* Called when the profile websocket is opened.
*
* @private
*/
_onProfileWebsocketOpen() {
this.emit('open');
}
/**
* Called when the profile websocket receives a message.
*
* @param {Object} payload
* @private
*/
_onProfileWebsocketMessage(payload) {
if (payload.type !== 'message') return;
const data = JSON.parse(payload.data);
debug(`websocket event message received`);
this.emit('message', {
args: data.args.map((arg) => _parseArg(arg)),
callback: data.callback,
method: data.method,
timestamp: payload.timestamp,
});
}
/**
* Called when the profile websocket receives an error.
*
* @param {Error} err
* @private
*/
_onProfileWebsocketError(err) {
this.emit('error', err);
}
/**
* Called when the profile websocket is closed.
*
* @param {Event} event
* @private
*/
_onProfileWebsocketClose(event) {
this.emit('close', event);
}
/**
* Evaluates a script within the browser.
*
* @param {string} script
* @param {boolean} awaitPromise
* @param {boolean} returnByValue
* @return {*}
*/
async evaluate(script, awaitPromise = false, returnByValue = true) {
debug(`evaluating script: \n${script}`);
const response = await this.protocol.Runtime.evaluate({
awaitPromise: awaitPromise,
expression: script,
returnByValue: returnByValue,
});
if (response.exceptionDetails) {
debug('script failed');
debug(response.exceptionDetails);
return null;
}
if (response.result) {
debug(response.result.value);
return response.result.value;
}
return undefined;
}
/**
* Evaluates a prebuilt script within the browser.
*
* @param {string} name
* @param {Object} params
* @return {*}
* @private
*/
_evaluateScript(name, params = undefined) {
debug(`inserting script: ${name}`);
const normalizedPath = path.join(__dirname, `scripts/${name}.js`);
let script = fs.readFileSync(normalizedPath, 'utf8');
if (params) {
Object.keys(params).forEach((key) => {
script = script.replace(`<${key}>`, params[key]);
});
}
return this.evaluate(script);
}
/**
* Performs an HTTP fetch within the browser.
*
* @param {string} url
* @param {Object} params
* @param {?Object} options
* @return {*}
*/
fetch(url, params = {}, options) {
debug(`making fetch call to: ${url}`);
const qs = querystring.stringify(params);
const opts = JSON.stringify(options || {
credentials: 'include',
});
const expression = `fetch('${url}?${qs}', ${opts}).then((r) => r.text())`;
return this.evaluate(expression, true);
}
}
module.exports = ChaturbateBrowser;