From 72246cbc1fd7d7fdf7b9ab4c1c918d04d89871e2 Mon Sep 17 00:00:00 2001 From: jj Date: Thu, 23 May 2024 18:29:39 +0200 Subject: [PATCH] tiktok: use new cookie for each request (#514) * api: allow passing headers from service handler * tiktok: use new cookie for each request --- src/core/api.js | 7 ++- src/modules/processing/matchActionDecider.js | 2 + src/modules/processing/services/tiktok.js | 46 +++++++++++++------- src/modules/stream/manage.js | 2 + src/modules/stream/shared.js | 4 -- 5 files changed, 39 insertions(+), 22 deletions(-) diff --git a/src/core/api.js b/src/core/api.js index 04e190cd..f9b84c7a 100644 --- a/src/core/api.js +++ b/src/core/api.js @@ -192,8 +192,11 @@ export function runAPI(express, app, gitCommit, gitBranch, __dirname) { const streamInfo = getInternalStream(req.query.id); if (!streamInfo) return res.sendStatus(404); - streamInfo.headers = req.headers; - + streamInfo.headers = { + ...req.headers, + ...streamInfo.headers + }; + return stream(res, { type: 'internal', ...streamInfo }); } catch { return res.destroy(); diff --git a/src/modules/processing/matchActionDecider.js b/src/modules/processing/matchActionDecider.js index 6b44f8ab..42a35f1a 100644 --- a/src/modules/processing/matchActionDecider.js +++ b/src/modules/processing/matchActionDecider.js @@ -9,6 +9,7 @@ export default function(r, host, userFormat, isAudioOnly, lang, isAudioMuted, di responseType = "stream", defaultParams = { u: r.urls, + headers: r.headers, service: host, filename: r.filenameAttributes ? createFilename(r.filenameAttributes, filenamePattern, isAudioOnly, isAudioMuted) : r.filename, @@ -80,6 +81,7 @@ export default function(r, host, userFormat, isAudioOnly, lang, isAudioMuted, di service: "tiktok", type: audioStreamType, u: r.urls, + headers: r.headers, filename: r.audioFilename, isAudioOnly: true, audioFormat, diff --git a/src/modules/processing/services/tiktok.js b/src/modules/processing/services/tiktok.js index 8edb2a5b..0984ec04 100644 --- a/src/modules/processing/services/tiktok.js +++ b/src/modules/processing/services/tiktok.js @@ -4,9 +4,9 @@ import { extract } from "../url.js"; import Cookie from "../cookie/cookie.js"; const shortDomain = "https://vt.tiktok.com/"; -export const cookie = new Cookie({}); export default async function(obj) { + const cookie = new Cookie({}); let postId = obj.postId; if (!postId) { @@ -75,32 +75,46 @@ export default async function(obj) { if (audio.includes("mime_type=audio_mpeg")) bestAudio = 'mp3'; } - if (video) return { - urls: video, - filename: videoFilename + if (video) { + return { + urls: video, + filename: videoFilename, + headers: { cookie } + } } - if (images && obj.isAudioOnly) return { - urls: audio, - audioFilename: audioFilename, - isAudioOnly: true, - bestAudio + + if (images && obj.isAudioOnly) { + return { + urls: audio, + audioFilename: audioFilename, + isAudioOnly: true, + bestAudio, + headers: { cookie } + } } + if (images) { let imageLinks = images .map(i => i.imageURL.urlList.find(p => p.includes(".jpeg?"))) - .map(url => ({ url })) + .map(url => ({ url })); + return { picker: imageLinks, urls: audio, audioFilename: audioFilename, isAudioOnly: true, - bestAudio + bestAudio, + headers: { cookie } } } - if (audio) return { - urls: audio, - audioFilename: audioFilename, - isAudioOnly: true, - bestAudio + + if (audio) { + return { + urls: audio, + audioFilename: audioFilename, + isAudioOnly: true, + bestAudio, + headers: { cookie } + } } } diff --git a/src/modules/stream/manage.js b/src/modules/stream/manage.js index 6c9e9d3c..bccf5c80 100644 --- a/src/modules/stream/manage.js +++ b/src/modules/stream/manage.js @@ -38,6 +38,7 @@ export function createStream(obj) { filename: obj.filename, audioFormat: obj.audioFormat, isAudioOnly: !!obj.isAudioOnly, + headers: obj.headers, copy: !!obj.copy, mute: !!obj.mute, metadata: obj.fileMetadata || false, @@ -82,6 +83,7 @@ export function createInternalStream(url, obj = {}) { internalStreamCache[streamID] = { url, service: obj.service, + headers: obj.headers, controller: new AbortController(), dispatcher }; diff --git a/src/modules/stream/shared.js b/src/modules/stream/shared.js index 42df2758..8b4c9109 100644 --- a/src/modules/stream/shared.js +++ b/src/modules/stream/shared.js @@ -1,5 +1,4 @@ import { genericUserAgent } from "../config.js"; -import { cookie as tiktokCookie } from "../processing/services/tiktok.js"; const defaultHeaders = { 'user-agent': genericUserAgent @@ -14,9 +13,6 @@ const serviceHeaders = { origin: 'https://www.youtube.com', referer: 'https://www.youtube.com', DNT: '?1' - }, - tiktok: { - cookie: tiktokCookie } }