ملخص المباراة رقم 6
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request))
})
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"
async function handleRequest(request) {
const url = new URL(request.url)
// 🔒 حماية الدومين
const allowedDomain = "https://www.123tv.fun"
const referer = request.headers.get("referer") || ""
const origin = request.headers.get("origin") || ""
if (!referer.startsWith(allowedDomain) && !origin.startsWith(allowedDomain)) {
return new Response("Access Denied ❌", { status: 403 })
}
// ================= MASTER PLAYLIST =================
if (url.searchParams.get("url")) {
const target = url.searchParams.get("url")
const response = await fetch(target, {
headers: { "User-Agent": windowsUA },
cf: {
cacheEverything: true,
cacheTtl: 10, // للـ live playlist قصير
cacheKey: target
}
})
const text = await response.text()
// ================= MAP =================
const mapCacheKey = new Request("https://segment-map")
let map = {}
const cache = caches.default
const oldMap = await cache.match(mapCacheKey)
if (oldMap) map = await oldMap.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].replace(/\.(ts|js)$/, "")
map[segName] = fullUrl
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",
"X-Cache-Status": "MISS"
}
})
}
// ================= SEGMENT =================
if (url.pathname.startsWith("/segment/")) {
const segName = url.pathname.split("/segment/")[1]
const cacheKey = new Request(url.toString())
const cache = caches.default
// أولاً جرب من cache Worker
let cached = await cache.match(cacheKey)
if (cached) return cached
// احصل على map
const mapRes = await cache.match("https://segment-map")
if (!mapRes) return new Response("No segment map found", { status: 500 })
const map = await mapRes.json()
const originalUrl = map[segName]
if (!originalUrl) return new Response("Segment not found", { status: 404 })
// جلب segment من المصدر باستخدام Cloudflare Edge Cache
const segmentResponse = await fetch(originalUrl, {
headers: { "User-Agent": windowsUA },
cf: {
cacheEverything: true,
cacheTtl: 180, // مدة تخزين segment
cacheKey: originalUrl
}
})
const response = new Response(segmentResponse.body, {
status: segmentResponse.status,
headers: {
"Content-Type": "video/mp2t",
"Access-Control-Allow-Origin": "*",
"Cache-Control": "public, max-age=180",
"X-Cache-Status": "MISS"
}
})
await cache.put(cacheKey, response.clone())
return response
}
return new Response("Worker Ready ✅")
}
...........
export default {
async fetch(request, env, ctx) {
const allowedDomain = "https://www.123tv.fun";
const referer = request.headers.get("referer") || "";
// 🔒 منع أي موقع آخر
if (!referer.startsWith(allowedDomain)) {
return new Response("Forbidden", { status: 403 });
}
const fullUrl = request.url;
const parts = fullUrl.split("?url=");
if (parts.length < 2) {
return new Response("Missing ?url=", { status: 400 });
}
const target = parts.slice(1).join("?url=");
const cache = caches.default;
const cacheKey = new Request(request.url, request);
// 🔥 تحقق من الكاش أولاً
let response = await cache.match(cacheKey);
if (response) return response;
const headers = {
"User-Agent":
request.headers.get("user-agent") ||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64)",
"Referer": "https://www.facebook.com/",
"Origin": "https://www.facebook.com",
};
response = await fetch(target, {
headers,
cf: { cacheEverything: true },
});
const contentType = response.headers.get("content-type") || "";
// ===== لو MPD =====
if (contentType.includes("application/dash+xml")) {
let body = await response.text();
// 🔥 تعديل إعدادات البافر
body = body.replace(/timeShiftBufferDepth="[^"]+"/, 'timeShiftBufferDepth="PT60S"');
body = body.replace(/suggestedPresentationDelay="[^"]+"/, 'suggestedPresentationDelay="PT10S"');
body = body.replace(/minBufferTime="[^"]+"/, 'minBufferTime="PT5S"');
const targetUrl = new URL(target);
const base =
targetUrl.origin +
targetUrl.pathname.substring(0, targetUrl.pathname.lastIndexOf("/") + 1);
// 🔗 تحويل الروابط إلى بروكسي
body = body.replace(/(initialization|media)="([^"]+)"/g, (match, attr, link) => {
const absolute = new URL(link, base).href;
const proxied = new URL(request.url).origin + "/?url=" + absolute;
return `${attr}="${proxied}"`;
});
response = new Response(body, {
headers: {
"Content-Type": "application/dash+xml",
"Access-Control-Allow-Origin": "*",
"Cache-Control": "public, max-age=10",
},
});
} else {
// ===== لو سيغمنت =====
response = new Response(response.body, {
headers: {
"Content-Type": contentType,
"Access-Control-Allow-Origin": "*",
"Cache-Control": "public, max-age=180",
},
});
}
// 🔥 تخزين في الكاش
ctx.waitUntil(cache.put(cacheKey, response.clone()));
return response;
},
};
............
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 (KHTML, like Gecko) Chrome/122.0 Safari/537.36"
// ================= PLAYLIST =================
if (url.searchParams.get("url")) {
const target = url.searchParams.get("url")
const streamId = btoa(target)
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/"
},
cf: {
cacheEverything: true,
cacheTtl: 10
}
})
const text = await response.text()
const mapCacheKey = new Request("https://map/" + streamId)
let map = {}
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 segName = fullUrl.split("/").pop().split("?")[0]
map[segName] = fullUrl
return `${url.origin}/segment/${streamId}/${segName}`
}).join("\n")
ctx.waitUntil(
cache.put(
mapCacheKey,
new Response(JSON.stringify(map), {
headers: { "Cache-Control": "public, max-age=180" }
})
)
)
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 — refresh playlist", { 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/"
},
cf: {
cacheEverything: true,
cacheTtl: 180
}
})
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 ✅")
}
}