بث مباشر بين سبورت | bein sport live 123 TV

export default { async fetch(request, env, ctx) { const referer = request.headers.get("Referer") || ""; const origin = request.headers.get("Origin") || ""; const allowedDomain = "123tv.fun"; if ( !referer.includes(allowedDomain) && !origin.includes(allowedDomain) ) { return new Response("Forbidden", { status: 403 }); } const target = new URL(request.url).searchParams.get("url"); if (!target) { return new Response("Missing url", { status: 400 }); } const isM3U8 = target.includes(".m3u8"); const isTS = target.includes(".ts"); const cache = caches.default; const cacheKey = new Request(request.url, request); if (!isM3U8) { const cached = await cache.match(cacheKey); if (cached) { return cached; } } const res = await fetch(target, { headers: { "Referer": "https://player.syria-player.live/albaplayer/beinmax3/?serv=1", "origin": "https://player.syria-player.live", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36" } }); let response; if (isM3U8) { let text = await res.text(); const base = target.substring( 0, target.lastIndexOf("/") + 1 ); // تحويل جميع الروابط إلى Worker text = text.replace( /^(?!#)(.+)$/gm, line => { if (!line.trim()) return line; if ( line.startsWith("http://") || line.startsWith("https://") ) { return request.url.split("?")[0] + "?url=" + encodeURIComponent(line); } return request.url.split("?")[0] + "?url=" + encodeURIComponent(base + line); } ); // ========================== // إضافة 23 مقطعًا سابقًا // ========================== const EXTRA_SEGMENTS = 23; const seqMatch = text.match(/#EXT-X-MEDIA-SEQUENCE:(\d+)/); if (seqMatch) { const currentSeq = parseInt(seqMatch[1], 10); const startSeq = Math.max(0, currentSeq - EXTRA_SEGMENTS); const segmentRegex = /#EXTINF:([\d.]+),\s*\n([^\n]+)/g; const matches = [...text.matchAll(segmentRegex)]; if (matches.length > 0) { const duration = matches[0][1]; const workerUrl = matches[0][2]; const realUrl = decodeURIComponent(workerUrl.split("?url=")[1]); const match = realUrl.match(/^(.*?)(\d+)\.ts(.*)$/); if (match) { const prefix = match[1]; const suffix = match[3]; let extra = ""; for (let i = startSeq; i < currentSeq; i++) { extra += `#EXTINF:${duration},\n`; extra += request.url.split("?")[0] + "?url=" + encodeURIComponent(`${prefix}${i}.ts${suffix}`) + "\n"; } // تحديث Media Sequence text = text.replace( /#EXT-X-MEDIA-SEQUENCE:\d+/, `#EXT-X-MEDIA-SEQUENCE:${startSeq}` ); // إدراج المقاطع قبل أول EXTINF const firstExtInf = text.indexOf("#EXTINF:"); if (firstExtInf !== -1) { text = text.slice(0, firstExtInf) + extra + text.slice(firstExtInf); } } } } response = new Response(text, { headers: { "Content-Type": "application/vnd.apple.mpegurl", "Access-Control-Allow-Origin": "*", "Cache-Control": "no-store, no-cache, must-revalidate" } }); } else { const headers = new Headers(res.headers); if (isTS) { headers.set("Content-Type", "video/mp2t"); headers.set("Cache-Control", "public, max-age=180"); } headers.set("Access-Control-Allow-Origin", "*"); response = new Response(res.body, { status: res.status, statusText: res.statusText, headers }); ctx.waitUntil( cache.put(cacheKey, response.clone()) ); } return response; } };