كود سكريبت
addEventListener('fetch', event => {
event.respondWith(handleRequest(event))
})
async function handleRequest(event) {
const request = event.request
const url = new URL(request.url)
// ===== DOMAIN PROTECTION =====
const allowedDomain = "123tv.fun"
const referer = request.headers.get("referer") || ""
const host = url.hostname
if (!referer.includes(allowedDomain) && !referer.includes(host)) {
return new Response("Access Denied", { status: 403 })
}
const windowsUA =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36"
const fixedReferer = "https://beinmatch11.com"
const fixedOrigin = "https://beinmatch11.com"
// ================= MASTER PLAYLIST =================
if (url.pathname === "/master.m3u8") {
const list = url.searchParams.get("list")
if (!list) return new Response("No stream list", { status: 400 })
const streams = decodeURIComponent(list).split("|")
let master = "#EXTM3U\n\n"
streams.forEach((s, i) => {
let bw = 450000
let res = "426x240"
if (i == 0) { bw = 1800000; res = "1024x576" }
if (i == 1) { bw = 900000; res = "640x360" }
if (i == 2) { bw = 450000; res = "426x240" }
master += `#EXT-X-STREAM-INF:BANDWIDTH=${bw},RESOLUTION=${res}\n`
master += `${url.origin}?proxy=${encodeURIComponent(s)}&mode=1\n\n`
})
return new Response(master, {
headers: {
"Content-Type": "application/vnd.apple.mpegurl",
"Access-Control-Allow-Origin": "*"
}
})
}
// ================= SEGMENT =================
if (url.pathname.startsWith("/segment/")) {
const segName = url.pathname.split("/segment/")[1]
const cache = caches.default
const cacheKey = new Request(url.toString())
let cachedSegment = await cache.match(cacheKey)
if (cachedSegment) return cachedSegment
const mapCacheKey = new Request("https://segment-map")
const cachedMap = await cache.match(mapCacheKey)
if (!cachedMap) return new Response("No segment map found", { status: 500 })
const map = await cachedMap.json()
const originalUrl = map[segName]
if (!originalUrl) return new Response("Segment not found", { status: 404 })
const segmentResponse = await fetch(originalUrl, {
headers: {
"User-Agent": windowsUA,
"Referer": fixedReferer,
"Origin": fixedOrigin,
"Accept": "*/*"
}
})
const response = new Response(segmentResponse.body, {
status: segmentResponse.status,
headers: {
"Content-Type": originalUrl.includes(".js") ? "application/javascript" : "video/mp2t",
"Access-Control-Allow-Origin": "*",
"Cache-Control": "public, max-age=180, s-maxage=180, stale-while-revalidate=60"
}
})
await cache.put(cacheKey, response.clone())
return await cache.match(cacheKey)
}
// ================= PROXY =================
if (url.searchParams.get("proxy")) {
const target = url.searchParams.get("proxy")
const mode = url.searchParams.get("mode")
const response = await fetch(target, {
headers: {
"User-Agent": windowsUA,
"Referer": fixedReferer,
"Origin": fixedOrigin,
"Accept": "*/*"
}
})
const contentType = response.headers.get("content-type") || ""
if ((target.includes(".json") || target.includes(".m3u8")) && mode === "1") {
const text = await response.text()
const cache = caches.default
const mapCacheKey = new Request("https://segment-map")
let map = {}
const oldMapResponse = await cache.match(mapCacheKey)
if (oldMapResponse) map = await oldMapResponse.json()
const rewritten = text.split("\n").map(line => {
if (line.startsWith("#") || line.trim() === "") return line
const fullUrl = line.startsWith("http") ? line : new URL(line, target).href
let segName = fullUrl.split("/").pop().split("?")[0]
segName = segName.replace(/\.(js|ts)$/, "")
map[segName] = fullUrl
const segCacheKey = new Request(`${url.origin}/segment/${segName}`)
caches.default.match(segCacheKey).then(async cached => {
if (!cached) {
const r = await fetch(fullUrl, {
headers: {
"User-Agent": windowsUA,
"Referer": fixedReferer,
"Origin": fixedOrigin
}
})
const resp = new Response(r.body, {
headers: {
"Content-Type": fullUrl.includes(".js") ? "application/javascript" : "video/mp2t",
"Access-Control-Allow-Origin": "*",
"Cache-Control": "public, max-age=180, s-maxage=180, stale-while-revalidate=60"
}
})
await caches.default.put(segCacheKey, resp.clone())
}
})
return `${url.origin}/segment/${segName}`
}).join("\n")
await cache.put(mapCacheKey, new Response(JSON.stringify(map)))
return new Response(rewritten, {
headers: {
"Content-Type": "application/vnd.apple.mpegurl",
"Access-Control-Allow-Origin": "*"
}
})
}
return new Response(response.body, {
status: response.status,
headers: {
"Content-Type": contentType,
"Access-Control-Allow-Origin": "*"
}
})
}
// ================= PLAYER =================
const video0Url = url.searchParams.get("video0")
const video1Url = url.searchParams.get("video1")
let proxiedUrl = ""
let playerType = "plyr"
if (video0Url) {
playerType = "videojs"
if (video0Url.includes("|")) {
proxiedUrl = `${url.origin}/master.m3u8?list=${encodeURIComponent(video0Url)}`
} else {
proxiedUrl = `${url.origin}?proxy=${encodeURIComponent(video0Url)}&mode=1`
}
} else if (video1Url) {
playerType = "plyr"
if (video1Url.includes("|")) {
proxiedUrl = `${url.origin}/master.m3u8?list=${encodeURIComponent(video1Url)}`
} else {
proxiedUrl = `${url.origin}?proxy=${encodeURIComponent(video1Url)}&mode=1`
}
} else {
proxiedUrl = `${url.origin}?proxy=${encodeURIComponent("https://test-streams.mux.dev/x36xhzz/x36xhzz.m3u8")}&mode=1`
}
// ================= HTML PLAYER =================
return new Response(`
`, {
headers: {
"Content-Type": "text/html;charset=UTF-8",
"Content-Security-Policy": "frame-ancestors https://www.123tv.fun"
}
})
}