From f208becd275338fcfd2a554fb427921d330e3dcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=CA=9C=C9=AA=C9=B4=E1=B4=87=C9=AA=20N=E1=B4=8F=E1=B4=9C?= =?UTF-8?q?=E1=B4=A2=E1=B4=87=C9=B4?= <157171073+Shineii86@users.noreply.github.com> Date: Thu, 25 Jun 2026 08:06:39 +0530 Subject: [PATCH] fix: rewrite relative M3U8 URLs (variant playlists like 313625720.m3u8) --- api/proxy.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/api/proxy.js b/api/proxy.js index 1b0fbc3..a127b07 100644 --- a/api/proxy.js +++ b/api/proxy.js @@ -30,13 +30,21 @@ module.exports = async function handler(req, res) { if (contentType.includes("mpegurl") || url.endsWith(".m3u8") || contentType.includes("x-mpegURL")) { let m3u8 = Buffer.from(response.data).toString("utf-8"); const baseUrl = new URL(url); + const proxyBase = "/api/proxy?url="; + const refParam = `&referer=${encodeURIComponent(baseUrl.origin + "/")}`; + + // Rewrite absolute URLs (https://...) m3u8 = m3u8.replace(/^(https?:\/\/[^\s]+)$/gm, (match) => { - return `/api/proxy?url=${encodeURIComponent(match)}&referer=${encodeURIComponent(baseUrl.origin + "/")}`; + return `${proxyBase}${encodeURIComponent(match)}${refParam}`; }); - m3u8 = m3u8.replace(/^([^\s#][^\s]*\.(ts|m4s|m4v|mp4|cmfv|cmfa|vtt|srt|ass|json))(?:\?[^\s]*)?$/gm, (match) => { + + // Rewrite relative URLs (no scheme, not tags, not comments) + // Matches: filename.m3u8, filename.ts, path/file.m3u8, etc. + m3u8 = m3u8.replace(/^([^\s#<][^\s]*\.(m3u8|ts|m4s|m4v|mp4|cmfv|cmfa|vtt|srt|ass|json|aac|mp3))(?:\?[^\s]*)?$/gm, (match) => { const absolute = new URL(match, baseUrl).href; - return `/api/proxy?url=${encodeURIComponent(absolute)}&referer=${encodeURIComponent(baseUrl.origin + "/")}`; + return `${proxyBase}${encodeURIComponent(absolute)}${refParam}`; }); + res.setHeader("Content-Type", "application/vnd.apple.mpegurl"); return res.send(m3u8); }