forked from robdodson/defcon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
165 lines (129 loc) · 4.42 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
const
express = require('express'),
app = module.exports = express(),
server = require('http').createServer(app),
Stopwatch = require('./models/stopwatch'),
errorHandler = require('errorhandler'),
expressLayouts = require('express-ejs-layouts');
const auth = (req, res, next) => {
const authHeader = req.headers.authorization;
if (!authHeader) {
return res.set("WWW-Authenticate", "Basic realm='timer'").status(401).send('Unauthorized');
}
else {
const authTokens = authHeader.split(' ');
if (authTokens[0] === 'Basic') {
const credentials = Buffer.from(authTokens[1], 'base64').toString('utf8');
if (credentials !== process.env.BASIC_AUTH) { // this is obviously not the most secure way, but we don't care
return res.status(401).send('Unauthorized');
}
else {
next();
}
} else {
return res.status(401).send('Unauthorized');
}
}
};
// Configuration
if (process.env.BASIC_AUTH) {
app.use(auth);
}
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
if (process.env.NODE_ENV === 'development') {
app.use(errorHandler({ dumpExceptions: true, showStack: true }));
}
if (process.env.NODE_ENV === 'production') {
app.use(errorHandler());
}
// Use the port and host
var port = process.env.PORT || 5000;
var host = process.env.HOST || '0.0.0.0';
var prefix = process.env.PREFIX || '';
const halls = (process.env.HALLS || ((process.env.PREFIX || "") + ":" + (process.env.TITLE || "TIMER"))).replace("; ?$", "").split(';').map(tuple => {
const parts = tuple.split(':');
return {"prefix": parts[0].trim(), "title": parts[1].trim()};
});
console.log("Configuring halls:", halls)
halls.map(hall => {
const title = hall["title"]
const prefix = hall["prefix"]
const router = express.Router();
const io = require('socket.io')(server, {path: prefix + '/socket.io'})
router.use(expressLayouts);
router.use(express.static(__dirname + '/public'));
var stopwatch = new Stopwatch();
stopwatch.on('tick:stopwatch', function(time) {
io.sockets.emit('time', { time: time });
});
stopwatch.on('reset:stopwatch', function(time) {
io.sockets.emit('time', { time: time });
});
// stopwatch.start(); // we probably don't want autostart on server start
io.sockets.on('connection', function (socket) {
io.sockets.emit('time', { time: stopwatch.getTime() });
socket.on('click:start', function () {
stopwatch.start();
});
socket.on('click:stop', function () {
stopwatch.stop();
});
socket.on('click:zero', function () {
stopwatch.zero();
});
socket.on('click:reset', function () {
stopwatch.reset();
});
socket.on('click:resetShort', function () {
stopwatch.resetShort();
});
});
router.get('/', function(req, res) {
res.render('index', { title: title, prefix: prefix });
});
const control = express.Router();
control.get('/', function(req, res) {
res.render('control', { title: title, prefix: prefix });
});
control.post('/reset/', function (req, res) {
stopwatch.reset();
res.send("OK");
});
control.post('/reset-short/', function (req, res) {
stopwatch.resetShort();
res.send("OK");
});
control.post('/start-from-reset/', function (req, res) {
stopwatch.reset();
stopwatch.start();
res.send("OK");
});
control.post('/start-from-reset-short/', function (req, res) {
stopwatch.resetShort();
stopwatch.start();
res.send("OK");
});
control.post('/start/', function (req, res) {
stopwatch.start();
res.send("OK");
});
control.post('/stop/', function (req, res) {
stopwatch.stop();
res.send("OK");
});
control.post('/zero/', function (req, res) {
stopwatch.zero();
res.send("OK");
});
router.use('/control', control);
app.use(prefix, router);
});
server.listen(port, host, function() {
console.log("Express server listening on %j in %s mode", server.address(), app.settings.env);
});
if (halls.length != 1 && halls[0]["prefix"] != "") {
app.get('/', function(req, res) {
res.render("list", { layout: false, halls: halls });
});
}