Spaces:
Paused

Download / s4itmm-proxy
LiveSportmm's picture
Create s4itmm-proxy
04a4c1e verified
Raw History Blame Contribute Delete
2.27 kB
export default {
async fetch(request, env) {
const url = new URL(request.url);
const targetUrl = url.searchParams.get('url');
if (!targetUrl) {
return new Response('No URL provided', { status: 400 });
}
try {
const response = await fetch(targetUrl, {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36',
'Referer': new URL(targetUrl).origin
}
});
const contentType = response.headers.get('content-type') || '';
// M3U8 Playlist ဖြစ်ခဲ့ရင် အထဲက Link တွေကို Proxy ပြန်ပတ်မယ်
if (targetUrl.includes('.m3u8') || contentType.includes('mpegurl')) {
let text = await response.text();
const basePath = targetUrl.substring(0, targetUrl.lastIndexOf('/') + 1);
const proxyBase = `${url.origin}${url.pathname}?url=`;
// Absolute link မဟုတ်တဲ့ ကောင်တွေကို လိုက်ပြင်တာ
text = text.replace(/^(?!http)(.*)$/mg, (match) => {
if (match.trim() === '' || match.startsWith('#')) return match;
let fullUrl;
if (match.startsWith('/')) {
fullUrl = new URL(targetUrl).origin + match;
} else {
fullUrl = basePath + match;
}
return proxyBase + encodeURIComponent(fullUrl);
});
return new Response(text, {
headers: {
'Content-Type': 'application/vnd.apple.mpegurl',
'Access-Control-Allow-Origin': '*'
}
});
}
// Video segments (.ts) သို့မဟုတ် အခြား binary data များအတွက်
const data = await response.arrayBuffer();
return new Response(data, {
headers: {
'Content-Type': contentType || 'video/mp2t',
'Access-Control-Allow-Origin': '*',
'Cache-Control': 'public, max-age=3600' // Speed မြန်အောင် 1 နာရီ Cache လုပ်ထားမယ်
}
});
} catch (e) {
return new Response('Error: ' + e.message, { status: 500 });
}
}
};