changed some files to ts
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
const { proxyRequest, processM3U8Content, streamToReadable } = require('./proxy.service');
|
||||
import { FastifyInstance, FastifyReply } from 'fastify';
|
||||
import { proxyRequest, processM3U8Content, streamToReadable } from './proxy.service';
|
||||
import { ProxyRequest } from '../../types';
|
||||
|
||||
async function proxyRoutes(fastify, options) {
|
||||
|
||||
fastify.get('/proxy', async (req, reply) => {
|
||||
async function proxyRoutes(fastify: FastifyInstance) {
|
||||
fastify.get('/proxy', async (req: ProxyRequest, reply: FastifyReply) => {
|
||||
const { url, referer, origin, userAgent } = req.query;
|
||||
|
||||
if (!url) {
|
||||
@@ -34,7 +35,7 @@ async function proxyRoutes(fastify, options) {
|
||||
|
||||
return processedContent;
|
||||
} else {
|
||||
return reply.send(streamToReadable(response.body));
|
||||
return reply.send(streamToReadable(response.body!));
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
@@ -44,4 +45,4 @@ async function proxyRoutes(fastify, options) {
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = proxyRoutes;
|
||||
export default proxyRoutes;
|
||||
@@ -1,7 +1,19 @@
|
||||
const { Readable } = require('stream');
|
||||
import { Readable } from 'stream';
|
||||
|
||||
async function proxyRequest(url, { referer, origin, userAgent }) {
|
||||
const headers = {
|
||||
interface ProxyHeaders {
|
||||
referer?: string;
|
||||
origin?: string;
|
||||
userAgent?: string;
|
||||
}
|
||||
|
||||
interface ProxyResponse {
|
||||
response: Response;
|
||||
contentType: string | null;
|
||||
isM3U8: boolean;
|
||||
}
|
||||
|
||||
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'
|
||||
@@ -26,14 +38,18 @@ async function proxyRequest(url, { referer, origin, userAgent }) {
|
||||
};
|
||||
}
|
||||
|
||||
function processM3U8Content(text, baseUrl, { referer, origin, userAgent }) {
|
||||
export function processM3U8Content(
|
||||
text: string,
|
||||
baseUrl: URL,
|
||||
{ referer, origin, userAgent }: ProxyHeaders
|
||||
): string {
|
||||
return text.replace(/^(?!#)(?!\s*$).+/gm, (line) => {
|
||||
line = line.trim();
|
||||
let absoluteUrl;
|
||||
let absoluteUrl: string;
|
||||
|
||||
try {
|
||||
absoluteUrl = new URL(line, baseUrl).href;
|
||||
} catch(e) {
|
||||
} catch (e) {
|
||||
return line;
|
||||
}
|
||||
|
||||
@@ -47,12 +63,6 @@ function processM3U8Content(text, baseUrl, { referer, origin, userAgent }) {
|
||||
});
|
||||
}
|
||||
|
||||
function streamToReadable(webStream) {
|
||||
return Readable.fromWeb(webStream);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
proxyRequest,
|
||||
processM3U8Content,
|
||||
streamToReadable
|
||||
};
|
||||
export function streamToReadable(webStream: ReadableStream): Readable {
|
||||
return Readable.fromWeb(webStream as any);
|
||||
}
|
||||
Reference in New Issue
Block a user