-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
87 lines (73 loc) · 2.06 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
/**
* Module dependencies.
*/
const fileUtility = require('./file-utility');
let masterDirectoryToList = '/home/pasu';
const express = require('express')
, http = require('http')
, bodyParser = require('body-parser')
, cookieparser = require('cookie-parser')
, session = require('express-session')
, path = require('path'),
serviceIndex = require('serve-index')
;
const app = express();
const mustacheExpress = require('mustache-express');
app.set('port', process.env.PORT || 3000);
app.engine('html', mustacheExpress());
app.set('views', __dirname + '/views');
app.set('view engine', 'html');
app.use(cookieparser());
app.use(bodyParser.json({limit: '1mb'}));
//app.use('/ftp', serviceIndex(__dirname + '/node_modules'));
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(session({ secret: 'passsss',
resave: true,
saveUninitialized: true
}));
app.get('/login', function (req, res) {
res.render('login');
});
app.post('/login', function (req, res) {
var post = req.body;
if (post.user && post.password && validateAuth(post.user, post.password)) {
req.session.user_id = post.user;
res.redirect('/ftp');
} else {
res.send('Bad user/pass');
}
});
app.use('/ftp', checkAuth, express.static(masterDirectoryToList));
app.use('/ftp', checkAuth, serviceIndex(masterDirectoryToList));
app.get('/logout', function (req, res) {
delete req.session.user_id;
res.redirect('/login');
});
app.get('/*', checkAuth, function (req, res) {
res.render('error404');
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
// Party starts here
function checkAuth(req, res, next) {
if (req.session && req.session.user_id) {
next();
} else {
res.redirect('/login');
}
}
function validateAuth(user, pass) {
let authKeys = fileUtility.readAuthFile();
let isValid = false;
if(authKeys.users) {
authKeys.users.forEach(function (userObject) {
if(userObject.user === user && userObject.pass === pass) {
isValid = true;
}
});
}
return isValid;
}