كود سكريبت بث مباشر بين سبورت | bein sport live كود سكريبت

كود سكريبت

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": "*", "Cache-Control": "public, max-age=10, s-maxage=10" } }) } // ================= 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, { cf: { cacheEverything: true, cacheTtl: 10 }, 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": "*", "Cache-Control": "public, max-age=10, s-maxage=10" } }) } 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" } }) } ............... export default { async fetch(request, env, ctx) { const allowedOrigin = "https://www.123tv.fun" const origin = request.headers.get("origin") || "" const referer = request.headers.get("referer") || "" if ( !origin.startsWith(allowedOrigin) && !referer.startsWith(allowedOrigin) ) { return new Response("Forbidden", { status: 403 }) } const url = new URL(request.url) const cache = caches.default const customUA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/122 Safari/537.36" // ================= PLAYLIST ================= if (url.searchParams.get("url")) { const target = url.searchParams.get("url") const streamId = btoa(target).replace(/=/g, "") const playlistCacheKey = new Request(url.toString()) let cachedPlaylist = await cache.match(playlistCacheKey) if (cachedPlaylist) return cachedPlaylist const response = await fetch(target, { headers: { "User-Agent": customUA, "Referer": "https://ok.ru/" } }) let text = await response.text() const baseUrl = target // ================= MASTER OR MEDIA DETECTION ================= const isMaster = text.includes("#EXT-X-STREAM-INF") const mapCacheKey = new Request("https://map/" + streamId) let map = {} const rewritten = text.split("\n").map(line => { if (!line || line.startsWith("#")) return line const fullUrl = line.startsWith("http") ? line : new URL(line, baseUrl).href const fileName = fullUrl.split("?")[0].split("/").pop() map[fileName] = fullUrl // 🔥 إذا كان Master Playlist (m3u8 داخل m3u8) if (isMaster) { const nestedId = btoa(fullUrl).replace(/=/g, "") return `${url.origin}/?url=${encodeURIComponent(fullUrl)}` } // 🔥 Media Playlist → segments return `${url.origin}/segment/${streamId}/${fileName}` }).join("\n") ctx.waitUntil( cache.put( mapCacheKey, new Response(JSON.stringify(map), { headers: { "Cache-Control": "public, max-age=3600" } }) ) ) const finalResponse = new Response(rewritten, { headers: { "Content-Type": "application/vnd.apple.mpegurl", "Access-Control-Allow-Origin": allowedOrigin, "Cache-Control": "public, max-age=10" } }) ctx.waitUntil(cache.put(playlistCacheKey, finalResponse.clone())) return finalResponse } // ================= SEGMENT ================= if (url.pathname.startsWith("/segment/")) { const parts = url.pathname.split("/") const streamId = parts[2] const segName = parts[3] const cacheKey = new Request(url.toString()) let cached = await cache.match(cacheKey) if (cached) return cached const mapCacheKey = new Request("https://map/" + streamId) const mapRes = await cache.match(mapCacheKey) if (!mapRes) { return new Response("Map expired", { status: 404 }) } const map = await mapRes.json() const originalUrl = map[segName] if (!originalUrl) { return new Response("Segment not found", { status: 404 }) } const segmentResponse = await fetch(originalUrl, { headers: { "User-Agent": customUA, "Referer": "https://ok.ru/" } }) const response = new Response(segmentResponse.body, { status: segmentResponse.status, headers: { "Content-Type": "video/mp2t", "Access-Control-Allow-Origin": allowedOrigin, "Cache-Control": "public, max-age=180" } }) ctx.waitUntil(cache.put(cacheKey, response.clone())) return response } return new Response("Worker Ready ✅") } }