CloudFlare Workers

james bond
By -
0
// ============================================ // CloudFlare Workers - GEO Redirect Script // ============================================ // 📍 Step 1: এখানে আপনার সব landing page URL দিন const GEO_ROUTES = { // Format: 'Country_Code': 'Your_Landing_Page_URL' 'US': 'https://kiimasterdata.kyocera.com/vlc/', // United States 'GB': 'https://kiimasterdata.kyocera.com/vlc/', // United Kingdom // আরও দেশ যোগ করতে চাইলে এখানে লিখুন }; // 🌍 Default URL - যদি কোন দেশ match না হয় বা detect না হয় const DEFAULT_URL = 'https://kiimasterdata.kyocera.com/jav/'; // ⚙️ Configuration Options const CONFIG = { // Redirect type: 301 (Permanent) or 302 (Temporary) // SEO এর জন্য 302 ভালো (testing phase এ) redirectType: 301, // Enable logging for debugging enableLogging: true, // Show loading page before redirect (true/false) showLoadingPage: false, }; // ============================================ // Main Worker Event Listener // ============================================ addEventListener('fetch', event => { event.respondWith(handleRequest(event.request)); }); // ============================================ // Handle Request Function // ============================================ async function handleRequest(request) { try { // CloudFlare automatically provides country in request const country = request.cf?.country || 'XX'; // Get visitor's IP for logging const ip = request.headers.get('CF-Connecting-IP') || 'Unknown'; // Find redirect URL for this country const redirectUrl = GEO_ROUTES[country] || DEFAULT_URL; // Log request info (if enabled) if (CONFIG.enableLogging) { console.log(JSON.stringify({ timestamp: new Date().toISOString(), ip: ip, country: country, redirectTo: redirectUrl, userAgent: request.headers.get('User-Agent') })); } // Choose redirect method if (CONFIG.showLoadingPage) { return createLoadingPageResponse(redirectUrl, country); } else { return Response.redirect(redirectUrl, CONFIG.redirectType); } } catch (error) { // Error handling - redirect to default URL console.error('Worker Error:', error.message); return Response.redirect(DEFAULT_URL, CONFIG.redirectType); } } // ============================================ // Create Loading Page Response // ============================================ function createLoadingPageResponse(redirectUrl, country) { const html = ` Redirecting...

Redirecting to your region...

Please wait a moment

Detected: ${country}
`; return new Response(html, { status: 200, headers: { 'Content-Type': 'text/html;charset=UTF-8', 'Cache-Control': 'no-cache, no-store, must-revalidate', 'X-Redirected-Country': country, } }); }

একটি মন্তব্য পোস্ট করুন

0মন্তব্যসমূহ

একটি মন্তব্য পোস্ট করুন (0)