From 8477e940117790d595a6dbf5679ea520c24b420e 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 | 52 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/lib/jobs/potoken.ts b/src/lib/jobs/potoken.ts index 0b1873b..7d82b80 100644 --- a/src/lib/jobs/potoken.ts +++ b/src/lib/jobs/potoken.ts @@ -22,8 +22,58 @@ 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( + "using external token generator at " + externalTokenGenerator, + ); + try { + 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); + 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("error fetch token from the external token generator: " + e); + } + } + // setInterval(() => { + // fetch( + // `${externalTokenGenerator}/generate`, + // ).then((response) => response.json()).then((data) => { + // innertubeClient.session.po_token = data.potoken; + // innertubeClient.session.context.client.visitorData = + // data.visitorData; + // console.log("po_token: "+data.potoken); + // console.log("visitor_data"+data.visitorData); + // return (await Innertube.create({ + // po_token: poTokenResult.poToken, + // visitor_data: visitorData, + // fetch: getFetchClient(konfigStore), + // cache: innertubeClientCache, + // generate_session_locally: true, + // })); + // }).catch(e => { + // console.error("error getting tokens from external token generator: " + e) + // console.log("tokens will not change") + // }); + // }, 5000); + if (innertubeClient.session.po_token) { innertubeClient = await Innertube.create({ retrieve_player: false }); } @@ -69,6 +119,8 @@ export const poTokenGenerate = async ( await BG.PoToken.generatePlaceholder(visitorData); + console.log("realpotoken: "+poTokenResult.poToken) + console.log("realvisitordata: "+visitorData) return (await Innertube.create({ po_token: poTokenResult.poToken, visitor_data: visitorData,