| |
| |
| |
| function proxyUriInTag(line: string, base: URL, origin: string): string { |
| const uriMatch = line.match(/URI="([^"]+)"/); |
| if (uriMatch && uriMatch[1]) { |
| const uri = uriMatch[1]; |
| |
| if (uri.includes('/api/proxy')) { |
| return line; |
| } |
| try { |
| const absoluteUrl = new URL(uri, base).toString(); |
| const proxiedUrl = `${origin}/api/proxy?url=${encodeURIComponent(absoluteUrl)}`; |
| return line.replace(/URI="[^"]+"/, `URI="${proxiedUrl}"`); |
| } catch { |
| return line; |
| } |
| } |
| return line; |
| } |
|
|
| export async function processM3u8Content( |
| content: string, |
| baseUrl: string, |
| origin: string |
| ): Promise<string> { |
| const lines = content.split('\n'); |
| const base = new URL(baseUrl); |
|
|
| const processedLines = lines.map(line => { |
| const trimmed = line.trim(); |
|
|
| |
| if (trimmed.startsWith('#EXT-X-KEY:')) { |
| return proxyUriInTag(trimmed, base, origin); |
| } |
|
|
| |
| if (trimmed.startsWith('#EXT-X-MAP:')) { |
| return proxyUriInTag(trimmed, base, origin); |
| } |
|
|
| |
| if (trimmed.startsWith('#EXT-X-MEDIA:')) { |
| return proxyUriInTag(trimmed, base, origin); |
| } |
|
|
| |
| |
| if (trimmed.startsWith('#EXT-X-STREAM-INF:')) { |
| return line; |
| } |
|
|
| |
| if (trimmed.startsWith('#') || !trimmed) { |
| return line; |
| } |
|
|
| |
| |
| if (trimmed.includes('/api/proxy')) { |
| return line; |
| } |
|
|
| try { |
| const absoluteUrl = new URL(trimmed, base).toString(); |
| return `${origin}/api/proxy?url=${encodeURIComponent(absoluteUrl)}`; |
| } catch { |
| return line; |
| } |
| }); |
|
|
| return processedLines.join('\n'); |
| } |
|
|