const { getStreamsNew } = require('../routes/getStreams') const { updateStreamData } = require('./updateStreams') const { decryptPassword } = require('./password') const { getAllUserAccounts, getUserAccountsCheck } = require('./getUser') const { removeAccountFromDB, storeAccountToDB } = require('../lib/Accounts') const axios = require('axios') const { wrapper } = require('axios-cookiejar-support') // import { wrapper } from 'axios-cookiejar-support'; const { CookieJar } = require('tough-cookie') // import { CookieJar } from 'tough-cookie'; const jar = new CookieJar(); const client = wrapper(axios.create({ jar })); // axios.defaults.timeout = 5000; 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 } async function splitURL(url) { try { let extractedURL = url.match(/[^/]*\/\/[^/]*/)[0] let extractedUserPass = url.match(/\/player_api\.php\?username=([\s\S]*)$/)[1] let UserPass = extractedUserPass.split('&password=') return { "extractedUrl": extractedURL, "username": UserPass[0], "password": UserPass[1] } } catch { } } async function updateAccounts(userAccounts, data, url) { try { let URL = await splitURL(url) let account = userAccounts.filter(account => account.username == URL.username && account.passwordDecryped == URL.password)[0] await updateStreamData({ "username": account.username, "password": account.password, "userId": account.userId }, URL.extractedUrl, data.user_info.exp_date) var date = new Date(data.user_info.exp_date * 1000).toLocaleDateString(undefined, { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }) console.log(`${userAccount.username} - - ${URL.extractedUrl} - Expires ${date}.`) } catch (error) { } } async function addNewAccount(userAccount, data, url) { try { let URL = await splitURL(url) await storeAccountToDB({ "username": userAccount.username, "password": userAccount.password, "userId": userAccount.userId, "streamName": userAccount.stream, "streamURL": URL.extractedUrl, "expiryDate": data.user_info.exp_date }, URL.extractedUrl, data.user_info.exp_date) var date = new Date(data.user_info.exp_date * 1000).toLocaleDateString(undefined, { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }) console.log(`${userAccount.username} - - ${URL.extractedUrl} - Expires ${date}.`) } catch (error) { } } async function userCheck(accountData) { console.log(Date.now()) streamURLS = await getStreamsNew() let userAccounts = await getUserAccountsCheck(accountData.userId) userAccounts = userAccounts.map(user => { user.userId = accountData.userId user.passwordDecryped = decryptPassword(user.password) return user }) urlList = [] for (let index = 0; index < userAccounts.length; index++) { let userAccount = userAccounts[index]; userAccount.urls = [] for (let index = 0; index < streamURLS.length; index++) { const streamURL = streamURLS[index]; userAccount.urls.push(await buildURL(streamURL, userAccount.username, userAccount.passwordDecryped)) urlList.push(await buildURL(streamURL, userAccount.username, userAccount.passwordDecryped)) } } userAccounts urlList await Promise.all(urlList.map(async (url) => { try { const response = await axios({ method: 'GET', headers: { 'Content-Type': 'application/json', 'User-Agent': 'PostmanRuntime/7.28.4', 'Accept': '*/*' }, url: url, }) console.log(url) if (response.status == 200 && response.data !== "") { try { let data = response.data if (data.user_info.auth) { console.log(url) updateAccounts(userAccounts, data, url) return true } else { return false } } catch (error) { console.log(error) return false } } else { return false } } catch (error) { console.log(error) return false } })).then((values) => { console.log(values) if (values.includes(true)) { let count = values.filter(function (value) { return value === true; }).length let count2 = values.filter(function (value) { return value === false; }).length console.log('true ' + count) console.log('fail ' + count2) t = count + count2 console.log(t) endresponse = true } else { console.log('true ' + count) console.log('fail ' + count2) t = count + count2 console.log(t) endresponse = false } }); return endresponse } // async function newCheck() { // streamURLS = await getStreamsNew() // let userAccounts = await getAllUserAccounts() // userAccounts = userAccounts.map(async user => { // user.passwordDecryped = decryptPassword(user.password) // return user // }) // for (let userAccount of userAccounts) { // if (userAccount.expiaryDate != 123 && Math.round(Date.now() / 1000) > userAccount.expiaryDate) { // setExpired(userAccount) // } else { // for (let streamURL of streamURLS) { // process.stdout.write('.') // let url = await buildURL(streamURL, userAccount.username, userAccount.passwordDecryped) // let t = await gotRequest(url) // let body = t.body // if (t.statusCode == 200 && body !== "") { // try { // body = JSON.parse(body) // if (body.user_info.auth) { // var date = new Date(body.user_info.exp_date * 1000).toLocaleDateString(undefined, { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }) // // process.stdout.write('\n') // console.log(`${userAccount.username} - ${userAccount.stream} - ${streamURL} - Expires ${date}.`) // await updateStreamData(userAccount, streamURL, body.user_info.exp_date) // break // } // } catch (error) { // } // } // } // } // } // } // async function updateAccountDNS(userAccount, streamURLS) { // // return new Promise(async (resolve, reject) => { // await Promise.all(streamURLS.map(async (streamURL) => { // process.stdout.write('.') // let url = await buildURL(streamURL, userAccount.username, userAccount.passwordDecryped) // // console.log('tested url ' + url) // let t = await gotRequest(url) // let body = t.body // if (t.statusCode == 200 && body !== "") { // try { // body = JSON.parse(body) // if (body.user_info.auth) { // var date = new Date(body.user_info.exp_date * 1000).toLocaleDateString(undefined, { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }) // // process.stdout.write('\n') // console.log(`${userAccount.username} - ${userAccount.stream} - ${streamURL} - Expires ${date}.`) // await updateStreamData(userAccount, streamURL, body.user_info.exp_date) // resolve(true) // } // } catch (error) { // } // } else { // } // })); // } // async function updateAccounts(userAccount, streamURLS) { // return new Promise(async (resolve, reject) => { // await streamURLS.forEach(async streamURL => { // process.stdout.write('.') // let url = await buildURL(streamURL, userAccount.username, userAccount.passwordDecryped) // console.log('tested url ' + url) // let t = await gotRequest(url) // let body = t.body // if (t.statusCode == 200 && body !== "") { // try { // body = JSON.parse(body) // if (body.user_info.auth) { // var date = new Date(body.user_info.exp_date * 1000).toLocaleDateString(undefined, { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }) // // process.stdout.write('\n') // console.log(`${userAccount.username} - ${userAccount.stream} - ${streamURL} - Expires ${date}.`) // await updateStreamData(userAccount, streamURL, body.user_info.exp_date) // resolve(true) // } // } catch (error) { // } // } else { // } // }) // reject(userAccount) // }) // } // async function singleCheck(accountData) { // streamURLS = await getStreamsNew() // let userAccounts = await getUserAccountsCheck(accountData.userId) // userAccounts = userAccounts.map(user => { // user.userId = accountData.userId // user.passwordDecryped = decryptPassword(user.password) // return user // }) // userAccounts = userAccounts.filter(obj => { // return obj.username === accountData.username && obj.passwordDecryped === accountData.password && obj.stream === accountData.stream // }) // try { // for (let i = 0; i < userAccounts.length; ++i) { // const userAccount = userAccounts[i]; // if (userAccount.expiaryDate != 123 && Math.round(Date.now() / 1000) > userAccount.expiaryDate) { // setExpired(userAccount) // } else { // await updateAccounts(userAccount, streamURLS) // } // } // console.log(Date.now()) // return true // } catch (userAccount) { // console.log('notfoundincatch') // // console.error(error); // removeAccountFromDB({ // "user": userAccount.username, // "stream": userAccount.stream, // "userId": accountData.userId // }) // } // } async function singleAccountCheck(accountData) { let endresponse console.log(Date.now()) 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 await Promise.all(urlList.map(async (url) => { try { // console.log(url) const response = await axios.get(url) // console.log(response) if (response.status == 200 && response.data !== "") { try { let data = response.data if (data.user_info.auth) { addNewAccount(accountData, data, url) return true } } catch (error) { } } } catch { } })).then((values) => { console.log(values) if (values.includes(true)) { endresponse = true } else { endresponse = false } }); return endresponse } module.exports = { userCheck, singleAccountCheck }