-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
189 lines (170 loc) · 4.55 KB
/
Copy pathindex.js
File metadata and controls
189 lines (170 loc) · 4.55 KB
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
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const session = require('express-session');
const cors = require('cors');
const uuid = require('uuid/v4');
require('dotenv').config();
//Initiate our app
const app = express();
//Configure our app
app.use(cors());
// app.use(require('morgan')('dev'));
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({
secret: 'passport-tutorial',
cookie: {
maxAge: 60000
},
resave: false,
saveUninitialized: false
}));
const convertRoomToSend = ({
name,
owner,
maxClients,
id
}) => ({
name,
owner,
maxClients,
id
});
const rooms = {};
const router = require('express').Router();
router.get('/', (req, res, next) => {
let responseHTML = `<!doctype html><html><body><h2>Room server at ${server.address().address}</h2>`;
const roomsArray = Object.values(rooms);
if (roomsArray && roomsArray.length) {
responseHTML += `<h3>These are the registered rooms:</h3>`;
responseHTML = roomsArray.reduce((html, room) => html + `<p>${JSON.stringify(room)}</p>`, responseHTML);
} else {
responseHTML += `<h3>There are no registered rooms 🤷♂️</h3>`;
}
responseHTML += `</body></html>`;
res.send(responseHTML);
})
router.get('/rooms', (req, res, next) => {
if (rooms) {
console.log('Queried for rooms');
res.status(200).json(Object.values(rooms).map(x => convertRoomToSend(x)));
return;
} else {
res.status(404).json({});
return;
}
});
const validateOwner = ({
name,
publicIP,
publicPort,
privateIP,
privatePort
}) => {
if (!name) {
console.error(name);
return false;
}
const isIP = require('net').isIPv4;
const isValidPort = (port) => (+port) < 65536 && (+port) > 0 && port === (+port).toString();
if (!(isIP(publicIP) && isIP(privateIP) && isValidPort(publicPort) && isValidPort(privatePort))) {
console.error(isIP(publicIP), publicPort, isIP(privateIP), privatePort);
return false;
}
return true;
}
const validateRoom = ({
name,
owner,
maxClients
}) => {
if (!(maxClients && maxClients > 1)) {
console.error(maxClients);
return false;
}
if (!name) {
console.error(name);
return false;
}
if (!(owner && validateOwner(owner))) {
console.error(JSON.stringify(owner));
return false;
}
return true;
}
// TODO: All rooms should use user tokens, so that someone knowing the username & room name, can't modify them
// Create a room
router.post('/register', (req, res) => {
const {
name,
owner,
maxClients
} = req.body;
// Check if it already exists
const existingRoom = Object.values(rooms).filter(x => x.owner.name === owner.name).find(x => x.name === name);
if (existingRoom) {
res.status(409).json(convertRoomToSend(existingRoom));
return;
}
const room = {
name,
owner,
maxClients,
id: uuid(),
lastUpdate: Date.now()
};
if (validateRoom(room)) {
rooms[room.id] = room;
// Created resource
res.status(201).json(convertRoomToSend(room));
return;
} else {
// Bad request
res.status(400).json({
error: "Invalid Room"
});
return;
}
});
router.post('/keepalive', (req, res) => {
const room = rooms[req.body.id];
if (!room) {
res.status(404);
return;
} else {
room.lastUpdate = Date.now();
res.status(200);
return;
}
});
router.post('/close', (req, res) => {
const room = rooms[req.body.id];
if (!room) {
res.status(404);
return;
} else {
delete rooms[req.body.id];
res.status(200);
return;
}
});
const roomTimeout = 10000;
const clearTimedOutRooms = setInterval(() => {
const now = Date.now();
Object.keys(rooms).forEach(key => {
const room = rooms[key];
if (roomTimeout < now - room.lastUpdate) {
// TODO: Is that 100% safe?
// I'm returning a new array and iterating over it, delete shouldn't affect it
delete rooms[key];
}
})
}, roomTimeout);
router.get
app.use('/', router);
const envPort = process.env.PORT || 5000;
const server = app.listen(envPort, () => console.log(`🖥 👍 Server running on http://localhost:${envPort}/`));