UNPKG

ignitetnt

Version:

IGNITE OS — Advanced proxy-enabled gaming platform with comprehensive filter bypass system

156 lines (137 loc) • 5.35 kB
// Vercel serverless proxy function // Handles CORS and URL rewriting to bypass network filters export default async function handler(req, res) { // Handle preflight requests if (req.method === 'OPTIONS') { return res.status(200).json({}); } try { const { target, url } = req.query; let targetUrl; // Decode target URL if (target) { // Base64 decode with URL-safe characters let decoded = target; while (decoded.length % 4) { decoded += '='; } targetUrl = atob(decoded.replace(/-/g, '+').replace(/_/g, '/')); } else if (url) { targetUrl = decodeURIComponent(url); } else { return res.status(400).json({ error: 'Missing target URL' }); } // Validate URL if (!targetUrl.startsWith('http')) { return res.status(400).json({ error: 'Invalid URL protocol' }); } // Set CORS headers res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); res.setHeader('Access-Control-Allow-Headers', '*'); res.setHeader('X-Frame-Options', 'ALLOWALL'); // Create fetch options const fetchOptions = { method: req.method, headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36', 'Accept': req.headers.accept || '*/*', 'Accept-Language': 'en-US,en;q=0.9', 'Accept-Encoding': 'gzip, deflate, br', 'Cache-Control': 'no-cache', 'Pragma': 'no-cache', 'Sec-Fetch-Dest': 'empty', 'Sec-Fetch-Mode': 'cors', 'Sec-Fetch-Site': 'cross-site' } }; // Add body for non-GET requests if (req.method !== 'GET' && req.method !== 'HEAD' && req.body) { fetchOptions.body = JSON.stringify(req.body); fetchOptions.headers['Content-Type'] = 'application/json'; } // Make the request const response = await fetch(targetUrl, fetchOptions); if (!response.ok) { return res.status(response.status).json({ error: `Target server returned ${response.status}` }); } // Get content type const contentType = response.headers.get('content-type') || 'application/octet-stream'; // Set response headers res.setHeader('Content-Type', contentType); res.setHeader('Cache-Control', 'public, max-age=300'); // Handle different content types if (contentType.includes('application/json')) { const data = await response.json(); return res.status(200).json(data); } else if (contentType.includes('text/')) { const text = await response.text(); // Modify HTML to inject proxy configurations if (contentType.includes('text/html')) { const modifiedHtml = injectProxyScript(text, targetUrl); return res.status(200).send(modifiedHtml); } return res.status(200).send(text); } else { // Binary content const buffer = await response.arrayBuffer(); return res.status(200).send(Buffer.from(buffer)); } } catch (error) { console.error('Proxy error:', error); return res.status(500).json({ error: 'Proxy request failed', details: error.message }); } } // Inject proxy configuration into HTML responses function injectProxyScript(html, baseUrl) { const proxyScript = ` <script> // Override fetch to use proxy const originalFetch = window.fetch; window.fetch = function(url, options = {}) { if (typeof url === 'string' && !url.startsWith('data:') && !url.startsWith('blob:')) { const absoluteUrl = new URL(url, '${baseUrl}').href; const proxyUrl = '/api/proxy?url=' + encodeURIComponent(absoluteUrl); return originalFetch(proxyUrl, options); } return originalFetch(url, options); }; // Override XMLHttpRequest const OriginalXHR = window.XMLHttpRequest; window.XMLHttpRequest = function() { const xhr = new OriginalXHR(); const originalOpen = xhr.open; xhr.open = function(method, url, ...args) { if (typeof url === 'string' && !url.startsWith('data:') && !url.startsWith('blob:')) { const absoluteUrl = new URL(url, '${baseUrl}').href; const proxyUrl = '/api/proxy?url=' + encodeURIComponent(absoluteUrl); return originalOpen.call(this, method, proxyUrl, ...args); } return originalOpen.call(this, method, url, ...args); }; return xhr; }; // Override window.open const originalOpen = window.open; window.open = function(url, ...args) { if (typeof url === 'string' && !url.startsWith('data:') && !url.startsWith('blob:')) { const absoluteUrl = new URL(url, '${baseUrl}').href; const proxyUrl = '/api/proxy?url=' + encodeURIComponent(absoluteUrl); return originalOpen(proxyUrl, ...args); } return originalOpen(url, ...args); }; </script> `; // Inject before closing head tag, or at the beginning if no head if (html.includes('</head>')) { return html.replace('</head>', proxyScript + '</head>'); } else { return proxyScript + html; } }