From bdae587bd5f9b6f2ed31bcd7eea727d466422dbd Mon Sep 17 00:00:00 2001 From: kuekhaoyang Date: Sat, 29 Nov 2025 14:50:48 +0800 Subject: [PATCH] feat: proxy HLS manifest encryption key URIs and refine segment URL proxying --- lib/utils/proxy-utils.ts | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/utils/proxy-utils.ts b/lib/utils/proxy-utils.ts index 7dcd1a1..afc1afc 100644 --- a/lib/utils/proxy-utils.ts +++ b/lib/utils/proxy-utils.ts @@ -8,15 +8,32 @@ export async function processM3u8Content( const base = new URL(baseUrl); const processedLines = lines.map(line => { - // Skip comments and empty lines - if (line.trim().startsWith('#') || !line.trim()) { + const trimmed = line.trim(); + + // Handle EXT-X-KEY encryption keys + if (trimmed.startsWith('#EXT-X-KEY:')) { + // Extract URI from the key tag + const uriMatch = trimmed.match(/URI="([^"]+)"/); + if (uriMatch && uriMatch[1]) { + const keyUri = uriMatch[1]; + try { + const absoluteUrl = new URL(keyUri, base).toString(); + const proxiedUrl = `${origin}/api/proxy?url=${encodeURIComponent(absoluteUrl)}`; + return trimmed.replace(/URI="[^"]+"/, `URI="${proxiedUrl}"`); + } catch { + return line; + } + } + } + + // Skip other comments and empty lines + if (trimmed.startsWith('#') || !trimmed) { return line; } - // Resolve relative URLs + // Resolve relative URLs for segments try { - const absoluteUrl = new URL(line.trim(), base).toString(); - // Wrap in proxy + const absoluteUrl = new URL(trimmed, base).toString(); return `${origin}/api/proxy?url=${encodeURIComponent(absoluteUrl)}`; } catch { return line;