|
| 1 | +/** |
| 2 | + * Cloudflare Bypass Module |
| 3 | + * Advanced techniques for bypassing Cloudflare protection |
| 4 | + * φ² + 1/φ² = 3 = TRINITY |
| 5 | + */ |
| 6 | + |
| 7 | +// User agent pool - real browser fingerprints |
| 8 | +const USER_AGENTS = [ |
| 9 | + 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36', |
| 10 | + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36', |
| 11 | + 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0', |
| 12 | + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2 Safari/605.1.15', |
| 13 | + 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36', |
| 14 | + 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0' |
| 15 | +]; |
| 16 | + |
| 17 | +// Accept-Language variations |
| 18 | +const ACCEPT_LANGUAGES = [ |
| 19 | + 'en-US,en;q=0.9', |
| 20 | + 'en-GB,en;q=0.9,en-US;q=0.8', |
| 21 | + 'en-US,en;q=0.9,es;q=0.8', |
| 22 | + 'en,en-US;q=0.9' |
| 23 | +]; |
| 24 | + |
| 25 | +// φ-based timing (golden ratio delays) |
| 26 | +const PHI = 1.618033988749895; |
| 27 | +const PHI_SQUARED = PHI * PHI; // 2.618... |
| 28 | +const PHI_INVERSE = 1 / PHI; // 0.618... |
| 29 | + |
| 30 | +function phiDelay(base = 1000) { |
| 31 | + // Golden ratio based random delay |
| 32 | + const factor = 1 + (Math.random() * (PHI - 1)); |
| 33 | + return Math.floor(base * factor); |
| 34 | +} |
| 35 | + |
| 36 | +// φ-mutation: Generate unique fingerprint variations using golden ratio |
| 37 | +function phiMutate(seed = Date.now()) { |
| 38 | + // Use φ to create deterministic but varied mutations |
| 39 | + const mutations = []; |
| 40 | + let value = seed; |
| 41 | + |
| 42 | + for (let i = 0; i < 5; i++) { |
| 43 | + value = (value * PHI) % 1000000; |
| 44 | + mutations.push(Math.floor(value)); |
| 45 | + } |
| 46 | + |
| 47 | + return mutations; |
| 48 | +} |
| 49 | + |
| 50 | +// Generate φ-mutated headers for each request |
| 51 | +function generatePhiHeaders(requestCount = 0) { |
| 52 | + const mutations = phiMutate(Date.now() + requestCount); |
| 53 | + const baseHeaders = generateHeaders(); |
| 54 | + |
| 55 | + // Apply φ-mutations to create unique fingerprint |
| 56 | + const phiHeaders = { |
| 57 | + ...baseHeaders, |
| 58 | + // Mutate sec-ch-ua version based on φ |
| 59 | + 'sec-ch-ua': `"Not_A Brand";v="${8 + (mutations[0] % 3)}", "Chromium";v="${118 + (mutations[1] % 5)}", "Google Chrome";v="${118 + (mutations[1] % 5)}"`, |
| 60 | + // Add unique request ID based on φ |
| 61 | + 'X-Request-ID': `${mutations[2].toString(16)}-${mutations[3].toString(16)}`, |
| 62 | + // Vary cache control |
| 63 | + 'Cache-Control': mutations[4] % 2 === 0 ? 'max-age=0' : 'no-cache' |
| 64 | + }; |
| 65 | + |
| 66 | + return phiHeaders; |
| 67 | +} |
| 68 | + |
| 69 | +function getRandomUserAgent() { |
| 70 | + return USER_AGENTS[Math.floor(Math.random() * USER_AGENTS.length)]; |
| 71 | +} |
| 72 | + |
| 73 | +function getRandomAcceptLanguage() { |
| 74 | + return ACCEPT_LANGUAGES[Math.floor(Math.random() * ACCEPT_LANGUAGES.length)]; |
| 75 | +} |
| 76 | + |
| 77 | +// Generate Cloudflare-evading headers |
| 78 | +function generateHeaders() { |
| 79 | + return { |
| 80 | + 'User-Agent': getRandomUserAgent(), |
| 81 | + 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8', |
| 82 | + 'Accept-Language': getRandomAcceptLanguage(), |
| 83 | + 'Accept-Encoding': 'gzip, deflate, br', |
| 84 | + 'Connection': 'keep-alive', |
| 85 | + 'Upgrade-Insecure-Requests': '1', |
| 86 | + 'Sec-Fetch-Dest': 'document', |
| 87 | + 'Sec-Fetch-Mode': 'navigate', |
| 88 | + 'Sec-Fetch-Site': 'none', |
| 89 | + 'Sec-Fetch-User': '?1', |
| 90 | + 'Cache-Control': 'max-age=0', |
| 91 | + 'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"', |
| 92 | + 'sec-ch-ua-mobile': '?0', |
| 93 | + 'sec-ch-ua-platform': '"Windows"' |
| 94 | + }; |
| 95 | +} |
| 96 | + |
| 97 | +// Wait for Cloudflare challenge to complete |
| 98 | +async function waitForCloudflare(page, timeout = 30000) { |
| 99 | + const startTime = Date.now(); |
| 100 | + |
| 101 | + while (Date.now() - startTime < timeout) { |
| 102 | + const title = await page.title(); |
| 103 | + const url = page.url(); |
| 104 | + |
| 105 | + // Check if still on challenge page |
| 106 | + if (title.includes('Just a moment') || |
| 107 | + title.includes('Checking your browser') || |
| 108 | + title.includes('Please wait') || |
| 109 | + url.includes('challenge')) { |
| 110 | + |
| 111 | + console.log(' Waiting for Cloudflare challenge...'); |
| 112 | + await page.waitForTimeout(phiDelay(2000)); |
| 113 | + continue; |
| 114 | + } |
| 115 | + |
| 116 | + // Challenge passed |
| 117 | + return true; |
| 118 | + } |
| 119 | + |
| 120 | + return false; |
| 121 | +} |
| 122 | + |
| 123 | +// Attempt navigation with Cloudflare bypass |
| 124 | +async function navigateWithBypass(page, url, options = {}) { |
| 125 | + const maxRetries = options.maxRetries || 3; |
| 126 | + const headers = generateHeaders(); |
| 127 | + |
| 128 | + for (let attempt = 1; attempt <= maxRetries; attempt++) { |
| 129 | + console.log(` Attempt ${attempt}/${maxRetries} with bypass...`); |
| 130 | + |
| 131 | + try { |
| 132 | + // Set extra headers |
| 133 | + await page.setExtraHTTPHeaders(headers); |
| 134 | + |
| 135 | + // Navigate with longer timeout |
| 136 | + await page.goto(url, { |
| 137 | + waitUntil: 'domcontentloaded', |
| 138 | + timeout: 30000 |
| 139 | + }); |
| 140 | + |
| 141 | + // Wait for potential Cloudflare challenge |
| 142 | + const passed = await waitForCloudflare(page, 15000); |
| 143 | + |
| 144 | + if (passed) { |
| 145 | + const title = await page.title(); |
| 146 | + if (!title.includes('Just a moment') && !title.includes('Checking')) { |
| 147 | + console.log(` Cloudflare bypassed on attempt ${attempt}`); |
| 148 | + return true; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + // Rotate user agent for next attempt |
| 153 | + headers['User-Agent'] = getRandomUserAgent(); |
| 154 | + |
| 155 | + // φ-based delay before retry |
| 156 | + await page.waitForTimeout(phiDelay(3000)); |
| 157 | + |
| 158 | + } catch (error) { |
| 159 | + console.log(` Attempt ${attempt} failed: ${error.message}`); |
| 160 | + if (attempt < maxRetries) { |
| 161 | + await page.waitForTimeout(phiDelay(2000)); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + return false; |
| 167 | +} |
| 168 | + |
| 169 | +// Create browser context with Cloudflare evasion |
| 170 | +async function createBypassContext(browser) { |
| 171 | + const userAgent = getRandomUserAgent(); |
| 172 | + |
| 173 | + const context = await browser.newContext({ |
| 174 | + userAgent, |
| 175 | + viewport: { width: 1920, height: 1080 }, |
| 176 | + locale: 'en-US', |
| 177 | + timezoneId: 'America/New_York', |
| 178 | + geolocation: { latitude: 40.7128, longitude: -74.0060 }, |
| 179 | + permissions: ['geolocation'], |
| 180 | + extraHTTPHeaders: generateHeaders() |
| 181 | + }); |
| 182 | + |
| 183 | + // Inject stealth scripts |
| 184 | + await context.addInitScript(() => { |
| 185 | + // Hide webdriver |
| 186 | + Object.defineProperty(navigator, 'webdriver', { get: () => false }); |
| 187 | + |
| 188 | + // Fake plugins |
| 189 | + Object.defineProperty(navigator, 'plugins', { |
| 190 | + get: () => [1, 2, 3, 4, 5] |
| 191 | + }); |
| 192 | + |
| 193 | + // Fake languages |
| 194 | + Object.defineProperty(navigator, 'languages', { |
| 195 | + get: () => ['en-US', 'en'] |
| 196 | + }); |
| 197 | + |
| 198 | + // Override permissions |
| 199 | + const originalQuery = window.navigator.permissions.query; |
| 200 | + window.navigator.permissions.query = (parameters) => ( |
| 201 | + parameters.name === 'notifications' ? |
| 202 | + Promise.resolve({ state: Notification.permission }) : |
| 203 | + originalQuery(parameters) |
| 204 | + ); |
| 205 | + |
| 206 | + // Fake chrome object |
| 207 | + window.chrome = { |
| 208 | + runtime: {}, |
| 209 | + loadTimes: function() {}, |
| 210 | + csi: function() {}, |
| 211 | + app: {} |
| 212 | + }; |
| 213 | + |
| 214 | + // Override toString to hide modifications |
| 215 | + const originalToString = Function.prototype.toString; |
| 216 | + Function.prototype.toString = function() { |
| 217 | + if (this === navigator.permissions.query) { |
| 218 | + return 'function query() { [native code] }'; |
| 219 | + } |
| 220 | + return originalToString.call(this); |
| 221 | + }; |
| 222 | + }); |
| 223 | + |
| 224 | + return context; |
| 225 | +} |
| 226 | + |
| 227 | +module.exports = { |
| 228 | + USER_AGENTS, |
| 229 | + PHI, |
| 230 | + PHI_SQUARED, |
| 231 | + PHI_INVERSE, |
| 232 | + phiDelay, |
| 233 | + phiMutate, |
| 234 | + getRandomUserAgent, |
| 235 | + getRandomAcceptLanguage, |
| 236 | + generateHeaders, |
| 237 | + generatePhiHeaders, |
| 238 | + waitForCloudflare, |
| 239 | + navigateWithBypass, |
| 240 | + createBypassContext |
| 241 | +}; |
0 commit comments