-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
252 lines (223 loc) · 9.49 KB
/
server.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
const PORT = 3000;
const MESSAGE_TYPE_CONNECTION = 0;
const MESSAGE_TYPE_DISCONNECTION = 1;
const MESSAGE_TYPE_CHAT = 2;
const MESSAGE_TYPE_UPDATE = 3;
const ROOM_STATE_CLOSED = 0;
const ROOM_STATE_OPEN = 1;
const ROOM_STATE_INPROGRESS = 2;
const FILTER_LEVEL_OFF = 0;
const FILTER_LEVEL_LOW = 1;
const FILTER_LEVEL_HIGH = 2;
// Node dependencies
const express = require('express');
const app = express();
const server = require('http').createServer(app);
const bodyParser = require('body-parser');
const res = require('express/lib/response');
const path = require('path');
const { WebSocket } = require('ws');
const { runInNewContext } = require('vm');
const { send } = require('process');
const Filter = require('bad-words');
// Globals
var filterHigh = new Filter();
var clientIDs = Array();
var roomIDs = Array();
var wsClients = Array();
var rooms = Array();
// Types
class Client {
constructor(ws, nickname, filter=FILTER_LEVEL_HIGH) {
this.ws = ws;
this.nickname = nickname;
this.room = null;
this.filter = filter;
// Generates unique nonzero id ranging from 1 to 999999999
this.id = 0;
while (this.id == 0 || clientIDs.includes(this.id)) {
this.id = Math.floor(Math.random() * 1000000000);
}
clientIDs.push(this.id);
}
}
class Room {
constructor(client = null, maxClients = 2, roomState = ROOM_STATE_OPEN) {
this.clients = Array();
this.maxClients = maxClients;
this.roomState = roomState;
if (client != null) {
this.clients.push(client);
}
// Generates unique nonzero id ranging from 1 to 999999999
this.id = 0;
while (this.id == 0 || roomIDs.includes(this.id)) {
this.id = Math.floor(Math.random() * 1000000000);
}
roomIDs.push(this.id);
}
// Adds new client to array.
// Params: client -> Client
addClient(client) {
this.clients.push(client);
}
// Removes given client from array.
// Params: client -> Client
removeClient(client) {
let index = this.clients.indexOf(client);
this.clients.splice(index, 1);
let json = {"type": 1, "room": this.id, "roomCount": this.clients.length, "roomMax": this.maxClients, "roomState": this.roomState, "nickname": client.nickname, "users": wsClients.length};
this.broadcastMessage(JSON.stringify(json));
}
// Sends the given json message to every cleint in the room.
// Params: message -> string, sender? -> Client
broadcastMessage(message, sender=null) {
for (let i = 0; i < this.clients.length; i++) {
let client = this.clients[i];
if (client !== sender) {
let cleansedMessage = filterMessage(message, client.filter);
client.ws.send(cleansedMessage);
}
}
}
}
// Converts websocket raw data into a string.
// Params: message -> byte[]
// Returns: string
function parseSocketData(message) {
let str = "";
for (let n = 0; n < message.length; n += 1) {
str += String.fromCharCode(message[n]);
}
return str;
}
// Filters the given message for undesirable text.
// Params: message -> string, filterLevel -> int
// Returns: string
function filterMessage(message, filterLevel) {
cleansedMessage = message;
// Low filter
if (filterLevel >= FILTER_LEVEL_LOW) {
if (message.includes("http") || message.includes("www") || message.includes(".com") || message.includes(".org") || message.includes(".hk")
|| message.includes(".net") || message.includes(".co") || message.includes(".us") || message.includes(".xyz") || message.includes(".ly")
|| message.includes("goo.gl") || message.includes("bit.ly") || message.includes("tinyurl") || message.includes(".io") || message.includes(".uk")
|| message.includes(".au") || message.includes(".jp") || message.includes(".ca") || message.includes(".in") || message.includes(".cn") // removes url links.
|| message.match("[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}") != null // removes phone numbers.
|| message.match('/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/') != null) // removes email addresses.
{
cleansedMessage = "";
}
}
// High filter
if (filterLevel >= FILTER_LEVEL_HIGH) {
cleansedMessage = filterHigh.clean(message);
}
return cleansedMessage;
}
// ----- Express http server -----
app.use(express.static(path.join(__dirname, 'public')))
app.use(bodyParser.urlencoded({extended:true}))
app.use(express.json())
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'ejs')
// Views
app.get('/', (req, res) => {
console.log(`http request received: type: GET, path: '/', from: ${req.socket.remoteAddress}`);
res.render('pages/chatroom', {userCount: wsClients.length + 1});
});
// Binds server to the port and stars listening.
server.listen(PORT, () => {
console.log(`listening on port ${PORT}`)
});
// ----- Websocket server -----
const wss = new WebSocket.Server({ server:server });
// Listens for new connections and adds event listeners to each new client.
wss.on('connection', (ws, req) => {
console.log(`Client websocket connected (${req.socket.remoteAddress})`);
// Listens for messages from the client.
ws.on('message', (message) => {
let strMessage = parseSocketData(message);
let jsonMessage = JSON.parse(strMessage);
console.log(`Message received from client websocket (${req.socket.remoteAddress}): ${strMessage}`);
switch (jsonMessage.type) {
case MESSAGE_TYPE_CONNECTION:
// Create new client.
let newClient = new Client(ws, jsonMessage.nickname);
wsClients.push(newClient);
// Attemps to place client into a prexisting open room.
let foundRoom = null;
for (let i = 0; i < rooms.length; i++) {
let room = rooms[i];
// Room must have less than the maximum number of clients and be in the 'open' state.
if (room.clients.length < room.maxClients && room.roomState == ROOM_STATE_OPEN) {
room.addClient(newClient);
newClient.room = room;
foundRoom = room;
break;
}
}
// If no rooms are found for the client, create a new one.
if (foundRoom == null) {
let newRoom = new Room(newClient);
rooms.push(newRoom);
newClient.room = newRoom;
foundRoom = newRoom;
}
// Broadcast to room that a new client has joined.
let json = {"type": MESSAGE_TYPE_CONNECTION, "room": foundRoom.id, "roomCount": foundRoom.clients.length, "roomMax": foundRoom.maxClients, "roomState": foundRoom.roomState, "nickname": newClient.nickname, "users": wsClients.length};
newClient.room.broadcastMessage(JSON.stringify(json));
break;
case MESSAGE_TYPE_DISCONNECTION:
break;
case MESSAGE_TYPE_CHAT:
for (let i = 0; i < wsClients.length; i++) {
if (wsClients[i].ws == ws) {
let sender = wsClients[i];
let json = {"type": 2, "from": sender.nickname, "message": jsonMessage.message, "users": wsClients.length};
sender.room.broadcastMessage(JSON.stringify(json), sender);
break;
}
}
break;
case MESSAGE_TYPE_UPDATE:
for (let i = 0; i < wsClients.length; i++) {
if (wsClients[i].ws == ws) {
let client = wsClients[i];
//client.nickname = jsonMessage.nickname;
client.filter = jsonMessage.filter;
}
}
default:
break;
}
});
// Listens for when the client disconnects.
ws.on('close', () => {
console.log(`Client websocket disconnected (${req.socket.remoteAddress})`);
for (let i = wsClients.length - 1; i >= 0; i--) {
if (wsClients[i].ws == ws) {
let client = wsClients[i];
// Frees the clients id so that it can be reused.
let idIndex = clientIDs.indexOf(client.id);
clientIDs.splice(idIndex, 1);
// Removes client from its room
client.room.removeClient(client);
// Destroys the client's room if it is now empty.
if (client.room.clients.length < 1) {
// Removes room from global rooms array
let room = client.room;
for (let j = rooms.length - 1; j >= 0; j--) {
if (rooms[j] === room) {
rooms.splice(j, 1);
}
}
// Frees the rooms id so that it can be reused.
let roomIndex = roomIDs.indexOf(room.id);
roomIDs.splice(roomIndex, 1);
}
// Removes client from global wsClients array.
wsClients.splice(i, 1);
}
}
});
});