-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
283 lines (231 loc) · 7.68 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
console.log = function(){}; // Uncomment this line to remove all console logs if you're pushing to production!
require("dotenv").config();
const express = require("express");
const path = require("path");
const { Readable } = require("stream")
const crypto = require("crypto");
const bcrypt = require("bcrypt"); // Will be used in /register
const mongoose = require("mongoose");
const socket = require("socket.io");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const methodOverride = require("method-override");
const { login, refresh, verify, skip } = require("./jwt");
const Grid = require("gridfs-stream");
const { WriteStream } = require("fs");
const app = express();
const port = process.env.PORT || 3000;
const mongoURI = process.env.MONGO_URI;
mongoose.connect(mongoURI, { useUnifiedTopology: true, useNewUrlParser: true })
.then(() => {
console.log("Connected to database...");
})
.catch((e) => {
console.log("Failed to connect to database: ", e);
});
var connection = mongoose.connection;
let gfs;
connection.once("open", () => {
gfs = Grid(connection.db, mongoose.mongo);
gfs.collection("videos");
});
app.set('view engine', 'ejs');
app.use(express.static(__dirname));
app.use(bodyParser.json({limit: '120mb'}));
app.use(cookieParser());
app.use(methodOverride("_method"));
app.use(express.urlencoded({ extended: false }));
app.get("/", (req, res) => {
res.sendFile(__dirname + "/index.html");
});
app.get("/robots.txt", (req, res) => {
res.sendFile(__dirname + "/robots.txt");
});
app.get("/alt", (req, res) => {
res.sendFile(__dirname + "/pages/alt.html");
});
app.post("/altlog", (req, res)=>{
if(req.body.username == "admin" && req.body.password == "admin"){
res.status(200).json({"message": "Well Done! You are awesome!", "key": 'FLAG: 7c2ddf7e16ecbb444def8ff33caf3b83'});
} else{
res.status(401).redirect("/alt");
}
})
app.post("/login", login);
app.post("/refresh", refresh);
app.get("/panel", verify, (req, res) => {
res.sendFile(__dirname + "/pages/panel.html");
});
app.get("/files", verify, (req, res) => {
gfs.files.find().toArray((err, files) => {
// Check if files
if (!files || files.length === 0) {
return res.status(404).json({
err: "No files exist",
});
}
// Files exist
return res.json(files);
});
});
app.get("/files/:filename", verify, (req, res) => {
gfs.files.findOne({ filename: req.params.filename }, (err, file) => {
// Check if file
if (!file || file.length === 0) {
return res.status(404).json({
err: "No file exists",
});
}
// File exists
return res.json(file);
});
});
app.get("/video/:filename", verify, (req, res) => {
gfs.files.findOne({ filename: req.params.filename }, (err, file) => {
// Check if file
if (!file || file.length === 0) {
return res.status(404).json({
err: "No file exists",
});
}
if (file.contentType.substring(0, file.contentType.lastIndexOf("/")) === "video") {
// Read output to browser
res.setHeader("Content-Type", file.contentType);
const readstream = gfs.createReadStream(file.filename);
readstream.pipe(res);
} else {
res.status(404).json({
err: "Not a video",
});
}
});
});
app.get("/toggle/:filename", verify, (req, res) => {
gfs.files.findOne({ filename: req.params.filename }, (err, file) => {
// Check if file
if (!file || file.length === 0) {
return res.status(404).json({
err: "No file exists",
});
}
gfs.files.updateOne(
{ filename: file.filename },
{ $set: { "metadata.public": !file.metadata.public } }
);
return res.redirect("back");
});
});
app.get("/src", (req, res) => {
gfs.files.findOne({ filename: req.query.watch }, (err, file) => {
// Check if file
if (!file || file.length === 0 || !file.metadata.public) {
return res.status(404).json({
err: "Sorry, we couldn't find the video you were looking for :(",
});
}
if (file.contentType.substring(0, file.contentType.lastIndexOf("/")) === "video") {
res.setHeader("Content-Type", file.contentType);
res.setHeader("Content-Length", file.length);
const readstream = gfs.createReadStream(file.filename);
readstream.pipe(res);
} else {
res.status(404).json({
err: "Not a video",
});
}
});
});
app.get("/v", (req, res) => {
gfs.files.findOne({ filename: req.query.watch }, (err, file) => {
// Check if file
if (!file || file.length === 0) {
return res.render("video", {'title': "VodBox", 'public': false});
}
if (!file.metadata.public) {
return res.render("video", {'title': file.metadata.name, 'public': false});
}
if (file.contentType.substring(0, file.contentType.lastIndexOf("/")) === "video") {
res.render("video", {'title': file.metadata.name, 'public': true , 'video': "src?watch=" + file.filename, 'type': file.contentType, 'filename': file.filename});
} else {
return res.render("video", {'title': file.metadata.name, 'public': false});
}
});
});
app.get("/poster", (req, res)=>{
res.setHeader("Content-Type", "image/png");
res.sendFile(__dirname + "/assets/poster.png");
})
app.delete("/files/:id", verify, (req, res) => {
gfs.remove({ _id: req.params.id, root: "videos" }, (err, gridStore) => {
if (err) {
return res.status(404).json({ err: err });
}
});
res.redirect("/panel");
});
let server = app.listen(port, () => {
console.log(`Server listening on port ${port}...`);
});
let io = socket(server);
io.on("connection", (socket) => {
console.log("Socket connexion was established...");
let = writestream = null;
socket.on("started", (data) => {
if(data["filetype"].substring(0, data["filetype"].lastIndexOf("/")) !== "video"){
io.emit("refresh");
return;
}
console.log("Started video upload...");
writestream = gfs.createWriteStream(generateOptions(data["originalname"], data["filetype"]));
app.post("/upload", verify, (req, res)=>{
if(req.body){
console.log("Received fragment of file: ", req.body["slice"]);
writestream.write(Buffer.from(req.body["slice"].flat(Infinity)));
io.emit("fragment", req.body["number"]);
res.status(204).send();
} else{
res.redirect("/panel");
}
})
socket.on("completed", () => {
writestream.end();
writestream = null;
console.log("Finished uploading file!");
console.log("Refreshing client page...");
io.emit("refresh");
});
});
});
// let io = socket(server);
// io.on("connection", (socket) => {
// console.log("Socket connexion was established...");
// var file = [];
// socket.on("file", (data) => {
// console.log("Received fragment of file: ", data["slice"]);
// file.push(data["slice"]);
// io.emit("fragment", data["number"]);
// });
// socket.on("completed", (data) => {
// const writestream = gfs.createWriteStream(generateOptions(data["originalname"], data["filetype"]));
// Readable.from(Buffer.from(file.flat(Infinity))).pipe(writestream);
// writestream.on('finish', () => {
// console.log("Finished uploading file!");
// console.log("Refreshing client page...");
// io.emit("refresh");
// });
// });
// });
generateOptions = (originalname, filetype) => {
var randomName = crypto.randomBytes(16).toString("hex") + path.extname(originalname);
return {
filename: randomName,
content_type: filetype,
root: 'videos',
bucketName: "videos",
metadata: {
name: path.basename(originalname, path.extname(originalname)),
extension: path.extname(originalname),
public: false,
}
}
}