2023-11-02 17:16:15 +00:00
|
|
|
const { LogType } = require("loguix")
|
|
|
|
const fs = require("fs")
|
|
|
|
const path = require("path")
|
2023-11-28 20:05:44 +00:00
|
|
|
const { __glob } = require("./global-variables.js")
|
|
|
|
const auth = require("./auth.js")
|
|
|
|
const files = require("./files.js")
|
2023-11-05 11:12:18 +00:00
|
|
|
const links = require("./links.js")
|
2023-11-28 20:05:44 +00:00
|
|
|
const service = require("./services.js")
|
2023-12-06 21:56:58 +00:00
|
|
|
const pipeline = require("./pipelines.js")
|
2023-11-02 17:16:15 +00:00
|
|
|
const plog = new LogType("Web")
|
|
|
|
const cook = require("cookie")
|
|
|
|
const http = require("http")
|
2023-11-28 20:05:44 +00:00
|
|
|
const servermetrics = require("./server-metrics.js")
|
2023-11-02 17:16:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* NOTE INTERNE
|
|
|
|
*
|
|
|
|
* Changer les pictures de users autre part
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {http.Server} server
|
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports.serverIO = function(server) {
|
|
|
|
|
|
|
|
const io = require('socket.io')(server)
|
|
|
|
|
|
|
|
io.on("connection", (socket) => {
|
|
|
|
let token = cook.parse(socket.handshake.headers.cookie).token
|
|
|
|
const user = auth.getUserByToken(token)
|
|
|
|
|
|
|
|
if(user) {
|
|
|
|
plog.log("Connexion au panel par '" + user.username + "' avec le socket : " + socket.id)
|
|
|
|
user.setLastLogin(new Date())
|
2023-11-03 21:21:21 +00:00
|
|
|
/**
|
|
|
|
* GET REQUEST
|
|
|
|
*/
|
2023-11-02 17:16:15 +00:00
|
|
|
|
|
|
|
// Get Users
|
|
|
|
GetRequest("USERINFO", () => {
|
|
|
|
|
|
|
|
GetAnswer("USERINFO", {username: user.username, display_name: user.display_name ,picture: user.picture, permission: user.permission})
|
|
|
|
})
|
|
|
|
|
2023-11-03 21:21:21 +00:00
|
|
|
/**
|
|
|
|
* POST REQUEST
|
|
|
|
*/
|
|
|
|
|
2023-11-04 13:50:28 +00:00
|
|
|
if(user.checkPermission("FILES_EXPLORER")) {
|
|
|
|
|
2023-12-06 21:56:58 +00:00
|
|
|
PostRequest("FX_GET", (root) => {
|
2023-11-03 13:25:39 +00:00
|
|
|
|
2023-12-06 21:56:58 +00:00
|
|
|
PostAnswer("FX_GET", files.getFiles(root))
|
|
|
|
})
|
2023-11-03 21:21:21 +00:00
|
|
|
|
2023-12-06 21:56:58 +00:00
|
|
|
PostRequest("FX_NEW_FOLDER", (root) => {
|
2023-11-03 21:21:21 +00:00
|
|
|
|
2023-12-06 21:56:58 +00:00
|
|
|
PostAnswer("FX_NEW_FOLDER", files.createFolder(root))
|
|
|
|
})
|
2023-11-03 21:21:21 +00:00
|
|
|
|
2023-12-06 21:56:58 +00:00
|
|
|
PostRequest("FX_DELETE", (root) => {
|
2023-11-03 21:21:21 +00:00
|
|
|
|
2023-12-06 21:56:58 +00:00
|
|
|
PostAnswer("FX_DELETE", files.deleteFile(root))
|
|
|
|
} )
|
2023-11-03 21:21:21 +00:00
|
|
|
|
2023-12-06 21:56:58 +00:00
|
|
|
PostRequest("FX_RENAME", (settings) => {
|
2023-11-03 21:21:21 +00:00
|
|
|
|
2023-12-06 21:56:58 +00:00
|
|
|
PostAnswer("FX_RENAME", files.renameFile(settings))
|
|
|
|
})
|
2023-11-03 21:21:21 +00:00
|
|
|
|
2023-12-06 21:56:58 +00:00
|
|
|
PostRequest("FX_SHARE", (settings) => {
|
|
|
|
|
|
|
|
PostAnswer("FX_SHARE", files.shareFile(settings))
|
|
|
|
})
|
2023-11-03 21:21:21 +00:00
|
|
|
|
2023-12-06 21:56:58 +00:00
|
|
|
PostRequest("FX_GETFILE", (root) => {
|
|
|
|
|
|
|
|
PostAnswer("FX_GETFILE", files.getFile(root))
|
|
|
|
})
|
2023-11-03 21:21:21 +00:00
|
|
|
|
2023-12-06 21:56:58 +00:00
|
|
|
PostRequest("FX_SAVEFILE", (settings) => {
|
2023-11-03 21:21:21 +00:00
|
|
|
|
2023-12-06 21:56:58 +00:00
|
|
|
PostAnswer("FX_SAVEFILE", files.saveFile(settings))
|
|
|
|
})
|
2023-11-03 21:21:21 +00:00
|
|
|
|
2023-12-06 21:56:58 +00:00
|
|
|
PostRequest("FX_NEW_FILE", (settings) => {
|
|
|
|
|
|
|
|
PostAnswer("FX_NEW_FILE", files.newFile(settings))
|
|
|
|
})
|
|
|
|
|
|
|
|
PostRequest("FX_UPLOAD", (settings) => {
|
2023-11-03 21:21:21 +00:00
|
|
|
|
2023-12-06 21:56:58 +00:00
|
|
|
PostAnswer("FX_UPLOAD", files.uploadFile(settings))
|
2023-11-03 21:21:21 +00:00
|
|
|
|
2023-12-06 21:56:58 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
PostRequest("FX_PASTE", (settings) => {
|
|
|
|
|
|
|
|
PostAnswer("FX_PASTE", files.pasteFile(settings))
|
|
|
|
})
|
2023-11-28 20:05:44 +00:00
|
|
|
|
2023-11-04 13:50:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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})
|
|
|
|
})
|
|
|
|
|
2023-11-05 11:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
})
|
2023-11-04 13:50:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-11-28 20:05:44 +00:00
|
|
|
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});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-12-06 21:56:58 +00:00
|
|
|
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))
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-11-02 17:16:15 +00:00
|
|
|
socket.on("disconnect", () => {
|
|
|
|
|
2023-11-04 14:19:37 +00:00
|
|
|
plog.log("Déconnexion du panel par '" + user.username + "' avec le socket : " + socket.id)
|
2023-11-02 17:16:15 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
function GetRequest(GQname, GQcallback) {
|
|
|
|
socket.on("GET/" + GQname, () => {
|
2023-11-03 13:25:39 +00:00
|
|
|
|
2023-11-02 17:16:15 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-11-03 13:25:39 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-11-02 17:16:15 +00:00
|
|
|
} else {
|
|
|
|
|
|
|
|
socket.disconnect()
|
|
|
|
plog.warn("Connexion directe vers le panel avec un token inexistant : " + this.token)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
}
|