neutral/bin/server.js

143 lines
3.9 KiB
JavaScript
Raw Normal View History

const { LogType } = require("loguix")
const fs = require("fs")
const path = require("path")
const { __glob } = require("./global-variables")
const auth = require("./auth")
const files = require("./files")
const plog = new LogType("Web")
const cook = require("cookie")
const http = require("http")
/**
* 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())
/**
* GET REQUEST
*/
// Get Users
GetRequest("USERINFO", () => {
GetAnswer("USERINFO", {username: user.username, display_name: user.display_name ,picture: user.picture, permission: user.permission})
})
/**
* POST REQUEST
*/
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))
})
socket.on("disconnect", () => {
plog.log("Déconnexion au panel par '" + user.username + "' avec le socket : " + socket.id)
})
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)
}
})
}