mirror of
https://github.com/p-stream/simple-proxy.git
synced 2026-01-11 20:10:35 +00:00
fix headers?
idkkkk
This commit is contained in:
parent
0192aaed21
commit
6e044470af
2 changed files with 118 additions and 24 deletions
|
|
@ -4,6 +4,9 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { setResponseHeaders } from 'h3';
|
import { setResponseHeaders } from 'h3';
|
||||||
|
import { IncomingMessage } from 'http';
|
||||||
|
import https from 'node:https';
|
||||||
|
import http from 'node:http';
|
||||||
|
|
||||||
// Helper function to parse URLs
|
// Helper function to parse URLs
|
||||||
function parseURL(req_url: string, baseUrl?: string) {
|
function parseURL(req_url: string, baseUrl?: string) {
|
||||||
|
|
@ -42,6 +45,56 @@ function parseURL(req_url: string, baseUrl?: string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Helper function to perform HTTP/HTTPS request
|
||||||
|
function makeRequest(url: string, headers: any): Promise<{ data: string, statusCode: number }> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const parsed = new URL(url);
|
||||||
|
const isHttps = parsed.protocol === 'https:';
|
||||||
|
const options = {
|
||||||
|
hostname: parsed.hostname,
|
||||||
|
port: parsed.port || (isHttps ? 443 : 80),
|
||||||
|
path: parsed.pathname + parsed.search,
|
||||||
|
method: 'GET',
|
||||||
|
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',
|
||||||
|
...headers
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
console.log('Making request to:', url);
|
||||||
|
console.log('With headers:', JSON.stringify(options.headers));
|
||||||
|
|
||||||
|
const requestLib = isHttps ? https : http;
|
||||||
|
const req = requestLib.request(options, (res: IncomingMessage) => {
|
||||||
|
console.log('Status code:', res.statusCode);
|
||||||
|
console.log('Response headers:', JSON.stringify(res.headers));
|
||||||
|
|
||||||
|
let data = '';
|
||||||
|
|
||||||
|
res.on('data', (chunk) => {
|
||||||
|
data += chunk;
|
||||||
|
});
|
||||||
|
|
||||||
|
res.on('end', () => {
|
||||||
|
if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300) {
|
||||||
|
resolve({ data, statusCode: res.statusCode });
|
||||||
|
} else {
|
||||||
|
reject(new Error(`HTTP Error: ${res.statusCode} ${res.statusMessage}`));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
req.on('error', (error) => {
|
||||||
|
console.error('Request error:', error);
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
|
|
||||||
|
req.end();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Proxies m3u8 files and replaces the content to point to the proxy
|
* Proxies m3u8 files and replaces the content to point to the proxy
|
||||||
*/
|
*/
|
||||||
|
|
@ -60,23 +113,20 @@ async function proxyM3U8(event: any) {
|
||||||
try {
|
try {
|
||||||
headers = headersParam ? JSON.parse(headersParam) : {};
|
headers = headersParam ? JSON.parse(headersParam) : {};
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
console.error('Error parsing headers JSON:', e, 'Raw headers:', headersParam);
|
||||||
return sendError(event, createError({
|
return sendError(event, createError({
|
||||||
statusCode: 400,
|
statusCode: 400,
|
||||||
statusMessage: 'Invalid headers format'
|
statusMessage: 'Invalid headers format'
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log('Processing m3u8 request for URL:', url);
|
||||||
|
console.log('With headers:', JSON.stringify(headers));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Use native fetch instead of axios
|
// Use Node's http/https modules instead of fetch
|
||||||
const response = await fetch(url, {
|
const response = await makeRequest(url, headers);
|
||||||
headers: headers as HeadersInit
|
const m3u8Content = response.data;
|
||||||
});
|
|
||||||
|
|
||||||
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
|
// Get the base URL for the host
|
||||||
const host = getRequestHost(event);
|
const host = getRequestHost(event);
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,9 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { setResponseHeaders } from 'h3';
|
import { setResponseHeaders } from 'h3';
|
||||||
|
import https from 'node:https';
|
||||||
|
import http from 'node:http';
|
||||||
|
import { IncomingMessage } from 'http';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Proxies TS (transport stream) files
|
* Proxies TS (transport stream) files
|
||||||
|
|
@ -26,26 +29,21 @@ export default defineEventHandler(async (event) => {
|
||||||
try {
|
try {
|
||||||
headers = headersParam ? JSON.parse(headersParam) : {};
|
headers = headersParam ? JSON.parse(headersParam) : {};
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
console.error('Error parsing headers JSON:', e, 'Raw headers:', headersParam);
|
||||||
return sendError(event, createError({
|
return sendError(event, createError({
|
||||||
statusCode: 400,
|
statusCode: 400,
|
||||||
statusMessage: 'Invalid headers format'
|
statusMessage: 'Invalid headers format'
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log('Processing TS request for URL:', url);
|
||||||
|
console.log('With headers:', JSON.stringify(headers));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(url, {
|
const parsed = new URL(url);
|
||||||
method: 'GET',
|
const isHttps = parsed.protocol === 'https:';
|
||||||
headers: {
|
|
||||||
...headers as HeadersInit,
|
|
||||||
'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',
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
// Set appropriate headers for video content first, before starting the stream
|
||||||
throw new Error(`Failed to fetch TS file: ${response.status} ${response.statusText}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set appropriate headers for each video segment
|
|
||||||
setResponseHeaders(event, {
|
setResponseHeaders(event, {
|
||||||
'Content-Type': 'video/mp2t',
|
'Content-Type': 'video/mp2t',
|
||||||
'Access-Control-Allow-Origin': '*',
|
'Access-Control-Allow-Origin': '*',
|
||||||
|
|
@ -54,8 +52,54 @@ export default defineEventHandler(async (event) => {
|
||||||
'Cache-Control': 'public, max-age=3600' // Allow caching of TS segments
|
'Cache-Control': 'public, max-age=3600' // Allow caching of TS segments
|
||||||
});
|
});
|
||||||
|
|
||||||
// Return the binary data directly - Nitro should handle this properly
|
// Create a promise that will download and stream the TS file
|
||||||
return new Uint8Array(await response.arrayBuffer());
|
return await new Promise((resolve, reject) => {
|
||||||
|
const options = {
|
||||||
|
hostname: parsed.hostname,
|
||||||
|
port: parsed.port || (isHttps ? 443 : 80),
|
||||||
|
path: parsed.pathname + parsed.search,
|
||||||
|
method: 'GET',
|
||||||
|
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',
|
||||||
|
...headers
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
console.log('Making TS request to:', url);
|
||||||
|
console.log('With headers:', JSON.stringify(options.headers));
|
||||||
|
|
||||||
|
const requestLib = isHttps ? https : http;
|
||||||
|
const req = requestLib.request(options, (res: IncomingMessage) => {
|
||||||
|
if (!res.statusCode || res.statusCode < 200 || res.statusCode >= 300) {
|
||||||
|
reject(new Error(`HTTP Error: ${res.statusCode} ${res.statusMessage}`));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('TS response status:', res.statusCode);
|
||||||
|
|
||||||
|
// Create an array to collect chunks
|
||||||
|
const chunks: Buffer[] = [];
|
||||||
|
|
||||||
|
res.on('data', (chunk) => {
|
||||||
|
chunks.push(Buffer.from(chunk));
|
||||||
|
});
|
||||||
|
|
||||||
|
res.on('end', () => {
|
||||||
|
// Combine all chunks into a single buffer
|
||||||
|
const buffer = Buffer.concat(chunks);
|
||||||
|
resolve(buffer);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
req.on('error', (error) => {
|
||||||
|
console.error('TS request error:', error);
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
|
|
||||||
|
req.end();
|
||||||
|
});
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error('Error proxying TS file:', error);
|
console.error('Error proxying TS file:', error);
|
||||||
return sendError(event, createError({
|
return sendError(event, createError({
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue