#!/bin/bash
# Server-side solution for Cloudflare IP blocking

echo "=== Cloudflare IP Blocking Solution ==="

# Option 1: Try with different user agents and delays
cat > cf_bypass_test.mjs << 'EOF'
import puppeteer from 'puppeteer-core';
import fs from 'fs';

const userAgents = [
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
    'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
    'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0'
];

async function testDifferentApproaches() {
    const browser = await puppeteer.launch({
        executablePath: '/usr/bin/chromium-browser',
        headless: true,
        args: [
            '--no-sandbox',
            '--disable-setuid-sandbox',
            '--disable-dev-shm-usage',
            '--disable-gpu'
        ]
    });

    try {
        for (let i = 0; i < userAgents.length; i++) {
            console.log(`\n=== Test ${i + 1}/${userAgents.length} ===`);
            console.log(`User Agent: ${userAgents[i].substring(0, 50)}...`);

            const page = await browser.newPage();
            await page.setUserAgent(userAgents[i]);
            await page.setViewport({ width: 1920, height: 1080 });

            // Add random delay
            const delay = Math.floor(Math.random() * 5000) + 3000;
            console.log(`Waiting ${delay}ms...`);
            await new Promise(resolve => setTimeout(resolve, delay));

            try {
                console.log('Loading page...');
                const response = await page.goto('https://marketkarsilastir.com/ara?q=8690526076379', {
                    waitUntil: 'networkidle2',
                    timeout: 30000
                });

                const title = await page.title();
                const status = response.status();

                console.log(`Status: ${status}, Title: ${title}`);

                if (status === 200 && !title.includes('Cloudflare') && !title.includes('Access denied')) {
                    console.log('✓ SUCCESS: Got valid response!');

                    // Save successful attempt
                    const content = await page.content();
                    fs.writeFileSync(`success_attempt_${i}.html`, content);
                    await page.screenshot({ path: `success_attempt_${i}.png`, fullPage: true });

                    console.log(`Saved successful response as success_attempt_${i}.html and .png`);
                    await page.close();
                    break;
                } else {
                    console.log('✗ Still blocked');
                }

            } catch (error) {
                console.log(`Error: ${error.message}`);
            }

            await page.close();
        }

    } finally {
        await browser.close();
    }
}

testDifferentApproaches().catch(console.error);
EOF

echo "Testing different approaches to bypass Cloudflare..."
node cf_bypass_test.mjs

echo -e "\n=== Alternative Solutions ==="
echo "1. Use a proxy service (recommended)"
echo "2. Implement request queuing with longer delays"
echo "3. Use your local machine as a scraping service"
echo "4. Contact the website owner for API access"

echo -e "\n=== Quick Proxy Setup (if you have proxy access) ==="
echo "You can modify the scraper to use a proxy:"
echo "const browser = await puppeteer.launch({"
echo "    executablePath: '/usr/bin/chromium-browser',"
echo "    args: ["
echo "        '--proxy-server=your-proxy:port',"
echo "        '--no-sandbox'"
echo "    ]"
echo "});"
