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));
|
||||
|
||||
|
||||
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);
|
||||
|
||||
for (let index = 0; index < urlList.length; index++) {
|
||||
const url = urlList[index];
|
||||
console.log(index + 1 + ' of ' + urlList.length + ' urls checked')
|
||||
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
|
||||
}
|
||||
} 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
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
async function singleAccountCheck(accountData) {
|
||||
streamURLS = await getStreamsNew()
|
||||
const streamURLS = await getStreamsNew();
|
||||
const urlList = streamURLS.map(streamURL => buildURL(streamURL, accountData.username, accountData.password));
|
||||
|
||||
urlList = []
|
||||
|
||||
for (let index = 0; index < streamURLS.length; index++) {
|
||||
const streamURL = streamURLS[index];
|
||||
urlList.push(await buildURL(streamURL, accountData.username, accountData.password))
|
||||
const checkUrl = (url) => new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
// 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) {
|
||||
// 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
|
||||
}
|
||||
}
|
||||
}
|
||||
// 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;
|
||||
}
|
||||
urlList
|
||||
return await accountChecker(accountData, urlList)
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user