From eb6ada60ebf886093089077d702436594301124c Mon Sep 17 00:00:00 2001 From: Fijxu Date: Mon, 16 Dec 2024 15:26:33 -0300 Subject: [PATCH] add support for external token generator --- src/main.ts | 78 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 56 insertions(+), 22 deletions(-) diff --git a/src/main.ts b/src/main.ts index fd3fb95..3fdfa9b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -50,6 +50,10 @@ if (!innertubeClientOauthEnabled) { innertubeClientCache = new UniversalCache(false); } +const externalTokenGenerator = konfigStore.get( + "server.external_token_generator", +) as string; + innertubeClient = await Innertube.create({ cache: innertubeClientCache, retrieve_player: innertubeClientFetchPlayer, @@ -65,27 +69,58 @@ if (!innertubeClientOauthEnabled) { innertubeClientCache as UniversalCache, ); } - 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, - retrieve_player: innertubeClientFetchPlayer, - }); - } - }, - konfigStore.get("jobs.youtube_session.frequency_seconds") as number * 1000 || 50000); + if (externalTokenGenerator != "" && externalTokenGenerator != undefined) { + console.log( + "using external token generator at " + externalTokenGenerator, + ); + setInterval(async () => { + const response = await fetch( + `${externalTokenGenerator}/generate`, + ); + const data = await response.json(); + innertubeClient.session.po_token = data.potoken; + innertubeClient.session.context.client.visitorData = + data.visitorData; + console.log(data.potoken); + console.log(data.visitorData); + }, 1000); + } else { + console.log( + "using internal token generator" + ); + 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, + ); + console.log( + "visitor_data: " + + innertubeClient.session.context.client.visitorData, + ); + } else { + innertubeClient = await Innertube.create({ + cache: innertubeClientCache, + retrieve_player: innertubeClientFetchPlayer, + }); + } + }, + 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) => { @@ -121,7 +156,6 @@ 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");