fix parsing

This commit is contained in:
Pas 2025-05-14 17:40:05 -06:00
parent 28e6e3c632
commit 146f3a0b42
2 changed files with 101 additions and 62 deletions

View file

@ -5,37 +5,29 @@
// Helper function to parse URLs // Helper function to parse URLs
function parseURL(req_url: string, baseUrl?: string) { function parseURL(req_url: string, baseUrl?: string) {
if (baseUrl) { try {
return new URL(req_url, baseUrl).href; if (baseUrl) {
} return new URL(req_url, baseUrl).toString();
const match = req_url.match(/^(?:(https?:)?\/\/)?(([^\/?]+?)(?::(\d{0,5})(?=[\/?]|$))?)([\/?][\S\s]*|$)/i);
if (!match) {
return null;
}
if (!match[1]) {
if (/^https?:/i.test(req_url)) {
return null;
} }
// Scheme is omitted // If it's already a fully qualified URL
if (req_url.lastIndexOf("//", 0) === -1) { if (/^https?:\/\//i.test(req_url)) {
// "//" is omitted return new URL(req_url).toString();
req_url = "//" + req_url;
} }
req_url = (match[4] === "443" ? "https:" : "http:") + req_url;
} // If it starts with // (protocol-relative URL)
if (req_url.startsWith('//')) {
try { return new URL(`https:${req_url}`).toString();
const parsed = new URL(req_url);
if (!parsed.hostname) {
// "http://:1/" and "http:/notenoughslashes" could end up here
return null;
} }
return parsed.href;
// If it's a relative URL and we have a base URL
if (baseUrl) {
return new URL(req_url, baseUrl).toString();
}
return null;
} catch (error) { } catch (error) {
console.error("URL parsing error:", error, "for URL:", req_url, "with base:", baseUrl);
return null; return null;
} }
} }
@ -117,21 +109,40 @@ export default async function(request: Request, env: any, ctx: any) {
if (line.startsWith("#")) { if (line.startsWith("#")) {
if (line.startsWith("#EXT-X-KEY:")) { if (line.startsWith("#EXT-X-KEY:")) {
// Proxy the key URL // Proxy the key URL
const regex = /https?:\/\/[^\""\s]+/g; const regex = /URI="([^"]+)"/g;
const keyUrl = regex.exec(line)?.[0]; const match = regex.exec(line);
const keyUrl = match ? match[1] : null;
if (keyUrl) { if (keyUrl) {
const proxyKeyUrl = `${baseProxyUrl}/ts-proxy?url=${encodeURIComponent(keyUrl)}&headers=${encodeURIComponent(JSON.stringify(customHeaders))}`; let fullKeyUrl;
newLines.push(line.replace(keyUrl, proxyKeyUrl)); 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 { } else {
newLines.push(line); newLines.push(line);
} }
} else if (line.startsWith("#EXT-X-MEDIA:")) { } else if (line.startsWith("#EXT-X-MEDIA:")) {
// Proxy alternative media URLs (like audio streams) // Proxy alternative media URLs (like audio streams)
const regex = /https?:\/\/[^\""\s]+/g; const regex = /URI="([^"]+)"/g;
const mediaUrl = regex.exec(line)?.[0]; const match = regex.exec(line);
const mediaUrl = match ? match[1] : null;
if (mediaUrl) { if (mediaUrl) {
const proxyMediaUrl = `${baseProxyUrl}/m3u8-proxy?url=${encodeURIComponent(mediaUrl)}&headers=${encodeURIComponent(JSON.stringify(customHeaders))}`; try {
newLines.push(line.replace(mediaUrl, proxyMediaUrl)); // 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 { } else {
newLines.push(line); newLines.push(line);
} }
@ -140,11 +151,13 @@ export default async function(request: Request, env: any, ctx: any) {
} }
} else if (line.trim()) { } else if (line.trim()) {
// This is a quality variant URL // This is a quality variant URL
const variantUrl = parseURL(line, targetUrl); try {
if (variantUrl) { // 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))}`); newLines.push(`${baseProxyUrl}/m3u8-proxy?url=${encodeURIComponent(variantUrl)}&headers=${encodeURIComponent(JSON.stringify(customHeaders))}`);
} else { } catch (error) {
newLines.push(line); console.error("Error processing variant URL:", line, error);
newLines.push(line); // Keep original if error
} }
} else { } else {
// Empty line, preserve it // Empty line, preserve it
@ -164,11 +177,20 @@ export default async function(request: Request, env: any, ctx: any) {
if (line.startsWith("#")) { if (line.startsWith("#")) {
if (line.startsWith("#EXT-X-KEY:")) { if (line.startsWith("#EXT-X-KEY:")) {
// Proxy the key URL // Proxy the key URL
const regex = /https?:\/\/[^\""\s]+/g; const regex = /URI="([^"]+)"/g;
const keyUrl = regex.exec(line)?.[0]; const match = regex.exec(line);
const keyUrl = match ? match[1] : null;
if (keyUrl) { if (keyUrl) {
const proxyKeyUrl = `${baseProxyUrl}/ts-proxy?url=${encodeURIComponent(keyUrl)}&headers=${encodeURIComponent(JSON.stringify(customHeaders))}`; try {
newLines.push(line.replace(keyUrl, proxyKeyUrl)); // 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))}`;
newLines.push(line.replace(keyUrl, proxyKeyUrl));
} catch (error) {
console.error("Error processing key URL:", keyUrl, error);
newLines.push(line); // Keep original if error
}
} else { } else {
newLines.push(line); newLines.push(line);
} }
@ -177,11 +199,13 @@ export default async function(request: Request, env: any, ctx: any) {
} }
} else if (line.trim() && !line.startsWith("#")) { } else if (line.trim() && !line.startsWith("#")) {
// This is a segment URL (.ts file) // This is a segment URL (.ts file)
const segmentUrl = parseURL(line, targetUrl); try {
if (segmentUrl) { // 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))}`); newLines.push(`${baseProxyUrl}/ts-proxy?url=${encodeURIComponent(segmentUrl)}&headers=${encodeURIComponent(JSON.stringify(customHeaders))}`);
} else { } catch (error) {
newLines.push(line); console.error("Error processing segment URL:", line, error);
newLines.push(line); // Keep original if error
} }
} else { } else {
// Comment or empty line, preserve it // Comment or empty line, preserve it

View file

@ -16,23 +16,34 @@ 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 });
}
let customHeaders = {};
try {
customHeaders = headersParam ? JSON.parse(headersParam) : {};
} catch (e) {
return new Response('Invalid headers format', { status: 400 });
}
try { try {
// 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 });
}
let customHeaders = {};
try {
customHeaders = headersParam ? JSON.parse(headersParam) : {};
} catch (e) {
return new Response('Invalid headers format', { status: 400 });
}
// Validate the URL
try {
new URL(targetUrl);
} catch (error) {
console.error('Invalid target URL:', targetUrl, error);
return new Response('Invalid target URL', {
status: 400,
headers: { 'Access-Control-Allow-Origin': '*' }
});
}
// Create request headers // Create request headers
const requestHeaders = new 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', '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',
@ -41,6 +52,10 @@ export default async function(request: Request, env: any, ctx: any) {
...customHeaders ...customHeaders
}); });
console.log('Fetching TS from:', targetUrl);
console.log('With headers:', JSON.stringify(Object.fromEntries(requestHeaders.entries())));
// Fetch the TS file
const response = await fetch(targetUrl, { const response = await fetch(targetUrl, {
method: 'GET', method: 'GET',
headers: requestHeaders headers: requestHeaders