-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
127 lines (117 loc) · 3.65 KB
/
server.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const fs = require('fs'); // Loads the "fs" npm.
const config = require("./config.json") // Loads the database
const port = 80
const express = require("express");
const app = express();
const listener = app.listen(port, function() {
console.log("Your app is listening on port " + listener.address().port);
configSave();
});
app.get("/createtoken", async function(req, res) {
if (req.query.token) {
let token = decodeURIComponent(req.query.token)
if (decodeURIComponent(config["token"]).split(",").includes(token)) {
if (decodeURIComponent(config["admintoken"]).split(",").includes(token)) {
if (req.query.newtoken) {
if (decodeURIComponent(config["token"]).split(",").includes(req.query.newtoken)) {
res.send(`{\n status: "already exists"\n}`)
} else {
configSet("token", decodeURIComponent(config["token"]) + "," + decodeURIComponent(req.query.newtoken))
res.send(`{\n status: "success"\n}`)
}
} else {
res.send(`{\n status: "undefined newtoken"\n}`)
}
} else {
res.send(`{\n status: "no permission"\n}`)
}
} else {
res.send(`{\n status: "invalid token"\n}`)
}
} else {
res.send(`{\n status: "undefined token"\n}`)
}
return
});
app.get("/createfile", async function(req, res) {
if (req.query.token) {
let token = decodeURIComponent(req.query.token)
if (decodeURIComponent(config["token"]).split(",").includes(token)) {
if (req.query.pasteid) {
if (req.query.message) {
configSet("pastes-" + token + "/" + decodeURIComponent(req.query.pasteid), decodeURIComponent(req.query.message))
res.send(`{\n status: "success"\n}`)
} else {
res.send(`{\n status: "undefined message\n}`)
}
} else {
res.send(`{\n status: "undefined pasteid"\n}`)
}
} else {
res.send(`{\n status: "invalid token"\n}`)
}
} else {
res.send(`{\n status: "undefined token"\n}`)
}
return
});
app.get("/deletefile", async function(req, res) {
if (req.query.token) {
let token = decodeURIComponent(req.query.token)
if (decodeURIComponent(config["token"]).split(",").includes(token)) {
if (req.query.pasteid) {
if (config["pastes-" + token + "/" + decodeURIComponent(req.query.pasteid)]) {
configSet("pastes-" + token + "/" + decodeURIComponent(req.query.pasteid), "")
res.send(`{\n status: "success"\n}`)
} else {
res.send(`{\n status: "invalid pasteid"\n}`)
}
} else {
res.send(`{\n status: "undefined pasteid"\n}`)
}
} else {
res.send(`{\n status: "invalid token"\n}`)
}
} else {
res.send(`{\n status: "undefined token"\n}`)
}
return
});
app.get("/pastes/*", async function(req, res) {
if (config["pastes-" + req.originalUrl.slice(8)]) {
res.send(decodeURIComponent(config["pastes-" + req.originalUrl.slice(8)]))
} else {
res.send("")
}
return
});
app.get("/pastes", async function(req, res) {
res.send("")
return
});
app.get("*", async function(req, res) {
res.send(`{\n status: "invalid page"\n}`)
return
});
async function configSet(name, response) {
//let config = require("./config.json");
try {
if (config[name] === undefined) {
} else {
await delete config[name];
}
if (response.toString() === "") {
} else {
config[name.toString()] = encodeURIComponent(response.toString());
}
} catch(err) {
}
}
function configSave() {
fs.writeFile("config.json", JSON.stringify(config, null, 2), function(err) {
if (err) throw err;
});
setTimeout(() => {
configSave();
}, 2000);
}