-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
249 lines (212 loc) · 6.73 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
const express = require("express");
const fs = require("fs");
const colors = require("colors");
const yaml = require("js-yaml");
const path = require("path");
const { strings } = require("./strings");
const {
outputFolderSize,
checkSecurityIssue
} = require("./utils");
const cors = require("cors");
const routes = require("./routes");
const CryptoJS = require("crypto-js");
const jwt = require('jsonwebtoken');
const app = express();
const { createServer } = require("http");
const { Server } = require("socket.io");
const httpServer = createServer(app);
const io = new Server(httpServer, { cors: { origin: "*" } });
let config;
let abort = false;
if (!fs.existsSync("./config.yml") || fs.existsSync("./config.yaml")) {
console.log("config.yml is missing...".yellow);
console.log("Creating new config...\n".green);
if (!fs.existsSync("./test")) {
fs.mkdirSync("./test")
}
fs.writeFileSync("config.yml", strings.defaultConfig);
}
if (!fs.existsSync("./recovery_bin")) {
console.log("recovery_bin is missing...".yellow);
console.log("Creating new recovery_bin...\n".green);
fs.mkdirSync("recovery_bin");
}
config = yaml.load(fs.readFileSync('config.yml', 'utf8'));
if (!fs.existsSync(config.folder)) {
console.log(`${config.folder} is missing...`.yellow);
console.log(`Creating new ${config.folder}...\n`.green);
fs.mkdirSync(config.folder);
}
// Get config data on start
// console.log(config);
if (!config.port) {
console.log("Port is missing...".yellow);
abort = true;
setTimeout(() => {
process.exit();
}, 3000);
}
if (!config.folder) {
console.log("Folder data is missing...".yellow);
abort = true;
setTimeout(() => {
process.exit();
}, 3000);
}
io.use((socket, next) => {
let username;
let password;
if (!socket.handshake.auth.token) {
const err = new Error('Bad request!');
return next(err);
}
if (socket.handshake.auth.watch) {
if (socket.handshake.auth.watch.match(/\/\.\./)) {
const err = new Error('Bad request!');
return next(err);
}
}
const token = socket.handshake.auth.token;
let bytes = CryptoJS.AES.decrypt(token, config.secret_encryption_key);
bytes = bytes.toString(CryptoJS.enc.Utf8);
let decoded;
try {
decoded = jwt.verify(bytes, config.secret_jwt_key);
} catch (err) {
if (err.message === "jwt expired") {
const err = new Error('Session expired');
err.data = { content: "Please try to login again." };
return next(err);
} else {
const err = new Error('Unknown session error');
err.data = { content: "Please try to login again." };
return next(err);
}
}
username = decoded.name;
password = decoded.password;
function findUser(account) {
return account.name === username
}
let account = config.accounts.find(findUser);
if (account !== undefined) {
if (password !== account.password) {
const err = new Error('Incorrect password');
err.data = { content: "Your password is incorrect." };
return next(err);
}
} else {
const err = new Error('Wrong username!');
err.data = { content: "That account does not exist." };
return next(err);
}
socket.handshake.auth.account = account;
next();
});
const socketEvents = require('./socketEvents');
socketEvents(io, config);
app.use(cors());
app.use("/api", express.json());
app.use("/api", async (req, res, next) => {
let username;
let password;
if (!req.body.username || !req.body.password) {
if (!req.body.token && !req.headers.token) {
res.status(400);
res.json({ err: "Bad request!" })
return
}
}
if (await checkSecurityIssue(req)) {
res.status(400);
res.json({ err: "Bad request!" })
return
}
if (req.body.username && req.body.password) {
username = req.body.username;
password = req.body.password;
} else if (req.body.token) {
let bytes = CryptoJS.AES.decrypt(req.body.token, config.secret_encryption_key);
bytes = bytes.toString(CryptoJS.enc.Utf8);
let decoded;
try {
decoded = jwt.verify(bytes, config.secret_jwt_key);
} catch (err) {
if (err.message === "jwt expired") {
res.status(401);
res.json({ err: "Session expired!" })
return
} else {
res.status(401);
res.json({ err: "Unknown session error!" })
}
return
}
username = decoded.name;
password = decoded.password;
} else if (req.headers.token) {
let bytes = CryptoJS.AES.decrypt(req.headers.token, config.secret_encryption_key);
bytes = bytes.toString(CryptoJS.enc.Utf8);
let decoded;
try {
decoded = jwt.verify(bytes, config.secret_jwt_key);
} catch (err) {
console.error(err.message);
if (err.message === "jwt expired") {
res.status(401);
res.json({ err: "Session expired!" })
return
} else {
res.status(401);
res.json({ err: "Unknown session error!" })
}
return
}
username = decoded.name;
password = decoded.password;
} else {
res.status(400);
res.json({ err: "Bad request!" })
return
}
function findUser(account) {
return account.name === username
}
let account = config.accounts.find(findUser);
if (account !== undefined) {
if (password !== account.password) {
res.status(401);
res.json({ err: "Wrong password!" })
return
}
} else {
res.status(401);
res.json({ err: "Wrong username!" })
return
}
req.body.account = account;
next()
})
app.use("/api", routes)
app.use("/", express.static(path.join(__dirname, "client")))
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "client", 'index.html'));
});
// Get folder size on start
if (config.get_foldersize_on_start && fs.existsSync(config.folder)) {
outputFolderSize(config)
}
if (!abort) {
httpServer.listen(config.port, () => {
console.log(`
_______ __ __
/ _____/ / / / /
/ /__ / /__/ /
/ ___/ / ___ /
/ / / / / /
/_/ /_/ /_/ `.cyan.bold, "By MertJSX".underline.brightCyan.italic);
console.log(`\nThe server has started on port ${config.port}!`.gray);
console.log("IP: ".gray + `http://127.0.0.1:${config.port}\n`.yellow);
})
}