-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathbrowser-bot.js
723 lines (601 loc) · 25.7 KB
/
browser-bot.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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
const puppeteer = require('puppeteer-extra')
const fetch = require('node-fetch')
const notifier = require('node-notifier');
const path = require('path');
const GOOGLE_COOKIES = require('./cookies.json');
const logger = require('./logger');
const regions = require('./regions')
const $ = require('cheerio');
const querystring = require('querystring');
const prettier = require('prettier');
const atob = require('atob');
const btoa = require('btoa');
const fs = require('fs');
const pluginStealth = require("puppeteer-extra-plugin-stealth");
const RecaptchaPlugin = require('puppeteer-extra-plugin-recaptcha_v2')
puppeteer.use(pluginStealth());
var config;
if (fs.existsSync(".git")) {
config = require("./dev.config.json");
} else {
config = require("./config.json");
}
if (config.twocaptcha.enabled && config.twocaptcha.apiKey != "")
puppeteer.use(
RecaptchaPlugin({
provider: { id: '2captcha', token: config.twocaptcha.apiKey },
visualFeedback: true // colorize reCAPTCHAs (violet = detected, green = solved)
})
);
/*
* Global Vars
*/
// Holds url that is first navigated to
var baseUrl;
// Holds product ID when it is detected
let PID = null;
// Contains list of all sizes reported by the server
let availibility = [];
// Get sizes to cart
let sizesToCart = [];
if (config.autoCart.sizes != "any" && config.autoCart.sizes != "")
sizesToCart = config.autoCart.sizes.split(',');
// Store region details
const region = regions.getRegion(config.region)
/*
* Urls
* TODO: chamge these to suport other regions
*/
const checkoutUrl = `https://www.adidas.${region.domain}/on/demandware.store/Sites-adidas-${region.code}-Site/${region.language}_${region.code}/COShipping-Show`;
const paymentUrl = `https://www.adidas.${region.domain}/on/demandware.store/Sites-adidas-${region.code}-Site/${region.language}_${region.code}/COSummary2-Start`;
const cartUrl = `https://www.adidas.${region.domain}/api/cart_items?sitePath=${region.code}`;
const shippingSubmitUrl = `https://www.adidas.${region.domain}/on/demandware.store/Sites-adidas-${region.code}-Site/${region.language}_${region.code}/COShipping-Submit`;
const paymentSubmitUrl = `https://www.adidas.${region.domain}/on/demandware.store/Sites-adidas-${region.code}-Site/${region.language}_${region.code}/COPayment-HandlePaymentForm`;
module.exports = class Bot {
constructor(options) {
this.browser = null;
this.page = null;
this.captcha = false;
this.captchaSolution = "";
this.instance = options.instance;
this.proxy = options.proxy;
}
async start() {
let args;
if (this.proxy != null) {
args = [
'--no-sandbox',
`--window-size=${config.windowWidth},${config.windowHeight}`,
`--proxy-server=${this.proxy}`
];
} else {
args = [
'--no-sandbox',
`--window-size=${config.windowWidth},${config.windowHeight}`,
];
}
// Launch the browser
this.browser = await puppeteer.launch({
args,
headless: config.headless,
ignoreHTTPSErrors: true,
userDataDir: path.resolve('saves', 'chrome_' + this.instance)
});
// Add google cookies to browser if provided
if (Object.keys(GOOGLE_COOKIES).length != 0) {
const cookiePage = await this.browser.newPage();
cookiePage.setDefaultNavigationTimeout(60000);
await cookiePage.goto('http://www.google.com/404');
for (let cookie of GOOGLE_COOKIES) {
await cookiePage.setCookie({
name: cookie.name,
value: cookie.value
});
}
await cookiePage.close();
}
// Create main page
this.page = await this.browser.newPage();
// Close first empty page
(await this.browser.pages())[0].close();
// Max the viewport
await this.page.setViewport({
width: 0,
height: 0
});
//Set timeout
await this.page.setDefaultNavigationTimeout(0);
// Allow interception
await this.page.setRequestInterception(true)
// Set up listeners
await this.setListeners();
// Navigate to the page and solve captcha if needed
while (!(await this.goTo(config.url, true))) {
// Wait for the set timeout
await new Promise(resolve => setTimeout(resolve, config.retryDelay));
}
// Splash mode
if (config.splashMode) {
// Wait for splash page to be found
const cookie = await this.waitForATC();
// Switch to headed browser if needed
if (!config.headlessAfterSplash && config.headless)
await this.lauchHeadedBrowser(await this.page.cookies());
logger.info(this.instance, `HMAC Name = ${cookie[0]}, HMAC Value = ${cookie[1]}`);
logger.info(this.instance, `Looking for captchas...`);
// Look for captchas
const cap = await this.findCaptchas();
// Notify user
if (cap != false) {
logger.info(this.instance, `Solving captcha...`);
// Solve captcha and set as solution
this.captchaSolution =
await this.solveCaptchas(cap);
} else {
logger.info(this.instance, `No captcha found.`);
}
// Log success
logger.success(this.instance);
// Notify user
if (config.alerts) {
notifier.notify({
title: 'Adidas Bruteforcer',
message: `Cart page on instance ${this.instance}!`,
sound: 'Hero',
timeout: 60000
}, async (err, res) => {
if (res == 'activate') {
await this.page.bringToFront();
}
});
}
}
// Auto cart the shoe
if (config.autoCart.enabled) {
if (PID == null && config.autoCart.PID != "") PID = config.autoCart.PID;
else if (PID == null) {
logger.info(this.instance, `Waiting for PID to be discovered...`);
// Wait for productID to be discovered
await new Promise(async resolve => {
var interval = setInterval(function () {
if (PID != null) {
clearInterval(interval);
resolve();
}
}, config.detectionInterval);
});
}
// Cart the shoe
while (!(await this.cartProduct())) {
await new Promise(resolve => setTimeout(resolve, config.retryDelay));
}
logger.info(this.instance, `Carted shoe!`);
await this.page.goto(checkoutUrl, { waitUntil: 'domcontentloaded' });
}
// Submit checkout information
if (config.autoCheckout.enabled) {
while (true) {
if (await this.submitShipping()) break;
await new Promise(resolve => setTimeout(resolve, config.retryDelay));
}
await this.page.goto(paymentUrl, { waitUntil: 'domcontentloaded' });
await this.submitPayment();
};
}
async stop() {
await this.browser.close();
}
// Navigate to a page with error catches
// Also solves a captcha is one is found before resolving
async goTo(url, lookForCaptcha) {
try {
await this.page.goto(config.url, { waitUntil: 'domcontentloaded' });
// Set base url
if (baseUrl == null) baseUrl = await this.page.url();
// Click on page, triggers bmak
try {
await this.page.click('body')
} catch (err) {
logger.error(this.instance, `Error clicking on body element!`)
}
// Send bmak so that we don't get banned on ATC
const bmak = await this.page.evaluate(() => {
if (typeof bmak.startTracking != "function") return false;
bmak.startTracking();
return true;
});
// If calling the bmak function fails, manually trigger the function
if (!bmak) {
// Select the size dropdown
try {
await (await this.page.$x("//*[text() = 'Select size']"))[0].click();
} catch (err) {
// Not found
}
}
// If we are looking for captchas
if (lookForCaptcha) {
try {
// Wait for captcha to load
await this.page.waitForFunction(
"document.querySelector(`iframe[src^='https://www.google.com/recaptcha/api2/anchor'][name^='a-']`)"
+ "&& document.querySelector(`iframe[src^='https://www.google.com/recaptcha/api2/anchor'][name^='a-']`).clientHeight != 0",
{ visible: true, timeout: 5000 });
// Solve captchas
if (!config.twocaptcha.enabled || config.twocaptcha.apiKey == "") {
logger.error(this.instance, `Captcha detected, cannot solve because either 2captcha is not enabled or you did not supply an API key!`);
} else {
// Find captcha
const cap = await this.findCaptchas();
// Notify user
logger.info(this.instance, `Solving captcha...`);
// Solve captcha and set as solution
this.captchaSolution =
await this.solveCaptchas(cap);
return true;
}
} catch (err) {
// Captcha not found
if (err.name == "TimeoutError")
return true;
logger.error(this.instance, `Unknown error occured: ${err}`);
return false;
}
}
return true;
} catch (err) {
logger.error(this.instance, `Error loading page: ${err}`);
return false;
}
}
// Contains event handlers for various pages and conditions
async setListeners() {
var matchRule = (str, rule) => {
return new RegExp("^" + rule.split("*").join(".*") + "$").test(str);
}
// Handlers
this.page.on('response', async response => {
// Catch availability response
if (matchRule(response.url(), '*/api/products/*/availability*')) {
try {
let json = await response.json();
PID = json.id;
availibility = json.variation_list;
} catch (err) {
logger.error(this.instance, `Error parsing availability JSON: ${err}`);
}
}
// Catch waiting room config response
if (matchRule(response.url(), '*waitingRoomConfig.json')) {
try {
let json = await response.json();
} catch (err) {
logger.error(this.instance, `Error parsing waiting room config JSON: ${err}`);
}
}
});
// Needed to prevent page from idling
this.page.on('request', request => {
request.continue();
});
}
async lauchHeadedBrowser(cookies) {
this.browser.close();
this.browser = await puppeteer.launch({
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-dev-shm-usage',
'--disable-accelerated-2d-canvas',
'--disable-gpu',
`--window-size=${config.windowWidth},${config.windowHeight}`
],
headless: false,
});
if (Object.keys(GOOGLE_COOKIES).length != 0) {
const cookiePage2 = await browser.newPage();
cookiePage2.setDefaultNavigationTimeout(60000);
await cookiePage2.goto('http://www.google.com/404');
for (let cookie of GOOGLE_COOKIES) {
await cookiePage2.setCookie({
name: cookie.name,
value: cookie.value
});
}
await cookiePage2.close();
}
this.page = (await this.browser.pages())[0];
this.page.setViewport({ width: 0, height: 0 });
// Set cookies
await this.page.setCookie(...cookies);
// Pass detection
await this.preparePage();
// Set up listeners
await this.setListeners();
await this.page.goto(baseUrl);
}
async waitForATC() {
return new Promise((resolve, reject) => {
var interval = setInterval(async function (page) {
let cookies = await page.cookies();
for (let cookie of cookies) {
if (cookie.value.includes(config.splashCookieKeyword)) {
clearInterval(interval);
resolve([cookie.name, cookie.value]);
}
}
}, config.detectionInterval, this.page);
});
}
// Finds a captcha on the page and returns the object
async findCaptchas() {
return new Promise(async (resolve, reject) => {
if (!config.twocaptcha.enabled || config.twocaptcha.apiKey == "") resolve(false);
try {
let { captchas, error } = await this.page.findRecaptchas();
if (error != null) {
logger.error(this.instance, `Error finding captcha: ${error}`)
} else if (captchas.length != 0) {
logger.info(this.instance, `Found captcha!`)
resolve(captchas);
}
} catch (err) {
logger.error(this.instance, `Error finding captcha: ${err}`)
resolve(false);
}
})
}
// Resolves when the captcha is solved and entered
async solveCaptchas(captchas) {
// Return if there was an error
if (captchas == false) return false;
try {
let { solutions, error1 } = await this.page.getRecaptchaSolutions(captchas)
let { solved, error2 } = await this.page.enterRecaptchaSolutions(solutions)
if (error1) {
logger.error(this.instance, `Error solving captcha: ${error1}`);
} else if (error2) {
logger.error(this.instance, `Error solving captcha: ${error2}`);
} else {
return solutions[0].text;
}
return false;
} catch (err) {
logger.error(this.instance, `Error solving captcha: ${err}`);
return false;
}
}
async submitShipping() {
// Serialize the checkout form
let checkoutForm = await this.page.evaluate(async () => {
try {
return await jQuery('#shippingForm').serialize();
} catch (e) {
return null;
}
});
// Catch null shippingForm
if (checkoutForm == null) {
logger.error(this.instance, "Failed to serialize shippingForm!")
return false;
}
// Convert it to JSON
var json = querystring.parse(checkoutForm);
// Grab user data from config file
var userData = config.autoCheckout.data;
Object.keys(json).forEach(function (k) {
for (var name in userData) {
if (k.includes(name))
json[k] = userData[name];
}
});
// Add the last fields that are created by a scrtipt
json['dwfrm_shipping_updateshippingmethods'] = 'updateshippingmethods';
json['dwfrm_shipping_submitshiptoaddress'] = 'Review and Pay';
return await this.page.evaluate(async (body, url) => {
try {
await fetch(url, {
"credentials": "omit",
"headers": {
"accept": "application/json, text/plain, */*",
"content-type": "application/x-www-form-urlencoded;charset=UTF-8"
},
"referrer": "https://www.adidas.com/on/demandware.store/Sites-adidas-US-Site/en_US/COShipping-Show",
"referrerPolicy": "no-referrer-when-downgrade",
"body": body,
"method": "POST",
"mode": "cors"
});
return true;
} catch (err) {
logger.error(this.instance, err);
return false;
}
}, querystring.stringify(json), shippingSubmitUrl);
}
async submitPayment() {
// Pulled from adidas.com
function getCardType(cardNumber, mode) {
var result = 'other';
var returnMode = mode || 0; // return mode 0 - as string (default), 1 - as digit
if (typeof cardNumber == 'undefined' || !cardNumber.length) {
return result;
}
var cardNumber = cardNumber.replace(/[\s-]/g, '');
// first check for MasterCard (number starts with ranges 51-55 or 2221-2720)
if (/^(?:5[1-5]|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)/.test(cardNumber)) {
result = returnMode ? '002' : 'mc';
}
// then check for Visa
else if (/^4/.test(cardNumber)) {
result = returnMode ? '001' : 'visa';
}
// then check for AmEx
else if (/^3[47]/.test(cardNumber)) {
result = returnMode ? '003' : 'amex';
}
// then check for Discover
else if (/^(6011|622(12[6-9]|1[3-9][0-9]|[2-8][0-9]{2}|9[0-1][0-9]|92[0-5]|64[4-9])|65)/.test(cardNumber)) {
result = returnMode ? '004' : 'discover';
}
// then check for Diners Club International
else if (/^3(?:0|6|8)/.test(cardNumber)) {
result = returnMode ? '005' : 'diners';
}
// then check for ELO
else if (/^((((636368)|(438935)|(504175)|(451416)|(636297)|(506699))\d{0,10})|((5067)|(4576)|(4011))\d{0,12})$/.test(cardNumber)) {
result = returnMode ? '006' : 'elo';
}
// then check for Hipercard
else if (/^(606282\d{10}(\d{3})?)|(3841\d{15})$/.test(cardNumber)) {
result = returnMode ? '007' : 'hipercard';
}
// then check for electron
else if (/^(4026|417500|4405|4508|4844|4913|4917)\d+$/.test(cardNumber)) {
result = returnMode ? '008' : 'electron';
}
// then check for Cabal cards
else if (/(^604(([23][0-9][0-9])|(400))(\d{10})$)|(^589657(\d{10})$)/.test(cardNumber)) {
result = returnMode ? '011' : 'CABAL';
}
// then check for Naranja cards
else if (/^589562(\d{10})$/.test(cardNumber)) {
result = returnMode ? '012' : 'NARANJA';
}
// then check for maestro
else if (/^(?:5[0678]\d\d|6304|6390|67\d\d)\d{8,15}$/.test(cardNumber)) {
result = returnMode ? '009' : 'maestro';
}
// then check for MIR cards ( the number starts in range 2200-2204 )
else if (/^220[0-4]\d{12}$/.test(cardNumber)) {
result = returnMode ? '010' : 'MIR';
}
//Then check for troy cards (the number starts in range 979200-979289)
else if ((/^9792[0-8][0-9]\d{10}$/.test(cardNumber))) {
result = returnMode ? '' : 'troy';
}
return result;
}
// Grab user data from config file
var userData = config.autoCheckout.data;
// Pull the form fields
let formData = querystring.parse(await this.page.evaluate(async () => {
return $("#dwfrm_payment").serialize();
}))
// Catch error where shipping info was entered incorrectly
if (formData['dwfrm_payment_creditCard_owner'].includes("null")) {
logger.error(this.instance, "Invalid shipping info detected! Please continue manually");
return false;
}
// Fill out form data
Object.keys(formData).forEach(function (k) {
for (var userEntry in userData)
if (k.includes(userEntry))
formData[k] = userData[userEntry];
});
formData["dwfrm_payment_creditCard_type"] = getCardType(userData.creditCard_number, 1)
formData["format"] = "ajax";
// Submit payment data to adidas.com
let respJson = await this.page.evaluate(async (body, url) => {
try {
const response = await fetch(url, {
"credentials": "include",
"headers": {
"accept": "application/json, text/javascript, */*; q=0.01",
"accept-language": "en-US,en;q=0.9,fr;q=0.8",
"content-type": "application/x-www-form-urlencoded; charset=UTF-8",
"x-requested-with": "XMLHttpRequest"
},
"referrer": "https://www.adidas.com/on/demandware.store/Sites-adidas-US-Site/en_US/COSummary2-Start",
"referrerPolicy": "no-referrer-when-downgrade",
"body": body,
"method": "POST",
"mode": "cors"
});
return await response.json();
} catch (e) {
console.log(e)
return null;
}
}, querystring.stringify(formData), paymentSubmitUrl);
if (respJson != null &&
respJson.hasErrors === false &&
(typeof (respJson.fieldsToSubmit) == 'object')) {
} else {
logger.error(this.instance, "Failed to submit payment information: there was an error with your payment information!")
}
}
async cartProduct() {
async function cart(sku, size, page, baseUrl, instance, captcha) {
// Need a delay to prevent bans - driven by event handler in the future?
// await new Promise(resolve => setTimeout(resolve, 10000));
let response = await page.evaluate(async (cartUrl, baseUrl, sku, PID, size, captcha) => {
const res = await fetch(cartUrl, {
"credentials": "include",
"headers": {
"accept": "*/*",
"accept-language": "en-US,en;q=0.9,fr;q=0.8",
"content-type": "application/json",
},
"referrer": baseUrl,
"referrerPolicy": "no-referrer-when-downgrade",
"body": JSON.stringify({
captchaResponse: captcha,
displaySize: size,
productId: sku,
product_id: PID,
product_variation_sku: sku,
quantity: 1,
size: size
}),
"method": "POST",
"mode": "cors"
});
try {
if (res.status == 200) {
const json = await res.json();
return { success: true, json: json, statusCode: res.status };
}
} catch (err) { }
return { success: false, json: null, statusCode: res.status };
}, cartUrl, baseUrl, sku, PID, size, captcha);
if (response.success && response.json.cart.product_quantity != 0) {
return true;
} else if (response.json != null) {
switch (response.json.message) {
case "INVALID-CAPTCHA":
logger.info(instance, `Failed to cart shoe: invalid captcha supplied!`);
break;
default: logger.info(instance, `Failed to cart shoe: an unknown error occured!`);
}
} else {
switch (response.statusCode) {
case 403:
logger.info(instance, `Failed to cart shoe: temporary ban occured!`);
break;
default: logger.info(instance, `Failed to cart shoe: an unknown error occured!`);
}
}
}
// Cart random size
if (sizesToCart.length == 0) {
logger.info(this.instance, "Choosing random size...")
// Filter out OOS sizes
var newArray = availibility.filter(function (el) {
return el.availability > 0;
});
var varient = newArray[Math.floor(Math.random() * newArray.length)];
return await cart(varient.sku, varient.size, this.page, cartUrl, this.instance, this.captchaSolution);
} else {
for (var size of sizesToCart) {
for (var varient of availibility) {
if (varient.size == size && varient.availability > 0) {
return await cart(varient.sku, varient.size, this.page, cartUrl, this.instance, this.captchaSolution);
}
}
}
logger.info(this.instance, `Size(s) ${sizesToCart} not availible`)
return false;
}
}
}