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
| const config = { basic: { upstream: 'https://github.com/', mobileRedirect: 'https://github.com/', },
firewall: { blockedRegion: ['KP', 'SY', 'PK', 'CU'], blockedIPAddress: [], scrapeShield: true, },
routes: { TW: 'https://github.com/', },
optimization: { cacheEverything: false, cacheTtl: 5, mirage: true, polish: 'off', minify: { javascript: true, css: true, html: true, }, }, };
async function isMobile(userAgent) { const agents = ['Android', 'iPhone', 'SymbianOS', 'Windows Phone', 'iPad', 'iPod']; return agents.any((agent) => userAgent.indexOf(agent) > 0); }
async function fetchAndApply(request) { const region = request.headers.get('cf-ipcountry') || ''; const ipAddress = request.headers.get('cf-connecting-ip') || ''; const userAgent = request.headers.get('user-agent') || '';
if (region !== '' && config.firewall.blockedRegion.includes(region.toUpperCase())) { return new Response( 'Access denied: booster.js is not available in your region.', { status: 403, }, ); } if (ipAddress !== '' && config.firewall.blockedIPAddress.includes(ipAddress)) { return new Response( 'Access denied: Your IP address is blocked by booster.js.', { status: 403, }, ); }
const requestURL = new URL(request.url); let upstreamURL = null;
if (userAgent && isMobile(userAgent) === true) { upstreamURL = new URL(config.basic.mobileRedirect); } else if (region && region.toUpperCase() in config.routes) { upstreamURL = new URL(config.routes[region.toUpperCase()]); } else { upstreamURL = new URL(config.basic.upstream); }
requestURL.protocol = upstreamURL.protocol; requestURL.host = upstreamURL.host; requestURL.pathname = upstreamURL.pathname + requestURL.pathname;
let newRequest; if (request.method === 'GET' || request.method === 'HEAD') { newRequest = new Request(requestURL, { cf: { cacheEverything: config.optimization.cacheEverything, cacheTtl: config.optimization.cacheTtl, mirage: config.optimization.mirage, polish: config.optimization.polish, minify: config.optimization.minify, scrapeShield: config.firewall.scrapeShield, }, method: request.method, headers: request.headers, }); } else { const requestBody = await request.text(); newRequest = new Request(requestURL, { cf: { cacheEverything: config.optimization.cacheEverything, cacheTtl: config.optimization.cacheTtl, mirage: config.optimization.mirage, polish: config.optimization.polish, minify: config.optimization.minify, scrapeShield: config.firewall.scrapeShield, }, method: request.method, headers: request.headers, body: requestBody, }); }
const fetchedResponse = await fetch(newRequest);
const modifiedResponseHeaders = new Headers(fetchedResponse.headers); if (modifiedResponseHeaders.has('x-pjax-url')) { const pjaxURL = new URL(modifiedResponseHeaders.get('x-pjax-url')); pjaxURL.protocol = requestURL.protocol; pjaxURL.host = requestURL.host; pjaxURL.pathname = pjaxURL.path.replace(requestURL.pathname, '/');
modifiedResponseHeaders.set( 'x-pjax-url', pjaxURL.href, ); }
return new Response( fetchedResponse.body, { headers: modifiedResponseHeaders, status: fetchedResponse.status, statusText: fetchedResponse.statusText, }, ); }
addEventListener('fetch', (event) => { event.respondWith(fetchAndApply(event.request)); });
|