This commit is contained in:
CICD - Pipeline 2023-02-23 14:47:21 +01:00
parent 3e2ff38897
commit ae5bb81637
13 changed files with 857 additions and 24 deletions

6
app.js
View File

@ -8,7 +8,7 @@ var fs = require("fs")
var uuid = require('uuid');
var CryptoJS = require("crypto-js")
var fileUpload = require('express-fileupload')
var ntr = require("./neutral-functions.js")
var indexRouter = require('./routes/index');
@ -18,6 +18,7 @@ var getRouter = require('./routes/get');
var setRouter = require('./routes/set');
var uploadRouter = require('./routes/upload');
var userManagerRouter = require('./routes/usermanager');
var linkRouter = require('./routes/link');
@ -44,6 +45,7 @@ app.use('/get', getRouter);
app.use('/set', setRouter)
app.use('/upload', uploadRouter)
app.use('/usermanager', userManagerRouter)
app.use('/link', linkRouter)
// catch 404 and forward to error handler
@ -63,7 +65,7 @@ app.use(function(err, req, res, next) {
});
ntr.checkUser()
module.exports = app;

10
links.json Normal file
View File

@ -0,0 +1,10 @@
{
"Git GeekSchool": {
"src": "https://git.geekschool.fr",
"dest": "gitschool"
},
"Gmail": {
"src": "https://mail.google.com/mail/",
"dest": "gmail"
}
}

View File

@ -3,7 +3,20 @@ var uuid = require('uuid')
var path = require("path")
var CryptoJS = require("crypto-js")
var Jimp = require("jimp")
const { compileString } = require("sass")
module.exports.checkUser = () => {
const userDir = fs.readdirSync(__dirname + path.sep + "users" + path.sep)
if(userDir.length == 0) {
this.createUser("root","neutral",4,"Administrateur")
}
}
module.exports.createUser = (name, password, level, fullname) => {
@ -46,6 +59,10 @@ module.exports.createUser = (name, password, level, fullname) => {
}
module.exports.deleteUser = (name) => {
const userUUID = this.getUUID(name)

526
public/templates/link.html Normal file
View File

@ -0,0 +1,526 @@
<div class="link">
<h1>Générateur de lien</button></h1>
</br>
<div class="homepanel">
<div class="row w-100">
<div class="hbox col-lg">
<h1><i class="fa fa-link"></i> Liens raccourcis</h1>
</br>
<div id="lkcontent">
</div>
</br>
<button id="lk_add" class="lk_add"><i class="fa fa-plus"></i> Ajouter</button>
</div>
</div>
</div>
</div>
<dialog class="lk_adduser" id="lk_add_form">
<h1><i class="fa fa-plus"></i> Ajouter un lien raccourci</h1>
<p>Nom du raccourci :</p>
<input class="inp" id="lk_add_name" type="text">
<p>Lien à raccourcir :</p>
<input class="inp" id="lk_add_src" type="text">
<p>Nouveau lien (neutral.raphix.fr/link/<i>votrelien</i>):</p>
<input class="inp" id="lk_add_dest" type="text">
<div style="color: rgb(255, 76, 76);" id="lk_add_info"></div>
</div>
<button onclick="getPage('link.html')" id="lk_add_close" class="lk_add_close">Annuler</button>
<button id="lk_add_confirm" class="lk_add_confirm">Ajouter</button>
</dialog>
<div id="lk_dialog">
</div>
<script>
if(typeof lkaddform == 'undefined'){
const lkcontent = document.getElementById("lkcontent")
const lkdialogs = document.getElementById("lk_dialog")
getLinks()
function getLinks() {
var linksContent = new Array()
var dialogContent = new Array()
fetch('/link', {
method: 'POST',
redirect: 'follow',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({"request":"get", "value": ""})
})
.then(response => response.json())
.then(response => showLink(response))
function showLink(response) {
var links = response.content
for(var link of links) {
linksContent.push('<div class="row lk_line"> <p class="col-lg"><i class="fas fa-sticky-note"></i> Nom du raccourci : ' + link.name + '</p> <p class="col-lg"><i class="fa-sharp fa-solid fa-link"></i> Lien raccourci : <a href="http://localhost/link/' + link.dest + '">http://localhost/link/' + link.dest + '</a></p> <p class="col-lg"><i class="fa-solid fa-up-right-from-square"></i> Lien réel : <a href="' + link.src + '"><strong>Vers le lien</strong></a></p> <div class="col-lg "> <button id="' + link.name + '_edit_btn" class="lk_edit"><i class="fa fa-pen"></i></button> <button id="' + link.name + '_delete" class="lk_delete"><i class="fa fa-trash"></i></button> </div> </div>')
dialogContent.push('<dialog class="lk_adduser" id="' + link.name + '_dialog"> <h1><i class="fa fa-pen"></i> Editer un lien raccourci</h1> <p>Nom du raccourci :</p> <input class="inp" id="' + link.name + '_edit_name" type="text"> <p>Lien à raccourcir :</p> <input class="inp" id="' + link.name + '_edit_src" type="text"> <p>Nouveau lien (neutral.raphix.fr/link/<i>votrelien</i>):</p> <input class="inp" id="' + link.name + '_edit_dest" type="text"> <div style="color: rgb(255, 76, 76);" id="' + link.name + '_edit_info"></div> </div> <button onclick="getPage(\'link.html\')" id="lk_add_close" class="lk_add_close">Annuler</button> <button id="' + link.name + '_edit_confirm" class="lk_add_confirm">Editer</button> </dialog>')
lkcontent.innerHTML = linksContent.join("")
lkdialogs.innerHTML = dialogContent.join("")
}
for(var link of links) {
makeAction(link)
}
if(links.length == 0) {
lkcontent.innerHTML = '<span style="margin: 2vw;color: rgb(255, 76, 76);">Aucun lien raccourci n\'a été crée.</span>' // lkcontent.innerHTML = linksContent.join("")
}
}
}
function makeAction(link) {
const lkdelete = document.getElementById(link.name + "_delete")
const lkedit = document.getElementById(link.name + '_edit_btn')
const lkdialog = document.getElementById(link.name + "_dialog")
const lkname = document.getElementById(link.name + '_edit_name')
const lksrc = document.getElementById(link.name + "_edit_src")
const lkdest = document.getElementById(link.name + "_edit_dest")
const lkinfo = document.getElementById(link.name + "_edit_info")
const lkconfirm = document.getElementById(link.name + "_edit_confirm")
lkdelete.addEventListener("click", () => {
fetch('/link', {
method: 'POST',
redirect: 'follow',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({"request":"del", "value": link.name})
})
getPage("link.html")
})
lkedit.addEventListener("click", () => {
lkname.value = link.name
lksrc.value = link.src
lkdest.value = link.dest
lkdialog.showModal()
})
lkconfirm.addEventListener("click", () => {
lkinfo.innerHTML = ""
if(lkname.value == "" | lkdest.value == "" | lksrc.value == "") {
lkinfo.innerHTML = "Tout les champs doivent être remplis."
} else {
if(lkdest.value.includes(" ") | lksrc.value.includes(" ")) {
lkinfo.innerHTML = "Aucun espace n'est accepté dans les liens"
} else {
const requestData = {
"name":lkname.value,
"src":lksrc.value,
"dest":lkdest.value,
"original":link.name
}
fetch('/link', {
method: 'POST',
redirect: 'follow',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({"request":"edit", "value": requestData})
})
.then(response => response.json())
.then(response => afteredit(response))
function afteredit(response) {
if(response.result == "failed") {
content.innerHTML = ' <h1 style="color:red;"><i style="color:yellow; font-size: 2vw;" class="fa fa-warning"></i> Erreur de chargement des données - Erreur : ' + response.content + '</h1>'
console.log("FAILED")
} else if(response.result == "success") {
if(response.content == "EDI") {
getPage("link.html")
}else if(response.content == "NAME_ALREADY_USED") {
lkinfo.innerHTML = "Le nom du raccourci est déjà utilisé"
} else if(response.content == "DEST_ALREADY_USED") {
lkinfo.innerHTML = "L'URL de raccourci est déjà utilisé"
} else {
lkinfo.innerHTML = "ECHEC - Réessayez plus tard !"
}
}
}
}
}
})
}
const lkaddbtn = document.getElementById("lk_add")
const lkaddform = document.getElementById("lk_add_form")
const lkaddname = document.getElementById("lk_add_name")
const lkaddsrc = document.getElementById("lk_add_src")
const lkadddest = document.getElementById("lk_add_dest")
const lkaddconfirm = document.getElementById("lk_add_confirm")
const lkaddinfo = document.getElementById("lk_add_info")
lkaddbtn.addEventListener("click", () => {
lkaddform.showModal()
})
lkaddconfirm.addEventListener("click", () => {
lkaddinfo.innerHTML = ""
if(lkaddname.value == "" | lkadddest.value == "" | lkaddsrc.value == "") {
lkaddinfo.innerHTML = "Tout les champs doivent être remplis."
} else {
if(lkadddest.value.includes(" ") | lkaddsrc.value.includes(" ")) {
lkaddinfo.innerHTML = "Aucun espace n'est accepté dans les liens"
} else {
const requestData = {
"name":lkaddname.value,
"src":lkaddsrc.value,
"dest":lkadddest.value
}
fetch('/link', {
method: 'POST',
redirect: 'follow',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({"request":"add", "value": requestData})
})
.then(response => response.json())
.then(response => af(response))
function af(response) {
if(response.result == "failed") {
content.innerHTML = ' <h1 style="color:red;"><i style="color:yellow; font-size: 2vw;" class="fa fa-warning"></i> Erreur de chargement des données - Erreur : ' + response.content + '</h1>'
console.log("FAILED")
} else if(response.result == "success") {
if(response.content == "ADDED") {
getPage("link.html")
} else if(response.content == "NAME_ALREADY_USED") {
lkaddinfo.innerHTML = "Le nom du raccourci est déjà utilisé"
} else if(response.content == "DEST_ALREADY_USED") {
lkaddinfo.innerHTML = "L'URL de raccourci est déjà utilisé"
} else {
lkaddinfo.innerHTML = "ECHEC - Réessayez plus tard !"
}
}
}
}
}
})
}
</script>
<!-- Style-->
<style>
.hbox h1 {
font-size: large;
}
table {
color: white;
}
.lk_line {
padding: 0.5vw;
text-align: center;
align-items: center;
vertical-align: center;
}
.lk_line a {
color: white;
}
.hbox {
margin: 1%;
padding: 1%;
background-color: rgb(80, 80, 80);
border-radius: 1vw;
box-shadow: 5px 5px 5px rgba(80, 80, 80, 0.477) ;
}
.lk_add {
border-radius: 1vw;
border-color: rgb(0, 174, 255);
border-width: 1%;
border-style: solid;
color: white;
background-color: transparent;
transition: all 0.2s ease 0s;
padding: 0.5vw;
}
.lk_add:hover {
box-shadow: 1px 1px 10px rgb(0, 174, 255);
background-color: rgb(0, 174, 255);
color: black;
}
.lk_add:active {
box-shadow: none;
}
.lk_edit {
border-radius: 100%;
border-color: rgb(0, 174, 255);
border-width: 1%;
border-style: solid;
color: white;
background-color: transparent;
transition: all 0.2s ease 0s;
width: 2.5vw;
height: 2.5vw;
margin-left: 0.5vw;
margin-right: 0.5vw;
font-size: 1vw;
}
.lk_edit:hover {
box-shadow: 1px 1px 10px rgb(0, 174, 255);
background-color: rgb(0, 174, 255);
color: black;
}
.lk_edit:active {
box-shadow: none;
}
.lk_delete {
border-radius: 100%;
border-color: rgb(255, 48, 48);
border-width: 1%;
border-style: solid;
color: white;
background-color: transparent;
transition: all 0.2s ease 0s;
width: 2.5vw;
height: 2.5vw;
margin-left: 0.5vw;
margin-right: 0.5vw;
font-size: 1vw;
}
.lk_delete:hover {
box-shadow: 1px 1px 10px rgb(255, 48, 48);
background-color: rgb(255, 48, 48);
color: black;
}
.lk_delete:active {
box-shadow: none;
}
.lk_adduser {
border-radius: 1vw;
border-color: transparent;
background-color: rgb(80, 80, 80);
color: white;
text-align: center;
}
.lk_add_close {
border-radius: 1vw;
border-color: rgb(255, 48, 48);
border-width: 1%;
border-style: solid;
color: white;
background-color: transparent;
transition: all 0.2s ease 0s;
margin: 0.5vw;
padding: 0.5vw;
font-size: 1vw;
}
.lk_add_close:hover {
box-shadow: 1px 1px 10px rgb(255, 48, 48);
background-color: rgb(255, 48, 48);
color: black;
}
.lk_add_close:active {
box-shadow: none;
}
.lk_add_confirm {
border-radius: 1vw;
border-color: rgb(0, 174, 255);
border-width: 1%;
border-style: solid;
color: white;
background-color: transparent;
transition: all 0.2s ease 0s;
margin: 0.5vw;
padding: 0.5vw;
font-size: 1vw;
}
.lk_add_confirm:hover {
box-shadow: 1px 1px 10px rgb(0, 174, 255);
background-color: rgb(0, 174, 255);
color: black;
}
.lk_add_confirm:active {
box-shadow: none;
}
.inp {
border-style: hidden;
border-radius: 1vw;
padding: 0.2vw;
padding-left: 1vw;
transition: all 0.2s ease 0s;
margin-bottom: 0.5vw;
width: 100%;
}
.inp:hover {
box-shadow: 2px 2px 5px rgba(255, 255, 255, 0.477) ;
}
.inp:focus {
box-shadow: 5px 5px 5px rgba(0, 174, 255, 0.477) ;
}
</style>

View File

@ -4,6 +4,7 @@ var ntr = require("../neutral-functions.js");
var fs = require("fs")
var path = require("path")
/* GET home page. */
router.get('/', function(req, res, next) {

View File

@ -5,6 +5,9 @@ var ntr = require("../neutral-functions.js")
/* GET home page. */
router.get('/', function(req, res, next) {
ntr.checkUser()
var check = ntr.checkToken(req, res)
if(check.name == false) {

268
routes/link.js Normal file
View File

@ -0,0 +1,268 @@
var express = require('express');
const { compileString } = require('sass');
var router = express.Router();
var fs = require("fs")
var ntr = require("../neutral-functions.js")
/* GET home page. */
router.get('/', function(req, res, next) {
var check = ntr.checkToken(req, res)
if(check.name == false) {
res.redirect(302, "/login")
} else {
res.render('index', { title: ntr.getFullName(check.name), username: ntr.getFullName(check.name), accountpic: '<img width="200" class="w-25 sidebar-image noside" src="images/userspics/' + check.name + '.png">' });
}
});
router.post("/", function (req, res, next) {
var check = ntr.checkToken(req, res)
if(check.name == false) {
res.send({"result":"failed", "content":"ERROR_TOKEN_NOT_VALID"})
} else {
if(check.permLevel >= 3) {
if(req.body.request == "add") {
const linkn = req.body.value
const linkData = JSON.parse(fs.readFileSync(__dirname.replace("routes",'links.json')))
if(linkn.src.includes(" ") | linkn.dest.includes(" ")) {
res.send({"result":"failed", "content":"CONTAINS_SPACES"})
} else if(Object.keys(linkData).includes(linkn.name)) {
res.send({"result":"success", "content":"NAME_ALREADY_USED"})
} else if(checkDest(linkn.dest) == true) {
res.send({"result":"success", "content":"DEST_ALREADY_USED"})
} else {
linkData[linkn.name] = {
"src": linkn.src.toLowerCase(),
"dest": linkn.dest.toLowerCase()
}
fs.writeFileSync(__dirname.replace("routes",'links.json'), JSON.stringify(linkData, null, 2))
loadLinksSaved()
res.send({"result":"success", "content":"ADDED"})
}
} else if(req.body.request == "del") {
const linkData = JSON.parse(fs.readFileSync(__dirname.replace("routes",'links.json')))
Reflect.deleteProperty(linkData,req.body.value)
fs.writeFileSync(__dirname.replace("routes",'links.json'), JSON.stringify(linkData, null, 2))
loadLinksSaved()
} else if(req.body.request == "get") {
const linkData = JSON.parse(fs.readFileSync(__dirname.replace("routes",'links.json')))
const links = Object.keys(linkData)
var allLink = new Array()
for(var link of links) {
allLink.push({"name": link, "src":linkData[link].src, "dest":linkData[link].dest})
}
res.send({"result":"success", "content":allLink})
} else if(req.body.request == "edit") {
const linkn = req.body.value
const linkData = JSON.parse(fs.readFileSync(__dirname.replace("routes",'links.json')))
if(linkn.src.includes(" ") | linkn.dest.includes(" ")) {
res.send({"result":"failed", "content":"CONTAINS_SPACES"})
} else if(checkName(linkn.name, linkData) == true) {
res.send({"result":"success", "content":"NAME_ALREADY_USED"})
}else if(checkDest(linkn.dest, linkn.original) == true) {
res.send({"result":"success", "content":"DEST_ALREADY_USED"})
} else {
Reflect.deleteProperty(linkData, req.body.value.original)
linkData[linkn.name] = {
"src": linkn.src.toLowerCase(),
"dest": linkn.dest.toLowerCase()
}
fs.writeFileSync(__dirname.replace("routes",'links.json'), JSON.stringify(linkData, null, 2))
loadLinksSaved()
res.send({"result":"success", "content":"EDI"})
}
}
} else {
res.send({"result":"failed", "content":"ERROR_USER_PERMISSION_TOO_LOW"})
}
}
})
loadLinksSaved()
function loadLinksSaved() {
const linkData = JSON.parse(fs.readFileSync(__dirname.replace("routes",'links.json')))
const links = Object.keys(linkData)
for(var link of links) {
const dest = linkData[link].dest
const src = linkData[link].src
router.get('/' + dest, function (req, res, next) {
res.redirect(302, src)
})
}
}
function checkDest(src, name) {
if(name == null) {
const linkData = JSON.parse(fs.readFileSync(__dirname.replace("routes",'links.json')))
const links = Object.keys(linkData)
const destNotAvb = new Array()
for(var link of links) {
destNotAvb.push(linkData[link].dest)
}
if(destNotAvb.includes(src)) {
return true;
} else {
return false;
}
} else {
const linkData = JSON.parse(fs.readFileSync(__dirname.replace("routes",'links.json')))
const links = Object.keys(linkData)
const destNotAvb = new Array()
for(var link of links) {
if(link != name) {
destNotAvb.push(linkData[link].dest)
}
}
if(destNotAvb.includes(src)) {
return true;
} else {
return false;
}
}
}
function checkName(name, linkData) {
const names = Object.keys(linkData)
const namewithout = new Array()
for(var namei of names) {
if(namei != name) {
namewithout.push(linkData[namei].name)
}
}
if(namewithout.includes(name)) {
return true;
} else {
return false;
}
}
module.exports = router;

View File

@ -10,6 +10,7 @@ var ntr = require("../neutral-functions.js")
router.get('/', function(req, res, next) {
ntr.checkUser()
var check = ntr.checkToken(req, res)
if(check.name != false) {

View File

@ -0,0 +1,13 @@
{
"username": "raphix",
"password": "U2FsdGVkX19eUkHlyqAgwuLYtNjlqzUmVAD7tBEBtaY=",
"uuid": "0cee5da6-6812-49d8-97f5-2df5c1d78191",
"fullname": "Raphael PICOT",
"permissionLevel": 4,
"token": {
"tokenID": "1c80ca40-60e9-4e48-b986-753678de41ca",
"livableToken": true,
"createdAt": 1677159656255
},
"lastconnexion": 1677159976281
}

View File

@ -1,13 +0,0 @@
{
"username": "raphix",
"password": "U2FsdGVkX1+y5WFBRAA0GITGyxl0AOq2lygx7XDu6BU=",
"uuid": "9ace80e0-1ee3-4eed-924f-8a55fc55822b",
"permissionLevel": 4,
"fullname": "Raphael PICOT",
"token": {
"tokenID": "43b6f1bf-29be-4801-83f2-b1e85dcb3981",
"livableToken": true,
"createdAt": 1677070566103
},
"lastconnexion": 1677070621299
}

View File

@ -1,9 +0,0 @@
{
"username": "root",
"password": "U2FsdGVkX18KoGGTPqZw/E9PiLQaUuEZxyHdbdAErO0=",
"uuid": "aca3c9aa-5b42-477f-b179-f33211a425d3",
"fullname": "Administrateur",
"permissionLevel": 3,
"token": {},
"lastconnexion": 0
}

View File

@ -0,0 +1,13 @@
{
"username": "root",
"password": "U2FsdGVkX1/ICaOrZjiK+VFckzaSu/AcyQrpIPg2KuY=",
"uuid": "c8f353a4-d0df-4c10-bccc-d4f47daf0f51",
"fullname": "Administrateur",
"permissionLevel": 4,
"token": {
"tokenID": "00e7bb54-cd31-4c60-92a5-5c45164ea6ed",
"livableToken": false,
"createdAt": 1677159640804
},
"lastconnexion": 1677159652801
}

View File

@ -32,6 +32,7 @@
color: white;
background-color: red;
cursor: pointer;
}
</style>
</head>