-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.js
80 lines (74 loc) · 3.29 KB
/
admin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const express = require('express');
const app = express.Router();
const { query, mainConfig } = require('./index.js');
const { nanoid } = require('nanoid');
const bodyParser = require('body-parser');
const dirTree = require("directory-tree");
const multer = require('multer');
const fetch = require('node-fetch');
const fs = require('fs');
app.use(bodyParser.json())
app.use( async ( req,res,next ) => {
console.log(req.session.user)
if (req.session.user.admin == 0 || req.session.user.admin==null) return;
next()
});
app.get("/node", async ( req,res ) => {
if (req.query.update == "" ){
console.log(req.query)
const { node, url, name, newData } = req.query;
if (node == "0"){
if ( !name || !newData ) return res.status(403).send("missing type");
console.log(name, newData)
}
if ( !node || !url || !name || !newData ) return res.status(403).send("missing type");
let bucketUpdate;
if (newData == "true") {bucketUpdate = 1 } else {bucketUpdate = 0};
await query("UPDATE nodes SET URL = ? , name = ? , newBuckets = ? WHERE UID = ? ;",[url, name, bucketUpdate, parseInt(node)]);
} else if (req.query.delete == "") {
const { node } = req.query;
if (!node) return res.status(403).send("missing type");
await query("DELETE FROM nodes WHERE UID = ?;", [parseInt(node)]);
} else if (req.query.create == "") {
console.log("NEW NODE", req.query)
const { name, url } = req.query;
if ( !name || !url ) return res.status(403).send("missing type");
let token = nanoid(100);
let body = JSON.stringify({
token,
panelURL: mainConfig.panel.bace_url,
baceURL: url
});
let nodeRes = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body }).catch( e =>{ return res.send({error: 'ERR/NO CONNE/ I Could not connect to that panel!'})})
try{
nodeRes = await nodeRes.json();
}catch(e){
return res.send({error: `This is not a FileStore Panel!`})
}
if (nodeRes.error) return res.send({error: `ERR/ ${nodeRes.error}`})
if ( nodeRes.token == token ) {
await query(`INSERT INTO nodes( name, URL,newBuckets, key) VALUES ( ? , ? , 1, ? );`,[name, url, token]);
return res.send({});
} else {
return res.send({error: 'admin?nodeE=ERR/ There was an error while exchanging tokens! reset your node! '})
}
}
return res.send()
});
app.get("/settings", async ( req,res ) => {
if (req.query.name){
mainConfig.panel.name = req.query.name;
} else if (req.query.allowNewAccounts){
mainConfig.allow_new_users = req.query.allowNewAccounts == 'true';
} else if (req.query.panelURL) {
mainConfig.panel.bace_url = req.query.panelURL ;
} else if (req.query.createNewBuckets) {
mainConfig.built_in_node.allowNewBuckets = req.query.createNewBuckets == 'true' ? 1 : 0;
console.log(mainConfig)
fs.writeFile('config.json', JSON.stringify(mainConfig), e => console.log);
return res.redirect('../../admin#nodes')
}
fs.writeFile('config.json', JSON.stringify(mainConfig), e => console.log);
res.redirect('../../admin')
});
module.exports = { app };