Organized the differences between server and docker versions.
We are launching a docker version (server version) today so we want to just organize the repo so its easier to navigate.
This commit is contained in:
60
desktop/src/api/proxy/proxy.controller.ts
Normal file
60
desktop/src/api/proxy/proxy.controller.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import {FastifyReply} from 'fastify';
|
||||
import {processM3U8Content, proxyRequest, streamToReadable} from './proxy.service';
|
||||
import {ProxyRequest} from '../types';
|
||||
|
||||
export async function handleProxy(req: ProxyRequest, reply: FastifyReply) {
|
||||
const { url, referer, origin, userAgent } = req.query;
|
||||
|
||||
if (!url) {
|
||||
return reply.code(400).send({ error: "No URL provided" });
|
||||
}
|
||||
|
||||
try {
|
||||
const { response, contentType, isM3U8, contentLength } = await proxyRequest(url, {
|
||||
referer,
|
||||
origin,
|
||||
userAgent
|
||||
});
|
||||
|
||||
reply.header('Access-Control-Allow-Origin', '*');
|
||||
reply.header('Access-Control-Allow-Methods', 'GET, OPTIONS');
|
||||
reply.header('Access-Control-Allow-Headers', 'Content-Type, Range');
|
||||
reply.header('Access-Control-Expose-Headers', 'Content-Length, Content-Range, Accept-Ranges');
|
||||
|
||||
if (contentType) {
|
||||
reply.header('Content-Type', contentType);
|
||||
}
|
||||
|
||||
if (contentLength) {
|
||||
reply.header('Content-Length', contentLength);
|
||||
}
|
||||
|
||||
if (contentType?.startsWith('image/') || contentType?.startsWith('video/')) {
|
||||
reply.header('Cache-Control', 'public, max-age=31536000, immutable');
|
||||
}
|
||||
|
||||
reply.header('Accept-Ranges', 'bytes');
|
||||
|
||||
if (isM3U8) {
|
||||
const text = await response.text();
|
||||
const baseUrl = new URL(response.url);
|
||||
|
||||
const processedContent = processM3U8Content(text, baseUrl, {
|
||||
referer,
|
||||
origin,
|
||||
userAgent
|
||||
});
|
||||
|
||||
return reply.send(processedContent);
|
||||
}
|
||||
|
||||
return reply.send(streamToReadable(response.body!));
|
||||
|
||||
} catch (err) {
|
||||
req.server.log.error(err);
|
||||
|
||||
if (!reply.sent) {
|
||||
return reply.code(500).send({ error: "Internal Server Error" });
|
||||
}
|
||||
}
|
||||
}
|
||||
8
desktop/src/api/proxy/proxy.routes.ts
Normal file
8
desktop/src/api/proxy/proxy.routes.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { FastifyInstance } from 'fastify';
|
||||
import { handleProxy } from './proxy.controller';
|
||||
|
||||
async function proxyRoutes(fastify: FastifyInstance) {
|
||||
fastify.get('/proxy', handleProxy);
|
||||
}
|
||||
|
||||
export default proxyRoutes;
|
||||
138
desktop/src/api/proxy/proxy.service.ts
Normal file
138
desktop/src/api/proxy/proxy.service.ts
Normal file
@@ -0,0 +1,138 @@
|
||||
import { Readable } from 'stream';
|
||||
|
||||
interface ProxyHeaders {
|
||||
referer?: string;
|
||||
origin?: string;
|
||||
userAgent?: string;
|
||||
}
|
||||
|
||||
interface ProxyResponse {
|
||||
response: Response;
|
||||
contentType: string | null;
|
||||
isM3U8: boolean;
|
||||
contentLength: string | null;
|
||||
}
|
||||
|
||||
export async function proxyRequest(url: string, { referer, origin, userAgent }: ProxyHeaders): Promise<ProxyResponse> {
|
||||
const headers: Record<string, string> = {
|
||||
'User-Agent': userAgent || "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
||||
'Accept': '*/*',
|
||||
'Accept-Language': 'en-US,en;q=0.9',
|
||||
'Accept-Encoding': 'identity',
|
||||
|
||||
'Connection': 'keep-alive'
|
||||
};
|
||||
|
||||
if (referer) headers['Referer'] = referer;
|
||||
if (origin) headers['Origin'] = origin;
|
||||
|
||||
let lastError: Error | null = null;
|
||||
const maxRetries = 2;
|
||||
|
||||
for (let attempt = 0; attempt < maxRetries; attempt++) {
|
||||
try {
|
||||
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), 60000);
|
||||
|
||||
const response = await fetch(url, {
|
||||
headers,
|
||||
redirect: 'follow',
|
||||
signal: controller.signal
|
||||
});
|
||||
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
if (!response.ok) {
|
||||
|
||||
if (response.status === 404 || response.status === 403) {
|
||||
throw new Error(`Proxy Error: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
|
||||
if (attempt < maxRetries - 1) {
|
||||
await new Promise(resolve => setTimeout(resolve, 500));
|
||||
continue;
|
||||
}
|
||||
|
||||
throw new Error(`Proxy Error: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
|
||||
const contentType = response.headers.get('content-type');
|
||||
const contentLength = response.headers.get('content-length');
|
||||
const isM3U8 = (contentType && contentType.includes('mpegurl')) || url.includes('.m3u8');
|
||||
|
||||
return {
|
||||
response,
|
||||
contentType,
|
||||
isM3U8,
|
||||
contentLength
|
||||
};
|
||||
|
||||
} catch (error) {
|
||||
lastError = error as Error;
|
||||
|
||||
if (attempt === maxRetries - 1) {
|
||||
throw lastError;
|
||||
}
|
||||
|
||||
await new Promise(resolve => setTimeout(resolve, 500));
|
||||
}
|
||||
}
|
||||
|
||||
throw lastError || new Error('Unknown error in proxyRequest');
|
||||
}
|
||||
|
||||
export function processM3U8Content(text: string, baseUrl: URL, { referer, origin, userAgent }: ProxyHeaders): string {
|
||||
return text.replace(/^(?!#)(?!\s*$).+/gm, (line) => {
|
||||
line = line.trim();
|
||||
let absoluteUrl: string;
|
||||
|
||||
try {
|
||||
absoluteUrl = new URL(line, baseUrl).href;
|
||||
} catch (e) {
|
||||
return line;
|
||||
}
|
||||
|
||||
const proxyParams = new URLSearchParams();
|
||||
proxyParams.set('url', absoluteUrl);
|
||||
if (referer) proxyParams.set('referer', referer);
|
||||
if (origin) proxyParams.set('origin', origin);
|
||||
if (userAgent) proxyParams.set('userAgent', userAgent);
|
||||
|
||||
return `/api/proxy?${proxyParams.toString()}`;
|
||||
});
|
||||
}
|
||||
|
||||
export function streamToReadable(webStream: ReadableStream): Readable {
|
||||
const reader = webStream.getReader();
|
||||
let readTimeout: NodeJS.Timeout;
|
||||
|
||||
return new Readable({
|
||||
async read() {
|
||||
try {
|
||||
|
||||
const timeoutPromise = new Promise((_, reject) => {
|
||||
readTimeout = setTimeout(() => reject(new Error('Stream read timeout')), 10000);
|
||||
});
|
||||
|
||||
const readPromise = reader.read();
|
||||
const { done, value } = await Promise.race([readPromise, timeoutPromise]) as any;
|
||||
|
||||
clearTimeout(readTimeout);
|
||||
|
||||
if (done) {
|
||||
this.push(null);
|
||||
} else {
|
||||
this.push(Buffer.from(value));
|
||||
}
|
||||
} catch (error) {
|
||||
clearTimeout(readTimeout);
|
||||
this.destroy(error as Error);
|
||||
}
|
||||
},
|
||||
destroy(error, callback) {
|
||||
clearTimeout(readTimeout);
|
||||
reader.cancel().then(() => callback(error)).catch(callback);
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user