From e2fc5059e256d1ae8ccf979103f78e072d5d300b Mon Sep 17 00:00:00 2001 From: Karl Date: Sun, 13 Jul 2025 17:01:31 +0100 Subject: [PATCH] perf(checker): run account checks concurrently using Promise.any Refactors the `singleAccountCheck` function to use `Promise.any` for validating accounts against multiple stream URLs. This replaces the previous sequential `for` loop. By running the checks concurrently, the process is significantly faster as it no longer waits for each request to finish before trying the next. The function now resolves as soon as the first successful validation occurs. The fallback logic for 403 errors (from a tor instance to a direct request) is now cleanly encapsulated within each promise. --- lib/checker.js | 79 ++++++++++++++++++++++---------------------------- 1 file changed, 35 insertions(+), 44 deletions(-) diff --git a/lib/checker.js b/lib/checker.js index c6eb8aa..9431481 100644 --- a/lib/checker.js +++ b/lib/checker.js @@ -33,9 +33,8 @@ let axiosOptions = { // const delay = ms => new Promise(res => setTimeout(res, ms)); -async function buildURL(streamURL, username, password) { - let url = streamURL + "/player_api.php?username=" + username + "&password=" + password - return url +function buildURL(streamURL, username, password) { + return `${streamURL}/player_api.php?username=${username}&password=${password}` } async function splitURL(url) { @@ -173,56 +172,48 @@ async function userCheck(accountData) { } } -async function accountChecker(accountData, urlList) { - let ip = await inst.get('http://api.ipify.org') - console.log(ip.data); +async function singleAccountCheck(accountData) { + const streamURLS = await getStreamsNew(); + const urlList = streamURLS.map(streamURL => buildURL(streamURL, accountData.username, accountData.password)); - for (let index = 0; index < urlList.length; index++) { - const url = urlList[index]; - console.log(index + 1 + ' of ' + urlList.length + ' urls checked') + const checkUrl = (url) => new Promise(async (resolve, reject) => { try { - let response = await inst.get(url, axiosOptions) - if (response.data.user_info.auth) { - addNewAccount(accountData, response.data, url) - console.log('New Account Added') - // syncApache() - return true + // Use tor instance first + const response = await inst.get(url, axiosOptions); + if (response.data && response.data.user_info && response.data.user_info.auth) { + return resolve({ url, data: response.data }); } } catch (error) { - try { - if (error.response.status == 403) { - let response2 = await axios.get(url, axiosOptions) - if (response2.data.user_info.auth) { - try { - await addNewAccount(accountData, response2.data, url) - console.log('New Account Added') - // syncApache() - return true - } catch (error) { - continue - } + // Fallback to regular axios on 403 error + if (error.response && error.response.status === 403) { + try { + const response2 = await axios.get(url, axiosOptions); + if (response2.data && response2.data.user_info && response2.data.user_info.auth) { + return resolve({ url, data: response2.data }); } + } catch (e) { + // If fallback fails, proceed to reject } - } catch (error) { - continue } } + // Reject if any check fails or encounters an error + return reject(new Error(`Check failed for ${url}`)); + }); + + const promises = urlList.map(checkUrl); + + try { + // Wait for the first promise to resolve + const { url, data } = await Promise.any(promises); + await addNewAccount(accountData, data, url); + console.log('New Account Added'); + // syncApache() was commented out, preserving that. + return true; + } catch (error) { + // This block runs if all promises reject + console.log(`All checks failed for user ${accountData.username}.`); + return false; } - return false -} - - -async function singleAccountCheck(accountData) { - streamURLS = await getStreamsNew() - - urlList = [] - - for (let index = 0; index < streamURLS.length; index++) { - const streamURL = streamURLS[index]; - urlList.push(await buildURL(streamURL, accountData.username, accountData.password)) - } - urlList - return await accountChecker(accountData, urlList) }