· last year · Feb 24, 2024, 03:00 PM
1/*
2
3Cloudflare bypass with AntiBotCookieTask from Anti-Captcha.com.
4Install dependencies:
5
6npm install @antiadmin/anticaptchaofficial puppeteer
7
8
9* */
10
11const anticaptcha = require("@antiadmin/anticaptchaofficial");
12const pup = require('puppeteer');
13
14//address behind cloudflare
15const checkUrl = 'https://www.sportsbikeshop.co.uk/';
16const domainName = 'www.sportsbikeshop.co.uk';
17
18
19//Anti-captcha.com API key
20const apiKey = 'API_KEY_HERE';
21
22// STOP! IMPORTANT! Shared proxy services won't work!
23// Use ONLY self-installed proxies on your own infrastructure! Instruction: https://anti-captcha.com/apidoc/articles/how-to-install-squid
24// Again and again people people insist they have best purchased proxies. NO YOU DO NOT!
25// Absolutely recommended to read this FAQ about proxies: https://anti-captcha.com/faq/510_questions_about_solving_recaptcha_with_proxy__applies_to_funcaptcha__geetest__hcaptcha_
26
27
28const proxyAddress = '1.2.3.4';
29const proxyPort = 1234;
30const proxyLogin = 'mylogin';
31const proxyPassword = 'mypass';
32
33
34let browser = null;
35let page = null;
36
37
38(async () => {
39
40 anticaptcha.setAPIKey(apiKey);
41 const balance = await anticaptcha.getBalance();
42 if (balance <= 0) {
43 console.log('Topup your anti-captcha.com balance!');
44 return;
45 } else {
46 console.log('API key balance is '+balance+', continuing');
47 // anticaptcha.shutUp(); //uncomment for silent captcha recognition
48 }
49
50 let antigateResult = null;
51 try {
52 antigateResult = await anticaptcha.solveAntiBotCookieTask(
53 checkUrl,
54 proxyAddress,
55 proxyPort,
56 proxyLogin,
57 proxyPassword);
58 } catch (e) {
59 console.error("could not solve captcha: "+e.toString());
60 return;
61 }
62
63 const fingerPrint = antigateResult.fingerprint;
64
65 try {
66 console.log('opening browser ..');
67
68
69 let options = {
70 headless: true, //disable to see the browser window
71 devtools: false, //enable to see developers console
72 args: [
73 '--window-size='+fingerPrint['self.screen.width']+','+fingerPrint['self.screen.height'],
74 `--proxy-server=${proxyAddress}:${proxyPort}`
75 ],
76
77 };
78 browser = await pup.launch(options);
79
80 console.log('creating new page ..');
81 page = await browser.newPage();
82 } catch (e) {
83 console.log("could not open browser: "+e);
84 return false;
85 }
86
87 if (proxyPassword && proxyLogin) {
88 console.log(`setting proxy authentication ${proxyLogin}:${proxyPassword}`);
89 await page.authenticate({
90 username: proxyLogin,
91 password: proxyPassword,
92 });
93 }
94
95 //screen size
96 console.log('setting view port to '+fingerPrint['self.screen.width']+'x'+fingerPrint['self.screen.height']);
97 await page.setViewport({width: fingerPrint['self.screen.width'], height: fingerPrint['self.screen.height']});
98
99 //user agent
100 let userAgent = '';
101 if (fingerPrint['self.navigator.userAgent']) {
102 userAgent = fingerPrint['self.navigator.userAgent'];
103 } else {
104 if (fingerPrint['self.navigator.appVersion'] && fingerPrint['self.navigator.appCodeName']) {
105 userAgent = fingerPrint['self.navigator.appCodeName'] + '/' + fingerPrint['self.navigator.appVersion']
106 }
107 }
108 console.log('setting browser user agent to '+userAgent);
109 await page.setUserAgent(userAgent);
110
111 console.log('setting cookies', antigateResult.cookies);
112 let cookies = [];
113 for (const name in antigateResult.cookies) {
114 cookies.push({ name: name, value: antigateResult.cookies[name], domain: domainName })
115 }
116 await page.setCookie(...cookies);
117
118 try {
119 await page.goto(antigateResult.url, {
120 waitUntil: "networkidle0"
121 });
122 } catch (e) {
123 console.log('err while loading the page: '+e);
124 }
125
126 const htmlContent = await page.content();
127 console.log("page content:\n\n", htmlContent);
128
129
130})();
131