feat: proxy HLS manifest encryption key URIs and refine segment URL proxying

This commit is contained in:
kuekhaoyang
2025-11-29 14:50:48 +08:00
parent a4ff394695
commit bdae587bd5
+22 -5
View File
@@ -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;