From ccbf888946082d339234f06317517a09aa65fa69 Mon Sep 17 00:00:00 2001 From: Jorrin Date: Thu, 11 Jan 2024 23:45:33 +0100 Subject: [PATCH] firefox support (kinda with manual permission set) --- src/backend/extension/streams.ts | 2 +- src/backend/providers/fetchers.ts | 36 ++++++++++++++++++++++--------- src/backend/providers/utils.ts | 29 +++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 11 deletions(-) create mode 100644 src/backend/providers/utils.ts diff --git a/src/backend/extension/streams.ts b/src/backend/extension/streams.ts index 2afb900d..daa7a54c 100644 --- a/src/backend/extension/streams.ts +++ b/src/backend/extension/streams.ts @@ -34,7 +34,7 @@ function buildHeadersFromStream(stream: Stream): Record { export async function prepareStream(stream: Stream) { await setDomainRule({ - ruleId: 1, + ruleId: 2, targetDomains: extractDomainsFromStream(stream), requestHeaders: buildHeadersFromStream(stream), }); diff --git a/src/backend/providers/fetchers.ts b/src/backend/providers/fetchers.ts index 9db649f5..83e42835 100644 --- a/src/backend/providers/fetchers.ts +++ b/src/backend/providers/fetchers.ts @@ -1,9 +1,11 @@ import { Fetcher, makeSimpleProxyFetcher } from "@movie-web/providers"; -import { sendExtensionRequest } from "@/backend/extension/messaging"; +import { setDomainRule } from "@/backend/extension/messaging"; import { getApiToken, setApiToken } from "@/backend/helpers/providerApi"; import { getProviderApiUrls, getProxyUrls } from "@/utils/proxyUrls"; +import { makeFullUrl } from "./utils"; + function makeLoadbalancedList(getter: () => string[]) { let listIndex = -1; return () => { @@ -67,17 +69,31 @@ function makeFinalHeaders( export function makeExtensionFetcher() { const fetcher: Fetcher = async (url, ops) => { - const result = await sendExtensionRequest({ - url, - ...ops, + const fullUrl = makeFullUrl(url, ops); + const res = await setDomainRule({ + ruleId: 1, + targetDomains: [fullUrl], + requestHeaders: ops.headers, }); - if (!result?.success) throw new Error(`extension error: ${result?.error}`); - const res = result.response; + console.log(res, fullUrl); + const response = await fetch(fullUrl, { + method: ops.method, + headers: ops.headers, + body: ops.body as any, + }); + const contentType = response.headers.get("content-type"); + const body = contentType?.includes("application/json") + ? await response.json() + : await response.text(); + return { - body: res.body, - finalUrl: res.finalUrl, - statusCode: res.statusCode, - headers: makeFinalHeaders(ops.readHeaders, res.headers), + body, + finalUrl: response.url, + statusCode: response.status, + headers: makeFinalHeaders( + ops.readHeaders, + Object.fromEntries(response.headers.entries()), + ), }; }; return fetcher; diff --git a/src/backend/providers/utils.ts b/src/backend/providers/utils.ts new file mode 100644 index 00000000..7149046c --- /dev/null +++ b/src/backend/providers/utils.ts @@ -0,0 +1,29 @@ +import { DefaultedFetcherOptions } from "@movie-web/providers"; + +export function makeFullUrl( + url: string, + ops?: DefaultedFetcherOptions, +): string { + // glue baseUrl and rest of url together + let leftSide = ops?.baseUrl ?? ""; + let rightSide = url; + + // left side should always end with slash, if its set + if (leftSide.length > 0 && !leftSide.endsWith("/")) leftSide += "/"; + + // right side should never start with slash + if (rightSide.startsWith("/")) rightSide = rightSide.slice(1); + + const fullUrl = leftSide + rightSide; + if (!fullUrl.startsWith("http://") && !fullUrl.startsWith("https://")) + throw new Error( + `Invald URL -- URL doesn't start with a http scheme: '${fullUrl}'`, + ); + + const parsedUrl = new URL(fullUrl); + Object.entries(ops?.query ?? {}).forEach(([k, v]) => { + parsedUrl.searchParams.set(k, v as string); + }); + + return parsedUrl.toString(); +}