Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return signed URL directly in TikTokSignatureProvider instead of calling sign URL endpoint #215

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 10 additions & 94 deletions src/lib/tiktokSignatureProvider.js
Original file line number Diff line number Diff line change
@@ -1,114 +1,30 @@
const { EventEmitter } = require('node:events');
const { getUuc } = require('./tiktokUtils');
const pkg = require('../../package.json');
const axios = require('axios').create({
timeout: 5000,
headers: {
'User-Agent': `${pkg.name}/${pkg.version} ${process.platform}`,
},
});

let config = {
enabled: true,
signProviderHost: 'https://tiktok.eulerstream.com/',
signProviderFallbackHosts: ['https://tiktok-sign.zerody.one/'],
extraParams: {},
};

let signEvents = new EventEmitter();
const DEFAULT_SIGN_URL = 'https://tiktok.eulerstream.com/';

function signWebcastRequest(url, headers, cookieJar, signProviderOptions) {
return signRequest('webcast/sign_url', url, headers, cookieJar, signProviderOptions);
return signRequest('webcast/fetch', url, headers, cookieJar, signProviderOptions);
}

async function signRequest(providerPath, url, headers, cookieJar, signProviderOptions) {
if (!config.enabled) {
return url;
}
const urlParams = new URLSearchParams(url.split('?')[1]);

const roomId = urlParams.get('room_id');

let params = {
url,
room_id: roomId,
client: 'ttlive-node',
...config.extraParams,
uuc: getUuc(),
...signProviderOptions?.params,
};

params.uuc = getUuc();

let hostsToTry = [config.signProviderHost, ...config.signProviderFallbackHosts];
// Prioritize the custom host if provided
if (signProviderOptions?.host) {
// Remove any existing entries of the custom host to avoid duplication
hostsToTry = hostsToTry.filter((host) => host !== signProviderOptions.host);
hostsToTry.unshift(signProviderOptions.host);
}

let signHost;
let signResponse;
let signError;

try {
for (signHost of hostsToTry) {
try {
signResponse = await axios.get(signHost + providerPath, { params, headers: signProviderOptions?.headers, responseType: 'json' });

if (signResponse.status === 200 && typeof signResponse.data === 'object') {
break;
}
} catch (err) {
signError = err;
}
}

if (!signResponse) {
throw signError;
}

if (signResponse.status !== 200) {
throw new Error(`Status Code: ${signResponse.status}`);
}

if (!signResponse.data?.signedUrl) {
throw new Error('missing signedUrl property');
}

if (headers) {
headers['User-Agent'] = signResponse.data['User-Agent'];
}

if (cookieJar) {
cookieJar.setCookie('msToken', signResponse.data['msToken']);
}

signEvents.emit('signSuccess', {
signHost,
originalUrl: url,
signedUrl: signResponse.data.signedUrl,
headers,
cookieJar,
});

return signResponse.data.signedUrl;
} catch (error) {
signEvents.emit('signError', {
signHost,
originalUrl: url,
headers,
cookieJar,
error,
});
const host = signProviderOptions?.host || DEFAULT_SIGN_URL;

// If a sessionid is present, the signature is optional => Do not throw an error.
if (cookieJar.getCookieByName('sessionid')) {
return url;
}
const signedUrl = `${host}${providerPath}?${new URLSearchParams(params).toString()}`;

throw new Error(`Failed to sign request: ${error.message}; URL: ${url}`);
}
return signedUrl;
}

module.exports = {
config,
signEvents,
signWebcastRequest,
};