forked from Gratenes/m3u8CloudflareWorkerProxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
1 changed file
with
50 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
} |