forked from hexparrot/mineos-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebui.js
158 lines (133 loc) · 4.75 KB
/
webui.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
#!/usr/bin/env node
var mineos = require('./mineos');
var server = require('./server');
var async = require('async');
var express = require('express');
var passport = require('passport');
var LocalStrategy = require('passport-local');
var passportSocketIO = require("passport.socketio");
var expressSession = require('express-session');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var cookieParser = require('cookie-parser');
var sessionStore = new expressSession.MemoryStore();
var app = express();
var http = require('http').Server(app);
var BASE_DIR = '/var/games/minecraft';
var response_options = {root: __dirname};
// Authorization
var localAuth = function (username, password) {
var Q = require('q');
var auth = require('./auth');
var deferred = Q.defer();
auth.authenticate_shadow(username, password, function(authed_user) {
if (authed_user)
deferred.resolve({ username: authed_user });
else
deferred.reject(new Error('incorrect password'));
})
return deferred.promise;
}
// Passport init
passport.serializeUser(function(user, done) {
//console.log("serializing " + user.username);
done(null, user);
});
passport.deserializeUser(function(obj, done) {
//console.log("deserializing " + obj);
done(null, obj);
});
// Use the LocalStrategy within Passport to login users.
passport.use('local-signin', new LocalStrategy(
{passReqToCallback : true}, //allows us to pass back the request to the callback
function(req, username, password, done) {
localAuth(username, password)
.then(function (user) {
if (user) {
req.session.success = 'You are successfully logged in ' + user.username + '!';
done(null, user);
} else {
req.session.error = 'Could not log user in. Please try again.'; //inform user could not log them in
done(null, user);
}
})
.fail(function (err){
console.log(err.body);
});
}
));
// clean up sessions that go stale over time
function session_cleanup() {
//http://stackoverflow.com/a/10761522/1191579
sessionStore.all(function(err, sessions) {
for (var i = 0; i < sessions.length; i++) {
sessionStore.get(sessions[i], function() {} );
}
});
}
// Simple route middleware to ensure user is authenticated.
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
req.session.error = 'Please sign in!';
res.redirect('/admin/login.html');
}
app.use(bodyParser.urlencoded({extended: false}));
app.use(methodOverride());
app.use(expressSession({
secret: 'session_secret',
key:'express.sid',
store: sessionStore,
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
var io = require('socket.io')(http)
io.use(passportSocketIO.authorize({
cookieParser: cookieParser, // the same middleware you registrer in express
key: 'express.sid', // the name of the cookie where express/connect stores its session_id
secret: 'session_secret', // the session_secret to parse the cookie
store: sessionStore // we NEED to use a sessionstore. no memorystore please
}));
mineos.dependencies(function(err, binaries) {
if (err) {
console.log('MineOS is missing dependencies:', err);
console.log(binaries);
} else {
var be = new server.backend(BASE_DIR, io);
app.get('/', function(req, res){
res.redirect('/admin/index.html');
});
app.get('/admin/index.html', ensureAuthenticated, function(req, res){
res.sendfile('/html/index.html', response_options);
});
app.get('/login', function(req, res){
res.sendfile('/html/login.html');
});
app.post('/auth', passport.authenticate('local-signin', {
successRedirect: '/admin/index.html',
failureRedirect: '/admin/login.html'
})
);
app.get('/logout', function(req, res){
req.logout();
res.redirect('/admin/login.html');
});
app.use('/socket.io', express.static(__dirname + '/node_modules/socket.io'));
app.use('/angular', express.static(__dirname + '/node_modules/angular'));
app.use('/angular-translate', express.static(__dirname + '/node_modules/angular-translate/dist'));
app.use('/moment', express.static(__dirname + '/node_modules/moment'));
app.use('/angular-moment', express.static(__dirname + '/node_modules/angular-moment'));
app.use('/angular-moment-duration-format', express.static(__dirname + '/node_modules/moment-duration-format/lib'));
app.use('/admin', express.static(__dirname + '/html'));
process.on('SIGINT', function() {
console.log("Caught interrupt signal; closing webui....");
be.shutdown();
process.exit();
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
setInterval(session_cleanup, 3600000); //check for expired sessions every hour
}
})