This commit is contained in:
Pas 2025-05-14 17:42:51 -06:00
parent 146f3a0b42
commit eeb18a0efa
2 changed files with 255 additions and 167 deletions

View file

@ -3,26 +3,40 @@
* @description Proxies m3u8 files and their segments * @description Proxies m3u8 files and their segments
*/ */
// Helper function to safely create a URL object
function safeCreateURL(url: string, base?: string): URL | null {
try {
return base ? new URL(url, base) : new URL(url);
} catch (error) {
console.error("URL parsing error:", error, "URL:", url, "Base:", base);
return null;
}
}
// Helper function to parse URLs // Helper function to parse URLs
function parseURL(req_url: string, baseUrl?: string) { function parseURL(req_url: string, baseUrl?: string) {
try { try {
if (baseUrl) { if (baseUrl) {
return new URL(req_url, baseUrl).toString(); const url = safeCreateURL(req_url, baseUrl);
return url ? url.toString() : null;
} }
// If it's already a fully qualified URL // If it's already a fully qualified URL
if (/^https?:\/\//i.test(req_url)) { if (/^https?:\/\//i.test(req_url)) {
return new URL(req_url).toString(); const url = safeCreateURL(req_url);
return url ? url.toString() : null;
} }
// If it starts with // (protocol-relative URL) // If it starts with // (protocol-relative URL)
if (req_url.startsWith('//')) { if (req_url.startsWith('//')) {
return new URL(`https:${req_url}`).toString(); const url = safeCreateURL(`https:${req_url}`);
return url ? url.toString() : null;
} }
// If it's a relative URL and we have a base URL // If it's a relative URL and we have a base URL
if (baseUrl) { if (baseUrl) {
return new URL(req_url, baseUrl).toString(); const url = safeCreateURL(req_url, baseUrl);
return url ? url.toString() : null;
} }
return null; return null;
@ -45,181 +59,240 @@ export default async function(request: Request, env: any, ctx: any) {
}); });
} }
// Parse URL parameters
const url = new URL(request.url);
const targetUrl = url.searchParams.get('url');
const headersParam = url.searchParams.get('headers');
if (!targetUrl) {
return new Response('URL parameter is required', { status: 400 });
}
console.log("Processing m3u8 request for URL:", targetUrl);
let customHeaders = {};
try { try {
customHeaders = headersParam ? JSON.parse(headersParam) : {}; // Parse URL parameters
console.log("With headers:", JSON.stringify(customHeaders)); const url = new URL(request.url);
} catch (e) { const targetUrl = url.searchParams.get('url');
return new Response('Invalid headers format', { status: 400 }); const headersParam = url.searchParams.get('headers');
}
try {
// Create base request headers
const requestHeaders = new Headers({
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36',
'Accept': '*/*',
'Accept-Language': 'en-US,en;q=0.9',
...customHeaders
});
console.log("Making request to:", targetUrl); if (!targetUrl) {
console.log("With headers:", JSON.stringify(Object.fromEntries(requestHeaders.entries()))); return new Response('URL parameter is required', { status: 400 });
// Use fetch API which is natively available in Workers
const response = await fetch(targetUrl, {
headers: requestHeaders
});
if (!response.ok) {
throw new Error(`Failed to fetch M3U8: ${response.status} ${response.statusText}`);
} }
const m3u8Content = await response.text(); console.log("Processing m3u8 request for URL:", targetUrl);
// Get the base URL for the host let customHeaders = {};
const host = url.hostname; try {
const proto = url.protocol; customHeaders = headersParam ? JSON.parse(headersParam) : {};
const baseProxyUrl = `${proto}//${host}`; console.log("With headers:", JSON.stringify(customHeaders));
} catch (e) {
return new Response('Invalid headers format', { status: 400 });
}
const responseHeaders = { // Validate target URL
'Content-Type': 'application/vnd.apple.mpegurl', const validatedUrl = safeCreateURL(targetUrl);
'Access-Control-Allow-Origin': '*', if (!validatedUrl) {
'Access-Control-Allow-Headers': '*', return new Response(`Invalid target URL: ${targetUrl}`, {
'Access-Control-Allow-Methods': '*', status: 400,
'Cache-Control': 'no-cache, no-store, must-revalidate' headers: { 'Access-Control-Allow-Origin': '*' }
}; });
}
if (m3u8Content.includes("RESOLUTION=")) {
// This is a master playlist with multiple quality variants try {
const lines = m3u8Content.split("\n"); // Create base request headers
const newLines: string[] = []; const requestHeaders = new Headers({
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36',
for (const line of lines) { 'Accept': '*/*',
if (line.startsWith("#")) { 'Accept-Language': 'en-US,en;q=0.9',
if (line.startsWith("#EXT-X-KEY:")) { ...customHeaders
// Proxy the key URL
const regex = /URI="([^"]+)"/g;
const match = regex.exec(line);
const keyUrl = match ? match[1] : null;
if (keyUrl) {
let fullKeyUrl;
try {
// Try to get the full URL
fullKeyUrl = keyUrl.startsWith('http') ? keyUrl : new URL(keyUrl, targetUrl).toString();
const proxyKeyUrl = `${baseProxyUrl}/ts-proxy?url=${encodeURIComponent(fullKeyUrl)}&headers=${encodeURIComponent(JSON.stringify(customHeaders))}`;
newLines.push(line.replace(keyUrl, proxyKeyUrl));
} catch (error) {
console.error("Error processing key URL:", keyUrl, error);
newLines.push(line); // Keep original if error
}
} else {
newLines.push(line);
}
} else if (line.startsWith("#EXT-X-MEDIA:")) {
// Proxy alternative media URLs (like audio streams)
const regex = /URI="([^"]+)"/g;
const match = regex.exec(line);
const mediaUrl = match ? match[1] : null;
if (mediaUrl) {
try {
// Try to get the full URL
const fullMediaUrl = mediaUrl.startsWith('http') ? mediaUrl : new URL(mediaUrl, targetUrl).toString();
const proxyMediaUrl = `${baseProxyUrl}/m3u8-proxy?url=${encodeURIComponent(fullMediaUrl)}&headers=${encodeURIComponent(JSON.stringify(customHeaders))}`;
newLines.push(line.replace(mediaUrl, proxyMediaUrl));
} catch (error) {
console.error("Error processing media URL:", mediaUrl, error);
newLines.push(line); // Keep original if error
}
} else {
newLines.push(line);
}
} else {
newLines.push(line);
}
} else if (line.trim()) {
// This is a quality variant URL
try {
// Try to create a full URL
const variantUrl = line.startsWith('http') ? line : new URL(line, targetUrl).toString();
newLines.push(`${baseProxyUrl}/m3u8-proxy?url=${encodeURIComponent(variantUrl)}&headers=${encodeURIComponent(JSON.stringify(customHeaders))}`);
} catch (error) {
console.error("Error processing variant URL:", line, error);
newLines.push(line); // Keep original if error
}
} else {
// Empty line, preserve it
newLines.push(line);
}
}
return new Response(newLines.join("\n"), {
headers: responseHeaders
}); });
} else {
// This is a media playlist with segments
const lines = m3u8Content.split("\n");
const newLines: string[] = [];
for (const line of lines) { console.log("Making request to:", validatedUrl.toString());
if (line.startsWith("#")) { console.log("With headers:", JSON.stringify(Object.fromEntries(requestHeaders.entries())));
if (line.startsWith("#EXT-X-KEY:")) {
// Proxy the key URL // Use fetch API which is natively available in Workers
const regex = /URI="([^"]+)"/g; const response = await fetch(validatedUrl.toString(), {
const match = regex.exec(line); headers: requestHeaders
const keyUrl = match ? match[1] : null; });
if (keyUrl) { if (!response.ok) {
try { throw new Error(`Failed to fetch M3U8: ${response.status} ${response.statusText}`);
// Try to get the full URL }
const fullKeyUrl = keyUrl.startsWith('http') ? keyUrl : new URL(keyUrl, targetUrl).toString();
const proxyKeyUrl = `${baseProxyUrl}/ts-proxy?url=${encodeURIComponent(fullKeyUrl)}&headers=${encodeURIComponent(JSON.stringify(customHeaders))}`; const m3u8Content = await response.text();
newLines.push(line.replace(keyUrl, proxyKeyUrl));
} catch (error) { // Get the base URL for the host
console.error("Error processing key URL:", keyUrl, error); const host = url.hostname;
newLines.push(line); // Keep original if error const proto = url.protocol;
const baseProxyUrl = `${proto}//${host}`;
const responseHeaders = {
'Content-Type': 'application/vnd.apple.mpegurl',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': '*',
'Access-Control-Allow-Methods': '*',
'Cache-Control': 'no-cache, no-store, must-revalidate'
};
if (m3u8Content.includes("RESOLUTION=")) {
// This is a master playlist with multiple quality variants
const lines = m3u8Content.split("\n");
const newLines: string[] = [];
for (const line of lines) {
if (line.startsWith("#")) {
if (line.startsWith("#EXT-X-KEY:")) {
// Proxy the key URL
const regex = /URI="([^"]+)"/g;
const match = regex.exec(line);
const keyUrl = match ? match[1] : null;
if (keyUrl) {
let fullKeyUrl;
try {
// Try to get the full URL
const keyUrlObj = keyUrl.startsWith('http')
? safeCreateURL(keyUrl)
: safeCreateURL(keyUrl, validatedUrl.toString());
if (!keyUrlObj) {
throw new Error("Could not create valid URL for key");
}
fullKeyUrl = keyUrlObj.toString();
const proxyKeyUrl = `${baseProxyUrl}/ts-proxy?url=${encodeURIComponent(fullKeyUrl)}&headers=${encodeURIComponent(JSON.stringify(customHeaders))}`;
newLines.push(line.replace(keyUrl, proxyKeyUrl));
} catch (error) {
console.error("Error processing key URL:", keyUrl, error);
newLines.push(line); // Keep original if error
}
} else {
newLines.push(line);
}
} else if (line.startsWith("#EXT-X-MEDIA:")) {
// Proxy alternative media URLs (like audio streams)
const regex = /URI="([^"]+)"/g;
const match = regex.exec(line);
const mediaUrl = match ? match[1] : null;
if (mediaUrl) {
try {
// Try to get the full URL
const mediaUrlObj = mediaUrl.startsWith('http')
? safeCreateURL(mediaUrl)
: safeCreateURL(mediaUrl, validatedUrl.toString());
if (!mediaUrlObj) {
throw new Error("Could not create valid URL for media");
}
const fullMediaUrl = mediaUrlObj.toString();
const proxyMediaUrl = `${baseProxyUrl}/m3u8-proxy?url=${encodeURIComponent(fullMediaUrl)}&headers=${encodeURIComponent(JSON.stringify(customHeaders))}`;
newLines.push(line.replace(mediaUrl, proxyMediaUrl));
} catch (error) {
console.error("Error processing media URL:", mediaUrl, error);
newLines.push(line); // Keep original if error
}
} else {
newLines.push(line);
} }
} else { } else {
newLines.push(line); newLines.push(line);
} }
} else if (line.trim()) {
// This is a quality variant URL
try {
// Try to create a full URL
const variantUrlObj = line.startsWith('http')
? safeCreateURL(line)
: safeCreateURL(line, validatedUrl.toString());
if (!variantUrlObj) {
throw new Error("Could not create valid URL for variant");
}
const variantUrl = variantUrlObj.toString();
newLines.push(`${baseProxyUrl}/m3u8-proxy?url=${encodeURIComponent(variantUrl)}&headers=${encodeURIComponent(JSON.stringify(customHeaders))}`);
} catch (error) {
console.error("Error processing variant URL:", line, error);
newLines.push(line); // Keep original if error
}
} else { } else {
// Empty line, preserve it
newLines.push(line); newLines.push(line);
} }
} else if (line.trim() && !line.startsWith("#")) {
// This is a segment URL (.ts file)
try {
// Try to create a full URL
const segmentUrl = line.startsWith('http') ? line : new URL(line, targetUrl).toString();
newLines.push(`${baseProxyUrl}/ts-proxy?url=${encodeURIComponent(segmentUrl)}&headers=${encodeURIComponent(JSON.stringify(customHeaders))}`);
} catch (error) {
console.error("Error processing segment URL:", line, error);
newLines.push(line); // Keep original if error
}
} else {
// Comment or empty line, preserve it
newLines.push(line);
} }
return new Response(newLines.join("\n"), {
headers: responseHeaders
});
} else {
// This is a media playlist with segments
const lines = m3u8Content.split("\n");
const newLines: string[] = [];
for (const line of lines) {
if (line.startsWith("#")) {
if (line.startsWith("#EXT-X-KEY:")) {
// Proxy the key URL
const regex = /URI="([^"]+)"/g;
const match = regex.exec(line);
const keyUrl = match ? match[1] : null;
if (keyUrl) {
try {
// Try to get the full URL
const keyUrlObj = keyUrl.startsWith('http')
? safeCreateURL(keyUrl)
: safeCreateURL(keyUrl, validatedUrl.toString());
if (!keyUrlObj) {
throw new Error("Could not create valid URL for key");
}
const fullKeyUrl = keyUrlObj.toString();
const proxyKeyUrl = `${baseProxyUrl}/ts-proxy?url=${encodeURIComponent(fullKeyUrl)}&headers=${encodeURIComponent(JSON.stringify(customHeaders))}`;
newLines.push(line.replace(keyUrl, proxyKeyUrl));
} catch (error) {
console.error("Error processing key URL:", keyUrl, error);
newLines.push(line); // Keep original if error
}
} else {
newLines.push(line);
}
} else {
newLines.push(line);
}
} else if (line.trim() && !line.startsWith("#")) {
// This is a segment URL (.ts file)
try {
// Try to create a full URL
const segmentUrlObj = line.startsWith('http')
? safeCreateURL(line)
: safeCreateURL(line, validatedUrl.toString());
if (!segmentUrlObj) {
throw new Error("Could not create valid URL for segment");
}
const segmentUrl = segmentUrlObj.toString();
newLines.push(`${baseProxyUrl}/ts-proxy?url=${encodeURIComponent(segmentUrl)}&headers=${encodeURIComponent(JSON.stringify(customHeaders))}`);
} catch (error) {
console.error("Error processing segment URL:", line, error);
newLines.push(line); // Keep original if error
}
} else {
// Comment or empty line, preserve it
newLines.push(line);
}
}
return new Response(newLines.join("\n"), {
headers: responseHeaders
});
} }
} catch (error: any) {
return new Response(newLines.join("\n"), { console.error('Error proxying M3U8:', error);
headers: responseHeaders return new Response(error.message || 'Error proxying M3U8 file', {
status: 500,
headers: {
'Access-Control-Allow-Origin': '*'
}
}); });
} }
} catch (error: any) { } catch (error: any) {
console.error('Error proxying M3U8:', error); console.error('Unexpected error in m3u8-proxy:', error);
return new Response(error.message || 'Error proxying M3U8 file', { return new Response(error.message || 'Unexpected error in m3u8-proxy', {
status: 500, status: 500,
headers: { headers: {
'Access-Control-Allow-Origin': '*' 'Access-Control-Allow-Origin': '*'

View file

@ -3,6 +3,16 @@
* @description Proxies TS (transport stream) files * @description Proxies TS (transport stream) files
*/ */
// Helper function to safely create a URL object
function safeCreateURL(url: string, base?: string): URL | null {
try {
return base ? new URL(url, base) : new URL(url);
} catch (error) {
console.error("URL parsing error:", error, "URL:", url, "Base:", base);
return null;
}
}
export default async function(request: Request, env: any, ctx: any) { export default async function(request: Request, env: any, ctx: any) {
// Handle CORS preflight requests // Handle CORS preflight requests
if (request.method === 'OPTIONS') { if (request.method === 'OPTIONS') {
@ -23,21 +33,26 @@ export default async function(request: Request, env: any, ctx: any) {
const headersParam = url.searchParams.get('headers'); const headersParam = url.searchParams.get('headers');
if (!targetUrl) { if (!targetUrl) {
return new Response('URL parameter is required', { status: 400 }); return new Response('URL parameter is required', {
status: 400,
headers: { 'Access-Control-Allow-Origin': '*' }
});
} }
let customHeaders = {}; let customHeaders = {};
try { try {
customHeaders = headersParam ? JSON.parse(headersParam) : {}; customHeaders = headersParam ? JSON.parse(headersParam) : {};
} catch (e) { } catch (e) {
return new Response('Invalid headers format', { status: 400 }); return new Response('Invalid headers format', {
status: 400,
headers: { 'Access-Control-Allow-Origin': '*' }
});
} }
// Validate the URL // Validate the URL
try { const validatedUrl = safeCreateURL(targetUrl);
new URL(targetUrl); if (!validatedUrl) {
} catch (error) { console.error('Invalid target URL:', targetUrl);
console.error('Invalid target URL:', targetUrl, error);
return new Response('Invalid target URL', { return new Response('Invalid target URL', {
status: 400, status: 400,
headers: { 'Access-Control-Allow-Origin': '*' } headers: { 'Access-Control-Allow-Origin': '*' }
@ -52,11 +67,11 @@ export default async function(request: Request, env: any, ctx: any) {
...customHeaders ...customHeaders
}); });
console.log('Fetching TS from:', targetUrl); console.log('Fetching TS from:', validatedUrl.toString());
console.log('With headers:', JSON.stringify(Object.fromEntries(requestHeaders.entries()))); console.log('With headers:', JSON.stringify(Object.fromEntries(requestHeaders.entries())));
// Fetch the TS file // Fetch the TS file
const response = await fetch(targetUrl, { const response = await fetch(validatedUrl.toString(), {
method: 'GET', method: 'GET',
headers: requestHeaders headers: requestHeaders
}); });