Version 1.1.0 - Revert to app
All checks were successful
Neutral/pipeline/head This commit looks good

This commit is contained in:
2024-12-29 15:38:16 +01:00
commit 1219e6fecc
58 changed files with 10152 additions and 0 deletions

113
bin/auth.js Normal file
View File

@ -0,0 +1,113 @@
const { LogType } = require("loguix")
const fs = require("fs")
const path = require("path")
const { __glob } = require("./global-variables")
const alog = new LogType("Authentification")
const keygen = require("./keygen")
const users = require("./users")
/**
* Vérifie si le token est présent et appartient à un utilisateur
* @param {string} token
*/
module.exports.check = function(token) {
var isApproved = false;
var username = null
users.fetchUsers().forEach((fetchUser) => {
if(fetchUser.tokens.includes(token)) {
isApproved = true
username = fetchUser.username
}
})
if(isApproved) {
return true
} else {
if(token) {
alog.warn("Erreur d'authentification - Token n'existe pas : " + token)
}
return false
}
}
/**
* Permet de se connecter à Neutral
* @param {object} data
* @returns Token or AUTH_FAILED
*/
module.exports.login = function(data) {
var username = data.username
var password = data.password
if(users.fetchUsers().has(username)) {
const user = users.fetchUsers().get(username)
if(password == user.getPassword()) {
const token = user.generateToken()
alog.log("Connexion approuvé de l'utilisateur : " + username)
return token
} else {
alog.warn("Echec de connexion de l'utilisateur : " + username + " - Mot de passe incorrect")
return "AUTH_FAILED"
}
} else {
alog.warn("Echec de connexion de l'utilisateur : " + username + " - Utilisateur non-inscrit dans la base de donnée")
return "AUTH_FAILED"
}
}
/**
* Remove the token
* @param {string} token
* @returns
*/
module.exports.signout = function(token) {
var isDone = false;
var username = null
users.fetchUsers().forEach((fetchUser) => {
if(fetchUser.tokens.includes(token)) {
isDone = true
username = fetchUser.username
fetchUser.removeToken(token)
}
})
if(isDone) {
alog.log("Suppression du Token '" + token + "' de l'utilisateur : " + username)
return true
} else {
if(token) {
alog.warn("Erreur d'opération lors de la déconnexion - Token n'existe pas : " + token)
}
return false
}
}
module.exports.getUserByToken = function(token) {
var isApproved = false;
var userGetted = null
users.fetchUsers().forEach((fetchUser) => {
if(fetchUser.tokens.includes(token)) {
userGetted = fetchUser
}
})
if(userGetted) {
return userGetted
} else {
if(token) {
alog.warn("Erreur d'authentification - Token n'existe pas : " + token)
}
return false
}
}

48
bin/config.js Normal file
View File

@ -0,0 +1,48 @@
const { LogType } = require("loguix")
const fs = require("fs")
const path = require("path")
const { __glob } = require("./global-variables")
const clog = new LogType("Configuration")
setup()
function setup() {
if(!fs.existsSync(__glob.CONFIG)) {
clog.log("Création du fichier de configuration dans : " + __glob.CONFIG)
fs.writeFileSync(__glob.CONFIG, JSON.stringify({
ENCRYPTION_KEY: "1",
}, null, 2))
}
}
/**
*
* @returns Config File
*/
module.exports.getFile = function () {
const file = JSON.parse(fs.readFileSync(__glob.CONFIG))
return file
}
/**
* Update le fichier configuration avec un object
* @param {Array} file
*/
module.exports.updateFile = function (file) {
if(fs.existsSync(__glob.CONFIG)) {
clog.log("Mise à jour du fichier configuration dans : " + __glob.CONFIG)
fs.writeFileSync(__glob.CONFIG, JSON.stringify(file, null, 2))
}
}
module.exports.getSettings = function () {
const file = this.getFile()
return {"jenkins_token": file.JENKINS_TOKEN, "omega_token": file.OMEGA_KEY}
}
module.exports.saveSettings = function (settings) {
const file = this.getFile()
file.JENKINS_TOKEN = settings.jenkins_token
file.OMEGA_KEY = settings.omega_token
this.updateFile(file)
}

268
bin/files.js Normal file
View File

@ -0,0 +1,268 @@
const { LogType } = require("loguix")
const fs = require("fs")
const path = require("path")
const { __glob } = require("./global-variables")
const clog = new LogType("Fichier")
const os = require("os");
const uuid = require('uuid');
var mime = require('mime-types');
// check if shared folder exist, if not create it
if(!fs.existsSync(__glob.SHARED)) {
fs.mkdirSync(__glob.SHARED)
}
module.exports.getFiles = function(root) {
var response = new Object()
response.content = new Array()
try{
if(root == "homepath") {
root = os.homedir()
}
if(root == "sharepath") {
root = __glob.SHARED
}
if(root == "logpath") {
root = __glob.LOGS
}
if(!fs.existsSync(root)) {
response.content = "NOT_EXIST"
} else {
for(var file of fs.readdirSync(root)) {
const stat = fs.statSync(root + path.sep + file)
response.content.push({"name":file, id: uuid.v4().toString() ,"fileDirectory" : root + path.sep + file , "type":mime.lookup(file), "size":stat.size, "lastedition":stat.mtimeMs, "directory":stat.isDirectory()})
}
}
}catch(err) {
response.content = "NOT_PERMITTED"
}
response.root = root
response.parent = path.dirname(root)
return response
}
module.exports.createFolder = function(root) {
// Check if folder already exist
if(!fs.existsSync(root)) {
try {
fs.mkdirSync(root)
return "OK"
} catch(err) {
return "NOT_PERMITTED"
}
} else {
return 'EXIST'
}
}
module.exports.deleteFile = function(root) {
if(fs.existsSync(root)) {
try {
fs.rmSync(root, { recursive: true, force: true })
return "OK"
} catch(err) {
console.log(err)
return "NOT_PERMITTED"
}
} else {
return "NOT_EXIST"
}
}
module.exports.renameFile = function(settings) {
if(fs.existsSync(settings.root)) {
try {
fs.renameSync(settings.root + path.sep + settings.oldName, settings.root + path.sep + settings.newName)
return "OK"
} catch(err) {
return "NOT_PERMITTED"
}
} else {
return "NOT_EXIST"
}
}
module.exports.shareFile = function(settings) {
if(fs.existsSync(settings.root)) {
try {
//Create a sybolic link to shared folder
fs.symlinkSync(settings.root + path.sep + settings.name, __glob.SHARED + path.sep + settings.name)
// return the link of the file shared
if(process.env.DEV) {
return "http://localhost:3001/shared/" + settings.name
} else {
return "https://neutral.raphix.fr/shared/" + settings.name
}
} catch(err) {
console.log(err)
return "NOT_PERMITTED"
}
} else {
return "NOT_EXIST"
}
}
module.exports.getFile = function(root) {
if(fs.existsSync(root)) {
try {
// Check if the file is an image and if it is return the base64
if(mime.lookup(root).includes("image")) {
return "data:" + mime.lookup(root) + ";base64," + fs.readFileSync(root, "base64")
} else {
return fs.readFileSync(root, "utf-8")
}
} catch(err) {
console.log(err)
return "NOT_PERMITTED"
}
} else {
return "NOT_EXIST"
}
}
module.exports.saveFile = function(settings) {
try {
fs.writeFileSync(settings.root + path.sep + settings.name, settings.content)
return "OK"
} catch(err) {
console.log(err)
return "NOT_PERMITTED"
}
}
module.exports.downloadFile = function(root) {
if(fs.existsSync(root)) {
try {
return fs.readFileSync(root)
} catch(err) {
console.log(err)
return "NOT_PERMITTED"
}
} else {
return "NOT_EXIST"
}
}
module.exports.uploadFile = function(settings) {
try {
fs.writeFileSync(settings.root + path.sep + settings.name, settings.file)
return "OK"
} catch(err) {
console.log(err)
return "NOT_PERMITTED"
}
}
module.exports.newFile = function(settings) {
if(!fs.existsSync(settings)) {
try {
fs.writeFileSync(settings, "")
return "OK"
} catch(err) {
console.log(err)
return "NOT_PERMITTED"
}
} else {
return "EXIST"
}
}
module.exports.pasteFile = function(settings) {
if(settings.action == "cut") {
try {
fs.renameSync(settings.file.fileDirectory, settings.newPath + path.sep + settings.name)
return "OK"
} catch(err) {
console.log(err)
return "NOT_PERMITTED"
}
} else if(settings.action == "copy") {
try {
fs.copyFileSync(settings.file.fileDirectory, settings.newPath + path.sep + settings.name)
return "OK"
} catch(err) {
console.log(err)
return "NOT_PERMITTED"
}
}
}

20
bin/global-variables.js Normal file
View File

@ -0,0 +1,20 @@
const path = require("path");
const root = path.resolve(__dirname, '../')
const __glob = {
ROUTES: root + path.sep + "routes" + path.sep,
ROOT: root,
LOGS: root + path.sep + "logs",
ICON: root + path.sep + "public" + path.sep + 'images' + path.sep + "FormatLogo_WHITE.ico",
DATA: root + path.sep + "data",
USERS: root + path.sep + "data" + path.sep + "users.json",
CONFIG: root + path.sep + "data" + path.sep + "config.json",
SHARED: root + path.sep + "data" + path.sep + "shared",
USERS_IMAGES: root + path.sep + "data" + path.sep + "user_images",
PACKAGE_JSON: root + path.sep + "package.json",
};
module.exports = { __glob };

29
bin/keygen.js Normal file
View File

@ -0,0 +1,29 @@
const { LogType } = require("loguix")
const fs = require("fs")
const path = require("path")
var CryptoJS = require("crypto-js")
const { __glob } = require("./global-variables")
const clog = new LogType("KeyGen")
const config = require("./config")
const keypass = config.getFile().ENCRYPTION_KEY
setup()
function setup() {
if(keypass) {
clog.log("Clé de chiffrement trouvé et importé")
} else {
clog.error("Clé de chiffrement inconnu : Passage en mode par défaut")
}
}
module.exports.encrypt = function (text) {
let encryptedText = CryptoJS.AES.encrypt(text, keypass).toString();
return encryptedText;
}
module.exports.decrypt = function(text) {
let decryptedText = CryptoJS.AES.decrypt(text, keypass).toString(CryptoJS.enc.Utf8);
return decryptedText;
}

106
bin/links.js Normal file
View File

@ -0,0 +1,106 @@
const { LogType } = require("loguix")
const fs = require("fs")
const path = require("path")
const { __glob } = require("./global-variables")
const ulog = new LogType("Links")
const uuid = require("uuid")
const config = require("./config")
const {ApplyLinks} = require("../routes/link")
if(!fs.existsSync(__glob.DATA + path.sep + "links.json")) {
fs.writeFileSync(__glob.DATA + path.sep + "links.json", JSON.stringify([], null, 2))
}
module.exports.getLinks = function() {
return JSON.parse(fs.readFileSync(__glob.DATA + path.sep + "links.json"))
}
const FirstLinkManager = new ApplyLinks(this.getLinks())
module.exports.addLink = function(settings) {
var canDo = true
const links = this.getLinks()
var id = makeid(8)
if(settings.abstractLink) {
settings.dest = id.toString()
}
// Check if a destination already exists between links and if it's the case, we return an error "ALREADY_EXiST"
links.forEach((link) => {
if(link.dest == settings.dest) {
canDo = false
}
})
const link = {
id: id,
title: settings.title,
url: settings.url,
dest: settings.dest,
}
if(canDo) {
links.push(link)
fs.writeFileSync(__glob.DATA + path.sep + "links.json", JSON.stringify(links, null, 2))
const LinkManager = new ApplyLinks(this.getLinks())
return "OK"
} else {
return "ALREADY_EXIST"
}
}
module.exports.removeLink = function(id) {
const links = this.getLinks()
const newLinks = []
links.forEach((link) => {
if(link.id != id) {
newLinks.push(link)
}
})
fs.writeFileSync(__glob.DATA + path.sep + "links.json", JSON.stringify(newLinks, null, 2))
const LinkManager = new ApplyLinks(this.getLinks())
return "OK"
}
module.exports.updateLink = function(id, settings) {
const links = this.getLinks()
const newLinks = []
links.forEach((link) => {
if(link.id == id) {
link.title = settings.title
link.url = settings.url
}
newLinks.push(link)
})
fs.writeFileSync(__glob.DATA + path.sep + "links.json", JSON.stringify(newLinks, null, 2))
const LinkManager = new ApplyLinks(this.getLinks())
return "OK"
}
function makeid(length) {
let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
let counter = 0;
while (counter < length) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
counter += 1;
}
return result;
}

128
bin/metrics.js Normal file
View File

@ -0,0 +1,128 @@
const { LogType } = require("loguix")
const fs = require("fs")
const path = require("path")
var CryptoJS = require("crypto-js")
const { __glob } = require("./global-variables")
const { captureRejectionSymbol } = require("events")
const clog = new LogType("Metrics")
if(!fs.existsSync(__glob.DATA + path.sep + "metrics.json")) {
fs.writeFileSync(__glob.DATA + path.sep + "metrics.json", JSON.stringify([], null, 2))
}
module.exports.getDataMetrics = function() {
return JSON.parse(fs.readFileSync(__glob.DATA + path.sep + "metrics.json"))
}
module.exports.getMetrics = function() {
const metrics = this.getDataMetrics()
var metricsToReturn = new Array()
return new Promise(async (resolve, reject) => {
// Count the number processed
var processed = 0
if(metrics.length == 0) {
resolve(metricsToReturn)
}
await metrics.forEach(async (metric) => {
// Try to connect to the metric server with the key in query params named "privatekey"
// If the connection is successful, we add the metric to the list of metrics to return
const url = `http://${metric.address}:${metric.port}/metrics?privatekey=${metric.key}`
const fet = await fetch(url, {
method: "GET",
headers: {
"Accept": "application/json",
},
credentials: "include"
}).then(res => res.json())
.then(res => {
if(res) {
metric.data = res
metricsToReturn.push(metric)
processed++
} else {
metric.data = "ERROR"
metricsToReturn.push(metric)
}
if(processed == metrics.length) {
resolve(metricsToReturn)
}
}).catch((err) => {
metric.data = "ERROR"
metricsToReturn.push(metric)
processed++
if(processed == metrics.length) {
resolve(metricsToReturn)
}
})
})
})
}
module.exports.addMetric = function(settings) {
const metrics = this.getDataMetrics()
const metric = {
id: makeid(8),
name: settings.name,
address: settings.address,
port: settings.port,
key: settings.key,
}
metrics.push(metric)
fs.writeFileSync(__glob.DATA + path.sep + "metrics.json", JSON.stringify(metrics, null, 2))
return "OK"
}
module.exports.deleteMetric = function(id) {
const metrics = this.getDataMetrics()
metrics.forEach((metric) => {
if(metric.id == id) {
metrics.splice(metrics.indexOf(metric), 1)
}
})
fs.writeFileSync(__glob.DATA + path.sep + "metrics.json", JSON.stringify(metrics, null, 2))
return "OK"
}
function makeid(length) {
var result = [];
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result.push(characters.charAt(Math.floor(Math.random() *
charactersLength)));
}
return result.join('');
}

138
bin/pipelines.js Normal file
View File

@ -0,0 +1,138 @@
const { LogType } = require("loguix")
const fs = require("fs")
const path = require("path")
const { __glob } = require("./global-variables")
const clog = new LogType("Pïpeline")
const config = require("./config")
const tokenkey = config.getFile().JENKINS_TOKEN
module.exports.getAllPipelines = function() {
return new Promise((resolve, reject) => {
fetch(`https://jenkins.raphix.fr/api/json`, {
method: "GET",
headers: {
"Accept": "application/json",
"Authorization": `Basic ${tokenkey}`
},
credentials: "include"
})
.then(res => res.json())
.then(async list => {
const pipelinesJobs = new Array()
for(const job of list.jobs) {
await fetch(`${job.url}/api/json`, {
method: "GET",
headers: {
"Accept": "application/json",
"Authorization": `Basic ${tokenkey}`
},
credentials: "include"
})
.then(res => res.json())
.then(async res => {
if(res._class == "org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject") {
await getJobMain(res).then(resJ => {
res.jobs[0] = resJ
pipelinesJobs.push(res)
})
} else {
pipelinesJobs.push(res)
}
})
}
list.jobs = pipelinesJobs
resolve(list)
})
.catch(err => {
resolve("UNAVAILABLE")
})
})
}
module.exports.startPipeline = function(pipeline) {
// If it's a freestyle job, build with params
if(pipeline.type == "org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject") {
return new Promise((resolve, reject) => {
fetch(`${pipeline.url}/job/${pipeline.jobname}/build?delay=0sec`, {
method: "POST",
headers: {
"Authorization": `Basic ${tokenkey}`
},
credentials: "include"
})
.then(res => {
resolve("OK")
})
})
} else {
return new Promise((resolve, reject) => {
const parameters = pipeline.fields
const formatedParams = new URLSearchParams()
for(const param of parameters) {
formatedParams.append(param.name, param.value)
}
fetch(`${pipeline.url}/buildWithParameters?delay=0sec`, {
method: "POST",
headers: {
"Authorization": `Basic ${tokenkey}`,
"Content-Type": "application/x-www-form-urlencoded"
},
credentials: "include",
body: formatedParams
})
.then(res => {
resolve("OK")
})
})
}
}
function getJobMain(res) {
return new Promise(async (resolve, reject) => {
await fetch(`${res.jobs[0].url}/api/json`, {
method: "GET",
headers: {
'Accept': 'application/json',
'Authorization': `Basic ${tokenkey}`
},
credentials: 'include'
}).then(res => res.json())
.then(res => {
resolve(res)
})
})
}

74
bin/server-metrics.js Normal file
View File

@ -0,0 +1,74 @@
const { LogType } = require("loguix")
const fs = require("fs")
const path = require("path")
const { __glob } = require("./global-variables")
const clog = new LogType("Serveur Metrics")
const osutils = require("os-utils")
const os = require("os")
const { statfs } = require("fs")
const config = require("./config")
module.exports.getMetrics = async function(server) {
return new Promise((resolve, reject) => {
if(server == "Alpha") {
var resp = "NULL"
const space = statfs("/", (err, stats) => {
if (err) {
throw err
}
osutils.cpuUsage(function(cpuUsage) {
resp = {
cpu: Math.round(cpuUsage * 1000),
usedram: osutils.totalmem() - osutils.freemem(),
totalram: osutils.totalmem(),
usedisk: stats.blocks - stats.bfree,
totaldisk: stats.blocks,
name: server
}
resolve(resp)
})
})
} else if(server == "Omega") {
const key = config.getFile().OMEGA_KEY;
fetch("http://omega.raphix.fr:4000/metrics?key="+key, {
method: "GET",
headers: {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7",
"Cache-Control": "no-cache",
"Connection": "keep-alive"
}
})
.then(response => response.json())
.then(raw => {
raw.name = server;
resolve(raw);
})
.catch(error => {
reject(error);
});
}
})
}

296
bin/servers.js Normal file
View File

@ -0,0 +1,296 @@
const { LogType } = require("loguix")
const fs = require("fs")
const path = require("path")
const { __glob } = require("./global-variables.js")
const auth = require("./auth.js")
const users = require("./users.js")
const files = require("./files.js")
const config = require("./config.js")
const links = require("./links.js")
const service = require("./services.js")
const pipeline = require("./pipelines.js")
const plog = new LogType("Web")
const cook = require("cookie")
const http = require("http")
const servermetrics = require("./server-metrics.js")
const metrics = require("./metrics.js")
const pm2 = require('pm2');
/**
* NOTE INTERNE
*
* Changer les pictures de users autre part
*/
/**
*
* @param {http.Server} server
*/
module.exports.serverIO = function(server) {
const io = require('socket.io')(server, {
maxHttpBufferSize: 1e8,
pingTimeout: 60000
})
io.on("connection", (socket) => {
let token = cook.parse(socket.handshake.headers.cookie).token
var user = auth.getUserByToken(token)
if(user) {
plog.log("Connexion au panel par '" + user.username + "' avec le socket : " + socket.id)
user.setLastLogin(new Date())
/**
* GET REQUEST
*/
// Get Users
GetRequest("USERINFO", () => {
user = auth.getUserByToken(token)
GetAnswer("USERINFO", {username: user.username, display_name: user.display_name ,picture: user.picture, permission: user.permission})
})
/**
* POST REQUEST
*/
PostRequest("US_EDIT_PERSONNAL", async (settings) => {
user = auth.getUserByToken(token)
PostAnswer("US_EDIT_PERSONNAL", await users.editMySelf(settings, user))
})
if(user.checkPermission("FILES_EXPLORER")) {
PostRequest("FX_GET", (root) => {
PostAnswer("FX_GET", files.getFiles(root))
})
PostRequest("FX_NEW_FOLDER", (root) => {
PostAnswer("FX_NEW_FOLDER", files.createFolder(root))
})
PostRequest("FX_DELETE", (root) => {
PostAnswer("FX_DELETE", files.deleteFile(root))
} )
PostRequest("FX_RENAME", (settings) => {
PostAnswer("FX_RENAME", files.renameFile(settings))
})
PostRequest("FX_SHARE", (settings) => {
PostAnswer("FX_SHARE", files.shareFile(settings))
})
PostRequest("FX_GETFILE", (root) => {
PostAnswer("FX_GETFILE", files.getFile(root))
})
PostRequest("FX_SAVEFILE", (settings) => {
PostAnswer("FX_SAVEFILE", files.saveFile(settings))
})
PostRequest("FX_NEW_FILE", (settings) => {
PostAnswer("FX_NEW_FILE", files.newFile(settings))
})
PostRequest("FX_UPLOAD", (settings) => {
PostAnswer("FX_UPLOAD", files.uploadFile(settings))
})
PostRequest("FX_PASTE", (settings) => {
PostAnswer("FX_PASTE", files.pasteFile(settings))
})
}
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})
})
}
if(user.checkPermission("LINKS")) {
PostRequest("LINKS_GET_ALL", () => {
PostAnswer("LINKS_GET_ALL", {answer: "OK", links: links.getLinks()})
})
PostRequest("LINKS_ADD", (settings) => {
PostAnswer("LINKS_ADD", {answer: links.addLink(settings)})
})
PostRequest("LINKS_DELETE", (id) => {
PostAnswer("LINKS_DELETE", links.removeLink(id))
})
PostRequest("LINKS_EDIT", (settings) => {
PostAnswer("LINKS_EDIT", links.updateLink(settings.id, settings))
})
}
if(user.checkPermission("SERVERS")) {
PostRequest("SERVER_GET_METRICS_Alpha", async (settings) => {
PostAnswer("SERVER_GET_METRICS_Alpha", {answer: "OK", metrics: await servermetrics.getMetrics(settings), name: settings});
});
PostRequest("SERVER_GET_METRICS_Omega", async (settings) => {
PostAnswer("SERVER_GET_METRICS_Omega", {answer: "OK", metrics: await servermetrics.getMetrics(settings), name: settings});
});
}
if(user.checkPermission("PIPELINES")) {
GetRequest("PL_GET_ALL", async () => {
GetAnswer("PL_GET_ALL", await pipeline.getAllPipelines())
})
PostRequest("PL_START", async (settings) => {
PostAnswer("PL_START", await pipeline.startPipeline(settings))
})
}
if(user.checkPermission("USERS")) {
GetRequest("US_ALL", async () => {
GetAnswer("US_ALL", await users.getAllUsers())
})
PostRequest("US_ADD", async (settings) => {
PostAnswer("US_ADD", await users.addUser(settings))
})
PostRequest("US_DELETE", async (settings) => {
PostAnswer("US_DELETE", await users.deleteUser(settings))
})
PostRequest("US_EDIT", async (settings) => {
PostAnswer("US_EDIT", await users.editUser(settings))
})
PostRequest("US_CLEAR_TOKENS", async (settings) => {
PostAnswer("US_CLEAR_TOKENS", await users.clearTokens(settings))
})
}
if(user.checkPermission("SETTINGS")) {
PostRequest("SETTINGS_SAVE", async (settings) => {
PostAnswer("SETTINGS_SAVE", await config.saveSettings(settings))
})
PostRequest("SERVER_RESTART", async () => {
pm2.restart('Neutral')
})
PostRequest("SERVER_STOP", async () => {
pm2.stop('Neutral')
})
GetRequest("SERVER_GET_LOGS", async () => {
GetAnswer("SERVER_GET_LOGS", await fs.readdirSync(__glob.LOGS))
})
PostRequest("SERVER_READ_LOG", async (logs) => {
PostAnswer("SERVER_READ_LOG", await fs.readFileSync(__glob.LOGS + path.sep + logs).toString())
})
GetRequest("SETTINGS_GET", async () => {
GetAnswer("SETTINGS_GET", await config.getSettings())
})
}
if(user.checkPermission("METRICS")) {
GetRequest("MT_ALL", async () => {
GetAnswer("MT_ALL", await metrics.getMetrics())
})
PostRequest("MT_ADD", async (settings) => {
PostAnswer("MT_ADD", await metrics.addMetric(settings))
})
PostRequest("MT_DELETE", async (settings) => {
PostAnswer("MT_DELETE", await metrics.deleteMetric(settings))
})
}
socket.on("disconnect", (reason) => {
plog.log("Déconnexion du panel par '" + user.username + "' avec le socket : " + socket.id)
})
socket.on("connect_error", (err) => {
console.log(err)
console.log(err.message); // prints the message associated with the error
});
function GetRequest(GQname, GQcallback) {
socket.on("GET/" + GQname, () => {
plog.log(user.username + " - Socket : " + socket.id + " - GET/" + GQname + " - [RECIEVED]")
GQcallback()
})
}
function GetAnswer(GRname, GRvalue) {
plog.log(user.username + " - Socket : " + socket.id + " - GET/" + GRname + " - [ANSWERED]")
socket.emit("ANSWER/GET/" + GRname, GRvalue)
}
function PostRequest(GQname, GQcallback) {
socket.on("POST/" + GQname, (value) => {
plog.log(user.username + " - Socket : " + socket.id + " - POST/" + GQname + " - [RECIEVED]")
GQcallback(value)
})
}
function PostAnswer(GRname, GRvalue) {
plog.log(user.username + " - Socket : " + socket.id + " - POST/" + GRname + " - [ANSWERED]")
socket.emit("ANSWER/POST/" + GRname, GRvalue)
}
} else {
socket.disconnect()
plog.warn("Connexion directe vers le panel avec un token inexistant : " + this.token)
}
})
}

137
bin/services.js Normal file
View File

@ -0,0 +1,137 @@
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 -o StrictHostKeyChecking=no 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 -o StrictHostKeyChecking=no raphix@omega.raphix.fr sudo -S systemctl stop gitea`)
resolve("OK")
} else if(service == `https://jenkins.raphix.fr`) {
let req = child_process.exec(`ssh -o StrictHostKeyChecking=no raphix@omega.raphix.fr sudo -S systemctl stop jenkins `)
resolve("OK")
} else if(service == `https://raphix.fr`) {
let req = child_process.exec(`ssh -o StrictHostKeyChecking=no 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 -o StrictHostKeyChecking=no 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 -o StrictHostKeyChecking=no 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 -o StrictHostKeyChecking=no 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 -o StrictHostKeyChecking=no raphix@omega.raphix.fr sudo -S systemctl start gitea`)
resolve("OK")
} else if(service == `https://jenkins.raphix.fr`) {
let req = child_process.exec(`ssh -o StrictHostKeyChecking=no raphix@omega.raphix.fr sudo -S systemctl start jenkins `)
resolve("OK")
} else if(service == `https://raphix.fr`) {
let req = child_process.exec(`ssh -o StrictHostKeyChecking=no 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 -o StrictHostKeyChecking=no 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 -o StrictHostKeyChecking=no 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 -o StrictHostKeyChecking=no 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 -o StrictHostKeyChecking=no raphix@omega.raphix.fr sudo -S systemctl restart gitea`)
resolve("OK")
} else if(service == `https://jenkins.raphix.fr`) {
let req = child_process.exec(`ssh -o StrictHostKeyChecking=no raphix@omega.raphix.fr sudo -S systemctl restart jenkins `)
resolve("OK")
} else if(service == `https://raphix.fr`) {
let req = child_process.exec(`ssh -o StrictHostKeyChecking=no 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 -o StrictHostKeyChecking=no 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 -o StrictHostKeyChecking=no raphix@omega.raphix.fr sudo -S systemctl restart lavalink `)
resolve("OK")
} else {
resolve(false)
}
});
}

544
bin/users.js Normal file
View File

@ -0,0 +1,544 @@
const { LogType } = require("loguix")
const fs = require("fs")
const path = require("path")
const { __glob } = require("./global-variables")
const ulog = new LogType("Users")
const keygen = require("./keygen")
const uuid = require("uuid")
var usersList = new Map()
setup()
function setup() {
if(!fs.existsSync(__glob.USERS)) {
ulog.log("Création du fichier utilisateur dans : " + __glob.USERS)
fs.writeFileSync(__glob.USERS, JSON.stringify([], null, 2))
}
}
module.exports.getAllUsers = async function() {
return new Promise(async (resolve, reject) => {
const users = await this.fetchUsers()
// Remove for every people the password & the tokens
for(var user of users) {
user = user[1]
user.password = null
user.tokens = null
}
resolve(JSON.stringify(Array.from(users)))
})
}
/**
* Get all users from Users Data Base
*/
module.exports.fetchUsers = function () {
ulog.step.init("fetch_user", "Récupération de tous les utilisateurs inscrit dans la base de donnée")
const userFile = getFile()
usersList = new Map()
for(var userFetched of userFile) {
const user = new this.User({
username: userFetched.username,
password: userFetched.password,
display_name: userFetched.display_name,
permission: userFetched.permission,
tokens: userFetched.tokens,
lastLogin: userFetched.lastLogin,
picture: userFetched.picture
})
usersList.set(user.username, user)
}
if(usersList.size == 0) {
const adminUser = new this.User({
"username": "admin",
"password": "neutral",
"display_name": "Administrateur",
"permission": [
"FILES_EXPLORER",
"SERVICES",
"SERVERS",
"PIPELINES",
"METRICS",
"USERS",
"LINKS",
"SETTINGS"
],
"tokens": [],
"lastLogin": "DEFAULT ACCOUNT",
"picture": "/images/default.jpg"
})
adminUser.register()
}
ulog.step.end("fetch_user")
return usersList
}
/**
* User Class is used to access to default user's properties and methods
* @param {object} properties User properties with : username, password, display_name, permission...
*/
module.exports.User = class {
username = null
password = null;
display_name = null
permission = []
tokens = []
lastLogin = new Date()
picture = "/images/default.jpg"
constructor(properties) {
if(properties) {
this.username = properties.username
this.password = keygen.encrypt(properties.password)
this.display_name = properties.display_name
this.permission = properties.permission
this.tokens = properties.tokens
this.lastLogin = properties.lastLogin
this.picture = properties.picture
const userFile = getFile()
for(var userFetched of userFile) {
if(properties.username == userFetched.username) {
ulog.log("Récupération dans la base de donnée, de l'utilisateur : " + userFetched.username)
this.username = userFetched.username
this.password = userFetched.password
this.display_name = userFetched.display_name
this.permission = userFetched.permission
this.tokens = userFetched.tokens
this.lastLogin = userFetched.lastLogin
}
}
}
if(this.username == null) {
ulog.error("One of user is without username ! [IMPORANT_FIELD_IS_MISSING]")
this.username = Math.random()
}
if(this.password == null) {
ulog.error("'" + this.username + "' is without password ! Password reset to 'default' [IMPORANT_FIELD_IS_MISSING]")
this.password = keygen.encrypt("default")
}
if(this.display_name == null) {
ulog.warn("'" + this.username + "' is without display name !")
this.display_name = this.username
}
if(this.permission == null) {
ulog.warn("'" + this.username + "' has no longer permission !")
}
if(this.tokens == null) {
this.tokens = []
}
if(this.permission == null) {
this.permission = []
}
if(this.lastLogin == null) {
this.lastLogin = new Date()
}
if(this.picture == null) {
this.picture = "/images/default.jpg"
}
}
register() {
var alreadyExist = false
const userFile = getFile()
for(var userFetched of userFile) {
if(userFetched.username == this.username) {
userFile.splice(userFile.indexOf(userFetched), 1)
ulog.log("Mise à jour dans la base de donnée, de l'utilisateur : " + this.username)
alreadyExist = true
}
}
if(!alreadyExist) {
ulog.log("Création dans la base de donnée de l'utilisateur : " + this.username)
}
userFile.push(this)
updateFile(userFile)
usersList.set(this.username, this)
}
unregister() {
var alreadyExist = false
const userFile = getFile()
for(var userFetched of userFile) {
if(userFetched.username == this.username) {
userFile.splice(userFile.indexOf(userFetched), 1)
ulog.log("Mise à jour dans la base de donnée, de l'utilisateur : " + this.username)
alreadyExist = true
}
}
if(!alreadyExist) {
ulog.log("L'utilisateur n'est pas enregistré dans la base de donnée : " + this.username)
}
updateFile(userFile)
usersList.delete(this.username)
}
checkPermission(name) {
this.#sync()
if(this.permission.includes(name)) {
return true
} else {
return false
}
}
addPermission(name) {
this.#sync()
for(var perms of this.permission) {
if(name == perms) {
ulog.warn("'" + this.username + "' a déjà la permission : " + name)
return false
}
}
this.permission.push(name)
this.register()
}
removePermission(name) {
this.#sync()
var havePermission = false
for(var perms of this.permission) {
if(name == perms) {
havePermission = true
}
}
if(havePermission) {
this.permission.splice(this.permission.indexOf(name), 1)
this.register()
} else {
ulog.warn("'" + this.username + "' n'a pas la permission : " + name)
return false
}
}
setPassword(newPassword) {
this.#sync()
this.password = keygen.encrypt(newPassword)
this.register()
ulog.log("Le mot de passe de l'utilisateur a été modifié : " + this.username)
}
getPassword() {
this.#sync()
return keygen.decrypt(this.password)
}
generateToken() {
this.#sync()
const gToken = uuid.v4().toString()
this.tokens.push(gToken)
this.register()
return gToken
}
removeToken(token) {
this.#sync()
var haveToken = false
for(var aToken of this.tokens) {
if(token == aToken) {
haveToken = true
}
}
if(haveToken) {
this.tokens.splice(this.tokens.indexOf(token), 1)
this.register()
} else {
ulog.warn("'" + this.username + "' n'a pas le token : " + token)
return false
}
}
setPicture(text) {
this.#sync()
this.picture = text
this.register()
ulog.log("La photo de l'utilisateur a été modifié : " + this.username)
}
setDisplayName(text) {
this.#sync()
this.display_name = text
this.register()
ulog.log("Le nom d'affichage de l'utilisateur a été modifié : " + this.username)
}
setNewUsername(text) {
this.#sync()
module.exports.deleteUser(this.username)
this.username = text
this.register()
ulog.log("Le nom d'utilisateur de l'utilisateur a été modifié : " + this.username)
}
setLastLogin(text) {
this.#sync()
this.lastLogin = text
this.register()
}
setPicture(file) {
this.#sync()
var pictureDir = __glob.USERS_IMAGES + path.sep + uuid.v4().toString() + ".png"
if(!fs.existsSync(__glob.USERS_IMAGES)) {
fs.mkdirSync(__glob.USERS_IMAGES)
}
fs.writeFileSync(pictureDir, file)
this.picture = pictureDir.replace(__glob.USERS_IMAGES + path.sep, "/users/")
this.register()
}
setPermissions(permissions) {
this.#sync()
this.permission = permissions
this.register()
}
clearTokens() {
this.#sync()
this.tokens = []
this.register()
}
#sync() {
for(var userGet of usersList.keys()) {
const userFetched = usersList.get(userGet)
if(this.username == userFetched.username) {
this.username = userFetched.username
this.password = userFetched.password
this.display_name = userFetched.display_name
this.permission = userFetched.permission
this.tokens = userFetched.tokens
this.lastLogin = userFetched.lastLogin
}
}
}
}
module.exports.addUser = function(settings) {
if(settings.username == '') {
ulog.error("Le nom d'utilisateur est manquant")
return "USERNAME_MISSING"
} else if(settings.password == '') {
ulog.error("Le mot de passe est manquant")
return "PASSWORD_MISSING"
} else if(settings.display_name == '') {
ulog.error("Le nom d'affichage est manquant")
return "DISPLAY_NAME_MISSING"
} else if(this.getUser(settings.username)) {
ulog.error("L'utilisateur existe déjà : " + settings.username)
return "ALREADY_EXIST"
} else {
ulog.step.init("add_user", "Ajout d'un utilisateur dans la base de donnée : " + settings.username)
var pictureDir = null
if(settings.picture == null) {
pictureDir = "/images/default.jpg"
} else {
pictureDir = __glob.USERS_IMAGES + path.sep + uuid.v4().toString() + ".png"
fs.writeFileSync(pictureDir, settings.picture)
}
const user = new this.User({
username: settings.username,
display_name: settings.display_name,
permission: settings.permissions,
picture: pictureDir.replace(__glob.USERS_IMAGES + path.sep, "/images/users/")
})
user.setPassword(settings.password)
user.register()
ulog.step.end("add_user")
}
}
module.exports.deleteUser = function(username) {
ulog.step.init("delete_user", "Suppression d'un utilisateur dans la base de donnée : " + username)
const user = this.getUser(username)
user.unregister()
ulog.step.end("delete_user")
return "OK"
}
module.exports.getUser = function(username) {
return usersList.get(username)
}
module.exports.editUser = function(settings) {
if(settings.username == '') {
ulog.error("Le nom d'utilisateur est manquant")
return "USERNAME_MISSING"
} else if(settings.display_name == '') {
ulog.error("Le nom d'affichage est manquant")
return "DISPLAY_NAME_MISSING"
} else {
ulog.step.init("edit_user", "Modification d'un utilisateur dans la base de donnée : " + settings.username)
const user = this.fetchUsers().get(settings.username)
if(user) {
if(settings.newusername && settings.newusername != settings.username) {
if(this.getUser(settings.newusername)) {
ulog.error("L'utilisateur existe déjà : " + settings.username)
return "ALREADY_EXIST"
} else {
user.setNewUsername(settings.newusername)
}
}
if(settings.display_name) {
user.setDisplayName(settings.display_name)
}
if(settings.password) {
user.setPassword(settings.password)
}
if(settings.picture) {
user.setPicture(settings.picture)
}
if(settings.permissions) {
user.setPermissions(settings.permissions)
}
ulog.step.end("edit_user")
return "OK"
} else {
ulog.step.end("edit_user")
return "NOT_EXIST"
}
}
}
module.exports.editMySelf = function (settings, user) {
if(user.username == settings.username) {
if(settings.username == '') {
ulog.error("Le nom d'utilisateur est manquant")
return "USERNAME_MISSING"
} else if(settings.display_name == '') {
ulog.error("Le nom d'affichage est manquant")
return "DISPLAY_NAME_MISSING"
} else {
ulog.step.init("edit_user", "Modification d'un utilisateur dans la base de donnée : " + settings.username)
const user = this.fetchUsers().get(settings.username)
if(user) {
console.log(settings)
if(settings.newusername && settings.newusername != settings.username) {
if(this.getUser(settings.newusername)) {
ulog.error("L'utilisateur existe déjà : " + settings.username)
return "ALREADY_EXIST"
} else {
user.setNewUsername(settings.newusername)
}
}
if(settings.display_name) {
user.setDisplayName(settings.display_name)
}
if(settings.password) {
user.setPassword(settings.password)
}
if(settings.picture) {
user.setPicture(settings.picture)
}
ulog.step.end("edit_user")
return "OK"
} else {
ulog.step.end("edit_user")
return "NOT_EXIST"
}
}
} else {
ulog.error("Vous ne pouvez pas modifier les informations d'un autre utilisateur !")
return "NOT_ALLOWED"
}
}
module.exports.clearTokens = function(username) {
const user = this.fetchUsers().get(username)
user.clearTokens()
}
/**
*
* @returns User File
*/
function getFile() {
const file = JSON.parse(fs.readFileSync(__glob.USERS))
return file
}
/**
* Update le fichier utilisateur avec un object
* @param {Array} file
*/
function updateFile(file) {
if(fs.existsSync(__glob.USERS)) {
ulog.log("Mise à jour du fichier utilisateur dans : " + __glob.USERS)
fs.writeFileSync(__glob.USERS, JSON.stringify(file, null, 2))
}
}

107
bin/www Normal file
View File

@ -0,0 +1,107 @@
#!/usr/bin/env node
/**
* Module dependencies.
*/
var log = require("loguix")
var {LogType} = require("loguix")
var { __glob } = require("./global-variables")
log.setup(__glob.LOGS, __glob.PACKAGE_JSON)
const wlog = new LogType("Serveur")
if(process.env.DEV ) {
wlog.log("MODE DEVELOPEMENT ACTIF")
}
wlog.step.init("start_server", "Démarrage du serveur Express JS")
var app = require('../main');
var http = require('http');
var config = require("./config")
var serverIO = require("./servers")
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3001');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
serverIO.serverIO(server)
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
wlog.step.error("start_server", bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
wlog.step.error("start_server" , bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
wlog.log("Serveur entrain d'écouter sur le port : " + server.address().port)
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
wlog.step.end("start_server")
}