diff --git a/bin/server.js b/bin/server.js index 050d409..629622c 100644 --- a/bin/server.js +++ b/bin/server.js @@ -4,6 +4,7 @@ const path = require("path") const { __glob } = require("./global-variables") const auth = require("./auth") const files = require("./files") +const service = require("./services") const plog = new LogType("Web") const cook = require("cookie") const http = require("http") @@ -44,6 +45,8 @@ module.exports.serverIO = function(server) { * POST REQUEST */ + if(user.checkPermission("FILES_EXPLORER")) { + PostRequest("FX_GET", (root) => { PostAnswer("FX_GET", files.getFiles(root)) @@ -90,6 +93,30 @@ module.exports.serverIO = function(server) { }) + } + + if(user.checkPermission("SERVICES")) { + + PostRequest("SV_GET_SERVICE_STATUS", async (sv) => { + PostAnswer("SV_GET_SERVICE_STATUS", {answer: await service.getServiceStatus(sv), name: sv}) + }) + + PostRequest("SV_START_SERVICE", async (sv) => { + PostAnswer("SV_START_SERVICE", {answer: await service.startService(sv), name: sv}) + }) + + PostRequest("SV_STOP_SERVICE", async (sv) => { + PostAnswer("SV_STOP_SERVICE", {answer: await service.stopService(sv), name: sv}) + }) + + PostRequest("SV_RESTART_SERVICE", async (sv) => { + PostAnswer("SV_RESTART_SERVICE", {answer: await service.restartService(sv), name: sv}) + }) + + + + } + socket.on("disconnect", () => { plog.log("Déconnexion au panel par '" + user.username + "' avec le socket : " + socket.id) diff --git a/bin/services.js b/bin/services.js new file mode 100644 index 0000000..ae02025 --- /dev/null +++ b/bin/services.js @@ -0,0 +1,136 @@ +const { LogType } = require("loguix") +const fs = require("fs") +const path = require("path") +const { __glob } = require("./global-variables") +const clog = new LogType("Services") + +const http = require('http'); +const https = require('https'); + +module.exports.getServiceStatus = function(service) { + const protocol = service.startsWith('https') ? https : http; + const url = new URL(service); + const options = { + method: 'HEAD', + host: url.hostname, + port: url.port, + path: url.pathname, + }; + + return new Promise((resolve, reject) => { + const req = protocol.request(options, (res) => { + if(res.statusCode !== 502) { + resolve("ONLINE"); + } else { + + resolve("OFFLINE"); + } + + }); + + req.on('error', (err) => { + resolve("OFFLINE"); + }); + + req.end(); + }); +} + +module.exports.stopService = function(service) { + + + return new Promise((resolve, reject) => { + + const child_process = require('child_process'); + if(service == "https://subsonics.raphix.fr") { + let req = child_process.exec("ssh raphix@alpha.raphix.fr sudo -S -u gitlab-ci pm2 stop 'Subsonics'") + resolve("OK") + } else if(service == "https://git.raphix.fr" ) { + let req = child_process.exec("ssh raphix@omega.raphix.fr sudo -S systemctl stop gitea") + resolve("OK") + } else if(service == "https://jenkins.raphix.fr") { + let req = child_process.exec("ssh raphix@omega.raphix.fr sudo -S systemctl stop jenkins ") + resolve("OK") + + } else if(service == 'https://raphix.fr') { + let req = child_process.exec("ssh raphix@alpha.raphix.fr sudo -S -u gitlab-ci pm2 stop 'Website - Raphix'") + resolve("OK") + } else if(service == "https://cv.raphix.fr") { + let req = child_process.exec("ssh raphix@alpha.raphix.fr sudo -S -u gitlab-ci pm2 stop 'CV - Raphael'") + resolve("OK") + } else if(service == "http://omega.raphix.fr:2333") { + let req = child_process.exec(" ssh raphix@omega.raphix.fr sudo -S systemctl stop lavalink ") + resolve("OK") + } else { + resolve(false) + } + + }); + +} + +module.exports.startService = function(service) { + + + return new Promise((resolve, reject) => { + + const child_process = require('child_process'); + if(service == "https://subsonics.raphix.fr") { + let req = child_process.exec("ssh raphix@alpha.raphix.fr sudo -S -u gitlab-ci pm2 start /home/gitlab-ci/subsonic.config.js") + resolve("OK") + } else if(service == "https://git.raphix.fr" ) { + let req = child_process.exec("ssh raphix@omega.raphix.fr sudo -S systemctl start gitea") + resolve("OK") + } else if(service == "https://jenkins.raphix.fr") { + let req = child_process.exec("ssh raphix@omega.raphix.fr sudo -S systemctl start jenkins ") + resolve("OK") + + } else if(service == 'https://raphix.fr') { + let req = child_process.exec("ssh raphix@alpha.raphix.fr sudo -S -u gitlab-ci pm2 start /home/gitlab-ci/website.config.js") + resolve("OK") + } else if(service == "https://cv.raphix.fr") { + let req = child_process.exec("ssh raphix@alpha.raphix.fr sudo -S -u gitlab-ci pm2 start /home/gitlab-ci/cv.config.js") + resolve("OK") + } else if(service == "http://omega.raphix.fr:2333") { + let req = child_process.exec(" ssh raphix@omega.raphix.fr sudo -S systemctl start lavalink ") + resolve("OK") + } else { + resolve(false) + } + + }); + +} + +module.exports.restartService = function(service) { + + + return new Promise((resolve, reject) => { + + const child_process = require('child_process'); + if(service == "https://subsonics.raphix.fr") { + let req = child_process.exec("ssh raphix@alpha.raphix.fr sudo -S -u gitlab-ci pm2 restart 'Subsonics'") + resolve("OK") + } else if(service == "https://git.raphix.fr" ) { + let req = child_process.exec("ssh raphix@omega.raphix.fr sudo -S systemctl restart gitea") + resolve("OK") + } else if(service == "https://jenkins.raphix.fr") { + let req = child_process.exec("ssh raphix@omega.raphix.fr sudo -S systemctl restart jenkins ") + resolve("OK") + + } else if(service == 'https://raphix.fr') { + let req = child_process.exec("ssh raphix@alpha.raphix.fr sudo -S -u gitlab-ci pm2 restart 'Website - Raphix'") + resolve("OK") + } else if(service == "https://cv.raphix.fr") { + let req = child_process.exec("ssh raphix@alpha.raphix.fr sudo -S -u gitlab-ci pm2 restart 'CV - Raphael'") + resolve("OK") + } else if(service == "http://omega.raphix.fr:2333") { + let req = child_process.exec(" ssh raphix@omega.raphix.fr sudo -S systemctl restart lavalink ") + resolve("OK") + } else { + resolve(false) + } + + }); + +} \ No newline at end of file diff --git a/bin/users.js b/bin/users.js index b0c954f..f6d00a7 100644 --- a/bin/users.js +++ b/bin/users.js @@ -204,7 +204,6 @@ module.exports.User = class { this.#sync() if(this.permission.includes(name)) { return true - } else { return false diff --git a/public/images/services/cv.png b/public/images/services/cv.png new file mode 100644 index 0000000..0070038 Binary files /dev/null and b/public/images/services/cv.png differ diff --git a/public/images/services/gitea.svg b/public/images/services/gitea.svg new file mode 100644 index 0000000..ae433ce --- /dev/null +++ b/public/images/services/gitea.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/public/images/services/jenkins.svg b/public/images/services/jenkins.svg new file mode 100644 index 0000000..988991c --- /dev/null +++ b/public/images/services/jenkins.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/public/images/services/lavalink.svg b/public/images/services/lavalink.svg new file mode 100644 index 0000000..63fa153 --- /dev/null +++ b/public/images/services/lavalink.svg @@ -0,0 +1,48 @@ + + diff --git a/public/images/services/raphix.png b/public/images/services/raphix.png new file mode 100644 index 0000000..ca99963 Binary files /dev/null and b/public/images/services/raphix.png differ diff --git a/public/images/services/subsonics.png b/public/images/services/subsonics.png new file mode 100644 index 0000000..20cf698 Binary files /dev/null and b/public/images/services/subsonics.png differ diff --git a/public/javascripts/basics.js b/public/javascripts/basics.js index dcdfe6a..b57320a 100644 --- a/public/javascripts/basics.js +++ b/public/javascripts/basics.js @@ -93,7 +93,7 @@ class ViewWindow {
${properties.title}
- +Vous n'avez pas les permissions pour supprimer ce fichier.
` }) } else { @@ -383,7 +383,7 @@ function generateFileExplorer() { loadFiles(result) }) View.createPopup({ - title: ` Erreur`, + title: ` Erreur`, content: `Une erreur est survenue.
` }) @@ -394,7 +394,7 @@ function generateFileExplorer() { dropMenu.hide() View.createPopup({ - title: ` Renommer`, + title: ` Renommer`, content: ` @@ -416,7 +416,7 @@ function generateFileExplorer() { return } - const regex = new RegExp(/^[a-zA-Z0-9-_]+$/) + const regex = new RegExp(/^[a-zA-Z0-9-_.]+$/) if(!regex.test(rename.value) | rename.value.replace(/\s/g, '').length < 1) { renameInfo.err("Le nom du fichier / dossier est invalide.") @@ -451,12 +451,12 @@ function generateFileExplorer() { reqFiles.then((result) => { if(result == "NOT_PERMITTED") { View.createPopup({ - title: ` Erreur`, + title: ` Erreur`, content: `Vous n'avez pas les permissions pour partager ce fichier.
` }) } else { View.createPopup({ - title: ` Partager`, + title: ` Partager`, content: ` @@ -495,7 +495,7 @@ function generateFileExplorer() { reqFiles.then((result) => { if(result == "NOT_PERMITTED") { View.createPopup({ - title: ` Erreur`, + title: ` Erreur`, content: `Vous n'avez pas les permissions pour télécharger ce fichier.
` }) } else { @@ -527,7 +527,7 @@ function generateFileExplorer() { reqFiles.then((result) => { if(result == "NOT_PERMITTED") { View.createPopup({ - title: ` Erreur`, + title: ` Erreur`, content: `Vous n'avez pas les permissions pour éditer ce fichier.
` }) } else { @@ -564,13 +564,13 @@ function generateFileExplorer() { loadFiles(result) }) } else if(result == "NOT_PERMITTED") { - View.createPopup({ - title: ` Erreur`, + editor.createPopup({ + title: ` Erreur`, content: `Vous n'avez pas les permissions pour éditer ce fichier.
` }) } else { - View.createPopup({ - title: ` Erreur`, + editor.createPopup({ + title: ` Erreur`, content: `Une erreur est survenue.
` }) } diff --git a/public/javascripts/service.js b/public/javascripts/service.js new file mode 100644 index 0000000..5dbe1d8 --- /dev/null +++ b/public/javascripts/service.js @@ -0,0 +1,240 @@ +async function generateServiceView() { + + class Service { + name = null + description = null + icon = null + url = null + canAccess = false + isOnline = false + constructor(properties) { + this.name = properties.name + this.description = properties.description + this.icon = properties.icon + this.url = properties.url + this.canAccess = properties.canAccess + } + + generateHTML() { + return ` + + ` + + + } + + loadScript() { + + return new Promise((resolve, reject) => { + const statusSpan = getID(`${this.name}_status`) + const request = post(`SV_GET_SERVICE_STATUS`, this.url) + + + request.then((answer) => { + if(answer.name == this.url) { + if(answer.answer == "ONLINE") { + statusSpan.innerHTML = ' En ligne' + this.isOnline = true + } else { + statusSpan.innerHTML = ' Hors ligne' + + } + resolve("LOADED") + } + + }) + + const powerButton = getID(`${this.name}_svpower`) + + // Make a popup of View to select if you want to start, stop or restart the service by doing a request + + powerButton.addEventListener("click", () => { + View.createPopup({ + title: ` Gestion de l'alimentation du service`, + content: ` + +${this.name}
+ +