ktvmanager/lib/checker.js

236 lines
7.6 KiB
JavaScript
Raw Permalink Normal View History

2021-03-26 12:42:13 +00:00
const { getStreamsNew } = require('../routes/getStreams')
2021-11-19 14:25:42 +00:00
const { updateStreamData, updateOtherURLs } = require('./updateStreams')
2021-03-26 12:42:13 +00:00
const { decryptPassword } = require('./password')
2021-11-19 13:12:48 +00:00
const { getUserAccountsCheck, getAllUniqueAccounts, getAllUserAccounts, getUserUniqueAccounts } = require('./getUser')
2021-09-23 15:26:24 +00:00
const { storeAccountToDB } = require('../lib/Accounts')
2021-11-19 11:52:28 +00:00
const { syncApache } = require('./apache_functions')
2021-03-26 12:42:13 +00:00
2021-09-21 13:37:32 +00:00
const axios = require('axios')
2021-09-23 15:26:24 +00:00
const tor_axios = require('tor-axios');
const tor = tor_axios.torSetup({
2021-09-23 17:25:44 +00:00
ip: process.env.TORSSRV,
2021-09-23 15:26:24 +00:00
port: 9050,
2021-11-16 17:37:04 +00:00
controlPort: 9051,
2021-09-23 17:25:44 +00:00
controlPassword: process.env.TORSPWD,
2021-09-23 15:26:24 +00:00
})
2021-09-23 13:54:01 +00:00
2021-11-19 11:52:28 +00:00
axios.defaults.timeout = 3000;
tor.defaults.timeout = 3000
2021-09-24 11:34:49 +00:00
const inst = axios.create({
httpAgent: tor.httpAgent(),
httpsagent: tor.httpsAgent(),
});
let axiosOptions = {
headers: {
'User-Agent': 'IPTV Smarters Pro'
}
}
2021-11-19 11:52:28 +00:00
2021-09-21 13:37:32 +00:00
2021-11-19 15:45:10 +00:00
// const delay = ms => new Promise(res => setTimeout(res, ms));
2021-02-13 13:14:15 +00:00
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-11-19 14:25:42 +00:00
let urlData = await splitURL(url)
2021-09-21 13:26:03 +00:00
2021-11-19 14:25:42 +00:00
let account = userAccounts.filter(account => account.username == urlData.username && account.passwordDecryped == urlData.password)[0]
2021-09-21 13:26:03 +00:00
2021-11-19 14:25:42 +00:00
await updateStreamData({
"username": account.username,
"password": account.password,
"userId": account.userId
}, urlData.extractedUrl, data.user_info.exp_date)
console.log(`${urlData.extractedUrl}.`)
await updateOtherURLs(account.stream, urlData.extractedUrl)
2021-09-21 13:26:03 +00:00
}
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-11-16 17:37:04 +00:00
"expiryDate": data.user_info.exp_date,
"maxConnections": data.user_info.max_connections
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-11-19 11:52:28 +00:00
console.log(`${userAccount.username} - ${URL.extractedUrl} - Expires ${date}.`)
2021-09-21 13:26:03 +00:00
} catch (error) {
}
}
2021-11-19 11:52:28 +00:00
2021-11-19 14:25:42 +00:00
async function urlChecker(userAccounts) {
let ip = await inst.get('http://api.ipify.org')
console.log(ip.data);
2021-11-19 11:52:28 +00:00
2021-11-19 14:25:42 +00:00
let updateCount = 0
2021-11-19 11:52:28 +00:00
2021-11-19 14:25:42 +00:00
for (let index = 0; index < userAccounts.length; index++) {
const userAccount = userAccounts[index];
let count = index + 1
console.log(count + ' of ' + userAccounts.length + ' ' + userAccount.username)
for (let index2 = 0; index2 < userAccount.urls.length; index2++) {
const url = userAccount.urls[index2];
// console.log('url ' + index2 + 1 + ' of ' + userAccount.urls.length)
2021-11-19 15:45:10 +00:00
// console.log('.')
2021-11-19 14:25:42 +00:00
try {
let response = await inst.get(url, axiosOptions)
if (response.data.user_info.auth) {
await updateAccounts(userAccounts, response.data, url)
updateCount++
break
}
} catch (error) {
try {
if (error.response.status == 403) {
let response2 = await axios.get(url, axiosOptions)
if (response2.data.user_info.auth) {
try {
await updateAccounts(userAccounts, response2.data, url)
updateCount++
break
} catch (error) {
continue
}
}
}
} catch (error) {
continue
}
}
}
}
syncApache()
return {
"updatedCount": updateCount,
"totalAccounts": userAccounts.length
}
}
2021-11-19 11:52:28 +00:00
2021-09-21 13:26:03 +00:00
async function userCheck(accountData) {
2021-09-24 11:34:49 +00:00
let start = Date.now()
console.log(start)
2021-09-21 13:26:03 +00:00
streamURLS = await getStreamsNew()
2021-11-19 13:12:48 +00:00
let userAccounts = await getUserUniqueAccounts(accountData.userId)
2021-09-21 13:26:03 +00:00
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))
}
2021-11-19 14:25:42 +00:00
userAccount.urls = userAccount.urls.filter(function (e) { return !e.includes('stormtv') && !e.includes('megaott') })
2021-09-21 13:26:03 +00:00
}
2021-11-19 14:25:42 +00:00
values = await urlChecker(userAccounts)
if (values.updatedCount > 0) {
return {
2021-09-23 15:26:24 +00:00
"update": true,
2021-11-19 14:25:42 +00:00
"count": values.updatedCount
2021-09-23 15:26:24 +00:00
}
2021-11-19 14:25:42 +00:00
}
else {
2021-09-23 15:26:24 +00:00
return endresponse = false
}
}
2021-03-26 12:42:13 +00:00
2021-11-19 11:52:28 +00:00
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, data, url)
2021-11-19 15:45:10 +00:00
console.log('New Account Added')
syncApache()
return true
2021-11-19 11:52:28 +00:00
}
} 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
}
2021-09-20 19:30:47 +00:00
async function singleAccountCheck(accountData) {
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-11-19 11:52:28 +00:00
return await accountChecker(accountData, urlList)
}
2021-09-20 19:30:47 +00:00
2021-09-24 11:34:49 +00:00
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-09-20 19:30:47 +00:00
userCheck,
2021-11-19 11:52:28 +00:00
singleAccountCheck,
}
// singleAccountCheck({ "username": "Karl0ss01", "password": "YqQjYH9Nzw", "userId": 1, "stream": "Badger" })