export default { async fetch(request) { const targetBase = "http://161.248.38.40:8000"; const url = new URL(request.url); const targetUrl = targetBase + url.pathname + url.search; const response = await fetch(targetUrl); const contentType = response.headers.get("content-type") || ""; // If m3u8 playlist, rewrite segment URLs if (contentType.includes("application/vnd.apple.mpegurl") || url.pathname.endsWith(".m3u8")) { let text = await response.text(); // Rewrite relative URLs text = text.replace(/(?!#)(^.*\.m3u8.*$)/gm, (match) => { if (match.startsWith("http")) return match; return url.origin + match; }); text = text.replace(/(?!#)(^.*\.ts.*$)/gm, (match) => { if (match.startsWith("http")) return match; return url.origin + match; }); return new Response(text, { headers: { "Content-Type": "application/vnd.apple.mpegurl", "Access-Control-Allow-Origin": "*" } }); } // For .ts segments return new Response(response.body, { headers: { "Content-Type": contentType, "Access-Control-Allow-Origin": "*" } }); } };