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;