diff --git a/.vscode/launch.json b/.vscode/launch.json index 7367db7..fc42af6 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -11,7 +11,16 @@ "skipFiles": [ "/**" ], - "type": "pwa-node" + "type": "pwa-node", + "env": { + "DBHOST": "vps.k-world.me.uk", + "DBUSER": "root", + "DBPASS": "Grd555269", + "DATABASE": "BBLB_DNS", + "DBPORT": "3306", + "TORSSRV": "localhost", + "TORSPWD": "KarlMax", + } }, { "type": "node", @@ -45,6 +54,8 @@ "DBPASS": "Grd555269", "DATABASE": "BBLB_DNS", "DBPORT": "3306", + "TORSSRV": "localhost", + "TORSPWD": "KarlMax", } } ] diff --git a/lib/apache_functions.js b/lib/apache_functions.js index a1b978f..04d2e5b 100755 --- a/lib/apache_functions.js +++ b/lib/apache_functions.js @@ -1,6 +1,8 @@ -const { actions, configs } = require('apache-api'); +const { configs } = require('apache-api'); const { getStreamsNew } = require('../routes/getStreams') +const { getURLandStreams } = require('./getStreamNames') +const { allUserCheck } = require('./checker') apache_config = 'tv.k-world' @@ -50,22 +52,45 @@ async function split_streams(siteList) { return final_array } -// async function updateStream(sub, oldUrl, newUrl) { -// config = await get_config() -// config.children[0].children.map(function (item) { -// // string = `Redirect 302 /${sub} "${oldUrl}"` -// if (item.content.includes(oldUrl)) { -// console.log('found') -// return item.content = `Redirect 302 /${sub} "${newUrl}"` -// } -// }) -// try { -// await configs.saveConfig(apache_config, config, true, true) -// } catch (error) { -// console.log(error) -// } -// await actions.restartApache() -// } +async function updateStream(updatedStreams) { + config = await get_config() + updatedStreams.forEach(async stream => { + config.children[0].children.map(async function (item) { + if (item.content.includes(stream[1])) { + return item.content = `Redirect 302 /${stream[0]} "${stream[2]}"` + } + }) + try { + await configs.saveConfig(apache_config, config, true, true) + console.log(stream[0] + ' url updated') + } catch (error) { + console.log(stream[0] + ' url failed to update') + console.log(error) + } + }) +} + + + +async function updateStreamCheck(streamsFromKtvDB, streamsFromConfig) { + updatedStreams = [] + streamsFromConfig.forEach(sfc => { + streamsFromKtvDB.forEach(sfktv => { + if (sfc[0] == sfktv.stream) { + sfc[0] + sfktv.stream + if (sfc[1] != sfktv.streamURL) { + console.log(sfc[0] + ' url changed') + updatedStreams.push([sfc[0], sfc[1], sfktv.streamURL]) + } else { + // console.log(sfc[0] + ' not updated') + } + } + }); + }); + return updatedStreams +} + async function checkExists(fullStreamList, mappedStreams) { newstreams = mappedStreams.map(function (s) { @@ -94,16 +119,32 @@ async function addNewStreams(config, streamList) { } } -// update('DIAZ', 'http://google.com', 'https://watchtower.ddns.net:8443') +async function syncApache() { + // console.log('Updating all user account from ktv') + // await allUserCheck() + // console.log('Update complete') + console.log('Comparing streams from ktv and apache') + streams = await getStreamsNew() + config = await get_config() + streamsFromConfig = await split_streams(config.children[0].children) - -async function main() { + streamsFromKtvDB = await getURLandStreams() + updatedStreams = await updateStreamCheck(streamsFromKtvDB, streamsFromConfig) + await updateStream(updatedStreams) + console.log('Complete') + console.log('Adding any missing streams to apache') streams = await getStreamsNew() config = await get_config() streamsFromConfig = await split_streams(config.children[0].children) missingStreamsInApache = await checkExists(streams, streamsFromConfig) resultOfAddStreams = await addNewStreams(config, missingStreamsInApache) + console.log('Complete') + console.log('Restarting Apache') await restart() + console.log('Complete') +} + +module.exports = { + syncApache } -main() diff --git a/lib/checker.js b/lib/checker.js index 208451d..3df0917 100644 --- a/lib/checker.js +++ b/lib/checker.js @@ -1,8 +1,10 @@ const { getStreamsNew } = require('../routes/getStreams') const { updateStreamData } = require('./updateStreams') const { decryptPassword } = require('./password') -const { getUserAccountsCheck } = require('./getUser') +const { getUserAccountsCheck, getAllUniqueAccounts, getAllUserAccounts } = require('./getUser') const { storeAccountToDB } = require('../lib/Accounts') +const { syncApache } = require('./apache_functions') + const axios = require('axios') const tor_axios = require('tor-axios'); @@ -13,6 +15,9 @@ const tor = tor_axios.torSetup({ controlPassword: process.env.TORSPWD, }) +axios.defaults.timeout = 3000; +tor.defaults.timeout = 3000 + const inst = axios.create({ httpAgent: tor.httpAgent(), httpsagent: tor.httpsAgent(), @@ -23,8 +28,7 @@ let axiosOptions = { 'User-Agent': 'IPTV Smarters Pro' } } -// axios.defaults.timeout = 10000; -// tor.defaults.timeout = 10000 + const delay = ms => new Promise(res => setTimeout(res, ms)); @@ -81,13 +85,12 @@ async function addNewAccount(userAccount, data, url) { "maxConnections": data.user_info.max_connections }, 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}.`) + console.log(`${userAccount.username} - ${URL.extractedUrl} - Expires ${date}.`) } catch (error) { } } async function urlChecker(userAccounts, urlList) { - // await tor.torNewSession(); //change tor ip let ip = await inst.get('http://api.ipify.org') console.log(ip.data); await Promise.all(urlList.map(async (url) => { @@ -99,7 +102,7 @@ async function urlChecker(userAccounts, urlList) { try { let data = response.data if (data.user_info.auth) { - console.log(url) + console.log('.') updateAccounts(userAccounts, data, url) return true } @@ -115,6 +118,89 @@ async function urlChecker(userAccounts, urlList) { } +// async function allUserCheck() { +// let start = Date.now() +// console.log(start) +// streamURLS = await getStreamsNew() +// let userAccounts = await getAllUserAccounts() + +// 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)) +// } +// } + + +// values = await urlCheckerNew(userAccounts[3], urlList) +// if (values.includes(true)) { +// let updatedCount = values.filter(function (value) { +// return value === true; +// }).length +// console.log('true ' + updatedCount) +// console.log((Date.now() - start) / 1000) +// return endresponse = { +// "update": true, +// "count": updatedCount +// } +// } else { +// return endresponse = false +// } + +// } + +// async function allUserCheck() { +// let start = Date.now() +// console.log(start) +// streamURLS = await getStreamsNew() +// let userAccounts = await getAllUserAccounts() + +// 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)) +// } +// } + + +// values = await urlChecker(userAccounts, urlList) +// if (values.includes(true)) { +// let updatedCount = values.filter(function (value) { +// return value === true; +// }).length +// console.log('true ' + updatedCount) +// console.log((Date.now() - start) / 1000) +// return endresponse = { +// "update": true, +// "count": updatedCount +// } +// } else { +// return endresponse = false +// } + +// } + + async function userCheck(accountData) { let start = Date.now() console.log(start) @@ -156,9 +242,44 @@ 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, data, url) + break + } + } 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) { - let endresponse streamURLS = await getStreamsNew() urlList = [] @@ -168,39 +289,16 @@ async function singleAccountCheck(accountData) { urlList.push(await buildURL(streamURL, accountData.username, accountData.password)) } urlList - - let ip = await inst.get('http://api.ipify.org') - console.log(ip.data); - - await Promise.all(urlList.map(async (url) => { - - try { - response = await inst.get(url, axiosOptions) - if (response.status == 200 && response.data !== "") { - try { - let data = response.data - if (data.user_info.auth) { - addNewAccount(accountData, data, url) - return true - } - } catch { - } - } - } catch { - } - })).then((values) => { - console.log(values) - if (values.includes(true)) { - endresponse = true - } else { - endresponse = false - } - }); - return endresponse + return await accountChecker(accountData, urlList) } + + module.exports = { userCheck, - singleAccountCheck -} \ No newline at end of file + singleAccountCheck, + // allUserCheck +} + +// singleAccountCheck({ "username": "Karl0ss01", "password": "YqQjYH9Nzw", "userId": 1, "stream": "Badger" }) \ No newline at end of file diff --git a/lib/getStreamNames.js b/lib/getStreamNames.js index cb548ae..9b446b6 100644 --- a/lib/getStreamNames.js +++ b/lib/getStreamNames.js @@ -10,7 +10,19 @@ function getStreamNames() { } } -module.exports = { - getStreamNames + +function getURLandStreams() { + let data = sql.query(`SELECT DISTINCT stream, streamURL FROM BBLB_DNS.userAccounts`) + // console.log(data) + if (data.length == 0) { + return 'StreamsFailed' + } else { + return data + } +} + +module.exports = { + getStreamNames, + getURLandStreams } diff --git a/lib/getUser.js b/lib/getUser.js index a7f187c..201a19c 100644 --- a/lib/getUser.js +++ b/lib/getUser.js @@ -56,6 +56,17 @@ ORDER BY userAccounts.id DESC } } +function getAllUniqueAccounts() { + let data = sql.query(`SELECT DISTINCT(userAccounts.stream), userAccounts.username, userAccounts.password, userAccounts.stream FROM userAccounts + WHERE userAccounts.expiaryDate != '0' + GROUP BY userAccounts.stream;`) + if (data.length == 0) { + return 'User Not Found' + } else { + return data + } +} + function getUserId(user) { let data = sql.query(`SELECT id FROM users WHERE userName = '${user}'`) // console.log(data) @@ -71,6 +82,7 @@ module.exports = { getUserAccounts, getAllUserAccounts, getUserId, - getUserAccountsCheck + getUserAccountsCheck, + getAllUniqueAccounts }