perf(checker): run account checks concurrently using Promise.any
All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 22s
All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 22s
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.
This commit is contained in:
parent
07340e9341
commit
e2fc5059e2
@ -33,9 +33,8 @@ let axiosOptions = {
|
|||||||
// const delay = ms => new Promise(res => setTimeout(res, ms));
|
// const delay = ms => new Promise(res => setTimeout(res, ms));
|
||||||
|
|
||||||
|
|
||||||
async function buildURL(streamURL, username, password) {
|
function buildURL(streamURL, username, password) {
|
||||||
let url = streamURL + "/player_api.php?username=" + username + "&password=" + password
|
return `${streamURL}/player_api.php?username=${username}&password=${password}`
|
||||||
return url
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function splitURL(url) {
|
async function splitURL(url) {
|
||||||
@ -173,56 +172,48 @@ async function userCheck(accountData) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function accountChecker(accountData, urlList) {
|
async function singleAccountCheck(accountData) {
|
||||||
let ip = await inst.get('http://api.ipify.org')
|
const streamURLS = await getStreamsNew();
|
||||||
console.log(ip.data);
|
const urlList = streamURLS.map(streamURL => buildURL(streamURL, accountData.username, accountData.password));
|
||||||
|
|
||||||
for (let index = 0; index < urlList.length; index++) {
|
const checkUrl = (url) => new Promise(async (resolve, reject) => {
|
||||||
const url = urlList[index];
|
|
||||||
console.log(index + 1 + ' of ' + urlList.length + ' urls checked')
|
|
||||||
try {
|
try {
|
||||||
let response = await inst.get(url, axiosOptions)
|
// Use tor instance first
|
||||||
if (response.data.user_info.auth) {
|
const response = await inst.get(url, axiosOptions);
|
||||||
addNewAccount(accountData, response.data, url)
|
if (response.data && response.data.user_info && response.data.user_info.auth) {
|
||||||
console.log('New Account Added')
|
return resolve({ url, data: response.data });
|
||||||
// syncApache()
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
try {
|
// Fallback to regular axios on 403 error
|
||||||
if (error.response.status == 403) {
|
if (error.response && error.response.status === 403) {
|
||||||
let response2 = await axios.get(url, axiosOptions)
|
try {
|
||||||
if (response2.data.user_info.auth) {
|
const response2 = await axios.get(url, axiosOptions);
|
||||||
try {
|
if (response2.data && response2.data.user_info && response2.data.user_info.auth) {
|
||||||
await addNewAccount(accountData, response2.data, url)
|
return resolve({ url, data: response2.data });
|
||||||
console.log('New Account Added')
|
|
||||||
// syncApache()
|
|
||||||
return true
|
|
||||||
} catch (error) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} 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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user