Skip to content

Commit

Permalink
Update index.ts
Browse files Browse the repository at this point in the history
redo ip blocking
  • Loading branch information
swartie800 authored Oct 3, 2023
1 parent b00886f commit 4fbc70b
Showing 1 changed file with 50 additions and 67 deletions.
117 changes: 50 additions & 67 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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 });
}
}

0 comments on commit 4fbc70b

Please sign in to comment.