-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
291 lines (245 loc) · 8.77 KB
/
app.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
const colors = require("colors"); // pour avoir une belle console
// instant advertising
console.log(
" ╔═══════════════════════════════════════════════════════════╗".brightCyan
);
console.log(
" ║ Chaun14's launcher NodeJs server full by chaun14 🐑 ║".brightCyan
);
console.log(
" ╚═══════════════════════════════════════════════════════════╝".brightCyan
);
const bodyParser = require("body-parser");
let cookieParser = require("cookie-parser");
let path = require("path");
let morgan = require("morgan");
const fileUpload = require("express-fileupload");
let bcrypt = require("bcryptjs");
const passport = require("passport");
const LocalStrategy = require("passport-local").Strategy;
const expressSession = require("express-session");
const flash = require("flash");
const express = require("express");
const SelfReloadJSON = require("self-reload-json");
function loadApp(db) {
const utils = require("./modules/utils.js");
const config = new SelfReloadJSON("./config.json");
const debug = config.debug;
const app = express();
const http = require("http").createServer(app);
const io = require("socket.io")(http);
// les middleware
app.use("/", express.static("public"));
app.use("/files", express.static("files"));
app.use("/java", express.static("java"));
app.use(
fileUpload({
useTempFiles: true,
tempFileDir: "/tmp/",
})
);
app.use(cookieParser(config.secret));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(
expressSession({
secret: config.secret,
resave: false,
saveUninitialized: false,
})
);
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
if (debug)
app.use(
morgan(
"[" +
"DEBUG".magenta +
"] " +
"[" +
"REQUEST".green +
"] " +
":method :url :status :res[content-length] - :response-time ms"
)
);
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");
app.use("/dashboard", require("./routes/dashboard"));
app.use("/", require("./routes/index"));
io.on("connection", (socket) => {
// console.log('a user connected');
socket.on("disconnect", () => {
// console.log('user disconnected');
});
});
/* ################################# Auth stuff ################################# */
passport.use(
new LocalStrategy(function(username, password, done) {
let user = {
username: "user",
passwordHash: config.passwordHash,
id: 1,
};
if (debug)
utils.logDebug("[" + "AUTH".red + "] " + "je cherche " + username);
if (user.username == "user") {
if (debug)
utils.logDebug(
"[" + "AUTH".red + "] " + "User trouvé: " + user.toString
);
// User not found
if (!user) {
if (debug)
utils.logDebug(
"[" + "AUTH".red + "] " + "je ne trouve pas " + user
);
return done(null, false);
}
// Always use hashed passwords and fixed time comparison
bcrypt.compare(password, user.passwordHash, (err, isValid) => {
if (err) {
return done(err);
}
if (!isValid) {
if (debug)
utils.logDebug(
"[" + "AUTH".red + "] " + "mdp de " + user.username + " faux"
);
return done(null, false), { message: "Incorrect password." };
}
if (debug)
utils.logDebug(
"[" + "AUTH".red + "] " + "mdp de " + user.username + " validé"
);
return done(null, user);
});
} else {
if (debug)
utils.logDebug(
"[" + "AUTH".red + "] " + "Je ne trouve pas " + username
);
return done("user not found");
}
})
);
passport.serializeUser(function(user, done) {
if (debug)
utils.logDebug(
"[" + "AUTH".red + "] " + "sérialise l'user " + user.username
);
done(null, user);
});
passport.deserializeUser(function(testuser, done) {
let user = {
username: "user",
passwordHash: config.passwordHash,
id: 1,
};
if (debug)
utils.logDebug(
"[" + "AUTH".red + "] " + "désérialise l'user " + testuser.username
);
if (user.username == testuser.username) {
done(null, user);
} else {
if (debug)
utils.logDebug("[" + "AUTH".red + "] " + "décé user pas trouvé");
done("cc", user);
}
});
app.get("/login", function(req, res) {
if (req.session.passport !== undefined) {
res.redirect("/dashboard");
} else {
let hasPass = true;
if (config.passwordHash == "") hasPass = false;
res.render("login", {
message: "",
messageType: "success",
hasPass: hasPass,
});
}
});
// app.post('/login', passport.authenticate('local', { failureRedirect: '/login',failureFlash: true, successRedirect: '/dashboard' }))
app.post("/login", function(req, res, next) {
passport.authenticate("local", function(err, user, info) {
if (err) {
return next(err);
}
let hasPass = false;
if (!config.passwordHash == "") hasPass = true;
if (!user) {
return res.render("login", {
message: "Wrong password",
messageType: "error",
hasPass: hasPass,
});
}
req.logIn(user, function(err) {
if (err) {
return next(err);
}
return res.redirect("/dashboard");
});
})(req, res, next);
});
app.get("/register", (req, res) => {});
app.post("/register", (req, res) => {
console.log(req.body);
if (!config.passwordHash == "")
return res.render("login", {
message: "You already have a password",
messageType: "error",
hasPass: true,
});
if (req.body.password !== req.body.passwordrepeat)
return res.render("login", {
message: "Passwords doesn't match",
messageType: "error",
hasPass: false,
});
let hash = bcrypt.hashSync(req.body.password, 8);
config.passwordHash = hash;
config.save();
res.redirect("/login");
});
app.get("/logout", (req, res) => {
if (req.session.passport == undefined) {
res.redirect("/");
} else {
if (debug)
utils.logDebug("Déco de " + req.session.passport.user.username);
req.session.destroy(function(err) {
res.redirect("/"); //Inside a callback… bulletproof!
});
}
});
/* ################################# End Auth stuff ################################# */
// oui j'ai pris ce port wtf car c'est le tag discord de trxyy
const port = process.env.PORT || config.port;
// log informatif de démarrage terminé
http.listen(port, () => {
console.log("[STARTING] App started on port ".brightCyan + port);
});
}
function socketProgressEmit(event, state, fileName, i) {
io.emit(event, { state: state, fileName: fileName, id: i });
}
function socketDownloadEmit(event, action, message, fileName, i) {
io.emit(event, {
action: action,
message: message,
fileName: fileName,
id: i,
});
}
function socketBroadcast(message, type) {
io.emit("message", { msg: message, type: type });
}
module.exports = {
loadApp,
socketProgressEmit,
socketDownloadEmit,
socketBroadcast,
};