-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
96 lines (78 loc) · 2.56 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
const express = require('express');
const session = require('express-session');
const bodyParser = require('body-parser');
const app = express();
const server = require('http').createServer(app);
const memoryStore = require('memorystore')(session)
const io = require('socket.io')(server);
const api = require('./routes/api.js');
const path = require('path');
const watchPin = process.env.WATCH_PIN;
const adminPin = process.env.ADMIN_PIN || '4542';
var userCount = 0;
app.use(bodyParser.urlencoded({ extended: true }));
app.use(session({
cookie: { maxAge: 86400000 },
store: new memoryStore({
checkPeriod: 86400000 // prune expired entries every 24h
}),
secret: 'such secret. so secure. wow.',
resave: false,
saveUninitialized: false
}));
app.use(express.static('./html/public'));
app.use('/static', (req, res, next) => {checkForAuthStatic(req, res, next)});
app.use('/static', express.static('./video'));
app.use('/api', api.router);
app.get('/', function(req, res){
if(watchPin != undefined && req.session.watchPin != watchPin) {
return res.redirect('/pin');
}
res.sendFile(path.join(__dirname, './html', 'index.html'));
});
app.get('/admin', function(req, res){
if(req.session.adminPin != adminPin) {
return res.redirect('/pin');
}
res.sendFile(path.join(__dirname, './html', 'admin.html'));
});
app.get('/pin', function(req, res){
res.sendFile(path.join(__dirname, './html', 'pin.html'));
});
app.post('/pin', function(req, res){
let sess = req.session;
if(req.body.pin == watchPin) {
sess.watchPin = req.body.pin;
res.redirect('/');
} else if(req.body.pin == adminPin) {
sess.adminPin = req.body.pin;
res.redirect('/admin');
} else {
res.redirect('/pin');
}
});
io.sockets.on('connection', function(socket) {
socket.on('username', function(username) {
socket.username = username;
userCount++;
io.emit('is_online', `🔵 <i> ${socket.username} joined. (Users: ${userCount})</i>`);
});
socket.on('disconnect', function(username) {
if(socket.username != undefined) {
userCount--;
io.emit('is_online', `🔴 <i> ${socket.username} left. (Users: ${userCount})</i>`);
}
})
socket.on('chat_message', function(message) {
io.emit('chat_message', '<span>' + socket.username + '</span>: ' + message);
});
});
function checkForAuthStatic(req, res, next) {
if(watchPin != undefined && req.session.watchPin != watchPin) {
res.statusCode = 401;
res.send('401 Unauthorized');
} else {
next();
}
}
server.listen(3000, () => {console.log("Listening on port 3000.");});