110 lines
3.1 KiB
JavaScript
Executable File
110 lines
3.1 KiB
JavaScript
Executable File
const { actions, configs } = require('apache-api');
|
|
|
|
const { getStreamsNew } = require('../routes/getStreams')
|
|
|
|
apache_config = 'tv.k-world'
|
|
|
|
const { exec } = require("child_process");
|
|
|
|
function restart() {
|
|
exec("sudo service apache2 reload", (error, stdout, stderr) => {
|
|
if (error) {
|
|
console.log(`error: ${error.message}`);
|
|
return;
|
|
}
|
|
if (stderr) {
|
|
console.log(`stderr: ${stderr}`);
|
|
return;
|
|
}
|
|
console.log(`no issues`);
|
|
});
|
|
|
|
}
|
|
|
|
function makeid() {
|
|
var result = '';
|
|
var characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
|
|
var charactersLength = characters.length;
|
|
for (var i = 0; i < 5; i++) {
|
|
result += characters.charAt(Math.floor(Math.random() *
|
|
charactersLength));
|
|
}
|
|
return result;
|
|
}
|
|
|
|
async function get_config() {
|
|
return await configs.readConfig(apache_config, true, true)
|
|
}
|
|
|
|
async function split_streams(siteList) {
|
|
const notUndefined = anyValue => typeof anyValue !== 'undefined'
|
|
|
|
final_array = siteList.map(function (item) {
|
|
if (item.content.includes('Redirect')) {
|
|
var cleanString = item.content.replace(/^(.*?)\//g, "");
|
|
cleanString = cleanString.split(' ')
|
|
cleanString[1] = cleanString[1].replace(/"/g, "")
|
|
return cleanString
|
|
}
|
|
}).filter(notUndefined)
|
|
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 checkExists(fullStreamList, mappedStreams) {
|
|
newstreams = mappedStreams.map(function (s) {
|
|
return s[1]
|
|
})
|
|
myArray = fullStreamList.filter(function (el) {
|
|
return newstreams.indexOf(el) < 0;
|
|
});
|
|
if (myArray.length == 0) {
|
|
console.log('No new streams')
|
|
} else {
|
|
console.log('New streams ' + myArray)
|
|
}
|
|
return myArray
|
|
}
|
|
|
|
|
|
async function addNewStreams(config, streamList) {
|
|
streamList.forEach(stream => {
|
|
config.children[0].children.push({ "tagName": '$rule', "content": `Redirect 302 /${makeid()} "${stream}"` })
|
|
})
|
|
try {
|
|
await configs.saveConfig(apache_config, config, true, true)
|
|
} catch (error) {
|
|
console.log(error)
|
|
}
|
|
}
|
|
|
|
// update('DIAZ', 'http://google.com', 'https://watchtower.ddns.net:8443')
|
|
|
|
|
|
async function main() {
|
|
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)
|
|
await restart()
|
|
}
|
|
|
|
main()
|