-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
377 lines (318 loc) · 11.7 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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
const express = require('express');
const session = require('express-session');
const cors = require('cors');
const path = require('path');
const fs = require('fs');
const sharp = require('sharp');
const bcryypt = require('bcrypt');
const exif = require('jpeg-exif');
const mime = require('mime');
/**
* todo
* logout clear token
* token time out
*/
/**
* @type {fs.promises}
*/
const fsp = require('node:fs/promises');
const { HasPrimaryDirectory,upload,GetRequestFilePath,getRequestDirectory,createUserDirectory } = require('./DirectoryMiddleware');
const { isWhitelisted, isAdmin,db,rootDirectory,ensureDirectories,checkjwt } = require('./DatabaseFunctions');
const {makeToken} = require('./serverFunctions');
//app
const port = 8060;
const app = express();
app.set('trust proxy',1);
app.use(session({
secret : 'it is as clear as ink is on paper',
resave: false,
saveUninitialized: true
}));
const corsorigins = ['http://localhost:5173']
if(process.env.NEW_ORIGIN){
corsorigins.push(process.env.NEW_ORIGIN)
}
app.use(cors({
origin: corsorigins, // use your actual domain name (or localhost), using * is not recommended
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Origin', 'X-Requested-With', 'Accept', 'x-client-key', 'x-client-token', 'x-client-secret', 'Authorization'],
credentials: true
}));
app.use(express.json());
app.post('/admin',checkjwt,isWhitelisted,isAdmin,(req,res)=>{
res.sendStatus(200);
})
app.post('/admin/userdata',checkjwt,isWhitelisted,isAdmin,(req,res)=>{
const pckg = {}
if(req.body.whitelist != null) pckg.whitelist = req.body.whitelist ? true : false;
if(req.body.admin != null) pckg.admin = req.body.admin
if(req.body.email == null) return res.sendStatus(403)
if(req.body.email != null) pckg.email = req.body.email;
db('Users')
.where({email : pckg.email})
.update(pckg)
.then(()=>res.sendStatus(200))
.catch(()=>res.sendStatus(500))
});
//implement a flag for passwords that increment based on current allowed tokens
//example. Creation = 1; we can log out every token simply by increasing the
//creation value inside the token to some arbitrary number like 2; doing this will
//disable any older logins and force the user to sign in manually in order to regain token/
//refresh api access while still remaining stateless
app.post('/user/changepassword',checkjwt,isWhitelisted,(req,res)=>{
if(!req.body.password || !req.body.newPassword)return res.sendStatus(400);
//we have a password and old password
const newhash = bcryypt.hashSync(req.body.newPassword,10);
//verify the password
if(bcryypt.compareSync(req.body.password,req.meta.password)){
db('Users')
.select()
.where({email : req.meta.email})
.update({password : newhash})
.then(()=>res.sendStatus(200))
.catch(()=>res.sendStatus(500));
}else {
res.sendStatus(403);
}
})
app.get("/admin/tables",checkjwt,isWhitelisted,isAdmin,async(req,res)=>{
db('Users')
.select('whitelist','email','username','admin')
.then(rows=>{
res.status(200).json(rows);
})
.catch(()=>{
res.status(500);
});
})
app.get('/admin/whitelist',checkjwt,isWhitelisted,isAdmin,(req,res)=>{
db('Users')
.select('*')
.then(rows=>{
res.send(rows);
})
.catch(()=>{
res.sendStatus(500);
})
})
app.get('/admin/rootpath',checkjwt,isWhitelisted,isAdmin,async (req,res)=>{
const apath = await rootDirectory;
res.send(apath);
})
app.post('/admin/newUser',checkjwt,isWhitelisted,isAdmin,(req,res)=>{
const email = req.body && (req.body.email || req.body.email === '');
if(!email) return res.sendStatus(400);
db('Users')
.insert({email : email})
.then(()=>{
res.sendStatus(200)
})
.catch(()=>{
res.sendStatus(500)
})
});
app.post('/admin/setDirectory',checkjwt,isWhitelisted,isAdmin,(req,res)=>{
const apath = req.body && req.body.path;
//root -> dir
db('root').update({dir : apath})
.then(()=>{
res.sendStatus(200);
})
.catch(()=>{
res.sendStatus(500);
})
console.log('Home Directory Changed : restart needed');
});
//upload to file system
app.post('/account/upload',checkjwt,isWhitelisted,upload.any(),(req,res)=>{
res.sendStatus(200);
});
app.get('/image/get/user/:token/dir/:dir/name/:name/comp/:compression',checkjwt,isWhitelisted,async(req,res)=>{
//delim is !aasd!aa
const newbody = {
name : req.params.name,
path : req.params.dir.replace('!aasd!aa','/')
}
req.body = newbody;
const filepath = await GetRequestFilePath(req);
const fileexists = fs.existsSync(filepath);
if(!fileexists) return res.sendStatus(404);
const qual = req.params.compression;
//needs to be in sync with the request
const rimg = new RegExp(/\.((png)|(gif)|(jpeg)|(jpg))/,'i');
const rvideo = new RegExp(/\.((mp4)|(mov)|(wmv)|(webm)|(mkv))/,'i');
const raudio = new RegExp(/\.((ogg)|(mp3))/,'i');
const isphoto = rimg.test(filepath);
const isVideo = rvideo.test(filepath);
const isAudio = raudio.test(filepath);
const iselse = !(isphoto || isVideo || isAudio)
if(isphoto){
let imgdata = sharp(filepath)
.jpeg({mozjpeg : true})
if(qual == 'min'){
imgdata = imgdata.resize(200,200,{fit : 'contain'})
}
imgdata
.toBuffer()
.then(buf =>{
res.send(buf)
//console.log('full ',buf.length);
})
.catch((e)=>{
console.log("err",e);
res.sendStatus(500);
})
} else if(isAudio || isVideo){
//console.log(req.headers);
//res.sendFile(filepath);
//range: 'bytes=3604480-4194303',
//range: 'bytes=3604480-',
//range: 'bytes=0-'
///const vidpath = "C:/Users/mrmar/OneDrive/Desktop/mydrive/FileSystem/admin92668751/Citrus(MarcusWatson) - Discord 2022-03-25 00-01-50.mp4";
const vidpath = filepath;
//send as partial data.
let range = req.headers.range;
const fileStat = fs.statSync(vidpath);
const videoSize = fileStat.size;
const CHUNK_SIZE = 10 ** 6;
if (!range) {
const conttype = mime.getType(vidpath.split('.').pop())
const headers = {
//"Content-Range": `bytes ${0}-${videoSize}/${videoSize}`,
//"Accept-Ranges": "bytes",
"Content-Length": videoSize,
"Content-Type": conttype,
};
res.writeHead(200, headers);
const videoStream = fs.createReadStream(vidpath);
videoStream.pipe(res);
}else{
const parts = range.replace('bytes=','').split('-');
const start = parseInt(parts[0]);
const end = parts[1] != '' ? parseInt(parts[1]) : Math.min(start + CHUNK_SIZE, videoSize - 1);
const contentLength = end - start + 1;
const conttype = mime.getType(vidpath.split('.').pop())
const headers = {
"Content-Range": `bytes ${start}-${end}/${videoSize}`,
"Accept-Ranges": "bytes",
"Content-Length": contentLength,
"Content-Type": conttype,
};
res.set('Content-Type','video/mp4');
res.writeHead(206, headers);
const videoStream = fs.createReadStream(vidpath, { start, end });
videoStream.pipe(res);
}
} else if(iselse){
//dunno what this is, send as raw text
res.sendFile(filepath);
}
})
//create folders | escape (../) vunlerability?
app.post('/create/dir',checkjwt,isWhitelisted,async(req,res)=>{
const base = await getRequestDirectory(req);
const reqPath = path.join(base,req.body.name);
const pathexists = fs.existsSync(reqPath);
if(pathexists) {
//path aleady exists
res.sendStatus(200);
}else {
fs.mkdir(reqPath,()=>{
res.sendStatus(200);
},(e)=>{
console.log(e);
res.sendStatus(401);
})
}
})
app.post('/account/list',checkjwt,isWhitelisted,async (req,res)=>{
//req.params.path
//listDirectory,getprimarydir
const dirpath = await getRequestDirectory(req);
//read directory
fsp.readdir(dirpath,{withFileTypes : true})
.then((data)=>{
const alldirs = data.filter(item => item.isDirectory());
const allfiles = data.filter(item =>!item.isDirectory());
//jpgs have extra
const filesStats = allfiles.map((item)=>{
let stats = fs.statSync(path.join(dirpath,item.name));;
const rimg = new RegExp(/\.((jpg)|(jpeg))/,'i');
if(rimg.test(item.name)){
const morestats = exif.parseSync(path.join(dirpath,item.name));
stats = {...stats,...morestats}
}
return {
name : item.name,
...stats
}
})
res.send({"directories" : alldirs,"files" : filesStats});
})
.catch(()=>{
res.sendStatus(400);
})
});
app.get('/account/listsecret',checkjwt,isWhitelisted,(req,res)=>{
});
//before we listen we must make sure everything is in order
async function InitialCheck(){
//initialize the tables
const check1 = db.schema.hasTable('Users').then(async (exists)=>{
if(!exists){
//create the whitelist table
await db.schema.createTable('Users',(table)=>{
table.increments();
table.string('username');
table.string('email').unique().primary();
table.string('password');
table.json('favorite');
table.json('secretfavorite');
table.json('shared');
table.json('secretshared');
table.json('drives');
table.string('maindrive');
table.string('secretdrive');
table.string('trashdrive');
table.binary('ProfilePicture');
table.json('accessTokens');
table.json('refreshTokens');
table.boolean('admin');
table.boolean('whitelist');
//secret, trash will be actual folders
}).then(()=>{console.log("initialized table")})
//password : 'admin',
await db('Users').insert([{
username: 'admin',
email : 'admin',
password: bcryypt.hashSync('admin',10),
admin : true,
whitelist : true
}]).then(async ()=>{
await ensureDirectories('admin');
console.log('inserted user admin password admin. Change at /admin')
})
//default user is admin
//add users through admin panel
}
})
//set the base directory to be ./FileSystem
const check2 = db.schema.hasTable('meta').then(async (exists)=>{
if(!exists){
await db.schema.createTable('meta',(table)=>{
table.string('directory')
}).then(()=>{console.log('initilizad server meta. change at /admin')})
await db('meta').insert([{'directory':path.join(__dirname,'FileSystem')}]).then(()=>{
console.log('primary directory created');
});
}
})
await Promise.allSettled([check1,check2,rootDirectory])
.then(()=>{console.log("finished Server checks")})
.catch(()=>{console.error("couldn't finish checks, aborting...")})
//host the server
app.listen(port,()=>{
console.log(`listening on localhost:${port}/`);
})
}
InitialCheck()