-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
320 lines (306 loc) · 8.49 KB
/
index.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
const fs = require('fs');
const path = require('path')
var organizer = require('./organizer/organizer.js');
const fastify = require('fastify')({
/*https: {
key: fs.readFileSync("/etc/letsencrypt/live/pta.ddns.net/privkey.pem"),
cert: fs.readFileSync("/etc/letsencrypt/live/pta.ddns.net/fullchain.pem")
},*/
logger: true
})
const PATH = fs.readFileSync("path.txt").toString().replace("\n","").replace("\r","").trim()
const CryptoJS = require("crypto-js");
const { v4: uuidv4 } = require('uuid');
const master = 'SHA512 of the master key';
var authorized = [];
var authorized_su = [];
fastify.register(require('fastify-static'), {
root: path.join(__dirname, 'static'),
decorateReply: false
})
fastify.register(require('fastify-static'), {
root: PATH,
prefix: "/cdn/",
decorateReply: false
})
fastify.addContentTypeParser('application/json', { parseAs: 'string' }, function (req, body, done) {
try {
var json = JSON.parse(body)
done(null, json)
} catch (err) {
err.statusCode = 400
done(err, undefined)
}
})
// Declare routes
fastify.post('/checkauth', async (request, reply) => {
if(!request.body.hasOwnProperty("auth")) {
reply.send(JSON.stringify({
sucess: false,
error: "No 'auth' argument passed"
}))
return
}
var hash;
try {
hash = CryptoJS.SHA512(request.body.auth).toString();
} catch {
reply.send(JSON.stringify({
sucess: false,
error: "Authentification failed, please retry."
}))
return
}
if (authorized.includes(hash) || authorized_su.includes(hash)) {
reply.send(JSON.stringify({
sucess: true
}))
} else {
reply.send(JSON.stringify({
sucess: false,
error: "Authentification failed, please retry."
}))
return
}
})
fastify.post('/auth', async (request, reply) => {
if(!request.body.hasOwnProperty("auth")) {
reply.send(JSON.stringify({
sucess: false,
error: "No 'auth' argument passed"
}))
return
}
var hash;
var su_atp = false;
try {
var arr = request.body.auth.split("+")
if (arr[1] == "prv") su_atp = true;
hash = CryptoJS.SHA512(arr[0]).toString();
} catch {
reply.send(JSON.stringify({
sucess: false,
error: "Authentification failed, please retry."
}))
return
}
if (hash === master) {
var cookie = uuidv4();
var ck_hash = CryptoJS.SHA512(cookie).toString();
if (su_atp) authorized_su.push(ck_hash);
if (!su_atp) authorized.push(ck_hash);
reply.send(JSON.stringify({
sucess: true,
auth: cookie
}))
} else {
reply.send(JSON.stringify({
sucess: false,
error: "Authentification failed, please retry."
}))
return
}
})
fastify.get('/path', async (request, reply) => {
reply.send(PATH)
})
fastify.get('/retrieve', async (request, reply) => {
var auth_arr = request.raw.rawHeaders.filter((hd) => hd.startsWith("auth="));
if (auth_arr.length > 0) {
var hash = "";
try {
hash = CryptoJS.SHA512(auth_arr[0].substring("auth=".length)).toString();
} catch {
reply.send("{}");
return
}
if (authorized_su.includes(hash) || authorized.includes(hash)) {
var folder = 'libraries/'
var lib_arr = {}
fs.readdirSync(folder).forEach(function(file) {
new_file = folder+'/'+file;
if ((file.includes("prv") && authorized_su.includes(hash)) || !file.includes("prv")) lib_arr[file] = JSON.parse(fs.readFileSync(new_file));
});
reply.send(JSON.stringify(lib_arr))
} else {reply.send("{}"); return}
} else {reply.send("{}"); return}
})
fastify.post('/scan', async (request, reply) => {
if (!request.body.hasOwnProperty("name")) {
reply.send(JSON.stringify({
sucess: false,
error: "No 'name' argument passed"
}))
return
}
if (request.body.name.trim() == "") {
reply.send(JSON.stringify({
sucess: false,
error: "Empty 'name' argument"
}))
return
}
var lib_path = 'libraries/'+request.body.name
var library;
try {
library = JSON.parse(fs.readFileSync(lib_path))
} catch (err) {
reply.send(JSON.stringify({
sucess: false,
error: "Library not found"
}))
return
}
try {
library.lib = await organizer.folder(library.path, library.type)
} catch (err) {
reply.send(JSON.stringify({
sucess: false,
error: "An error has occured while parsing the library"
}))
console.log(err)
return
}
fs.writeFileSync(lib_path, JSON.stringify(library), function (err) {
if (err) throw err;
});
console.log('Library "'+request.body.name+'" scaned !');
reply.send(JSON.stringify({
sucess: true
}))
})
fastify.post('/del', async (request, reply) => {
if (!request.body.hasOwnProperty("name")
|| !request.body.hasOwnProperty("pwd")) {
reply.send(JSON.stringify({
sucess: false,
error: "No 'name' argument passed"
}))
return
}
if (request.body.name.trim() == "") {
reply.send(JSON.stringify({
sucess: false,
error: "Empty 'name' argument"
}))
return
}
var hash;
try {
hash = CryptoJS.SHA512(request.body.pwd).toString();
} catch {
reply.send(JSON.stringify({
sucess: false,
error: "Authentification failed, please retry."
}))
return
}
if (hash === master) {
var lib_path = 'libraries/'+request.body.name
var library;
try {
fs.unlinkSync(lib_path);
} catch (err) {
reply.send(JSON.stringify({
sucess: false,
error: "Library not found"
}))
return
}
reply.send(JSON.stringify({
sucess: true
}))
} else {
reply.send(JSON.stringify({
sucess: false,
error: "Wrong password."
}))
return
}
})
fastify.post('/add', async (request, reply) => {
if (!request.body.hasOwnProperty("pwd")
|| !request.body.hasOwnProperty("path")
|| !request.body.hasOwnProperty("type")
|| !request.body.hasOwnProperty("name")) {
reply.send(JSON.stringify({
sucess: false,
error: "Please fill all the fields."
}))
return
}
if (request.body.pwd.trim() == ""
|| request.body.path.trim() == ""
|| request.body.type.trim() == ""
|| request.body.name.trim() == "") {
reply.send(JSON.stringify({
sucess: false,
error: "Please fill all the fields."
}))
return
}
var hash;
try {
hash = CryptoJS.SHA512(request.body.pwd).toString();
} catch {
reply.send(JSON.stringify({
sucess: false,
error: "Authentification failed, please retry."
}))
return
}
if (hash === master) {
if (request.body.path != request.body.path.replace(/\/\.\./g, '')) {
reply.send(JSON.stringify({
sucess: false,
error: "Invalid path"
}))
return
}
var lib;
try {
lib = await organizer.folder(PATH+request.body.path, request.body.type)
} catch (err) {
reply.send(JSON.stringify({
sucess: false,
error: "An error has occured while parsing the library"
}))
console.log(err)
}
var path = 'libraries/'+request.body.name
if (!fs.existsSync(path)) {
fs.writeFileSync(path, JSON.stringify({
path: PATH+request.body.path,
type: request.body.type,
lib: lib
}), function (err) {
if (err) throw err;
});
console.log('New library added !');
reply.send(JSON.stringify({
sucess: true
}))
} else {
reply.send(JSON.stringify({
sucess: false,
error: "This name is already use for another library"
}))
}
} else {
reply.send(JSON.stringify({
sucess: false,
error: "Wrong password."
}))
return
}
})
// Run the server!
const start = async () => {
try {
await fastify.listen({ port: 2000, host: '127.0.0.1' })
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()