ملخص المباراة رقم 3
addEventListener('fetch', event => {
event.respondWith(handleRequest(event))
})
async function handleRequest(event) {
const request = event.request
const url = new URL(request.url)
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 =================
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": "*",
"Cache-Control": "no-store, no-cache, must-revalidate"
}
})
}
// ================= SEGMENT =================
if (url.pathname.startsWith("/segment/")) {
const segName = decodeURIComponent(url.pathname.replace("/segment/", ""))
const cache = caches.default
const cacheKey = new Request(url.toString())
let cached = await cache.match(cacheKey)
if (cached) return cached
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 r = await fetch(originalUrl, {
headers: {
"User-Agent": windowsUA,
"Referer": fixedReferer,
"Origin": fixedOrigin
}
})
const response = new Response(r.body, {
status: r.status,
headers: {
"Content-Type": originalUrl.includes(".js")
? "application/javascript"
: "video/mp2t",
"Access-Control-Allow-Origin": "*",
"Cache-Control": "public, max-age=180"
}
})
await cache.put(cacheKey, response.clone())
return response
}
// ================= 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
}
})
const contentType = response.headers.get("content-type") || ""
if ((target.includes(".m3u8") || target.includes(".json")) && mode === "1") {
const text = await response.text()
const cache = caches.default
const mapCacheKey = new Request("https://segment-map")
let map = {}
const old = await cache.match(mapCacheKey)
if (old) map = await old.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
const u = new URL(fullUrl)
let segName = u.search.replace("?", "")
if (!segName) {
segName = u.pathname.split("/").pop().replace(/\.(js|ts)$/, "")
}
map[segName] = fullUrl
const segCacheKey = new Request(
`${url.origin}/segment/${encodeURIComponent(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",
"Cache-Control": "public, max-age=180"
}
})
await caches.default.put(segCacheKey, resp.clone())
}
})
return `${url.origin}/segment/${encodeURIComponent(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": "*",
"Cache-Control": "no-store, no-cache, must-revalidate"
}
})
}
return new Response(response.body, {
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"
const build = (v) => {
if (!v) return null
if (v.includes("|")) {
return `${url.origin}/master.m3u8?list=${encodeURIComponent(v)}`
}
return `${url.origin}?proxy=${encodeURIComponent(v)}&mode=1`
}
proxiedUrl =
build(video0Url) ||
build(video1Url) ||
`${url.origin}?proxy=${encodeURIComponent("https://test-streams.mux.dev/x36xhzz/x36xhzz.m3u8")}&mode=1`
if (video0Url) playerType = "videojs"
return new Response(`
`, {
headers: {
"Content-Type": "text/html;charset=UTF-8",
"Content-Security-Policy": "frame-ancestors https://www.123tv.fun"
}
})
}