From 4fbc70bd5a2056a5367e443c807b935c3b448d30 Mon Sep 17 00:00:00 2001 From: swartie800 <146176789+swartie800@users.noreply.github.com> Date: Tue, 3 Oct 2023 10:42:31 +0700 Subject: [PATCH] Update index.ts redo ip blocking --- src/index.ts | 117 ++++++++++++++++++++++----------------------------- 1 file changed, 50 insertions(+), 67 deletions(-) diff --git a/src/index.ts b/src/index.ts index b5d5bcb..4cd95f6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,79 +1,62 @@ addEventListener("fetch", (event) => { - event.respondWith(allowOnlySpecificIP(event.request)); + event.respondWith(respondfetch(event.request)); }); -async function allowOnlySpecificIP(request) { - const allowedIP = "37.221.67.131"; // Replace with your allowed IP address +async function respondfetch(request) { + try { + const url = new URL(request.url); + const refererUrl = decodeURIComponent(url.searchParams.get("referer") || ""); + const targetUrl = decodeURIComponent(url.searchParams.get("url") || ""); + const originUrl = decodeURIComponent(url.searchParams.get("origin") || ""); + const proxyAll = decodeURIComponent(url.searchParams.get("all") || ""); - // Get the client's IP address from the request headers - const clientIP = request.headers.get("cf-connecting-ip") || ""; - - if (clientIP === allowedIP) { - // If the client's IP matches the allowed IP, allow the request to proceed - try { - const url = new URL(request.url); - const refererUrl = decodeURIComponent(url.searchParams.get("referer") || ""); - const targetUrl = decodeURIComponent(url.searchParams.get("url") || ""); - const originUrl = decodeURIComponent(url.searchParams.get("origin") || ""); - const proxyAll = decodeURIComponent(url.searchParams.get("all") || ""); - - if (!targetUrl) { - return new Response("Invalid URL", { status: 400 }); - } - - const response = await fetch(targetUrl, { - headers: { - "Access-Control-Allow-Origin": "*", - "Access-Control-Allow-Methods": "GET, HEAD, POST, PUT, DELETE, OPTIONS", - "Access-Control-Allow-Headers": "Content-Type", - Referer: refererUrl || "", - Origin: originUrl || "", - }, - }); + if (!targetUrl) { + return new Response("Invalid URL", { status: 400 }); + } - let modifiedM3u8; - if (targetUrl.includes(".m3u8")) { - modifiedM3u8 = await response.text(); - const targetUrlTrimmed = `${encodeURIComponent( - targetUrl.replace(/([^/]+\.m3u8)$/, "").trim() - )}`; - const encodedUrl = encodeURIComponent(refererUrl); - const encodedOrigin = encodeURIComponent(originUrl); - modifiedM3u8 = modifiedM3u8.split("\n").map((line) => { - if (line.startsWith("#") || line.trim() == "") { - return line; - } else if (proxyAll == 'yes' && line.startsWith('http')) { - // https://yourproxy.com/?url=https://somevideo.m3u8&all=yes - return `${url.origin}?url=${line}`; - } - return `?url=${targetUrlTrimmed}${line}${originUrl ? `&origin=${encodedOrigin}` : "" - }${refererUrl ? `&referer=${encodedUrl}` : "" - }`; - }).join("\n"); - } + const response = await fetch(targetUrl, { + headers: { + "Access-Control-Allow-Origin": "*", + "Access-Control-Allow-Methods": "GET, HEAD, POST, PUT, DELETE, OPTIONS", + "Access-Control-Allow-Headers": "Content-Type", + Referer: refererUrl || "", + Origin: originUrl || "", + }, + }); - return new Response(modifiedM3u8 || response.body, { - status: response.status, - statusText: response.statusText, - headers: { - "Access-Control-Allow-Origin": "*", - "Content-Type": - response.headers?.get("Content-Type") || - "application/vnd.apple.mpegurl", - "Content-Disposition": `attachment; filename="output.m3u8"`, // Set the desired filename - }, - }); - } catch (e) { - return new Response(e.message, { status: 500 }); + let modifiedM3u8; + if (targetUrl.includes(".m3u8")) { + modifiedM3u8 = await response.text(); + const targetUrlTrimmed = `${encodeURIComponent( + targetUrl.replace(/([^/]+\.m3u8)$/, "").trim() + )}`; + const encodedUrl = encodeURIComponent(refererUrl); + const encodedOrigin = encodeURIComponent(originUrl); + modifiedM3u8 = modifiedM3u8.split("\n").map((line) => { + if (line.startsWith("#") || line.trim() == "") { + return line; + } else if (proxyAll == 'yes' && line.startsWith('http')) { + // https://yourproxy.com/?url=https://somevideo.m3u8&all=yes + return `${url.origin}?url=${line}`; + } + return `?url=${targetUrlTrimmed}${line}${originUrl ? `&origin=${encodedOrigin}` : "" + }${refererUrl ? `&referer=${encodedUrl}` : "" + }`; + }).join("\n"); } - } else { - // If the client's IP does not match the allowed IP, return a 403 Forbidden response - return new Response("Access Forbidden", { - status: 403, - statusText: "Forbidden", + + return new Response(modifiedM3u8 || response.body, { + status: response.status, + statusText: response.statusText, headers: { - "Content-Type": "text/plain", + "Access-Control-Allow-Origin": "*", + "Content-Type": + response.headers?.get("Content-Type") || + "application/vnd.apple.mpegurl", + "Content-Disposition": `attachment; filename="output.m3u8"`, // Set the desired filename }, }); + } catch (e) { + return new Response(e.message, { status: 500 }); } }