diff --git a/src/entrypoint/controls.ts b/src/entrypoint/controls.ts index cfcd627..1441643 100644 --- a/src/entrypoint/controls.ts +++ b/src/entrypoint/controls.ts @@ -31,8 +31,6 @@ export interface RunnerOptions { // the media you want to see sources from media: ScrapeMedia; - - proxyStreams?: boolean; // temporary } export interface SourceRunnerOptions { @@ -44,8 +42,6 @@ export interface SourceRunnerOptions { // id of the source scraper you want to scrape from id: string; - - proxyStreams?: boolean; // temporary } export interface EmbedRunnerOptions { @@ -57,8 +53,6 @@ export interface EmbedRunnerOptions { // id of the embed scraper you want to scrape from id: string; - - proxyStreams?: boolean; // temporary } export interface ProviderControls { diff --git a/src/runners/individualRunner.ts b/src/runners/individualRunner.ts index da7fca9..0d9e29f 100644 --- a/src/runners/individualRunner.ts +++ b/src/runners/individualRunner.ts @@ -7,6 +7,7 @@ import { ProviderList } from '@/providers/get'; import { ScrapeContext } from '@/utils/context'; import { NotFoundError } from '@/utils/errors'; import { addOpenSubtitlesCaptions } from '@/utils/opensubtitles'; +import { requiresProxy, setupProxy } from '@/utils/proxy'; import { isValidStream, validatePlayableStreams } from '@/utils/valid'; export type IndividualSourceRunnerOptions = { @@ -16,6 +17,7 @@ export type IndividualSourceRunnerOptions = { media: ScrapeMedia; id: string; events?: IndividualScraperEvents; + proxyStreams?: boolean; // temporary }; export async function scrapeInvidualSource( @@ -56,6 +58,10 @@ export async function scrapeInvidualSource( output.stream = output.stream .filter((stream) => isValidStream(stream)) .filter((stream) => flagsAllowedInFeatures(ops.features, stream.flags)); + + output.stream = output.stream.map((stream) => + requiresProxy(stream) && ops.proxyStreams ? setupProxy(stream) : stream, + ); } if (!output) throw new Error('output is null'); @@ -107,6 +113,7 @@ export type IndividualEmbedRunnerOptions = { url: string; id: string; events?: IndividualScraperEvents; + proxyStreams?: boolean; // temporary }; export async function scrapeIndividualEmbed( @@ -138,6 +145,10 @@ export async function scrapeIndividualEmbed( .filter((stream) => flagsAllowedInFeatures(ops.features, stream.flags)); if (output.stream.length === 0) throw new NotFoundError('No streams found'); + output.stream = output.stream.map((stream) => + requiresProxy(stream) && ops.proxyStreams ? setupProxy(stream) : stream, + ); + const playableStreams = await validatePlayableStreams(output.stream, ops, embedScraper.id); if (playableStreams.length === 0) throw new NotFoundError('No playable streams found'); diff --git a/src/runners/runner.ts b/src/runners/runner.ts index 9e79b30..fca9410 100644 --- a/src/runners/runner.ts +++ b/src/runners/runner.ts @@ -9,6 +9,7 @@ import { ScrapeContext } from '@/utils/context'; import { NotFoundError } from '@/utils/errors'; import { reorderOnIdList } from '@/utils/list'; import { addOpenSubtitlesCaptions } from '@/utils/opensubtitles'; +import { requiresProxy, setupProxy } from '@/utils/proxy'; import { isValidStream, validatePlayableStream } from '@/utils/valid'; export type RunOutput = { @@ -36,6 +37,7 @@ export type ProviderRunnerOptions = { embedOrder?: string[]; events?: FullScraperEvents; media: ScrapeMedia; + proxyStreams?: boolean; // temporary }; export async function runAllProviders(list: ProviderList, ops: ProviderRunnerOptions): Promise { @@ -85,6 +87,10 @@ export async function runAllProviders(list: ProviderList, ops: ProviderRunnerOpt output.stream = (output.stream ?? []) .filter(isValidStream) .filter((stream) => flagsAllowedInFeatures(ops.features, stream.flags)); + + output.stream = output.stream.map((stream) => + requiresProxy(stream) && ops.proxyStreams ? setupProxy(stream) : stream, + ); } if (!output || (!output.stream?.length && !output.embeds.length)) { throw new NotFoundError('No streams found'); @@ -161,6 +167,9 @@ export async function runAllProviders(list: ProviderList, ops: ProviderRunnerOpt embedOutput.stream = embedOutput.stream .filter(isValidStream) .filter((stream) => flagsAllowedInFeatures(ops.features, stream.flags)); + embedOutput.stream = embedOutput.stream.map((stream) => + requiresProxy(stream) && ops.proxyStreams ? setupProxy(stream) : stream, + ); if (embedOutput.stream.length === 0) { throw new NotFoundError('No streams found'); } diff --git a/src/utils/proxy.ts b/src/utils/proxy.ts new file mode 100644 index 0000000..30e0291 --- /dev/null +++ b/src/utils/proxy.ts @@ -0,0 +1,16 @@ +import { flags } from '@/entrypoint/utils/targets'; +import { Stream } from '@/providers/streams'; + +export function requiresProxy(stream: Stream): boolean { + if (!stream.flags.includes('cors-allowed') && !!(stream.headers && Object.keys(stream.headers).length > 0)) + return true; + return false; +} + +export function setupProxy(stream: Stream): Stream { + // todo + + // stream.headers = {}; + stream.flags = [flags.CORS_ALLOWED]; + return stream; +}