ktvmanager/lib/checker.js

299 lines
10 KiB
JavaScript
Raw Normal View History

2021-03-26 12:42:13 +00:00
const { getStreamsNew } = require('../routes/getStreams')
const { updateStreamData } = require('./updateStreams')
2021-02-13 13:14:15 +00:00
const { gotRequest } = require('./gotRequest')
2021-03-26 12:42:13 +00:00
const { decryptPassword } = require('./password')
2021-09-14 13:05:07 +00:00
const { getAllUserAccounts, getUserAccountsCheck } = require('./getUser')
const { setExpired } = require('./setExpired')
2021-09-21 13:26:03 +00:00
const { removeAccountFromDB, storeAccountToDB } = require('../lib/Accounts')
2021-03-26 12:42:13 +00:00
2021-09-21 13:37:32 +00:00
const axios = require('axios')
axios.defaults.timeout = 10000;
2021-02-13 13:14:15 +00:00
const delay = ms => new Promise(res => setTimeout(res, ms));
2021-09-20 10:57:49 +00:00
2021-03-26 12:42:13 +00:00
async function buildURL(streamURL, username, password) {
let url = streamURL + "/player_api.php?username=" + username + "&password=" + password
return url
}
2021-09-21 13:37:32 +00:00
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) {
2021-09-21 13:26:03 +00:00
try {
2021-09-21 13:37:32 +00:00
let URL = await splitURL(url)
2021-09-21 13:26:03 +00:00
2021-09-21 13:37:32 +00:00
let account = userAccounts.filter(account => account.username == URL.username && account.passwordDecryped == URL.password)[0]
2021-09-21 13:26:03 +00:00
await updateStreamData({
"username": account.username,
"password": account.password,
"userId": account.userId
2021-09-21 13:37:32 +00:00
}, URL.extractedUrl, data.user_info.exp_date)
2021-09-21 13:26:03 +00:00
var date = new Date(data.user_info.exp_date * 1000).toLocaleDateString(undefined, { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' })
2021-09-21 13:37:32 +00:00
console.log(`${userAccount.username} - - ${URL.extractedUrl} - Expires ${date}.`)
2021-09-21 13:26:03 +00:00
} catch (error) {
}
}
async function addNewAccount(userAccount, data, url) {
try {
2021-09-21 13:37:32 +00:00
let URL = await splitURL(url)
2021-09-21 13:26:03 +00:00
await storeAccountToDB({
"username": userAccount.username,
"password": userAccount.password,
"userId": userAccount.userId,
"streamName": userAccount.stream,
2021-09-21 13:37:32 +00:00
"streamURL": URL.extractedUrl,
2021-09-21 13:26:03 +00:00
"expiryDate": data.user_info.exp_date
2021-09-21 13:37:32 +00:00
}, URL.extractedUrl, data.user_info.exp_date)
2021-09-21 13:26:03 +00:00
var date = new Date(data.user_info.exp_date * 1000).toLocaleDateString(undefined, { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' })
2021-09-21 13:37:32 +00:00
console.log(`${userAccount.username} - - ${URL.extractedUrl} - Expires ${date}.`)
2021-09-21 13:26:03 +00:00
} 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.get(url)
// console.log(url)
if (response.status == 200 && response.data !== "") {
try {
let data = response.data
if (data.user_info.auth) {
2021-09-21 13:37:32 +00:00
updateAccounts(userAccounts, data, url)
2021-09-21 13:26:03 +00:00
return true
} else {
return true
}
} catch (error) {
// return true
}
} else {
return true
}
} catch {
// return true
}
}))
return true
}
2021-02-13 13:14:15 +00:00
2021-03-26 12:42:13 +00:00
async function newCheck() {
streamURLS = await getStreamsNew()
let userAccounts = await getAllUserAccounts()
2021-09-20 10:57:49 +00:00
userAccounts = userAccounts.map(async user => {
2021-03-26 12:42:13 +00:00
user.passwordDecryped = decryptPassword(user.password)
return user
})
for (let userAccount of userAccounts) {
2021-09-14 13:05:07 +00:00
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) {
}
}
}
}
}
}
2021-09-21 13:26:03 +00:00
// 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 {
// }
// }));
// }
2021-09-14 13:05:07 +00:00
2021-09-21 15:59:29 +00:00
// 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)
// })
// }
2021-09-14 13:05:07 +00:00
2021-09-20 16:19:16 +00:00
async function singleCheck(accountData) {
2021-09-14 13:05:07 +00:00
streamURLS = await getStreamsNew()
let userAccounts = await getUserAccountsCheck(accountData.userId)
userAccounts = userAccounts.map(user => {
2021-09-20 10:57:49 +00:00
user.userId = accountData.userId
2021-09-14 13:05:07 +00:00
user.passwordDecryped = decryptPassword(user.password)
return user
})
2021-09-20 10:57:49 +00:00
userAccounts = userAccounts.filter(obj => {
2021-09-14 13:05:07 +00:00
return obj.username === accountData.username && obj.passwordDecryped === accountData.password && obj.stream === accountData.stream
})
2021-09-20 10:57:49 +00:00
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)
2021-03-26 12:42:13 +00:00
}
}
2021-09-20 10:57:49 +00:00
console.log(Date.now())
return true
} catch (userAccount) {
console.log('notfoundincatch')
// console.error(error);
removeAccountFromDB({
"user": userAccount.username,
"stream": userAccount.stream,
"userId": accountData.userId
})
2021-03-26 12:42:13 +00:00
}
}
2021-09-20 19:30:47 +00:00
async function singleAccountCheck(accountData) {
2021-09-21 13:26:03 +00:00
let endresponse
console.log(Date.now())
2021-09-20 19:30:47 +00:00
streamURLS = await getStreamsNew()
2021-09-21 13:26:03 +00:00
2021-09-20 19:30:47 +00:00
urlList = []
for (let index = 0; index < streamURLS.length; index++) {
const streamURL = streamURLS[index];
urlList.push(await buildURL(streamURL, accountData.username, accountData.password))
}
2021-09-21 13:26:03 +00:00
urlList
2021-09-20 19:30:47 +00:00
2021-09-21 13:26:03 +00:00
await Promise.all(urlList.map(async (url) => {
2021-09-20 19:30:47 +00:00
try {
2021-09-21 13:37:32 +00:00
// console.log(url)
2021-09-21 13:26:03 +00:00
const response = await axios.get(url)
2021-09-21 13:37:32 +00:00
// console.log(response)
2021-09-20 19:30:47 +00:00
if (response.status == 200 && response.data !== "") {
try {
let data = response.data
if (data.user_info.auth) {
2021-09-21 13:26:03 +00:00
addNewAccount(accountData, data, url)
2021-09-20 19:30:47 +00:00
return true
}
} catch (error) {
}
}
2021-09-21 13:26:03 +00:00
} catch {
2021-09-20 19:30:47 +00:00
}
2021-09-21 13:26:03 +00:00
})).then((values) => {
console.log(values)
if (values.includes(true)) {
endresponse = true
} else {
endresponse = false
}
});
return endresponse
2021-09-20 19:30:47 +00:00
}
2021-09-21 13:26:03 +00:00
2021-02-13 13:14:15 +00:00
module.exports = {
2021-03-26 12:42:13 +00:00
newCheck,
2021-09-20 16:30:41 +00:00
singleCheck,
2021-09-20 19:30:47 +00:00
userCheck,
singleAccountCheck
2021-02-13 13:14:15 +00:00
}