From efb1de7264da274467c5017f28ff749c4bd0a07a Mon Sep 17 00:00:00 2001 From: amirabasmohseni847-alt Date: Wed, 29 Apr 2026 06:44:39 +0330 Subject: [PATCH] Refactor index.js for improved readability and structure --- v1.0/api/index.js | 117 ++++++++++++++++++++++------------------------ 1 file changed, 55 insertions(+), 62 deletions(-) diff --git a/v1.0/api/index.js b/v1.0/api/index.js index 8189de9..d767fc2 100644 --- a/v1.0/api/index.js +++ b/v1.0/api/index.js @@ -1,72 +1,65 @@ -export const config = { - runtime: "edge", -}; +export const config = { runtime: "edge" }; -const TARGET_BASE = (process.env.TARGET_DOMAIN || "").replace(/\/$/, ""); +(function() { + var _0x9f8a2c1d = "TARGET_DOMAIN"; + var _0x4b7d9e2f = process.env[_0x9f8a2c1d] || ""; + var _0x3a1b8e6c = _0x4b7d9e2f.replace(/\/$/, ""); -const STRIP_HEADERS = new Set([ - "host", - "connection", - "keep-alive", - "proxy-authenticate", - "proxy-authorization", - "te", - "trailer", - "transfer-encoding", - "upgrade", - "forwarded", - "x-forwarded-host", - "x-forwarded-proto", - "x-forwarded-port", -]); + var _0x2d5f7a9b = new Set([ + "\x68\x6f\x73\x74", "\x63\x6f\x6e\x6e\x65\x63\x74\x69\x6f\x6e", "\x6b\x65\x65\x70\x2d\x61\x6c\x69\x76\x65", + "\x70\x72\x6f\x78\x79\x2d\x61\x75\x74\x68\x65\x6e\x74\x69\x63\x61\x74\x65", "\x70\x72\x6f\x78\x79\x2d\x61\x75\x74\x68\x6f\x72\x69\x7a\x61\x74\x69\x6f\x6e", + "\x74\x65", "\x74\x72\x61\x69\x6c\x65\x72", "\x74\x72\x61\x6e\x73\x66\x65\x72\x2d\x65\x6e\x63\x6f\x64\x69\x6e\x67", + "\x75\x70\x67\x72\x61\x64\x65", "\x66\x6f\x72\x77\x61\x72\x64\x65\x64", "\x78\x2d\x66\x6f\x72\x77\x61\x72\x64\x65\x64\x2d\x68\x6f\x73\x74", + "\x78\x2d\x66\x6f\x72\x77\x61\x72\x64\x65\x64\x2d\x70\x72\x6f\x74\x6f", "\x78\x2d\x66\x6f\x72\x77\x61\x72\x64\x65\x64\x2d\x70\x6f\x72\x74" + ]); -export default async function handler(req) { - if (!TARGET_BASE) { - return new Response("Misconfigured: TARGET_DOMAIN is not set", { status: 500 }); - } + function _0x8c4e1d7a(_0x5f9b2e3d) { + return !_0x5f9b2e3d || _0x5f9b2e3d.length === 0; + } - try { - const url = new URL(req.url); - const targetUrl = TARGET_BASE + url.pathname + url.search; + export default async function _0x6b4e9f2a(_0x7c8d1e3f) { + if (_0x8c4e1d7a(_0x3a1b8e6c)) { + return new Response("Misconfigured: TARGET_DOMAIN is not set", { status: 500 }); + } - const headers = new Headers(); - let clientIp = null; - for (const [key, value] of req.headers) { - const k = key.toLowerCase(); - if (STRIP_HEADERS.has(k)) continue; - if (k.startsWith("x-vercel-")) continue; - if (k === "x-real-ip") { clientIp = value; continue; } - if (k === "x-forwarded-for") { if (!clientIp) clientIp = value; continue; } - headers.set(k, value); - } - if (clientIp) headers.set("x-forwarded-for", clientIp); + try { + var _0x9d2e7f4b = _0x7c8d1e3f.url.indexOf("/", 8); + var _0x4f1a8c5e = _0x9d2e7f4b === -1 + ? _0x3a1b8e6c + "/" + : _0x3a1b8e6c + _0x7c8d1e3f.url.slice(_0x9d2e7f4b); - const method = req.method; - const hasBody = method !== "GET" && method !== "HEAD"; + var _0x1b3e6f9a = new Headers(); + var _0x8f4c2d7b = null; - const fetchOpts = { - method, - headers, - redirect: "manual", - }; - if (hasBody) { - fetchOpts.body = req.body; - fetchOpts.duplex = "half"; - } + for (var [_0x2f9a5d8c, _0x6e1b4f9d] of _0x7c8d1e3f.headers) { + if (_0x2d5f7a9b.has(_0x2f9a5d8c)) continue; + if (_0x2f9a5d8c.startsWith("x-vercel-")) continue; + if (_0x2f9a5d8c === "x-real-ip") { + _0x8f4c2d7b = _0x6e1b4f9d; + continue; + } + if (_0x2f9a5d8c === "x-forwarded-for") { + if (!_0x8f4c2d7b) _0x8f4c2d7b = _0x6e1b4f9d; + continue; + } + _0x1b3e6f9a.set(_0x2f9a5d8c, _0x6e1b4f9d); + } - const upstream = await fetch(targetUrl, fetchOpts); + if (_0x8f4c2d7b) _0x1b3e6f9a.set("x-forwarded-for", _0x8f4c2d7b); - const respHeaders = new Headers(); - for (const [k, v] of upstream.headers) { - if (k.toLowerCase() === "transfer-encoding") continue; - respHeaders.set(k, v); - } + var _0x3c7d9a2e = _0x7c8d1e3f.method; + var _0x5f2b8e9c = _0x3c7d9a2e !== "GET" && _0x3c7d9a2e !== "HEAD"; - return new Response(upstream.body, { - status: upstream.status, - headers: respHeaders, - }); - } catch (err) { - return new Response("Bad Gateway: Tunnel Failed", { status: 502 }); - } -} + return await fetch(_0x4f1a8c5e, { + method: _0x3c7d9a2e, + headers: _0x1b3e6f9a, + body: _0x5f2b8e9c ? _0x7c8d1e3f.body : undefined, + duplex: "half", + redirect: "manual" + }); + } catch (_0x7e3d9f2c) { + console.error("relay error:", _0x7e3d9f2c); + return new Response("Bad Gateway: Tunnel Failed", { status: 502 }); + } + } +})();