mirror of
https://github.com/p-stream/simple-proxy.git
synced 2026-04-21 11:32:17 +00:00
Add m3u8/ts parsing and routes
commitd50d90b89cAuthor: Pas <74743263+Pasithea0@users.noreply.github.com> Date: Wed May 14 17:51:49 2025 -0600 add user agent commit2d067ddbecAuthor: Pas <74743263+Pasithea0@users.noreply.github.com> Date: Wed May 14 17:47:13 2025 -0600 really... commit173e069628Author: Pas <74743263+Pasithea0@users.noreply.github.com> Date: Wed May 14 17:45:13 2025 -0600 revert commiteeb18a0efaAuthor: Pas <74743263+Pasithea0@users.noreply.github.com> Date: Wed May 14 17:42:51 2025 -0600 sdiufhs commit146f3a0b42Author: Pas <74743263+Pasithea0@users.noreply.github.com> Date: Wed May 14 17:40:05 2025 -0600 fix parsing commit28e6e3c632Author: Pas <74743263+Pasithea0@users.noreply.github.com> Date: Wed May 14 17:37:17 2025 -0600 fix headers commit29d35419d4Author: Pas <74743263+Pasithea0@users.noreply.github.com> Date: Wed May 14 17:30:58 2025 -0600 Revert "fix headers?" This reverts commit6e044470af. commit6e044470afAuthor: Pas <74743263+Pasithea0@users.noreply.github.com> Date: Wed May 14 17:20:18 2025 -0600 fix headers? idkkkk commit0192aaed21Author: Pas <74743263+Pasithea0@users.noreply.github.com> Date: Wed May 14 16:31:51 2025 -0600 init m3u8 proxying
This commit is contained in:
parent
cfaa503828
commit
c715d27e84
2 changed files with 255 additions and 0 deletions
196
src/routes/m3u8-proxy.ts
Normal file
196
src/routes/m3u8-proxy.ts
Normal file
|
|
@ -0,0 +1,196 @@
|
||||||
|
import { setResponseHeaders } from 'h3';
|
||||||
|
|
||||||
|
function parseURL(req_url: string, baseUrl?: string) {
|
||||||
|
if (baseUrl) {
|
||||||
|
return new URL(req_url, baseUrl).href;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 (req_url.lastIndexOf("//", 0) === -1) {
|
||||||
|
// "//" is omitted
|
||||||
|
req_url = "//" + req_url;
|
||||||
|
}
|
||||||
|
req_url = (match[4] === "443" ? "https:" : "http:") + req_url;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const parsed = new URL(req_url);
|
||||||
|
if (!parsed.hostname) {
|
||||||
|
// "http://:1/" and "http:/notenoughslashes" could end up here
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return parsed.href;
|
||||||
|
} catch (error) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Proxies m3u8 files and replaces the content to point to the proxy
|
||||||
|
*/
|
||||||
|
async function proxyM3U8(event: any) {
|
||||||
|
const url = getQuery(event).url as string;
|
||||||
|
const headersParam = getQuery(event).headers as string;
|
||||||
|
|
||||||
|
if (!url) {
|
||||||
|
return sendError(event, createError({
|
||||||
|
statusCode: 400,
|
||||||
|
statusMessage: 'URL parameter is required'
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
let headers = {};
|
||||||
|
try {
|
||||||
|
headers = headersParam ? JSON.parse(headersParam) : {};
|
||||||
|
} catch (e) {
|
||||||
|
return sendError(event, createError({
|
||||||
|
statusCode: 400,
|
||||||
|
statusMessage: 'Invalid headers format'
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await globalThis.fetch(url, {
|
||||||
|
headers: {
|
||||||
|
// Default User-Agent (from src/utils/headers.ts)
|
||||||
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0',
|
||||||
|
...(headers as HeadersInit),
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`Failed to fetch M3U8: ${response.status} ${response.statusText}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const m3u8Content = await response.text();
|
||||||
|
|
||||||
|
// Get the base URL for the host
|
||||||
|
const host = getRequestHost(event);
|
||||||
|
const proto = getRequestProtocol(event);
|
||||||
|
const baseProxyUrl = `${proto}://${host}`;
|
||||||
|
|
||||||
|
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 = /https?:\/\/[^\""\s]+/g;
|
||||||
|
const keyUrl = regex.exec(line)?.[0];
|
||||||
|
if (keyUrl) {
|
||||||
|
const proxyKeyUrl = `${baseProxyUrl}/ts-proxy?url=${encodeURIComponent(keyUrl)}&headers=${encodeURIComponent(JSON.stringify(headers))}`;
|
||||||
|
newLines.push(line.replace(keyUrl, proxyKeyUrl));
|
||||||
|
} else {
|
||||||
|
newLines.push(line);
|
||||||
|
}
|
||||||
|
} else if (line.startsWith("#EXT-X-MEDIA:")) {
|
||||||
|
// Proxy alternative media URLs (like audio streams)
|
||||||
|
const regex = /https?:\/\/[^\""\s]+/g;
|
||||||
|
const mediaUrl = regex.exec(line)?.[0];
|
||||||
|
if (mediaUrl) {
|
||||||
|
const proxyMediaUrl = `${baseProxyUrl}/m3u8-proxy?url=${encodeURIComponent(mediaUrl)}&headers=${encodeURIComponent(JSON.stringify(headers))}`;
|
||||||
|
newLines.push(line.replace(mediaUrl, proxyMediaUrl));
|
||||||
|
} else {
|
||||||
|
newLines.push(line);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
newLines.push(line);
|
||||||
|
}
|
||||||
|
} else if (line.trim()) {
|
||||||
|
// This is a quality variant URL
|
||||||
|
const variantUrl = parseURL(line, url);
|
||||||
|
if (variantUrl) {
|
||||||
|
newLines.push(`${baseProxyUrl}/m3u8-proxy?url=${encodeURIComponent(variantUrl)}&headers=${encodeURIComponent(JSON.stringify(headers))}`);
|
||||||
|
} else {
|
||||||
|
newLines.push(line);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Empty line, preserve it
|
||||||
|
newLines.push(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set appropriate headers
|
||||||
|
setResponseHeaders(event, {
|
||||||
|
'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'
|
||||||
|
});
|
||||||
|
|
||||||
|
return newLines.join("\n");
|
||||||
|
} 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 = /https?:\/\/[^\""\s]+/g;
|
||||||
|
const keyUrl = regex.exec(line)?.[0];
|
||||||
|
if (keyUrl) {
|
||||||
|
const proxyKeyUrl = `${baseProxyUrl}/ts-proxy?url=${encodeURIComponent(keyUrl)}&headers=${encodeURIComponent(JSON.stringify(headers))}`;
|
||||||
|
newLines.push(line.replace(keyUrl, proxyKeyUrl));
|
||||||
|
} else {
|
||||||
|
newLines.push(line);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
newLines.push(line);
|
||||||
|
}
|
||||||
|
} else if (line.trim() && !line.startsWith("#")) {
|
||||||
|
// This is a segment URL (.ts file)
|
||||||
|
const segmentUrl = parseURL(line, url);
|
||||||
|
if (segmentUrl) {
|
||||||
|
newLines.push(`${baseProxyUrl}/ts-proxy?url=${encodeURIComponent(segmentUrl)}&headers=${encodeURIComponent(JSON.stringify(headers))}`);
|
||||||
|
} else {
|
||||||
|
newLines.push(line);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Comment or empty line, preserve it
|
||||||
|
newLines.push(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set appropriate headers
|
||||||
|
setResponseHeaders(event, {
|
||||||
|
'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'
|
||||||
|
});
|
||||||
|
|
||||||
|
return newLines.join("\n");
|
||||||
|
}
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error('Error proxying M3U8:', error);
|
||||||
|
return sendError(event, createError({
|
||||||
|
statusCode: 500,
|
||||||
|
statusMessage: error.message || 'Error proxying M3U8 file'
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
// Handle CORS preflight requests
|
||||||
|
if (isPreflightRequest(event)) return handleCors(event, {});
|
||||||
|
|
||||||
|
return await proxyM3U8(event);
|
||||||
|
});
|
||||||
59
src/routes/ts-proxy.ts
Normal file
59
src/routes/ts-proxy.ts
Normal file
|
|
@ -0,0 +1,59 @@
|
||||||
|
import { setResponseHeaders } from 'h3';
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
// Handle CORS preflight requests
|
||||||
|
if (isPreflightRequest(event)) return handleCors(event, {});
|
||||||
|
|
||||||
|
const url = getQuery(event).url as string;
|
||||||
|
const headersParam = getQuery(event).headers as string;
|
||||||
|
|
||||||
|
if (!url) {
|
||||||
|
return sendError(event, createError({
|
||||||
|
statusCode: 400,
|
||||||
|
statusMessage: 'URL parameter is required'
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
let headers = {};
|
||||||
|
try {
|
||||||
|
headers = headersParam ? JSON.parse(headersParam) : {};
|
||||||
|
} catch (e) {
|
||||||
|
return sendError(event, createError({
|
||||||
|
statusCode: 400,
|
||||||
|
statusMessage: 'Invalid headers format'
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await globalThis.fetch(url, {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
// Default User-Agent (from src/utils/headers.ts)
|
||||||
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0',
|
||||||
|
...(headers as HeadersInit),
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`Failed to fetch TS file: ${response.status} ${response.statusText}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set appropriate headers for each video segment
|
||||||
|
setResponseHeaders(event, {
|
||||||
|
'Content-Type': 'video/mp2t',
|
||||||
|
'Access-Control-Allow-Origin': '*',
|
||||||
|
'Access-Control-Allow-Headers': '*',
|
||||||
|
'Access-Control-Allow-Methods': '*',
|
||||||
|
'Cache-Control': 'public, max-age=3600' // Allow caching of TS segments
|
||||||
|
});
|
||||||
|
|
||||||
|
// Return the binary data directly
|
||||||
|
return new Uint8Array(await response.arrayBuffer());
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error('Error proxying TS file:', error);
|
||||||
|
return sendError(event, createError({
|
||||||
|
statusCode: error.response?.status || 500,
|
||||||
|
statusMessage: error.message || 'Error proxying TS file'
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
});
|
||||||
Loading…
Reference in a new issue