From 465878355b1eb5c085b859d045fbb042959fba79 Mon Sep 17 00:00:00 2001 From: Fijxu Date: Mon, 16 Dec 2024 16:48:15 -0300 Subject: [PATCH] add support for external token generator --- src/lib/jobs/potoken.ts | 29 +++++++++++++++++++++++++++++ src/main.ts | 11 ++++------- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/lib/jobs/potoken.ts b/src/lib/jobs/potoken.ts index 0b1873b..b0b1d7a 100644 --- a/src/lib/jobs/potoken.ts +++ b/src/lib/jobs/potoken.ts @@ -22,8 +22,37 @@ export const poTokenGenerate = async ( konfigStore: Store>, innertubeClientCache: UniversalCache, ): Promise => { + const externalTokenGenerator = konfigStore.get( + "server.external_token_generator", + ) as string; const requestKey = "O43z0dpjhgX20SCx4KAo"; + if (externalTokenGenerator != "" && externalTokenGenerator != undefined) { + console.log( + "poTokenGenerate: using external token generator at " + + externalTokenGenerator, + ); + try { + const response = await fetch( + `${externalTokenGenerator}/generate`, + ); + const data = await response.json(); + return (await Innertube.create({ + po_token: data.potoken, + visitor_data: data.visitorData, + fetch: getFetchClient(konfigStore), + cache: innertubeClientCache, + generate_session_locally: true, + })); + } catch (e) { + console.error( + "poTokenGenerate: error fetch token from the external token generator: " + + e, + ); + console.log("poTokenGenerate: Using built-in token generator"); + } + } + if (innertubeClient.session.po_token) { innertubeClient = await Innertube.create({ retrieve_player: false }); } diff --git a/src/main.ts b/src/main.ts index fd3fb95..592ce49 100644 --- a/src/main.ts +++ b/src/main.ts @@ -67,17 +67,12 @@ if (!innertubeClientOauthEnabled) { } setInterval( async () => { - const currentDateTime = new Date(); - console.log("regenrate token called: " + currentDateTime) if (innertubeClientJobPoTokenEnabled) { innertubeClient = await poTokenGenerate( innertubeClient, konfigStore, innertubeClientCache, ); - const currentDateTimexd = new Date(); - console.log("regenerate token finished: " + currentDateTimexd) - console.log("po_token: " + innertubeClient.session.po_token) } else { innertubeClient = await Innertube.create({ cache: innertubeClientCache, @@ -85,7 +80,9 @@ if (!innertubeClientOauthEnabled) { }); } }, - konfigStore.get("jobs.youtube_session.frequency_seconds") as number * 1000 || 50000); + konfigStore.get("jobs.youtube_session.frequency_seconds") as number * + 1000 || 50000, + ); } else if (innertubeClientOauthEnabled) { // Fired when waiting for the user to authorize the sign in attempt. innertubeClient.session.on("auth-pending", (data) => { @@ -117,11 +114,11 @@ app.use("*", async (c, next) => { }); routes(app, konfigStore); + const https = Deno.env.get("HTTPS"); const port = konfigStore.get("server.port") as number; const host = konfigStore.get("server.host") as string; - if (https == "TRUE" || https == "true") { const cert = Deno.readTextFileSync("/data/cert.pem"); const key = Deno.readTextFileSync("/data/key.key");