pstream is dead; long live pstream
taciturnaxolotl.github.io/pstream-ng/
1import { getLoadbalancedM3U8ProxyUrl } from "@/backend/providers/fetchers";
2import { getM3U8ProxyUrls } from "@/utils/proxyUrls";
3
4/**
5 * Creates a proxied M3U8 URL for HLS streams using a random proxy from config
6 * @param url - The original M3U8 URL to proxy
7 * @param headers - Headers to include with the request
8 * @returns The proxied M3U8 URL
9 */
10export function createM3U8ProxyUrl(
11 url: string,
12 headers: Record<string, string> = {},
13): string {
14 // Get a random M3U8 proxy URL from the configuration
15 const proxyBaseUrl = getLoadbalancedM3U8ProxyUrl();
16
17 if (!proxyBaseUrl) {
18 console.warn("No M3U8 proxy URLs available in configuration");
19 return url; // Fallback to original URL
20 }
21
22 const encodedUrl = encodeURIComponent(url);
23 const encodedHeaders = encodeURIComponent(JSON.stringify(headers));
24 return `${proxyBaseUrl}/m3u8-proxy?url=${encodedUrl}${headers ? `&headers=${encodedHeaders}` : ""}`;
25}
26
27/**
28 * TODO: Creates a proxied MP4 URL for MP4 streams
29 * @param url - The original MP4 URL to proxy
30 * @param headers - Headers to include with the request
31 * @returns The proxied MP4 URL
32 */
33export function createMP4ProxyUrl(
34 url: string,
35 _headers: Record<string, string> = {},
36): string {
37 // TODO: Implement MP4 proxy for protected streams
38 // This would need a separate MP4 proxy service that can handle headers
39 // For now, return the original URL
40 console.warn("MP4 proxy not yet implemented - using original URL");
41 return url;
42}
43
44/**
45 * Checks if a URL is already using one of the configured M3U8 proxy services
46 * @param url - The URL to check
47 * @returns True if the URL is already proxied, false otherwise
48 */
49export function isUrlAlreadyProxied(url: string): boolean {
50 // Check if URL contains the m3u8-proxy pattern (Airplay format)
51 if (url.includes("/m3u8-proxy?url=")) {
52 return true;
53 }
54
55 // Check if URL contains the destination pattern (Chromecast format)
56 if (url.includes("/?destination=")) {
57 return true;
58 }
59
60 // Also check if URL starts with any of the configured proxy URLs
61 const proxyUrls = getM3U8ProxyUrls();
62 return proxyUrls.some((proxyUrl) => url.startsWith(proxyUrl));
63}