-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
250 lines (169 loc) · 5.75 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
const express = require('express');
const app = express();
var RateLimit = require('express-rate-limit');
const config = require("./config.json")
const mongoose = require("mongoose")
const passport = require("passport")
const flash = require("express-flash")
const session = require("express-session")
const methodOverride = require("method-override")
const logger = require("./handlers/logger")
const updater = require("./handlers/updater")
app.use(methodOverride("_method"))
const {checkAuth, checkNotAuth, checkVerify, checkNotVerify} = require("./handlers/authVerify")
const roles = require("./models/roles")
const csurf = require("tiny-csrf");
const cookieParser = require("cookie-parser")
const lang = require("./lang/default.json")
if (process.env.NODE_ENV !== "production") {
require("dotenv").config()
}
const domain = config.cloudurl.split("//")
// PASSPORT & SESSION
const initializePassport = require("./handlers/passportconfig")
initializePassport(passport)
app.use(flash())
let https;
if (config.useHTTPS) https = true
else https = false
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: {
secure: https,
httpOnly: true,
maxAge: 86400000
}
}))
app.use(cookieParser(process.env.SESSION_SECRET));
app.use(express.urlencoded({ extended: false}))
app.use(csurf(
process.env.CSRF_TOKEN,
["POST"],
["/", "/api/", "/upload/"]
));
app.use(passport.initialize())
app.use(passport.session())
// RATE LIMITING
const limiter = RateLimit({
windowMs: 15*60*1000, // 15 minute
max: 500
});
app.use(limiter);
// AUTH LIMITING
const AuthLimiter = RateLimit({
windowsMs: 15*60*1000, // 15 minutes
message: 'Too many authantifications, please try again after an 15 minutes',
max: 300
})
// API
app.use("/api/", require("./routes/api").api)
app.use("/", require("./routes/home"))
// UPLOAD
app.use("/upload/", require("./routes/upload"))
// DATABASE
const users = require("./models/users");
mongoose.connect(process.env.mongo_srv, {
}).then(() =>[
logger.logSuccess("Connected to the database!")
]).catch((err) =>{
logger.logError('Failed connect to the database!')
})
app.use("/addapikey/", require("./routes/api").addkey)
app.use("/delapikey/", require("./routes/api").delkey)
app.use("/files/", require("./routes/files"))
app.use('/favicon.ico', express.static( __dirname + "/img/daneecloudfavicon.png"));
app.use("/background", express.static( __dirname + "/img/background.png"));
app.use("/files/", AuthLimiter, checkAuth, checkVerify, express.static(__dirname + "/uploads/"))
app.set("view-engine", "ejs")
// REGISTER
app.use("/register", require("./routes/register"))
// LOGIN
app.use("/login", require("./routes/login"))
// SHARED FILES
app.use("/sf/", require("./routes/sharedFiles").sf)
app.use("/addsf/", require("./routes/sharedFiles").addsf)
app.use("/rmsf/", require("./routes/sharedFiles").rmsf)
// PLAY VIDEO
app.use("/playvideo/", require("./routes/vidplayer").player)
// EMAIL VERIFY
app.use("/verify", require("./routes/emailVerify").ver)
app.use("/verifycode", require("./routes/emailVerify").verCode)
// LOG OUT
app.delete("/logout", (req, res) => {
req.logOut( (err) => {
if (err) return logger.logError(err)
})
res.redirect("/login")
})
// MY FILES
app.use("/myfiles", require("./routes/filemgr").myfiles)
// DELETE FILE
app.use("/delete/", require("./routes/filemgr").del)
// RENAME FILE
app.use("/rename/", require("./routes/filemgr").ren)
// ADMIN PANEL
app.use("/admin/", require("./routes/admin").admin)
// DELETE ACCOUNT
app.use("/deleteaccount/", require("./routes/admin").del)
// EDIT ACCOUNT
app.use("/editaccount/", require("./routes/admin").edit)
// DOWNLOAD REDIRECT
app.use("/dwnl/", require("./routes/download").dwnl)
// DOWNLOAD
app.use("/download/", require("./routes/download").download)
app.use("/downloadf/", require("./routes/download").downloadf)
// ROLES
app.use("/updaterole/", require("./routes/roles").updaterole)
app.use("/addrole/", require("./routes/roles").addrole)
app.use("/delrole/", require("./routes/roles").delrole)
app.use("/editrole/", require("./routes/roles").editrole)
// AUDIO PLAYER
app.use("/playaudio/", require("./routes/audioplayer"))
// ADD SUBTITLES
app.use("/addsubtitles/", require("./routes/vidplayer").subtitles)
// REMOVE SUBTITLES
app.use("/rmsubtitles/", require("./routes/vidplayer").rmsubtitles)
// ACCOUNT MANAGMENT
app.use("/editmyaccount/", require("./routes/accountmgr").edit)
app.use("/delmyaccount/", require("./routes/accountmgr").delete)
// MY PROFILE
app.use("/myprofile", require("./routes/accountmgr").myprofile)
// CREATE FOLDER
app.use("/createfolder", require("./routes/filemgr").createfolder)
// DELETE FOLDER
app.use("/deletefolder", require("./routes/filemgr").deletefolder)
// SHOW FOLDER
app.use("/folder/", require("./routes/filemgr").showfolder)
// 404 PAGE
app.get("/*", async function (req, res) {
res.render(__dirname + "/views/message.ejs", {message: `<i class="fa-solid fa-square-xmark"></i> ${lang["Error404"]}`, cloudname: config.cloudname, lang: lang})
})
// CHECK IF DEFAULT ROLES ARE IN DB
async function roleCheck() {
const findUserRole = await roles.findOne({name: "user"})
const findAdminRole = await roles.findOne({name: "admin"})
if (!findUserRole) {
const userRole = new roles({
name: "user",
maxStorage: 128
})
userRole.save()
logger.logWarn("There wasn't role in database for user, so I created one.")
}
if (!findAdminRole) {
const adminRole = new roles({
name: "admin",
maxStorage: 256
})
adminRole.save()
logger.logWarn("There wasn't role in database for admin, so I created one.")
}
}
roleCheck()
// CHECK FOR UPDATES
updater.checkForUpdates()
app.listen(config.port, () => {
logger.logSuccess(`Server listening on port ${config.port}`);
});